/********************************************************/
/** Setting **/
/********************************************************/
var GlobalRoot = "/fxm/";
var _desktop = true;
var _mobile = false;
var _facebook = false;

/********************************************************/
/** Utility **/
/********************************************************/
if(typeof(console) == "undefined") {
	console		= new Object();
	console.log	= new Function();
}

function dateFormatTime(date) {
	var output = "";
	var hours = date.getHours(); 
	var mins = date.getMinutes();
	if (mins < 10) mins = "0" + mins; 
	mins = ":"+mins;
	if (hours < 12 ) {
		output = hours + mins +'am';
	}
	else if (hours == 12 ) {
		output = hours + mins +'pm';
	}
	else if (hours > 12 ) {
		hours -= 12;
		output = hours + mins +'pm';
	}
	return output;
}

function getDimensions () {
	var el = $(window);
	// fix a jQuery/Opera bug with determining the window height
	var h = $.browser.opera && $.browser.version > '9.5' && $.fn.jquery < '1.3'
				|| $.browser.opera && $.browser.version < '9.5' && $.fn.jquery > '1.2.6'
		? el[0].innerHeight : el.height();

	return [h, el.width()];
}

function getCookie(c_name){
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++){
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name){
			return unescape(y);
		}
	}
}

function chromeless() {
	setTimeout(function(){
		window.scrollTo(0, 1);
		iphonePopup(); 
		repositionPopup();     
		touchStart = false;
		document.addEventListener('touchstart', function(event) {
			touchStart = true;
			$('#iPhonePopup').hide();
		});
		document.addEventListener('touchend', function(event) {
			touchStart = false;
			$('#iPhonePopup').show();
			repositionPopup();
		});
		document.addEventListener('scroll', function(event) {
			if(!touchStart){
				repositionPopup();
			}
		});
	},100);   
}

/********************************************************/
/** Initiators **/
/********************************************************/
function initFxMenu() {
	$('#gn-watch-episodes a').click(function(e) {
		e.preventDefault();
		$('#episodes').css('display','block');
	});
	$('#episodes, #shows-titles').bind('blur mouseleave', function(e) {
		$(this).css('display','none');
	});
	$('#episodes a.close').click(function(e) {
		e.preventDefault();
		$(this).parents('#episodes').css('display', 'none');
	});
	$('#gn-shows a').click(function(e) {
		e.preventDefault();
		$('#shows-titles').css('display', 'block');
	});
	$('#shows-titles a.close').click(function(e) {
		e.preventDefault();
		$(this).parents('#shows-titles').css('display', 'none');
	});
}

function initModals() {
	$(".popthis").click(function (e) {
		e.preventDefault();
		var modal_link = $(this);
		var modal_content = modal_link.attr("href");
		if (modal_link.hasClass("locked")) {
			$(modal_content).modal({
				overlayClose : false,
				onShow: modalPin
			});
		}
		else {
			$(modal_content).modal({
				overlayClose : true,
				onShow: modalPin
			});
		}
	});
	$(".pop-iframe").click(function (e) {
		e.preventDefault();
		var modal_link = $(this);
		var modal_content = modal_link.attr("href");
		if (_mobile) { //mobile quiz/iframe
			$.modal('<iframe src="' + modal_content + '?mobile" height="450" width="300" style="border:0" allowtransparency="true">', {
				dataId: "modal_iframe",
				appendTo: "#wrapper",
				containerCss:{
					backgroundColor:"transparent",
					height:470,
					padding:0,
					width:300
				},
				position:[10,10],
				onOpen: function (dialog) {
					dialog.overlay.show();
					dialog.container.show();
					dialog.data.show();
					$('html,body').scrollTop(0);
					dialog.overlay.css('height', $('body').height());
				}
			});
		}
		if (_facebook) { //facebook quiz/iframe
			$.modal('<iframe src="' + modal_content + '" height="450" width="540" style="border:0" allowtransparency="true">', {
				dataId: "modal_iframe",
				containerCss:{
					backgroundColor:"transparent",
					height:442,
					padding:0,
					width:520
				},
				position:[1,1],
				onOpen: function (dialog) {
					dialog.overlay.show();
					dialog.container.show();
					dialog.data.show();
					$('html,body').scrollTop(0);
					dialog.overlay.css('height', $('body').height());
				}
			});
		}
		else {
			$.modal('<iframe src="' + modal_content + '" height="450" width="540" style="border:0" allowtransparency="true">', {
				dataId: "modal_iframe",
				containerCss:{
					backgroundColor:"transparent",
					height:442,
					padding:0,
					width:520
				}
			});
		}
	});
}

function initSmallSchedule()
{
	$("div#tonight-on-fxm div.head ul#week li a").click(function (e) {

		// disable default behavior
		if (e.preventDefault) {
			e.preventDefault();
		} else {
			e.returnValue = false;
		}
		
		// remove selected class from other items and hide all data
		$("div#tonight-on-fxm div.head ul#week li").removeClass("selected");
		$("div#tonight-on-fxm div.day-data").removeClass("show").addClass("hide");
		
		// make the parent the active item and show its data
		$(this).parent().addClass("selected");
		$("div#tonight-on-fxm div#" + $(this).attr('id')).removeClass("hide").addClass("show");
		$('.day-data.show').find('.tonight-more.va').vAlign();
	});
}


/********************************************************/
/** Main Init **/
/********************************************************/
$('document').ready(function() {
	
	initFxMenu();			//bind click action to menu tabs
	//initModals();			//bind click action to popup/modals
	initSmallSchedule();    //bind click action 
});
