/********************************************************************************************
TVNZ Javascript Library functions list:

jsTVNZ.initialize();
		
jsTVNZ.Common().getServerTime();
jsTVNZ.Common().animateLoop();
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().initSuperClick();

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();
});

// the Initialize function, which runs on load, all sub-functions are called in here.
jsTVNZ.initialize = function() {
	// 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();
	
	// 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();

	// tab control for Watch Episodes module
	jsTVNZ.Common().initWatchEpisodes();
	
	// 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();

	// uses the child a url so the entire element is clickable
	jsTVNZ.Common().initSuperClick();

	// initializes the video player
	jsTVNZ.Common().initVideoPlayer();
	
	// 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();
	}
	
	jsTVNZ.Common().checkIfSync();
}

// TVNZ Common functions, that can be re-called at any point.
jsTVNZ.Common = function() {

	var syncToFlash = function() {
		// calls the animation sync from the flash
		jsTVNZ.Common().animateLoop();
	}
	
	var checkIfSync = function() {
		if (!$("#heroPanelFlash").is(":visible")) {
			setInterval(function() {
				jsTVNZ.Common().animateLoop();
			}, 6000);
		}
	}

	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, .arrowRight").click();
				}
			});
		}
	}
	
	var stopAnimation = function() {
		stopAnimationLoop = true;
	}
	
	var startAnimation = function() {
		stopAnimationLoop = false;
	}
	
	var initLiveNews = function() {
		var selectedShow;
		var showTheShow = true;
		if (xmlTextDay == "Saturday" || xmlTextDay == "Sunday") {
			$(liveNewsDB.weekends).each(function(i) {
				if(this.day == "both" || (xmlTextDay == "Sunday" && this.day == "Sunday") || (xmlTextDay == "Saturday" && this.day == "Saturday") ){
					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 .videoLightbox").attr("title", selectedShow.title);
				$("#oneNews .videoLightbox img").attr("src", selectedShow.img).css({display: "block", alt: selectedShow.title});
				$("#oneNews .videoLightbox").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, href: selectedShow.url});
				$("#oneNews .thickbox img").attr("src", selectedShow.img).css({display: "block", alt: selectedShow.title});
				$("#oneNews .thickbox").click(function() {
					$("#iframeVideoStream").attr("src", selectedShow.url);
				});
				*/
				$("#oneNews .videoLightbox").attr({title: selectedShow.title, href: selectedShow.url});
				$("#oneNews .videoLightbox img").attr("src", selectedShow.img).css({display: "block", alt: selectedShow.title});
				jBox.init({
					type: 'iframe',
					id: 'iFrameVideo',
					src: selectedShow.url,
					closeSrc: 'images/jb_NewsClose.gif',
					height: 503,
					width: 540,
					buttons: '.videoLightbox',
					root: rootURL
				});
			}
		} else {
			/*
			$("#oneNews .thickbox").attr({title: selectedShow.title, href: selectedShow.url});
			$("#oneNews .thickbox img").attr("src", selectedShow.img).css({display: "block", alt: selectedShow.title});
			$("#oneNews .thickbox").click(function() {
				$("#iframeVideoStream").attr("src", selectedShow.url);
			});
			*/
			$("#oneNews .videoLightbox").attr({title: selectedShow.title, href: selectedShow.url});
			$("#oneNews .videoLightbox img").attr("src", selectedShow.img).css({display: "block", alt: selectedShow.title});
			jBox.init({
				type: 'iframe',
				id: 'iFrameVideo',
				src: selectedShow.url,
				closeSrc: 'images/jb_NewsClose.gif',
				height: 503,
				width: 540,
				buttons: '.videoLightbox',
				root: rootURL
			});
		}
	}
	
	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("/stylesheets/tvnz/news/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("/stylesheets/tvnz/news/flash/playerProductInstall.swf");

			so.addVariable("xmlSetup", "/stylesheets/tvnz/news/flash/xml/setup.xml");

		so.write("vidPlayer");
	}

	var removeBandWidthDetection = function() {
		if ($.browser.mozilla) {
			var version = parseInt($.browser.version.split(".")[0]) + (parseInt($.browser.version.split(".")[1]) / 10);
			if (version < 2) {
				setTimeout(jsTVNZ.Common().fireFoxRemoveSWF, 250);
			} else {
				jsTVNZ.Common().initVideoPlayer();
			}
		} else {
			jsTVNZ.Common().initVideoPlayer();
		}
	}

	var fireFoxRemoveSWF = function() {
		$("vidPlayer").html("<!-- -->");
		jsTVNZ.Common().initVideoPlayer();
	}

	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;
	}

	// 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 + "?adUrl=" + wmpAdUrl + "&content=" + wmpContent + wmpBW, wmpHeight, wmpWidth, wmpName, $("#vidPlayer").attr("id"), quality);
			}
			
			davInitStream(document[wmpName], _rsdURL,  _rsdCI, _rsdTL, _rsdFreq, _rsdCG, _rsdSI, _rsdTC, _rsdPCG, _rsdCustom);
			
			// sets the add's to the window media player adds
			if (typeof(DartPlayer) == 'function') {
//				alert("DartPlayer:"+DartPlayer);
				var dartPlayer = null;
				dartPlayer = new DartPlayer(wmpName, function () {
//					alert("DartPlayerReady")
					dartPlayer.setClickThroughWindowFeatures("status,toolbar,menubar,location");
					dartPlayer.attachOnAdStart(function () {
//						alert("DartPlayerOnAdStart" + "\n\n" +
//								"DartAdId:"+dartPlayer.getAd().getDartId() + "\n\n" +
//								"setCompanionAds"+setCompanionAds + 
//								"companionAdvertisements" + companionAdvertisements.length
//						);
						jsTVNZ.Common().switchVideoAdds("wmstreaming");
						setCompanionAds(dartPlayer.getAd().getDartId());
					});
				});
			}

			$(".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 Ad push down
	var initPushDown = function() {
		var openBanner = true;
		$("#pushDownAd img").each(function() {
			if ($(this).attr("src").match(/817-grey.gif$/) != null) {
				openBanner = false;
			}
		});
		if (openBanner) {
			jsTVNZ.Common().openPushDown();
		}
	}
	var openPushDown = function(delay) {
		$("#pushDownAd").animate({height: 'show'}, 1500, "easeout", function() {
			if (delay != undefined) {
				setTimeout(function() {
					jsTVNZ.Common().closePushDown();
				}, (delay * 1000));
			}
		});
	}

	var closePushDown = function() {
		$("#pushDownAd").animate({height: 'hide'}, 1500, "easeout");
	}
	
	// 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.
			if ($(this).find(".cfbNav, .nav").length>0){
				$(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;
				});
			}

			// added later for tv highlights
			if ($(this).find(".mainNav").length>0){
				$(this).find(".arrowLeft").click(function() {
					if ($(this).parent().parent().parent().find(".cfbCurrent").html() == 1) {
						$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).animate({opacity:'hide'}, 1500, "easeout");
						$(this).parent().parent().parent().find(".cfbCurrent").html($(this).parent().parent().parent().find(".cfbMax").html());
						$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).animate({opacity:'show'}, 1500, "easeout");
					} else {
						$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).animate({opacity:'hide'}, 1500, "easeout");
						$(this).parent().parent().parent().find(".cfbCurrent").html(parseInt($(this).parent().parent().parent().find(".cfbCurrent").html()) - 1);
						$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).animate({opacity:'show'}, 1500, "easeout");
					}
					$(".mainNav .navButton.selected").removeClass("selected");
					$(".mainNav .navButton").each(function(i) {
						if (i == ($(this).parent().parent().parent().find(".cfbCurrent").html() - 1)) {
							$(this).addClass("selected");
						}
					});
					return false;
				});
				$(this).find(".arrowRight").click(function() {
					if ($(this).parent().parent().parent().find(".cfbCurrent").html() == $(this).parent().parent().parent().find(".cfbMax").html()) {
						$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).animate({opacity:'hide'}, 1500, "easeout");
						$(this).parent().parent().parent().find(".cfbCurrent").html("1");
						$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).animate({opacity:'show'}, 1500, "easeout");
					} else {
						$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).animate({opacity:'hide'}, 1500, "easeout");
						$(this).parent().parent().parent().find(".cfbCurrent").html(parseInt($(this).parent().parent().parent().find(".cfbCurrent").html()) + 1);
						$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).animate({opacity:'show'}, 1500, "easeout");
					}
					$(".mainNav .navButton.selected").removeClass("selected");
					$(".mainNav .navButton").each(function(i) {
						if (i == ($(this).parent().parent().parent().find(".cfbCurrent").html() - 1)) {
							$(this).addClass("selected");
						}
					});
					return false;
				});
				$(".mainNav .navButton:first").addClass("selected");
				$(".mainNav .navButton").each(function(i) {
					$(this).click(function() {
						if (i != $(".mainNav .navButton").index($(".mainNav .navButton.selected"))) {
							$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).animate({opacity:'hide'}, 1500, "easeout");
							$(this).parent().parent().parent().find(".cfbCurrent").html((i+1));
							$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).animate({opacity:'show'}, 1500, "easeout");
							$(".mainNav .navButton.selected").removeClass("selected");
							$(this).addClass("selected");
						}
						return false;
					});
				});
			}

		});
	}
	
	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() - parseInt($("#latestNews .articles").css("padding-top")) - parseInt($("#latestNews .articles").css("padding-bottom"))) - x) > 0) {
				$(this).removeAttr("style").addClass("last");
				$(this).prev("li").removeClass("last");
			} else {
				$(this).css({display: "none"});
			}
		});
	}
	
	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 initWatchEpisodes = function() {
		//Watch episodes tabbed panel
		$("#watchEpisodes .tabs a").each(function() {
			$(this).click(function() {
				if (!$(this).hasClass("selected")) {
					$("#watchEpisodes .tabs .selected").removeClass("selected");
					$("#watchEpisodes .main .selected").removeClass("selected");
					$(this).addClass("selected");
					$("#watchEpisodes .main ." + $(this).parent().attr("class")).addClass("selected");
					//$(this).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 initSuperClick = function() {
		$(".superClick").click(function() {
			if ($(this).children("a").length>0) {
				document.location = $(this).children("a:first").attr("href");
			}
		});
	}
	
	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");
						jsTVNZ.Common().createCookie("TVNZ-Fontsize", newFontSize, 365);
					}
				}
				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");
						jsTVNZ.Common().createCookie("TVNZ-Fontsize", newFontSize, 365);
					}
				}
				return false;
			});
		});
		
		// Print function
		$(".tools .print").each(function() {
			$(this).click(function() {
				if (!$(this).parent().hasClass("disabled")) {
					window.print();
				}
				return false;
			});
		});

		// read the font-size cookie, and if its not null, set the font-size
		var tvnzFontSize = jsTVNZ.Common().readCookie("TVNZ-Fontsize");
		if (tvnzFontSize != null && $('.viewSwitch').is(":visible")) {
			$('.viewSwitch').css('font-size', tvnzFontSize + "px");
		}
	}

	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'});
	}
	
	
	return {
		getServerTime : getServerTime,
		animateLoop : animateLoop,
		syncToFlash : syncToFlash,
		checkIfSync : checkIfSync,
		initLiveNews : initLiveNews,
		initVideoPlayer : initVideoPlayer,
		removeBandWidthDetection : removeBandWidthDetection,
		fireFoxRemoveSWF : fireFoxRemoveSWF,
		switchVideoAdds : switchVideoAdds,
		changeVideoSize : changeVideoSize,
		changeWinMedia : changeWinMedia,
		stopAnimation : stopAnimation,
		startAnimation : startAnimation,

		setGlobal : setGlobal,
		getGlobal : getGlobal,
		
		initPushDown : initPushDown,
		openPushDown : openPushDown,
		closePushDown : closePushDown,
		initSlidePanel : initSlidePanel,
		initLatestNews : initLatestNews,
		initTabs : initTabs,
		initTVSchedule : initTVSchedule,
		initOnDemand : initOnDemand,
		initWatchEpisodes : initWatchEpisodes,
		initNewsAndAffares : initNewsAndAffares,
		initShareMore : initShareMore,
		initSetHomepage : initSetHomepage,
		initWeatherLocation : initWeatherLocation,
		initPoll : initPoll,
		initPageControls : initPageControls,
		initImageGallery : initImageGallery,
		initToggleAnimation : initToggleAnimation,
		detectVideoBandwidth : detectVideoBandwidth,
		initSuperClick : initSuperClick,

		setCurrentDate : setCurrentDate,
		disableTools : disableTools,
		createCookie : createCookie,
		readCookie : readCookie,
		eraseCookie : eraseCookie,
		dateWithin : dateWithin,
		
		fixUpMyIE6 : fixUpMyIE6
	}
}


function displayLayer( id, value )
{
    if ( document.getElementById )
    {
        document.getElementById( id ).style.display = value;
    }
    else if ( document.all )
    {
        document.all[ id ].style.display = value;
    }
}

function hide( id )
{
    displayLayer( id, 'none' );
}

function show( id )
{
    displayLayer( id, 'block' );
}

// 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 Sunday form to show and hide forms fields depending on user input
function onChangeSectionGoodSorts() {
	var Current =
   	document.good_sorts_contact.subject.selectedIndex;
	if (document.good_sorts_contact.subject.options[Current].text == "Send feedback about a Good Sorts story") {
		show( 'feedback' );
		hide( 'nominate' );
	}
	else if (document.good_sorts_contact.subject.options[Current].text == "Nominate someone for the Good Sorts series") {
		hide( 'feedback' );
		show( 'nominate' );
	}
	else {
		hide( 'feedback' );
		hide( 'nominate' );
	}
}
//function for Q+A form to show and hide forms fields depending on user input
function onChangeSectionQandA() {
	var Current =
   	document.q_a_contact.subject.selectedIndex;
	if (document.q_a_contact.subject.options[Current].text == "Send Feedback about Q+A") {
		show( 'feedback' );
		hide( 'suggest' );
		hide( 'share' );
	}
	else if (document.q_a_contact.subject.options[Current].text == "Suggest a story idea for Q+A") {
		hide( 'feedback' );
		show( 'suggest' );
		hide( 'share' );
	}
	else if (document.q_a_contact.subject.options[Current].text == "Share my own story with Q+A") {
		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: "Daily News in a Minute",
			url: "http://tvnz.co.nz/content/1664522/video_lightbox_skin.xhtml",
			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/content/1319541/video_lightbox_skin.xhtml",
			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/content/1319543/video_lightbox_skin.xhtml",
			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: "Daily News in a Minute",
			url: "http://tvnz.co.nz/content/1664522/video_lightbox_skin.xhtml",
			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/content/1397073/video_lightbox_skin.xhtml",
			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: "Daily News in a Minute",
			url: "http://tvnz.co.nz/content/1664522/video_lightbox_skin.xhtml",
			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/content/1397074/video_lightbox_skin.xhtml",
			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: "Daily News in a Minute",
			url: "http://tvnz.co.nz/content/1664522/video_lightbox_skin.xhtml",
			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/content/1319542/video_lightbox_skin.xhtml",
			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/content/1319544/video_lightbox_skin.xhtml",
			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: "Daily News in a Minute",
			url: "http://tvnz.co.nz/content/1664522/video_lightbox_skin.xhtml",
			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/content/1667847/video_lightbox_skin.xhtml",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/tvnz7_live.jpg",
			hours: 20,
			minutes: 0
		}, {
			// news in a minute
			title: "Daily News in a Minute",
			url: "http://tvnz.co.nz/content/1664522/video_lightbox_skin.xhtml",
			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,
			day: "both"
		}, {
			// Q+A live
			title: "Q+A Live",
			url: "http://tvnz.co.nz/content/2566686/video_lightbox_skin.xhtml",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/q+a_live.jpg",
			hours: 9,
			minutes: 0,
			day: "Sunday"
		}, {
			// time fuller
			title: "Watch ONE News at 6PM",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_next_live.jpg",
			hours: 10,
			minutes: 0,
			noshow: true,
			day: "both"
		}, {
			// one news live
			title: "Watch ONE News",
			url: "http://tvnz.co.nz/content/1319542/video_lightbox_skin.xhtml",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_live.jpg",
			hours: 18,
			minutes: 0,
			day: "both"
		}, {
			// 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,
			day: "both"
		}, {
			// tvnz news
			title: "Watch TVNZ News",
			url: "http://tvnz.co.nz/content/1667847/video_lightbox_skin.xhtml",
			img: "http://images.tvnz.co.nz/tvnz_site_images/images/tvnz7_live.jpg",
			hours: 20,
			minutes: 0,
			day: "both"
		}, {
			// 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,
			day: "both"
		},
	]
}