/********************************************************************************************
TVNZ EPG Javascript Library
********************************************************************************************/

var jsTVNZepg = window.jsTVNZepg || {};

// Makes the init run at Document ready // Playstation doesn't like the on load
//$(window).load(function() {
$(function() {
	jsTVNZepg.initialize();
});

// the Initialize function, which runs on load, all sub-functions are called in here.
jsTVNZepg.initialize = function() {
	jsTVNZepg.Common().initEPGSize();
	jsTVNZepg.Common().initEPGCollapse();
	jsTVNZepg.Common().initBigEPG();
	
	www.getServerTime(function() {
		jsTVNZepg.Common().initScheduleMenu();
	});	
}

// TVNZ Common functions, that can be re-called at any point.
jsTVNZepg.Common = function() {
	// Finds out whats minimised on load
	var initEPGSize = function() {	
		$("#channels a").each(function (i) {
			var id = $(this).parent().attr("id");
			var cookieName = "epgTVNZ" + id
			var myCookie = jsTVNZ.Common().readCookie(cookieName);

			if (myCookie == id) {
				animEPG(id, "hide", this);
			}
		});
		
		function loadSize() {
			var heightChannel = 1 + $("#channels").height();
			$(".overflow").animate({height: heightChannel + 16 + "px"}), 500;
			$("#epgBottom").animate({height: heightChannel + 16 + 4 + "px"}), 700;
		}
		setTimeout(loadSize, 500);
	}

	// EPG Show/Hide - Click functions
	var initEPGCollapse = function() {
		$("#channels li a").hover(function() {	
			$(this).find("img").css({display: "block"});
			 },function(){
			$(this).find("img").css({display: "none"});
		});

		$("#channels li").each(function (i) {
			channelEPGRows = i + 1;
			if($(this).attr("class") == "tvnz") {
				tvnzEPGRows++;
			}
		});

		$("#minimiseChannels").click(function() {	
			$("#channels li").each(function (i) {						 
				var id = $(this).attr("id");
				var cam = $(this).children("a");
				animEPG(id, "hide", cam);
			});
			resizeEPGAll("hide");
			return false;
		});
		
		$("#expandChannels").click(function() {
			$("#channels li").each(function (i) {							 
				var id = $(this).attr("id");
				var cam = $(this).children("a");
				animEPG(id, "show", cam);
			});
			resizeEPGAll("show");
			return false;
		});
		
		$("#channels a").click(function() {
			var id = $(this).parent().attr("id");
			var way = ($(this).is('.channelShow'))? 'hide' : 'show';		
			animEPG(id, way, this);
			resizeEPG(way, this);
			return false;
        });
	}

	// EPG Show/Hide - Expand/Collapse All resize
	var resizeEPG = function(way, obj) {	
		var overflow		= $(".overflow").height();
		var heightChannel 	= ($(obj).parent().attr("class") != "tvnz")? 34 : 70;
		
		if(way == 'hide') {
			$(".overflow").animate({height: overflow - heightChannel + 16 + "px"}), 500;
			$("#epgBottom").animate({height: overflow - heightChannel + 16 + 4 + "px"}), 700;
		} else {
			$("#epgBottom").animate({height: overflow + heightChannel - 16 + 4 + "px"}), 500;
			$(".overflow").animate({height: overflow + heightChannel - 16 + "px"}), 700;
		}
	}
	
	var resizeEPGAll = function(way) {	
		var thismanyrows	= channelEPGRows - tvnzEPGRows;
		var thirty			= thismanyrows *35;
		var seventy			= tvnzEPGRows *71;
		var expandedHeight	= thirty + seventy +17;
		var minimiseHeight	= channelEPGRows *17 +17;
		
		if(way == 'hide') {
			$(".overflow").animate({height: minimiseHeight + "px"}), 500;
			$("#epgBottom").animate({height: minimiseHeight + 4 + "px"}), 700;
		} else {
			$("#epgBottom").animate({height: expandedHeight + 4 + "px"}), 500;
			$(".overflow").animate({height: expandedHeight + "px"}), 700;
		}
	}
	
	// EPG Show/Hide - Channel Row hide show
	var animEPG = function(id, way, obj) {
		var heightChannel 	= ($(obj).parent().attr("class") != "tvnz")? 34 : 70;

		if(way == 'hide') {
			$("." + id).addClass('rowHide');
			$("." + id).stop().animate({height: "16px"}), 500;
			$(obj).stop().animate({height: "16px"}), 500;

			$(obj).find("img").attr("src", rootURL + "images/epg/plus.gif").attr("alt", "expand");
			$(obj).removeClass('channelShow');
			$(obj).addClass('channelHide');
			
			jsTVNZ.Common().createCookie("epgTVNZ" + id, id, 365);
		} else {
			$(obj).stop().animate({height: heightChannel + "px"}), 1000;
			$("." + id).stop().animate({height: heightChannel + "px"}), 500;

			$(obj).find("img").attr("src", rootURL + "images/epg/minus.gif").attr("alt", "minimise");
			$(obj).removeClass('channelHide');
			$(obj).addClass('channelShow');

			jsTVNZ.Common().eraseCookie("epgTVNZ" + id);

			$("." + id).queue(function () {
				$(this).removeClass('rowHide');
				$(this).dequeue();
			});
		}
	}

	// EPG/Schedule main sliding menu
	var initScheduleMenu = function() {
		// Right arrow - scrolls 1 hour (240px) to the right.
		$("#arrows .arrowRight").mousedown(function() {
			$("#scrollable .overflow").stop().animate({ scrollLeft: parseInt($("#scrollable .overflow").scrollLeft()) + 240 }, 300);
			intMouseDown = setInterval(function() {
				$("#scrollable .overflow").stop().animate({ scrollLeft: parseInt($("#scrollable .overflow").scrollLeft()) + 240 }, 300);
			}, 300);
		}).mouseup(function() {
			clearInterval(intMouseDown);
		}).mouseout(function() {
			clearInterval(intMouseDown);
		}).click(function() {
			return false;
		});
		
		// Left arrow - scrolls 1 hour (240px) to the left
		$("#arrows .arrowLeft").mousedown(function() {
			$("#scrollable .overflow").stop().animate({ scrollLeft: parseInt($("#scrollable .overflow").scrollLeft()) - 240 }, 300);
			intMouseDown = setInterval(function() {
				$("#scrollable .overflow").stop().animate({ scrollLeft: parseInt($("#scrollable .overflow").scrollLeft()) - 240 }, 300);
			}, 300);
		}).mouseup(function() {
			clearInterval(intMouseDown);
		}).mouseout(function() {
			clearInterval(intMouseDown);
		}).click(function() {
			return false;
		});
		
		$("#scrollable .overflow").scroll(function() {
			$("#timeScroll").scrollLeft($(this).scrollLeft());
		});
		
		if ($("a[name="+String(www.serverTime.hours)+"00]").is("a") && $("#scrollable .overflow").is("div")) {
			var scrollValue = ($("#scrollable .overflow").scrollLeft() + $("a[name="+String(www.serverTime.hours)+"00]").offset().left) - $("#scrollable .overflow").offset().left - 32;
			$("#scrollable .overflow").animate({ scrollLeft: scrollValue }, 1000);
		}
		
	}
	
	// EPG Truncation if it goes over the cells height.
	var initBigEPG = function() {
		$("#epgBottom .truncate, #fullScheduleFlash .showsView").each(function() {
			$(this).children("li").each(function() {
				if ($(this).children(".synopsis").is("span") && $(this).not(".spacer")) {
					var height = 0;
					$(this).children().each(function() {
						height += $(this).height();
					});
					var totalHeight = $(this).height();
					if (totalHeight < height) {
						truncateAndCheck($(this).children(".synopsis"), totalHeight);
					}
				}
			});
		});
	}

	var truncateAndCheck = function(ele, totalHeight) {
		var height = 0;
		$(ele).siblings().each(function() {
			height += $(this).height();
		});
		var $ele = ($(ele).children("a").is("a")) ? $(ele).children("a") : $(ele);
		var newStr = truncateText($ele.text());
		$ele.html(newStr);
		var eleHeight = $(ele).height();
		if (newStr.length > 50) {
			if (totalHeight < (height + eleHeight)) {
				truncateAndCheck(ele, totalHeight);
			}
		} else {
			$(ele).remove();
		}
	}

	var truncateText = function(text) {
		return text.substring(0, text.lastIndexOf(" ")) + "&hellip;";
	}

	return {
		initEPGSize : initEPGSize,
		initEPGCollapse : initEPGCollapse,
		initBigEPG : initBigEPG,
		initScheduleMenu : initScheduleMenu
	}
}
