//var root = "http://127.0.0.1/gfeed/public_html/";
var root = "http://www.gfeed.mobi/";
var fbUser = 0; // Are we still using this?
var offset = 0;

$(document).ready(function() {		   
	initsIFR();
	initQuestions();
	initCommonForm();
	initHUDGallery();
	initHUDYouTube();
	initSubscribe();
	initAlert();
	initBanner();
	initAccordion();
});

function initAccordion() {
	$("div#accordion div dl").hide();
	
	$("div#accordion a.title").toggle(function() {
		$(this).css({"borderBottom" : "1px solid #c3c3c3"});
		$(this).parent().find("dl").show();
	}, function() {
		$(this).css({"borderBottom" : "none"});
		$(this).parent().find("dl").hide();
	});
}

function initBanner() {
	if ($("div.right-col form.subscribe li.warning").length <= 0)
		$("div.right-col div.banner-wrap").hide();
	
	$("a.banner").toggle(function() {
		$("div.right-col div.banner-wrap").slideDown();						  
	}, function() {
		$("div.right-col div.banner-wrap").slideUp();
	});
}

function initAlert() {
	$("a.ok-thanks").click(function() {
		$(this).parent().parent().slideUp("normal", function() {
		 	if ($(this).find("a.ok-thanks").attr("rel") == "reload") {
				window.location.href = window.location.href;
			} else {
				return false;	
			}
		 });
		
		return false;
	});
	
	$("a.no").click(function() {
		$(this).parent().parent().slideUp();
	});
}

function initSubscribe() {
	$("input#email").focus(function() {
		if ($("input#email").val() == 'Email address') {
			$("input#email").val("");
		}
	});
}

function initHUDGallery() {
	$("a.gallery").click(function() {
		offset = 0; // Just to make sure ;)
		if ($(this).attr("rel") == 'iphone') {
			var imgs = new Array();
			for (var i = 0; i < 5; i++) {
				imgs[i] = root+'media/imgs/iphone-gallery-'+i+'.jpg';
			}
			var hudID = 'hud-frame-gallery';
			var sliderID = 'slider-iphone';
			var galleryClass = 'hud-gallery';
		} else {
			var imgs = new Array();
			for (var i = 0; i < 5; i++) {
				imgs[i] = root+'media/imgs/gFEED-gallery-'+i+'.jpg';
			}
			var hudID = 'hud-frame-gfeed';
			var sliderID = 'slider-gfeed';
			var galleryClass = 'hud-gallery-gfeed';
		}
	
		var html = '<ul class="'+galleryClass+'">';
		for (var i = 0; i < 5; i++) {
			html += '<li><img src="'+imgs[i]+'" title="Your iPhone App Gallery" alt="Your iPhone App Gallery" /></li>';
		}
		html += '</ul>';
		
		$("body").append('<div id="overlay"></div>'+
						 '<div id="'+hudID+'">'+
							'<div id="hud-content-gallery">'+
								'<a class="close" href="#">Close</a>'+
								'<div id="'+sliderID+'">'+html+'</div>'+
							'</div>'+
						 '</div>');
		
		correctOverlaySize();
		
		$("#"+sliderID).easySlider({
			prevId: 'hud-prev',
			nextId: 'hud-next'
		});

		$("div#overlay").click(function() {
			$(this).remove();
			$("div#"+hudID).remove();
		});
		
		$("a.close").click(function() {
			$("div#overlay").remove();
			$("div#"+hudID).remove();
		});
		
		return false;
	});
}

function correctOverlaySize() {
	var ele = $("div#overlay");
	
	ele.css({
							'height':	$(document).height(),
							'width':	$(document).width()
						 }); 
	
	var frame = ele.next();
	var marginTop = frame.css("marginTop");
	
	frame.css({
			  'marginTop':  parseInt(marginTop.replace('px', '')) + $(document).scrollTop()
			  }); 
}

function initHUDYouTube() {
	$("a.youtube").click(function() {
		$("body").append('<div id="overlay"></div>'+
						 '<div id="hud-frame">'+
							'<div id="hud-content">'+
								'<a class="close" href="#">Close</a>'+
								'<object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/'+$(this).attr("rel")+'&hl=en&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+$(this).attr("rel")+'&hl=en&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>'+
							'</div>'+
						 '</div>');
		
		correctOverlaySize();
		
		$("div#overlay").click(function() {
			$(this).remove();
			$("div#hud-frame").remove();
		});
		
		$("a.close").click(function() {
			$("div#overlay").remove();
			$("div#hud-frame").remove();
		});
		
		return false;
	 });
}

function initCommonForm() {
	/* SIGN UP */
	$("textarea#itunes_description").keyup(function () {								 
		if ($("textarea#itunes_description").val() != '') {
			showiTunesExample();
		}
	});
	
	if ($("textarea#itunes_description").length > 0) {
		if ($("textarea#itunes_description").val() != '') {
			showiTunesExample();	
		}
	}
	
	$("input#username").keyup(function() {
		var username = $(this).val();
		$(this).parent().find("span.helper strong").html(root+"gfeed/"+username+"/");									
	});
	
	/* PAYMENT */
	// hide by defualt.
	if ($("select#creditcardtype").val() != "Maestro" && $("select#creditcardtype").val() != "Solo") {
		$("select#startdate_month").parent().hide();
		$("select#startdate_year").parent().hide();
		$("input#issuenumber").parent().hide();
		$("fieldset.padlock li.helper").hide();
		$("fieldset.padlock").css({"paddingTop" : "90px",
								   "height"		: "277px"});
		
	}
	// required thus slideDown.
	$("select#creditcardtype").change(function() {
   		if ($(this).val() == "Maestro" || $(this).val() == "Solo") {
			
			$("fieldset.padlock").animate({
				paddingTop: "35px",
				height:		"329px"
			}, 
			500,
			function() {
				$("select#startdate_month").parent().slideDown();
				$("select#startdate_year").parent().slideDown();
				$("input#issuenumber").parent().slideDown();
				$("fieldset.padlock li.helper").slideDown();
			});
		} else if ($("select#startdate_year:visible").length > 0) {
			$("select#startdate_month").parent().slideUp();
			$("select#startdate_year").parent().slideUp();
			$("input#issuenumber").parent().slideUp();
			$("fieldset.padlock li.helper").slideUp();
			
			$("fieldset.padlock").animate({
				paddingTop	: "90px",
				height		: "277px"
			}, 
			500);
		}
   	});
	
	/* DATE PICKERS */
	if ($("input#start_signup").length > 0) {
		$('#start_signup').datepicker({
			inline: true,
			minDate: +14
		});
	}
	
	if ($("input#end_signup").length > 0) {	
		$('#end_signup').datepicker({
			inline: true,
			minDate: +14
		});
	}
}

function showiTunesExample() {
	var text = document.getElementById("itunes_description").value;
	$("div#example p.your-text").html(escapeVal(text, '<br />'));	
}

function initQuestions() {
	$("div.questions ul li p").hide();
	
	$("div.questions ul li a").toggle(function() {
   		$(this).parent().find("p").slideDown();
   	}, function() {
	   	$(this).parent().find("p").slideUp();
   	});
}

function initsIFR() {
	if(typeof sIFR == "function"){
		var swf = root+"media/plugins/sifr/fonts/helvetica-medium.swf";
    	sIFR.replaceElement("div#head h2", named({sFlashSrc: swf, sColor: "#dddddd", sBgColor: "#333333", sWmode: "transparent"}));
		sIFR.replaceElement("div.wide-col h1", named({sFlashSrc: swf, sColor: "#1d1d1d", sBgColor: "#dddddd", sWmode: "transparent"}));
		sIFR.replaceElement("form#signup legend", named({sFlashSrc: swf, sColor: "#1d1d1d", sBgColor: "#dddddd", sWmode: "transparent"}));
		sIFR.replaceElement("div#alert h2", named({sFlashSrc: swf, sColor: "#dddddd", sBgColor: "#dddddd", sWmode: "transparent"}));
		sIFR.replaceElement("h4", named({sFlashSrc: swf, sColor: "#1d1d1d1", sBgColor: "#dddddd", sWmode: "transparent"}));
		sIFR.replaceElement("body#login h3", named({sFlashSrc: swf, sColor: "#1d1d1d1", sBgColor: "#dddddd", sWmode: "transparent"}));
	};
}

function escapeVal(string, replaceWith) {
	string = escape(string); //encode textarea string's carriage returns
	
	for (i = 0; i < string.length; i++) {
		//loop through string, replacing carriage return encoding with HTML break tag
		if (string.indexOf("%0D%0A") > -1) {
			//Windows encodes returns as \r\n hex
			string = string.replace("%0D%0A", replaceWith);
		} else if (string.indexOf("%0A") > -1) {
			//Unix encodes returns as \n hex
			string = string.replace("%0A", replaceWith);
		} else if (string.indexOf("%0D") > -1) {
			//Macintosh encodes returns as \r hex
			string = string.replace("%0D", replaceWith);
		}
	
	}
	
	return unescape(string); // unescape all other encoded characters
}

