/********************************************************************************************
TVNZ Javascript Library functions list:

jsTVNZ.initialize();
		
jsTVNZ.Common().getServerTime();
jsTVNZ.Common().animateLoop();
jsTVNZ.Common().updateClock();
jsTVNZ.Common().animateHeroPanel();
jsTVNZ.Common().initLiveNews();
jsTVNZ.Common().addHeroVideo();
jsTVNZ.Common().initVideoPlayer();
jsTVNZ.Common().switchVideoAdds();
jsTVNZ.Common().changeVideoSize();
jsTVNZ.Common().changeWinMedia();
jsTVNZ.Common().stopAnimation();
jsTVNZ.Common().startAnimation();
		
jsTVNZ.Common().initSlidePanel();
jsTVNZ.Common().initHeroPanel();
jsTVNZ.Common().initLatestNews();
jsTVNZ.Common().initTabs();
jsTVNZ.Common().initTVSchedule();
jsTVNZ.Common().initOnDemand();
jsTVNZ.Common().initNewsAndAffares();
jsTVNZ.Common().initShareMore();
jsTVNZ.Common().initSetHomepage();
jsTVNZ.Common().initWeatherLocation();
jsTVNZ.Common().initPoll();
jsTVNZ.Common().initPageControls();
jsTVNZ.Common().initImageGallery();
jsTVNZ.Common().initToggleAnimation();

jsTVNZ.Common().setCurrentDate();
jsTVNZ.Common().disableTools();
jsTVNZ.Common().createCookie();
jsTVNZ.Common().readCookie();
		
jsTVNZ.Common().fixUpMyIE6();
		
jsTVNZ.Common().jsFlashExternalCall();

********************************************************************************************/

var jsTVNZ = window.jsTVNZ || {};
var newsPanel = new Object();
var heroAnimating = false;
var heroClicked = false;
var stopAnimationLoop = false;
var loopCount = 1;

var globalObj = new Object();
 
var xmlMinutes;
var xmlHours;
var xmlTextDay;
var xmlDay;
var xmlTextMonth;
var xmlMonth;
var xmlYeah;
var xmlDate;

var dartPlayer;

// Video page global variables
var backupAdvertisments;
var lastQualityPlayed;

var rootURL = "http://images.tvnz.co.nz/tvnz_site_images/";

// Makes the init run at page load
$(window).load(function() {
	jsTVNZ.initialize();
});

var jsToLoad = new Array(
						{ src: "js/jquery.flydom.js", embed: true },
						{ src: "js/jsHTML.js", embed: true },
						{ src: "js/swfobject.js", embed: true },
						{ src: "js/jquery.dimensions.min.js", embed: true },
						{ src: "js/jquery.mousewheel.min.js", embed: true },
						{ src: "js/jScrollPane.js", embed: true },
						{ src: "js/thickbox.js", embed: true }
						);

jsTVNZ.embedJS = function() {
	for (var i = 0; i < jsToLoad.length; i++) {
		if (jsToLoad[i].embed) {
			$("head").append(unescape("%3Cscript src='" + rootURL + jsToLoad[i].src + "' type='text/javascript'%3E%3C/script%3E"));
		}
	}
}

// the Initialize function, which runs on load, all sub-functions are called in here.
jsTVNZ.initialize = function() {

	// Checks to ensure all JS has been loaded
	for (var i = 0; i < jsToLoad.length; i++) {
		$("head script").each(function() {
			if ($(this).attr("src") != undefined) {
				if ($(this).attr("src").indexOf(jsToLoad[i].src) != -1) {
					jsToLoad[i].embed = false;
				}
			}
		});
	}

	// calls the embed function, to embed the remainding JS
	jsTVNZ.embedJS();

	// Get server time
	/*jsTVNZ.Common().getServerTime(function() {
		// functions to call once the time has been returned
		if ($("#ts").is(":visible")) {
			jsTVNZ.Common().initTVSchedule();
		}
		if ($("#hpDate") != undefined) {
			jsTVNZ.Common().setCurrentDate();
		}
		if ($("#oneNews").is(":visible")) {
			jsTVNZ.Common().initLiveNews();
		}
	});*/
	
	// Below creates an interval to flip the news panels
	var animateInterval = setInterval(function() {
		jsTVNZ.Common().animateLoop();
		jsTVNZ.Common().updateClock();
	}, 6000);
	
	// checks to see if the hero Panel Exsists, if it does, initialize it.
	if ($("#heroPanel").is(":visible")) {
		jsTVNZ.Common().initHeroPanel();
	}
	
	// checks to see if the slide Panel(s) Exsists, if it does, initialize them.
	if ($(".slidePanel").is(":visible")) {
		jsTVNZ.Common().initSlidePanel();
	}

	// initializes page controlls:
	jsTVNZ.Common().initPageControls();
	
	// this is a function to stop animation when using a light box, and other pop "overs"
	jsTVNZ.Common().initToggleAnimation();
	
	// this controls the tabs on the latest news module
	jsTVNZ.Common().initLatestNews();
	
	// other tabs that are JS driven (currently not used)
	//jsTVNZ.Common().initTabs();

	// initilializes the Polls (currently not used)
	//jsTVNZ.Common().initPoll();
	
	// sets up the "set my homepage" button as per browser
	jsTVNZ.Common().initSetHomepage();
	
	// sets the current location showed in the top nav
	jsTVNZ.Common().initWeatherLocation();
	
	// tab control for On Demand module
	jsTVNZ.Common().initOnDemand();
	
	// controls for current news and affaris pop down
	jsTVNZ.Common().initNewsAndAffares();
	
	// controls the "share more" rss feeds on the article page
	jsTVNZ.Common().initShareMore();

	// controls the image gallery
	jsTVNZ.Common().initImageGallery();

	// initializes the video player
	jsTVNZ.Common().initVideoPlayer();

	// updates the clock as soon as the page loads
	jsTVNZ.Common().updateClock();
	
	// Below rips out all the PNG images in IE6, and uses the filter in an IE6 only stylesheet
	if ($.browser.msie && $.browser.version <= 6) {
		jsTVNZ.Common().fixUpMyIE6();
	}
}

// TVNZ Common functions, that can be re-called at any point.
jsTVNZ.Common = function() {

	var getServerTime = function(_callback) {
		$.get("/timer.xml?rand="+ord, function(xml) {
			var dayArray = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
			var monthArray = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
			var splitSpace = $(xml).find("timer").attr("current_time").split(" ");
			var splitTime = splitSpace[1].split("-");
			var splitDate = splitSpace[0].split("-");
			
			// populates the global variables
				xmlDate = new Date(splitDate[0], parseInt(splitDate[1])-1, splitDate[2], splitTime[0], splitTime[1], splitTime[2]);
				xmlHours = splitTime[0];
				xmlMinutes = splitTime[1];
				xmlTextDay = dayArray[xmlDate.getDay()];
				xmlDay = splitDate[2];
				xmlTextMonth = monthArray[parseInt(splitDate[1])-1];
				xmlMonth = splitDate[1];
				xmlYeah = splitDate[0];

			if (typeof(_callback) == "function") {
				_callback();
			}
		});
	}

	
	// Animation Loop, anything that needs to be animated every 6 seconds needs to go in here
	var animateLoop = function() {
		// there is a function to stop all animation, so if this is true, it wont animate.
		if (!stopAnimationLoop) {
			// Slide Panel Animation
			$(".slidePanel").each(function(){
				// If the mouse it not over the Slide Panel, the next button will be clicked.
				if (!newsPanel[$(this).parent().attr("id")]) {
					$(this).find(".cfbNext").click();
				}
			});
			
			// Hero Panel Animation
			$("#heroPanel .hero.selected").each(function() {
				// Checks to see if the Hero Panel has been clicked, or is animating
				if (!heroClicked && !heroAnimating) {
					// Below works out which is the next one to show, and shows it.
					if ($(this).parent().find("#hero" + (parseInt($(this).attr("id").substring(4,5)) + 1) + "Mini").is(":visible")) {
						jsTVNZ.Common().animateHeroPanel("hero" + (parseInt($(this).attr("id").substring(4,5)) + 1) + "Mini");
					} else {
						jsTVNZ.Common().animateHeroPanel($(".heroMini.selected").filter(":first").attr("id"));
					}
				} else {
					// if it has been clicked recently, below changes it so it hasnt been clicked, so it will pick up on the next animation.
					heroClicked = false;
				}
			});
		}
	}
	
	// Update Clock Function, gets the time from local machine.
	var updateClock = function() {
		var jsDate = new Date();
		var clockMins = jsDate.getMinutes();
		var clockHours = jsDate.getHours();
		var strClockMinutes = clockMins.toString();
		var strClockHours = clockHours.toString();
		// Hours
		if (clockHours > 9) {
			// If the hours are over 9, do the following
			if (clockHours > 12) {
				// if the hours are over 12, its PM
				$("#latestNewsTime .ampm").css({backgroundPosition: "0px -16px"});
				// Takes 12 hours off because its not showen in 24 hour times
				clockHours = clockHours - 12;
				if (clockHours < 9) {
					// if the time is user 9 hours, only do the first didget
					$("#latestNewsTime .hour1").css({backgroundPosition: "0px 0px"});
					$("#latestNewsTime .hour2").css({backgroundPosition: "0px -" + (clockHours * 16) + "px"});
				} else {
					// else its got bot didgets
					// convets it to a new string so we can substring it.
					strClockHours = clockHours.toString();
					$("#latestNewsTime .hour1").css({backgroundPosition: "0px -" + (parseInt(strClockHours.substring(0,1)) * 16) + "px"});
					$("#latestNewsTime .hour2").css({backgroundPosition: "0px -" + (parseInt(strClockHours.substring(1,2)) * 16) + "px"});
				}
			} else {
				// If the time wasnt over 12, then its am and we use both didgets.
				$("#latestNewsTime .ampm").css({backgroundPosition: "0px 0px"});
				$("#latestNewsTime .hour1").css({backgroundPosition: "0px -" + (parseInt(strClockHours.substring(0,1)) * 16) + "px"});
				$("#latestNewsTime .hour2").css({backgroundPosition: "0px -" + (parseInt(strClockHours.substring(1,2)) * 16) + "px"});
			}	
		} else {
			//if the time is under 9 hours, its AM and we only use the second didget
			$("#latestNewsTime .ampm").css({backgroundPosition: "0px 0px"});
			$("#latestNewsTime .hour1").css({backgroundPosition: "0px 0px"});
			$("#latestNewsTime .hour2").css({backgroundPosition: "0px -" + (clockHours * 16) + "px"});
		}
		// Minutes
		if (clockMins > 9) {
			// If the minutes if over 9, change both minute didgets
			$("#latestNewsTime .min1").css({backgroundPosition: "0px -" + (parseInt(strClockMinutes.substring(0,1)) * 16) + "px"});
			$("#latestNewsTime .min2").css({backgroundPosition: "0px -" + (parseInt(strClockMinutes.substring(1,2)) * 16) + "px"});
		} else {
			// Else we will set the first didget to 0, and the second to the correct minute
			$("#latestNewsTime .min1").css({backgroundPosition: "0px 0px"});
			$("#latestNewsTime .min2").css({backgroundPosition: "0px -" + (clockMins * 16) + "px"});
		}

		$("#latestNewsTime").css({visibility: "visible"});
	}
	
	var stopAnimation = function() {
		stopAnimationLoop = true;
	}
	
	var startAnimation = function() {
		stopAnimationLoop = false;
	}
	
	var animateHeroPanel = function(element) {
		heroAnimating = true;
	
		var thisID = element.substring(0, 5);
		
		// Animates the arrow in
		$("#heroPanel .heroPanelContent .selectedArrow, #heroPanel .heroPanelContent .breakingNewsArrow").stop().animate({left: '390px'}, 125, function() {
		
			// Animates the old 'selected' hero out, and the mini version in
			$("#heroPanel .heroPanelNav .hero.selected").animate({opacity: '0'}, 500, function() {
				$(this).removeClass("selected");
				$(this).parent().find("#"+$(this).attr("id")+"Mini").addClass("selected").css({opacity: "0"}).animate({opacity: '1'}, 500);
			});
			var heroTicCounter = setInterval(function() {
				if (element == "hero1Mini") {
					$("#"+element).parent().find("#hero2Mini").animate({top: '134'}, 250);
				} else if (element == "hero3Mini") {
					$("#"+element).parent().find("#hero2Mini").animate({top: '43'}, 250);
				}
				clearInterval(heroTicCounter);
			}, 250);
			
			// Animates the 'clicked' mini hero out, and the big version in
			$("#"+element).animate({opacity: '0'}, 500, function() {
				$(this).removeClass("selected");
				if ($(this).parent().find("#"+thisID).hasClass("breakingNews")) {
					$(this).parent().find("#"+thisID).addClass("selected").css({opacity: "0"}).animate({opacity: '1'}, 500, function() {
						$("#heroPanel .heroPanelContent .breakingNewsArrow").css({top: (parseInt($(this).css("top").substring(0, ($(this).css("top").length - 2))) + 47) + "px"}).animate({left: '370px'}, 125);
					});
				} else {
					$(this).parent().find("#"+thisID).addClass("selected").css({opacity: "0"}).animate({opacity: '1'}, 500, function() {
						$("#heroPanel .heroPanelContent .selectedArrow").css({top: (parseInt($(this).css("top").substring(0, ($(this).css("top").length - 2))) + 47) + "px"}).animate({left: '370px'}, 125);
					});
				}
			});			
			
			// Animates the images the correct ones
			$("#heroPanel .heroPanelContent #" + $("#heroPanel .heroPanelNav .hero.selected").attr("id") +"Cnt").animate({opacity: '0'}, 500, "easeout", function() { 
				$(this).removeClass("selected");
				if ($("#heroPanel .heroPanelContent #" + thisID +"Cnt").hasClass("videoHolder")) {
					$("#heroPanel .heroPanelContent #" + thisID +"Cnt").css({opacity: "1"}).addClass("selected");
					jsTVNZ.Common().addHeroVideo(vidArray[thisID], thisID + "Cnt");
					var videoTicCounter = setInterval(function() {
						heroAnimating = false;
						clearInterval(videoTicCounter);
					}, 750);
				} else {
					$("#heroPanel .heroPanelContent #" + thisID +"Cnt").addClass("selected").css({opacity: '0'}).animate({opacity: '1'}, 500, function() {
						var videoTicCounter = setInterval(function() {
							heroAnimating = false;
							clearInterval(videoTicCounter);
						}, 250);
					});
				}
			});
		});
		var $that = $("#heroPanel .heroPanelContent .hoverArrow");
		// stops the hover Arrow and hides it, then animates the mini back to the un-hovered color
		$that.stop().animate({left: '390px'}, 125, function() {
			$("#heroPanel .heroPanelNav .heroMini").each(function() {
				if (($(this).css("backgroundColor") == "rgb(209, 213, 209)") || ($(this).css("backgroundColor") == "rgb(209,213,209)")) {
					$(this).css({backgroundColor: '#EBEFEF'});
				}
			});
		});
	}
	
	// add's a video into the js version of the hero panel.
	var addHeroVideo = function(name, target) {
		var so = new SWFObject(rootURL + "flash/videoHero.swf", "heroFlash" + name, 390, 220, 8, "#FFFFFF");
		so.addVariable("videoPath", rootURL + name + ".flv");
		so.addVariable("previewPath", rootURL + "flash/" + name + ".jpg");
		so.addParam("wmode", "transparent");
		so.write(target);
	}
	
	var initLiveNews = function() {
		var selectedShow;
		var showTheShow = true;
		if (xmlTextDay == "Saturday" || xmlTextDay == "Sunday") {
			$(liveNewsDB.weekends).each(function(i) {
				if ((this.hours < xmlHours) || ((this.hours == xmlHours) && (this.minutes <= xmlMinutes))) {
					selectedShow = this;
				}
			});
		} else {
			$(liveNewsDB.weekdays).each(function(i) {
				if ((this.hours < xmlHours) || ((this.hours == xmlHours) && (this.minutes <= xmlMinutes))) {
					selectedShow = this;
				}
			});
		}
		if (selectedShow.noshow != undefined) {
			if ((typeof(selectedShow.noshow) == "boolean" && selectedShow.noshow == true)) {
				showTheShow = false;
				$("#oneNews .thickbox").attr("title", selectedShow.title);
				$("#oneNews .thickbox img").attr("src", selectedShow.img).css({display: "block", alt: selectedShow.title});
				$("#oneNews .thickbox").unbind('click').removeAttr("href").removeAttr("class");
			} else {
				$(selectedShow.noshow).each(function(i) {
					if (jsTVNZ.Common().dateWithin(this.from, this.to, xmlDate) == true) {
						showTheShow = false;
					}
				});
			}
			if (showTheShow) {
				$("#oneNews .thickbox").attr("title", selectedShow.title);
				$("#oneNews .thickbox img").attr("src", selectedShow.img).css({display: "block", alt: selectedShow.title});
				$("#oneNews .thickbox").click(function() {
					$("#iframeVideoStream").attr("src", selectedShow.url);
				});
			}
		} else {
			$("#oneNews .thickbox").attr("title", selectedShow.title);
			$("#oneNews .thickbox img").attr("src", selectedShow.img).css({display: "block", alt: selectedShow.title});
			$("#oneNews .thickbox").click(function() {
				$("#iframeVideoStream").attr("src", selectedShow.url);
			});
		}
	}
	
	var dateWithin = function(beginDate,endDate,checkDate) {
		var b,e,c;
		b = Date.parse(beginDate);
		e = Date.parse(endDate);
		c = Date.parse(checkDate);
		if((c <= e && c >= b)) {
				return true;
		}
		return false;
	}
	
	var initVideoPlayer = function() {
		if ($("#vidPlayer").is(":visible")) {
			var videoQualityFLV = jsTVNZ.Common().readCookie("videoTVNZ-Quality-FLV");
			var videoQualityWMP = jsTVNZ.Common().readCookie("videoTVNZ-Quality-WMP");
			var videoType = $("#vidPlayer").hasClass("liveStream") ? "wmp" : jsTVNZ.Common().readCookie("videoTVNZ-Type");
			if (((videoQualityWMP != null) || (videoQualityFLV != null))&& (videoType != null)) {
				switch (videoType) {
					case "wmp":
						switch(videoQualityWMP) {
							case "low":
								if (wmpURL != "") {
									jsTVNZ.Common().changeWinMedia("low");	
								} else {
									jsTVNZ.Common().changeVideoSize("low");
								}
							break;
							default:
								if (wmpURL != "") {
									jsTVNZ.Common().changeWinMedia("mid");
								} else {
									jsTVNZ.Common().changeVideoSize("mid");
								}
							break;
						}
					break;
					default:
						switch(videoQualityFLV) {
							case "high":
								if (vpChapters != "") {
									jsTVNZ.Common().changeVideoSize("high");
								} else {
									jsTVNZ.Common().changeWinMedia("mid");
								}
							break;
							case "low":
								if (vpChapters != "") {
									jsTVNZ.Common().changeVideoSize("low");
								} else {
									jsTVNZ.Common().changeWinMedia("low");
								}
							break;
							default:
								if (vpChapters != "") {
									jsTVNZ.Common().changeVideoSize("mid");
								} else {
									jsTVNZ.Common().changeWinMedia("mid");
								}
							break;
						}
					break;
				}
			} else {
				//jsTVNZ.Common().changeVideoSize("mid");
				jsTVNZ.Common().detectVideoBandwidth();
			}
		}
	}
	
	var detectVideoBandwidth = function() {
		var so = new SWFObject(rootURL + "flash/bandwidthDetection.swf", "player", 400, 284, "9.0.124.0", "#FFFFFF");

			so.addParam("allowScriptAccess", "always");
			so.addParam("wmode", "transparent");
			so.addParam("allowFullScreen", "true");
			so.addParam("swLiveConnect", "true");

			so.useExpressInstall(rootURL + "flash/playerProductInstall.swf");

			so.addVariable("xmlSetup", rootURL + "flash/xml/setup.xml");

		so.write("vidPlayer");
	}

	var switchVideoAdds = function(type) {
		if (backupAdvertisments == undefined) {
			backupAdvertisments = companionAdvertisements;
		}
		var tmpArr = new Array();
		for (var i in backupAdvertisments) {
			if (backupAdvertisments[i].url.indexOf(type) != -1) {
				tmpArr.push(backupAdvertisments[i]);
			}
		}
		companionAdvertisements = tmpArr;
		if (type == "windowsmedia") {
			setCompanionAds(null);
		}
	}

	// add's a video into the js version of the hero panel.
	var changeVideoSize = function(quality) {

		if (vpChapters != "") {

			jsTVNZ.Common().createCookie("videoTVNZ-Quality-FLV", quality, 365);
			jsTVNZ.Common().createCookie("videoTVNZ-Type", "flash", 365);
			switch (quality) {
				case "high":
					var swfFile = rootURL + "flash/player_news_high.swf";
					var swfWidth = 640;
					var swfHeight = 420;
					var swfBW = "_300k.flv";/*"_256k.flv";*/
				break;
				case "low":
					var swfFile = rootURL + "flash/player_news_low.swf";
					var swfWidth = 400;
					var swfHeight = 284;
					var swfBW = "_56k.flv";
				break;
				default:
					var swfFile = rootURL + "flash/player_news_mid.swf";
					var swfWidth = 400;
					var swfHeight = 284;
					var swfBW = "_128k.flv";
				break;
			}

			if (quality == 'high') {
				$("#colOne .article").addClass("wideVideo");
			} else {
				$("#colOne .article").removeClass("wideVideo");
			}

			// switch's the add's to the flash adds
			jsTVNZ.Common().switchVideoAdds("flashprogressive");

			
			var so = new SWFObject(swfFile, "player", swfWidth, swfHeight, "9.0.124.0", "#FFFFFF");

				so.addParam("allowScriptAccess", "always");
				so.addParam("wmode", "transparent");
				so.addParam("allowFullScreen", "true");
				so.addParam("swLiveConnect", "true");

				so.useExpressInstall(rootURL + "flash/playerProductInstall.swf");

				so.addVariable("duration", vpDuration);
				so.addVariable("cid", vpCid);
				so.addVariable("title", vpTitle);
				so.addVariable("previewURL", vpPreview);
				so.addVariable("chapterURLs", vpChapters + swfBW);
				so.addVariable("CG", vpCG);
				so.addVariable("co", vpCo);

			so.write("vidPlayer");
		}
	}

	var changeWinMedia = function(quality) {
		if (wmpURL != "") {
			if (quality == undefined) {
				quality = "mid";
			}
			jsTVNZ.Common().createCookie("videoTVNZ-Quality-WMP", quality, 365);
			jsTVNZ.Common().createCookie("videoTVNZ-Type", "wmp", 365);
			switch (quality) {
				case "low":
					var wmpWidth = 400;/*200;*/
					var wmpHeight = 276;/*142;*/
					var wmpBW = "_56k.asx";
				break;
				default:
					var wmpWidth = 400;
					var wmpHeight = 276;
					var wmpBW = "_128k.asx";
			}
			// this ensures that if the last player was wide video it it now removed
			$("#colOne .article").removeClass("wideVideo");

			// this function is called from flash, it takes global variables that should be on the page
			if ($("#vidPlayer").hasClass("noVideoAd")) {
				jsHTML.objWMP(wmpContent + wmpBW, wmpHeight, wmpWidth, wmpName, $("#vidPlayer").attr("id"), quality);
			} else {
				jsHTML.objWMP(wmpURL + "?zone=" + wmpZone + "&content=" + wmpContent + wmpBW + "&ord=" + ord, wmpHeight, wmpWidth, wmpName, $("#vidPlayer").attr("id"), quality);
			}
			
			davInitStream(document.MediaPlayer, _rsdURL,  _rsdCI, _rsdTL, _rsdFreq, _rsdCG, _rsdSI, _rsdTC, _rsdPCG, _rsdCustom);
			
			// sets the add's to the window media player adds
			if (typeof(registerDartPlayer) == "function") {
				registerDartPlayer(wmpName);
			}
			
			jsTVNZ.Common().switchVideoAdds("windowsmedia");

			$(".changeToFullscreen").click(function() {
				var mp = document[wmpName];
				if (mp) {
					if (mp.playState === 3) mp.fullScreen = true;
					else if (mp.DisplaySize !== undefined) mp.DisplaySize = 3;
					else alert("Sorry, the Full Screen button is not supported in this browser.\n\n"+
							"Please right-click on the video picture and choose 'Full Screen' or 'Zoom' -> 'Full Screen'");
				}
				return false;
			});
			$(".changeToFlash").click(function() {
				if ($.browser.msie) {
					jsTVNZ.Common().createCookie("videoTVNZ-Quality-FLV", quality, 365);
					jsTVNZ.Common().createCookie("videoTVNZ-Type", "flash", 365);
					window.location.reload();
				} else {
					jsTVNZ.Common().changeVideoSize(quality);
				}
				return false;
			});
			$(".changeQualityHigh").click(function() {
				if ($.browser.msie) {
					jsTVNZ.Common().createCookie("videoTVNZ-Quality-WMP", "mid", 365);
					jsTVNZ.Common().createCookie("videoTVNZ-Type", "wmp", 365);
					window.location.reload();
				} else {
					jsTVNZ.Common().changeWinMedia("mid");
				}
				return false;
			});
			$(".changeQualityLow").click(function() {
				if ($.browser.msie) {
					jsTVNZ.Common().createCookie("videoTVNZ-Quality-WMP", "low", 365);
					jsTVNZ.Common().createCookie("videoTVNZ-Type", "wmp", 365);
					window.location.reload();
				} else {
					jsTVNZ.Common().changeWinMedia("low");
				}
				return false;
			});
		}
	}
	
	// Initilize Slide Panels
	var initSlidePanel = function() {
		// Below loops through all the newsPanels on this page, and add's the click functions.
		$("div.slidePanel").each(function() {
			var thisPanelsID = $(this).parent().attr("id");
			// Below gets the count of the number of articals(Groups) in each newsPanel
			$(this).find(".cfbMax").html($(this).find(".group").length);
			$(this).find(".cfbCurrent").html("1");
			// below loops through and hides all but the current artical(Group)
			for (var i = 1; i <= $(this).find(".cfbMax").html(); i++) {
				if (i != $(this).find(".cfbCurrent").html()) {
					$("#" + thisPanelsID + i).hide();
				}
			}
			if ($(this).find(".group").length == 1) { 
				// if there's only one, dont animate this panel	
				newsPanel[thisPanelsID] = true;
			} else {
				// Below toggles the global news panel hoverstate
				$(this).hover(function() {
					newsPanel[thisPanelsID] = true;
				}, function() {
					newsPanel[thisPanelsID] = false;
				});
			}
			// Below adds the click function to the next and previous buttons, spacific to the newsPanel it belongs too.
			$(this).find(".cfbPrevious").click(function() {
				if ($(this).parent().find(".cfbCurrent").html() == 1) {
					$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).animate({opacity:'hide'}, 1500, "easeout");
					$(this).parent().find(".cfbCurrent").html($(this).parent().find(".cfbMax").html());
					$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).animate({opacity:'show'}, 1500, "easeout");
				} else {
					$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).animate({opacity:'hide'}, 1500, "easeout");
					$(this).parent().find(".cfbCurrent").html(parseInt($(this).parent().find(".cfbCurrent").html()) - 1);
					$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).animate({opacity:'show'}, 1500, "easeout");
				}
				return false;
			});
			$(this).find(".cfbNext").click(function() {
				if ($(this).parent().find(".cfbCurrent").html() == $(this).parent().find(".cfbMax").html()) {
					$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).animate({opacity:'hide'}, 1500, "easeout");
					$(this).parent().find(".cfbCurrent").html("1");
					$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).animate({opacity:'show'}, 1500, "easeout");
				} else {
					$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).animate({opacity:'hide'}, 1500, "easeout");
					$(this).parent().find(".cfbCurrent").html(parseInt($(this).parent().find(".cfbCurrent").html()) + 1);
					$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).animate({opacity:'show'}, 1500, "easeout");
				}
				return false;
			});
		});
	}
	
	var initHeroPanel = function() {
		// Below loops through all Mini Hero buttons
		$("#heroPanel .heroPanelNav .heroMini").each(function() {
			// Add's a click function (if not Animating)
			$(this).click(function() {
				if (!heroAnimating) {	
					heroClicked = true;
					jsTVNZ.Common().animateHeroPanel($(this).attr("id"));
				}
			}).css({cursor: "pointer"}).removeAttr("href");
			// Hover over and out states
			$(this).hover(function() {
				$(this).css({backgroundColor: '#D1D5D1', textDecoration: 'underline'});
				$("#heroPanel .heroPanelContent .hoverArrow").stop().css({top: (parseInt($(this).css("top").substring(0, ($(this).css("top").length - 2))) + 13) + "px", left: "390px"}).animate({left: '380px'}, 125);
			}, function() {
				$(this).css({backgroundColor: '#EBEFEF', textDecoration: 'none'});
				$("#heroPanel .heroPanelContent .hoverArrow").stop().css({left: '390px'});
			});
			// Add's a one pixel white seperator that splits the "Mini" buttons appart
			if ($(this).attr("id") != "hero3Mini") {
				$(this).css({borderBottom: "1px solid #fff", height: "42px"})
			}
		});
		// Add's a one pixel white seperator that splits the full sized article appart
		$("#heroPanel .heroPanelNav .hero").each(function() {
			if ($(this).attr("id") != "hero3") {
				$(this).css({borderBottom: "1px solid #fff", height: "133px"})
			}
		});
		// Inits the video if the first panel is a video
		if ($("#heroPanel .heroPanelContent .selected").hasClass("videoHolder")) {
			jsTVNZ.Common().addHeroVideo(vidArray[$("#heroPanel .heroPanelContent .selected").attr("id").substring(0, 5)], $("#heroPanel .heroPanelContent .selected").attr("id"));
		}
	}
	
	var initLatestNews = function() {
		// Latest news tabs
		$(".quickTabModule .mainNav a").each(function() {
			$(this).click(function(){
				var $this = $(this);
				if (!$(this).hasClass("selected")) {
					$(this).parent().parent().find(".selected").removeClass("selected");
					$(this).addClass("selected");
					$(this).parent().parent().parent().parent().find(".articles .selected").removeClass("selected");
					$(this).parent().parent().parent().parent().find(".articles ." + $this.children("span").children("span").attr("class")).addClass("selected");
					$(this).removeAttr("style");
					$(this).find("span").removeAttr("style");
					$(this).find("span").find("span").removeAttr("style");
				}
				return false;
			}).css({cursor: "pointer"});
		});
		// checks the height of the items in the read tab, and will remove any overlapping new items
		var x = 0;
		$("#latestNews .read li").each(function() {
			x += $(this).height() + parseInt($(this).css("padding-top")) + parseInt($(this).css("padding-bottom")) + parseInt($(this).css("margin-top")) + parseInt($(this).css("margin-bottom"));
			if (($("#latestNews .articles").height() - x) > 0) {
				$(this).removeAttr("style").addClass("last");
				$(this).prev().removeClass("last");
			}
		});
	}
	
	var initTabs = function() {		
		// Tabs - used on Search and Article detail page 
		$(".tabs a").each(function() {
			$(this).click(function(){
				var $this = $(this);
				// Checks to see if the one clicked is already selected, if so, do nothing.
				if (!$(this).hasClass("selected")) {
					$(".tabs a.selected").removeClass("selected");
					$(".article .viewSwitch").removeClass("viewSwitch");
					$(".article ." + $this.attr("class")).addClass("viewSwitch");
					$(this).addClass("selected");
					if ($("#tertiaryNav").is(":visible")) {
						$("#tertiaryNav").removeAttr("style");
						$("#tertiaryNav").height($("#tertiaryNav").parent().height());
					}
				}
				// Below is for the "tool bar", if we are not ont he read tab, disable it, else if we are on the read tab, make sure its not disabled.
				if (!$(this).hasClass("readSwitch")) {
					$(".tools p").each(function() {
						$(this).filter(":first-child").addClass("disabled");
					});
				} else {
					$(".tools p").each(function() {
						$(this).filter(":first-child").removeClass("disabled");
					});
				}
				// Below checks to see if the "share move" dialog box is on the screen, if so, hide it.
				if ($("#shareMore").is(":visible")) {
					$("#shareMore").css({display: 'none'});
				}
				return false;
			}).css({cursor: "pointer"});
		});
		
		// Below if for controlls inside a tab that will also toggle the tab itself.
		$(".tabControl").each(function() {
			$(this).click(function() {
				if ($(this).hasClass("watch")) {
					$(".tabs a.watchSwitch").click();
				} else if ($(this).hasClass("see")) {
					$(".tabs a.seeSwitch").click();
				}
				return false;
			}).css({cursor: "pointer"});
		});
		
		// Tertiary Nav Height
		if ($("#tertiaryNav").is(":visible")) {
			$("#tertiaryNav").height($("#tertiaryNav").parent().height());
		}
	}
	
	var initTVSchedule = function() {
		// TV Schedule
		$("#ts .tsStation a").each(function() {
			$(this).click(function() {
				if ($("#ts .tsStation a.selected").parent().attr("class") != $(this).parent().attr("class")) {
					var oldSelected = $("#ts .tsStation a.selected").parent().attr("class");
					$("#ts .tsStation a.selected").removeClass("selected");
					$(this).addClass("selected");
					$("#ts .tsScroller ." + oldSelected).animate({left: "-282px"}, 250, function() {
						$(this).css({left: "282px"}).removeClass("selected");
					});
					$("#ts .tsScroller ." + $(this).parent().attr("class")).css({left: "282px"}).animate({left: "0px"}, 250, function() {
						$(this).addClass("selected");
						// add's the JS Scroll Bar to the TV Schedule
						$('.tsScroller').jScrollPane({
							scrollbarWidth:17,
							scrollbarMargin:4,
							showArrows:true,
							dragMinHeight: 36,
							dragMaxHeight: 36
						});
						var selectedEle;
						$(".scheduledData.selected .time").each(function() {
							var _time = $(this).html();
							_time = _time.toString().split(":");
							if ((_time[0] < xmlHours) || ((_time[0] == xmlHours) && (_time[1] <= xmlMinutes))) {
								selectedEle = this;
							}
						});
						var $pane = $('.tsScroller');
						$pane[0].scrollTo(selectedEle.offsetTop, true);
					});
				}
				return false;
			}).css({cursor: "pointer"});
		});
		
		// add's the JS Scroll Bar to the TV Schedule
		$('.tsScroller').jScrollPane({
			scrollbarWidth:17,
			scrollbarMargin:4,
			showArrows:true,
			dragMinHeight: 36,
			dragMaxHeight: 36
		});
		
		var firstEle = true;
		$(".scheduledData").each(function() {
			var selectedEle;
			$(this).find(".time").each(function() {
				var _time = $(this).html();
				_time = _time.toString().split(":");
				if ((_time[0] < xmlHours) || ((_time[0] == xmlHours) && (_time[1] <= xmlMinutes))) {
					selectedEle = this;
				}
			});
			$(selectedEle).addClass("selected");
			$(selectedEle).next("td").addClass("selected");
			if (firstEle) {
				var $pane = $('.tsScroller');
				$pane[0].scrollTo(selectedEle.offsetTop, true);
				firstEle = false;
			}
		});

	}
	
	var initOnDemand = function() {
		//onDemand
		$("#onDemand .mainNav a").each(function() {
			$(this).click(function() {
				if (!$(this).parent().hasClass("selected")) {
					$("#onDemand .mainNav .selected").removeClass("selected");
					$("#onDemand .main .selected").removeClass("selected");
					$(this).parent().addClass("selected");
					$("#onDemand .main ." + $(this).attr("class")).addClass("selected");
					$(this).removeAttr("style");
					$(this).find("span").removeAttr("style");
				}
				return false;
			}).css({cursor: "pointer"});
		});
	}
	
	var initNewsAndAffares = function() {
		// News and Current Affairs button (header)
		$("#header .search .affairs").click(function() {
			if (!$("#affairsContent").is(":visible")) {
				$("#affairsContent").show().css({visibility: "hidden"});
				$(this).parent().addClass("selected");
				$("#main").animate({paddingTop: ($("#affairsContent").height()-1) + "px"}, 250);
				$("#affairsContent").css({visibility: "visible"}).hide().animate({height: "show"}, 200);
			} else {
				$("#affairsContent").animate({height: "hide"}, 350, function() {
					$(this).css({display: "none"});
				});
				$(this).parent().removeClass("selected");
				$("#main").animate({paddingTop: "0px"}, 200);
			}
			return false;
		}).css({cursor: "pointer"});
		$("#header .search .affairs").hover(function() {
			if (!$(this).parent().hasClass("selected")) {
				$(this).parent().css({backgroundPosition: "0px 0px"});
			}
		}, function() {
			if (!$(this).parent().hasClass("selected")) {
				$(this).parent().css({backgroundPosition: "0px 37px"});
			}
		});
	}
	
	var initShareMore = function() {
		// Share Box
		$(".shareMore").each(function() {
			var $this = $(this);
			$(this).click(function() {
				if (($("#shareMore").is(":hidden") == false) && ($("#shareMore").is(":visible") == false)) {
					// calls the HTML from jsHTML
					jsHTML.shareMore(this, jsTVNZ.Common().getGlobal('articleTitle'), jsTVNZ.Common().getGlobal('articleURL'));
					$("#shareMore .close").click(function() {
						$("#shareMore").animate({height: "hide"}, 250);
						return false;
					}).css({cursor: "pointer"});
					$("#shareMore").animate({height: "show"}, 250);
				} else if ($("#shareMore").is(":visible")) {
					$("#shareMore").animate({height: "hide"}, 125, function() {
						$(this).css({left: ($this.offset().left - 380) + 'px', top: ($this.offset().top + 15) + 'px'}).animate({height: "show"}, 125);
					});
				} else {
					$("#shareMore").css({left: ($(this).offset().left - 380) + 'px', top: ($(this).offset().top + 15) + 'px'});
					$("#shareMore").animate({height: "show"}, 250);
				}
				return false;
			}).css({cursor: "pointer"});
		});
	}

	var setGlobal = function(_variable, _value) {
		globalObj[_variable] = _value;
	}

	var getGlobal = function(_variable) {
		return globalObj[_variable];
	}
	
	var initSetHomepage = function() {
		$("#header .topNav .setHomePage").click(function() {
			if ($.browser.msie) {
				// add's Behaviour to the body tag, then sets the homepage, IE Only
				$("body").css({behavior: 'url(#default#homepage)'});
				document.body.setHomePage('http://tvnz.co.nz');
				return false;
			} else if ($.browser.mozilla) {
				if (($("#makeThisMyHP").is(":hidden") == false) && ($("#makeThisMyHP").is(":visible") == false)) {
					// calls the HTML for Mozilla out of the jsHTML
					jsHTML.makeHomeMozilla(this);
					$("#makeThisMyHP .close").click(function() {
						$("#makeThisMyHP").animate({height: "hide"}, 250);
					}).css({cursor: "pointer"});
				}
				jsTVNZ.Common().stopAnimation();
				$("#makeThisMyHP").animate({height: "show"}, 250, function() {
					jsTVNZ.Common().startAnimation();
				});
				return false;
			}
		});
	}
	
	var initWeatherLocation = function() {
		// an alt text map to show the correct alt text on the icons
		var altTextMap = {
			'FEWSHOWERS': 'Few Showers',
			'SHOWERS': 'Showers',
			'CLOUD': 'Cloudy',
			'DRIZZLE': 'Drizzle',
			'FEWSNOWSHOWERS': 'Few Snow Showers',
			'FINE': 'Fine',
			'FOG': 'Foggy',
			'HAIL': 'Hail',
			'HEAVYRAIN': 'Heavy Rain',
			'PARTCLOUDY': 'Partially Cloudy',
			'RAIN': 'Rain',
			'SNOW': 'Snow',
			'SUNNY': 'Sunny',
			'THUNDER': 'Thunder',
			'WIND': 'Windy'
		}
		// grabs the weather cookie
		var myCookie = jsTVNZ.Common().readCookie("weatherTVNZ");
		// changes the visibility to visible, and display none (as some browsers will not alow you to edit an element thats invisible)
		$("#header .weather").css({visibility: "visible", display: "none"});
		// checks to see if the cookie exsists
		if (myCookie != null) {
			// if it exsists, use the cookie to write the current weather
			if (typeof(towns) == "object") {
				$("#header .weather strong.location").html(myCookie.replace("_", " ") + ": ");
				$("#header .weather img.icon").attr("src", rootURL + "images/weather/icon" + town_data[myCookie.toUpperCase()].icon + ".jpg").attr("alt", altTextMap[town_data[myCookie.toUpperCase()].icon]).attr("title", altTextMap[town_data[myCookie.toUpperCase()].icon]);
				$("#header .weather .high strong").html(town_data[myCookie.toUpperCase()].max);
				$("#header .weather .low strong").html(town_data[myCookie.toUpperCase()].min);
				$("#header .weather").css({display: "block"});
			}
		} else {
			// else it will pick a random number and use that.
			if (typeof(towns) == "object") {
				var randomNo = Math.floor(Math.random() * towns.length);
				$("#header .weather strong.location").html(towns[randomNo].toLowerCase().replace("_", " ") + ": ");
				$("#header .weather img.icon").attr("src", rootURL + "images/weather/icon" + town_data[towns[randomNo]].icon + ".jpg").attr("alt", altTextMap[town_data[towns[randomNo]].icon]).attr("title", altTextMap[town_data[towns[randomNo]].icon]);
				$("#header .weather .high strong").html(town_data[towns[randomNo]].max);
				$("#header .weather .low strong").html(town_data[towns[randomNo]].min);
				$("#header .weather").css({display: "block"});
			}
		}
		// add's the click function to the "change location" button
		$("#header .topNav .changeLocation").click(function() {
			if (($("#changeMyLocation").is(":hidden") == false) && ($("#changeMyLocation").is(":visible") == false)) {
				// call the jsHTML function to write the HTML to the page.
				jsHTML.weatherLocation(this);
				$("#changeMyLocation .close").click(function() {
					$("#changeMyLocation").animate({height: "hide"}, 250, function() {
						jsTVNZ.Common().startAnimation();
					});
					return false;
				}).css({cursor: "pointer"});
				// binds the A-Z button
				$("#changeMyLocation .btnAZ").click(function() {
					$("#changeMyLocation .btnHolder a.selected").removeClass("selected");
					$(this).addClass("selected");
					$("#changeMyLocation .group").animate({height: "hide"}, 250, function() {
						$(this).removeClass("selected");
						$("#changeMyLocation .groupAll").addClass("selected").animate({height: "show"}, 250);
					});
					return false;
				}).css({cursor: "pointer"});
				// Bind's the North - South button
				$("#changeMyLocation .btnIslands").click(function() {
					$("#changeMyLocation .btnHolder a.selected").removeClass("selected");
					$(this).addClass("selected");
					$("#changeMyLocation .groupAll").animate({height: "hide"}, 250, function() {
						$(this).removeClass("selected");
						$("#changeMyLocation .group").addClass("selected").animate({height: "show"}, 250);
					});
					return false;
				}).css({cursor: "pointer"});
				// makes the checkbox selected even if you click the text
				$("#changeMyLocation .rowRadio").each(function() {
					$(this).click(function() {
						$("#changeMyLocation .rowRadio.selected").removeClass("selected");
						$(this).find("input").click();
						$(this).addClass("selected");
					}).css({cursor: "pointer"});
				});
				// below ensure's both a_ check box and b_ checkbox with the same id's are both checked
				$("#changeMyLocation .rowRadio input").each(function() {
					$(this).click(function() {
						if ($(this).attr("id").substring(0,1) == "a") {
							$("#b_" + $(this).attr("id").substring(2, $(this).attr("id").length)).attr("checked", "true").parent().addClass("selected");
						} else {
							$("#a_" + $(this).attr("id").substring(2, $(this).attr("id").length)).attr("checked", "true").parent().addClass("selected");
						}
					});
				});
				// if the save button is clicked, change to the new weather and save a cookie!
				$("#changeWeather").click(function() {
					$("#header .weather strong.location").html($("#changeMyLocation .groupAll .rowRadio.selected input").val().replace("_", " ") + ": ");
					$("#header .weather img.icon").attr("src", rootURL + "images/weather/icon" + town_data[$("#changeMyLocation .groupAll .rowRadio.selected input").val().toUpperCase()].icon + ".jpg").attr("alt", altTextMap[town_data[$("#changeMyLocation .groupAll .rowRadio.selected input").val().toUpperCase()].icon]).attr("title", altTextMap[town_data[$("#changeMyLocation .groupAll .rowRadio.selected input").val().toUpperCase()].icon]);
					$("#header .weather .high strong").html(town_data[$("#changeMyLocation .groupAll .rowRadio.selected input").val().toUpperCase()].max);
					$("#header .weather .low strong").html(town_data[$("#changeMyLocation .groupAll .rowRadio.selected input").val().toUpperCase()].min);
					jsTVNZ.Common().createCookie("weatherTVNZ", $("#changeMyLocation .groupAll .rowRadio.selected input").val(), 365);
					$("#changeMyLocation").animate({height: "hide"}, 250);
					return false;
				});
			}
			// stops all page animation so we can show out pop-over
			jsTVNZ.Common().stopAnimation();
			$("#changeMyLocation").animate({height: "show"}, 250);
			return false;
		});
	}

	var initPoll = function() {
		$("#poll .pollContainer a").each(function() {
			$(this).click(function() {
				$(this).addClass("selected");
				$.get($(this).attr("href"), function(data) {
					$("#poll .pollContainer").html(data);
				});
				$("#poll .pollContainer a").each(function() {
					$(this).removeAttr("href");
					$(this).unbind("click");
				});
				return false;
			});
		});
	}
	
	var initPageControls = function() {
		// Increase Font Size
		$(".increaseFont").each(function() {
			$(this).click(function(){
				if (!$(this).parent().hasClass("disabled")) {
					var currentFontSize = $('.viewSwitch').css('font-size');
					var newFontSize = parseInt(currentFontSize.substring(0,2))+2;
					if (newFontSize < 19) {
						$('.viewSwitch').css('font-size', newFontSize + "px");
					}
				}
				return false;
			});
		});
		 
		// Decrease Font Size
		$(".decreaseFont").each(function() {
			$(this).click(function(){
				if (!$(this).parent().hasClass("disabled")) {
					var currentFontSize = $('.viewSwitch').css('font-size');
					var newFontSize = parseInt(currentFontSize.substring(0,2))-2;
					if (newFontSize > 11) {
						$('.viewSwitch').css('font-size', newFontSize + "px");
					}
				}
				return false;
			});
		});
		
		// Print function
		$(".tools .print").each(function() {
			$(this).click(function() {
				if (!$(this).parent().hasClass("disabled")) {
					window.print();
				}
				return false;
			});
		});
	}

	var initImageGallery = function() {
		$("#imageGalleries .imgHover").hover(function() {
			$(this).css({zIndex: "100"});
			if ($(this).hasClass("imgRight")) {
				$(this).find("div").css({display: "block"}).animate({left: "-146px"}, 250);
			} else {
				$(this).find("div").css({display: "block"}).animate({left: "140px"}, 250);
			}
		}, function() {
			if ($(this).hasClass("imgRight")) {
				$(this).find("div").animate({left: "-6px"}, 250, function() {
					$(this).parent().css({zIndex: "90"});
					$(this).css({display: "none"});
				});
			} else {
				$(this).find("div").animate({left: "0px"}, 250, function() {
					$(this).parent().css({zIndex: "90"});
					$(this).css({display: "none"});
				});
			}
		});
	}
	
	var initToggleAnimation = function() {
		$(".thickbox").each(function() {
			$(this).click(function() {
				jsTVNZ.Common().stopAnimation();
			});
		});
	}

	var setCurrentDate = function() {
		$("#hpDate").html(xmlTextDay + " - " + xmlTextMonth + " " + xmlDay + ", " + xmlYeah).css({visibility: "visible"});
		$("#ts .nav .date").html("Today, " + xmlTextDay + " " + xmlDay + " " + xmlTextMonth).css({visibility: "visible"});
	}
	
	var disableTools = function() {
		$(".print").css("display","none");
	}


	var createCookie = function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}

	var readCookie = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
	var eraseCookie = function(name) {
		jsTVNZ.Common().createCookie(name,"",-1);
	}
	
	var fixUpMyIE6 = function() {
		// IE6 PNG remover
		$("img").each(function(){
			if ($(this).attr("src").match(/\.png$/) != null) {
				if ($(this).hasClass("blog_png")) {
					$(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $(this).attr("src") + "', sizingMethod='crop')");
				}
				$(this).attr("src", rootURL + "images/spacer.gif");
			}
		});
		// IE6 Style sheet, to add filtered PNG's
		$("head").createAppend('link', {rel: 'stylesheet', href: rootURL + 'css/ie6.css', type: 'text/css', media: 'screen'});
	}
	
	var jsFlashExternalCall = function() {
		/* 
		if ($("#newsHero").is(":visible")) {
			document.getElementById("newsHero").syncAnimation();
		}
		*/
	}
	
	
	return {
		getServerTime : getServerTime,
		animateLoop : animateLoop,
		updateClock : updateClock,
		animateHeroPanel : animateHeroPanel,
		initLiveNews : initLiveNews,
		addHeroVideo : addHeroVideo,
		initVideoPlayer : initVideoPlayer,
		switchVideoAdds : switchVideoAdds,
		changeVideoSize : changeVideoSize,
		changeWinMedia : changeWinMedia,
		stopAnimation : stopAnimation,
		startAnimation : startAnimation,

		setGlobal : setGlobal,
		getGlobal : getGlobal,
		
		initSlidePanel : initSlidePanel,
		initHeroPanel : initHeroPanel,
		initLatestNews : initLatestNews,
		initTabs : initTabs,
		initTVSchedule : initTVSchedule,
		initOnDemand : initOnDemand,
		initNewsAndAffares : initNewsAndAffares,
		initShareMore : initShareMore,
		initSetHomepage : initSetHomepage,
		initWeatherLocation : initWeatherLocation,
		initPoll : initPoll,
		initPageControls : initPageControls,
		initImageGallery : initImageGallery,
		initToggleAnimation : initToggleAnimation,
		detectVideoBandwidth : detectVideoBandwidth,

		setCurrentDate : setCurrentDate,
		disableTools : disableTools,
		createCookie : createCookie,
		readCookie : readCookie,
		eraseCookie : eraseCookie,
		dateWithin : dateWithin,
		
		fixUpMyIE6 : fixUpMyIE6,
		
		jsFlashExternalCall : jsFlashExternalCall
	}
}

// random number for DC ad code
var ord=Math.random()*10000000000000000;

// checks the max length of the textarea
function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

// function for Close Up form to show and hide forms fields depending on user input
function onChangeSectionCloseUp() {
	var Current =
   	document.close_up_contact.subject.selectedIndex;
	if (document.close_up_contact.subject.options[Current].text == "Send Feedback about Close Up") {
		show( 'feedback' );
		hide( 'suggest' );
		hide( 'share' );
	}
	else if (document.close_up_contact.subject.options[Current].text == "Suggest a story idea for Close Up") {
		hide( 'feedback' );
		show( 'suggest' );
		hide( 'share' );
	}
	else if (document.close_up_contact.subject.options[Current].text == "Share my own story with Close Up") {
		hide( 'feedback' );
		hide( 'suggest' );
		show( 'share' );
	}
	else {
		hide( 'feedback' );
		hide( 'suggest' );
		hide( 'share' );
	}
}
// function for Fair Go form to show and hide forms fields depending on user input
function onChangeSectionFairGo() {
	var Current =
   	document.tv_one_fair_go_feedback.subject.selectedIndex;
	if (document.tv_one_fair_go_feedback.subject.options[Current].text == "Send Feedback about Fair Go") {
		show( 'feedback' );
		hide( 'suggest' );
	}
	else if (document.tv_one_fair_go_feedback.subject.options[Current].text == "Suggest or Share a Story Idea for Fair Go") {
		hide( 'feedback' );
		show( 'suggest' );
	}
	else {
		hide( 'feedback' );
		hide( 'suggest' );
	}
}
// function for 20 20 form to show and hide forms fields depending on user input
function onChangeSectionTwenty() {
	var Current =
   	document.twenty_contact.subject.selectedIndex;
	if (document.twenty_contact.subject.options[Current].text == "Send Feedback about 20/20") {
		show( 'feedback' );
		hide( 'suggest' );
		hide( 'share' );
	}
	else if (document.twenty_contact.subject.options[Current].text == "Suggest a story idea for 20/20") {
		hide( 'feedback' );
		show( 'suggest' );
		hide( 'share' );
	}
	else if (document.twenty_contact.subject.options[Current].text == "Share my own story with 20/20") {
		hide( 'feedback' );
		hide( 'suggest' );
		show( 'share' );
	}
	else {
		hide( 'feedback' );
		hide( 'suggest' );
		hide( 'share' );
	}
}
// function for Sunday form to show and hide forms fields depending on user input
function onChangeSectionSunday() {
	var Current =
   	document.sunday_contact.subject.selectedIndex;
	if (document.sunday_contact.subject.options[Current].text == "Send Feedback about Sunday") {
		show( 'feedback' );
		hide( 'suggest' );
		hide( 'share' );
	}
	else if (document.sunday_contact.subject.options[Current].text == "Suggest a story idea for Sunday") {
		hide( 'feedback' );
		show( 'suggest' );
		hide( 'share' );
	}
	else if (document.sunday_contact.subject.options[Current].text == "Share my own story with Sunday") {
		hide( 'feedback' );
		hide( 'suggest' );
		show( 'share' );
	}
	else {
		hide( 'feedback' );
		hide( 'suggest' );
		hide( 'share' );
	}
}
// function for TVNZ Contact form to show and hide forms fields depending on user input
function onChangeSection() {
 		var Current =
   	document.tvnz_contact.submit_to.selectedIndex;
 		document.tvnz_contact.subject.value = document.tvnz_contact.submit_to.options[Current].text;
	if (document.tvnz_contact.submit_to.options[Current].text == "A Programme") {
		show( 'programme_row' );
		hide( 'transmission_feedback' );
	}
	else if (document.tvnz_contact.submit_to.options[Current].text == "TV reception (transmission)") {
		show( 'transmission_feedback' );
		hide( 'programme_row' );
	}
	else {
		hide( 'programme_row' );
		hide( 'transmission_feedback' );
	}
}
// function for NZI Business form to show and hide forms fields depending on user input
function onChangeSectionNZIBusiness() {
	var Current =
   	document.nzi_business_contact.subject.selectedIndex;
	if (document.nzi_business_contact.subject.options[Current].text == "Send Feedback about NZI Business") {
		show( 'feedback' );
		hide( 'suggest' );
		hide( 'share' );
	}
	else if (document.nzi_business_contact.subject.options[Current].text == "Suggest a story idea for NZI Business") {
		hide( 'feedback' );
		show( 'suggest' );
		hide( 'share' );
	}
	else if (document.nzi_business_contact.subject.options[Current].text == "Share my own story with NZI Business") {
		hide( 'feedback' );
		hide( 'suggest' );
		show( 'share' );
	}
	else {
		hide( 'feedback' );
		hide( 'suggest' );
		hide( 'share' );
	}
}
// Sel's iframe function
function insertIframe(src, width, height) {
	document.write('<iframe src="' + src + '" width="' + width + '" height="' + height + '" frameBorder="0" ></iframe>');
}

/***************************************************************
* Live news Database, this is to replace the calltoaction.xml, *
* to help speed up the process                                 *
****************************************************************
* this also allows for one or multipule "noshow" periods, in a *
* from - to time period (date and time from, date and time too)*
* date format: dd/mm/yyyy hh:mm:ss AM/PM                       *
* e.g: 12/19/2008 12:00:00 AM                                  *
***************************************************************/
var liveNewsDB = {
	weekdays: [
		{
			// news in a minute
			title: "Watch ONE News Minute",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1664522",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
			hours: 0,
			minutes: 0
		}, {
			// nzi business
			title: "Watch NZI Business",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1319541",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/nzi_business_live.jpg",
			hours: 6,
			minutes: 0,
			noshow: [
				{
					from: "12/24/2008 00:00:00 AM",
					to: "1/25/2009 11:59:59 PM"
				}
			]
			}, {
			// breakfast
			title: "Watch Breakfast",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1319543",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/breakfast_live.jpg",
			hours: 6,
			minutes: 30,
			noshow: [
				{
					from: "12/24/2008 00:00:00 AM",
					to: "1/18/2009 11:59:59 PM"
				}
			]
		}, {
			// news in a minute
			title: "Watch ONE News Minute",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1664522",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
			hours: 9,
			minutes: 0
		}, {
			// midday
			title: "Watch Midday",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1397073",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_live.jpg",
			hours: 12,
			minutes: 0,
			noshow: [
				{
					from: "12/24/2008 00:00:00 AM",
					to: "1/18/2009 11:59:59 PM"
				}
			]
		}, {
			// news in a minute
			title: "Watch ONE News Minute",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1664522",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
			hours: 12,
			minutes: 30
		}, {
			// one news live
			title: "Watch ONE News",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1397074",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_live.jpg",
			hours: 16,
			minutes: 30,
			noshow: [
				{
					from: "12/19/2008 00:00:00 AM",
					to: "1/18/2009 11:59:59 PM"
				}
			]
		}, {
			// news in a minute
			title: "Watch ONE News Minute",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1664522",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
			hours: 17,
			minutes: 0
		}, {
			// one news live
			title: "Watch ONE News",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1397074",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_live.jpg",
			hours: 18,
			minutes: 0
		}, {
			// close up
			title: "Watch Close Up",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1319544",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/closeup_live.jpg",
			hours: 19,
			minutes: 0,
			noshow: [
				{
					from: "12/19/2008 00:00:00 AM",
					to: "1/18/2009 11:59:59 PM"
				}
			]
		}, {
			// news in a minute
			title: "Watch ONE News Minute",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1664522",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
			hours: 19,
			minutes: 30
		}, {
			// tvnz news
			title: "Watch TVNZ News",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1667847",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/tvnz7_live.jpg",
			hours: 20,
			minutes: 0
		}, {
			// news in a minute
			title: "Watch ONE News Minute",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1664522",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
			hours: 21,
			minutes: 0
		}
	],
	weekends: [
		{
			// time fuller
			title: "Watch ONE News at 6PM",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_next_live.jpg",
			hours: 0,
			minutes: 0,
			noshow: true
		}, {
			// one news live
			title: "Watch ONE News",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1397074",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_live.jpg",
			hours: 18,
			minutes: 0
		}, {
			// time fuller
			title: "Watch TVNZ News at 8PM",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/tvnz7_news_at_8.jpg",
			hours: 19,
			minutes: 0,
			noshow: true
		}, {
			// tvnz news
			title: "Watch TVNZ News",
			url: "http://tvnz.co.nz/view/video_popup_windows_skin/1667847",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/tvnz7_live.jpg",
			hours: 20,
			minutes: 0
		}, {
			// time fuller
			title: "Watch ONE News at 6PM",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_next_live.jpg",
			hours: 21,
			minutes: 0,
			noshow: true
		},
	]
}
