var is_widget = window.widget;
var interval, interval2;

function closeWidget()
{
	return window.close();
}
function toggle(imageId, divId)
{
	$("#" + divId).toggle();
	var obj = $("#" + imageId);
	if(obj.attr("title") == "Expand")
	{
		obj.attr({title: "Contract"});
		obj.removeClass("expand");
		obj.addClass("contract");
	}
	else
	{
		obj.attr({title: "Expand"});
		obj.removeClass("contract");
		obj.addClass("expand");
	}
}

function getCookie(key)
{				   
	c = $.cookie(key);
	if (c == null)
	{
		$.cookie(key, "");
		return "";
	}
	return c;
}
function setCookie(key, value)
{
	$.cookie(key, value, {expires: 365});
	return true;
}

function sendAlert(message)
{
	$("#alert span").html(message);
	$("#alert").fadeIn(1000, function () {
		$("#alert").fadeOut(500);
	  });
}

function removeFavourite(channelId)
{
	var favs = getFavourites();
	favs = favs.replace("," + channelId + ",", ",");
	if(setFavourites(favs))
		sendAlert("Favourite Removed");
	else
		sendAlert("Could not remove favourite!");
	loadChannels(false, false);
}
function addFavourite(channelId)
{
	var favs = getFavourites();
	favs = favs.replace("," + channelId + ",", ",");
	if(favs == "") favs = ",";
	favs = favs + channelId + ",";
  	if(setFavourites(favs))
		sendAlert("Favourite Added");
	else
		sendAlert("Could not add favourite!");
	loadChannels(false, false);
	//$("#debug").html("FAVS:" + getFavourites());
}

function getFavourites()
{
	if (!is_widget)
		return getCookie("favourites");
	else
		return widget.preferenceForKey(widget.identifier + "_favourites");
}

function setFavourites(value)
{
	if (!is_widget)
		return setCookie("favourites", value);
	else
	{
		widget.setPreferenceForKey(value, widget.identifier + "_favourites");
		return true;
	}
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function submitForm(formObj)
{
	var channel = formObj.channel.options[formObj.channel.selectedIndex].value;
	var day = formObj.day.options[formObj.day.selectedIndex].value;
	
	if(channel != "" && day != "")
	{
		loadGuide(channel, day);
	}
	else
	{
		initGuide();
	}
}
function showOverlay()
{
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	$("#overlay").css("height", arrayPageSize[1] + "px");
	$("#overlay").show();
}
function showLoading()
{
	showOverlay();
	$("#loading").show();
	checkScroll("loading");
	interval2 = window.setInterval("checkScroll('loading')", 500);
}
function hideLoading()
{
	$("#overlay").hide()
	$("#loading").hide()
}
function loadGuide(channel, day)
{	
	showLoading();
	loadUrl = "http://whatson.forbsoft.com/schedule.php?x=" + (new Date()).getTime() + "&f=" + getFavourites() + "&c=" + channel + "&d=" + day
	$("#result").load(loadUrl, {}, function(){
		hideLoading();
	});
}

function refreshGuide(channel, day)
{
	loadGuide(channel, day);
}
function toggleHelp()
{
	if(layerHidden())
	{
		showLoading();
		$("#layer h1").html("Help");
		$("#layer .content").load("http://whatson.forbsoft.com/content.php?t=help", {}, function(){
			hideLoading();
			toggleLayer();
		});
	}
	else
	{
		toggleLayer();
	}
}
function submitEmailProgramme(formObj)
{
	var email = $("#emailprogramme #email").val();
	var message = $("#emailprogramme #message").val();
	var vcode = $("#emailprogramme #vcode").val();
	var programmeId = $("#emailprogramme #programmeId").val();
	
	$("#emailprogramme .asterix").hide();
	var ok = true;
	if(email == "")
	{
		ok = false;
		$("#emailprogramme #email").css("border-color", "red");
	}
	if(message == "")
	{
		ok = false;
		$("#emailprogramme #message").css("border-color", "red");
	}
	if(ok)
	{
		$("#loading").show();
		checkScroll("loading");
		interval2 = window.setInterval("checkScroll('loading')", 500);
		var loadUrl = "http://whatson.forbsoft.com/content.php?t=emailsend&p=" + programmeId + "&e=" + encodeURIComponent(email) + "&m=" + encodeURIComponent(message) + "&v=" + encodeURIComponent(vcode);
		$("#layer .content").load(loadUrl, {}, function(){
			$("#loading").hide();
		});
	}
	return false;	
}
function skyPlus(programmeId)
{
	showLoading();
	$("#layer h1").html("Sky+");
	$("#layer .content").load("http://whatson.forbsoft.com/content.php?t=sky&p=" + programmeId, {}, function(){
		hideLoading();
		toggleLayer();
	});
}
function emailProgramme(programmeId)
{
	showLoading();
	$("#layer h1").html("Email");
	$("#layer .content").load("http://whatson.forbsoft.com/content.php?t=email&p=" + programmeId, {}, function(){
		hideLoading();
		toggleLayer();
	});
}
function layerHidden()
{
	return $("#layer").is(":hidden");
}
function toggleLayer()
{
	if(layerHidden())
	{
		$("#layer").show();
		checkScroll("layer");
		interval = window.setInterval("checkScroll('layer')", 500);
		showOverlay();
	}
	else
	{
		$("#overlay").hide();
		$("#layer").hide();
	}
}

function checkScroll(divId)
{
	var divObj = document.getElementById(divId);
    divObj.style.top = Math.round((document.documentElement.clientHeight/2)-(divObj.style.height/2)+document.documentElement.scrollTop)+'px';
    divObj.style.left = Math.round((document.documentElement.clientWidth/2)-(divObj.style.width/2))+"px";
}


function loadChannels(a, m)
{
	var loadUrl;
	if(a)
	{
		loadUrl = "http://whatson.forbsoft.com/channels.php?x=" + (new Date()).getTime() + "&a=1";
		$("#search").load(loadUrl, {}, function(){
			$(".fulllink").hide();
			$(".favslink").show();
		});
	}
	else
	{
		var favs = getFavourites();
		if(m)
		{
			if(favs.length < 2)		
				$("#title").html("<h1><img src=\"images/title-bullet.png\" alt=\"\" /> Programmes on now...</h1>");
			else
				$("#title").html("<h1><img src=\"images/title-bullet.png\" alt=\"\" /> On your favourites right now...</h1>");			
		}
		loadUrl = "http://whatson.forbsoft.com/channels.php?x=" + (new Date()).getTime() + "&f=" + favs;	
		$("#search").load(loadUrl);	
	}
}
function initGuide()
{
	var loadUrl;
	
	showLoading();
	loadChannels(false, true);
	$("#programmes").load("http://whatson.forbsoft.com/now.php?x=" + (new Date()).getTime() + "&f=" + getFavourites(), {}, function(){
		hideLoading();
	});
}

$('document').ready(function(){
	initGuide();						
	$('#dock').hide();						 

  if(!is_widget)
  {
    $('#widget_controls').hide();
  }
  
});

