/********************************************************************************************
TVNZ Javascript Library functions list:

jsTVNZ.initialize();
		
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().changeSilverlightPlayer();
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().initShareMore();
jsTVNZ.Common().initSetHomepage();
jsTVNZ.Common().initWeatherLocation();
jsTVNZ.Common().inityourWeatherSelect();
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 dc_tile = 0;
 
var globalObj = new Object();
 
var dartPlayer;

// Video page global variables
var backupAdvertisments;
var lastQualityPlayed;

var rootURL = "/stylesheets/tvnz/news/";
//var rootURL = "http://images.tvnz.co.nz/tvnz_site_images/";

//random number used in requests for DoubleClick ads. Should be different for each page view, but the same within the page.
var ord=Math.ceil(Math.random()*10000000000000000);

// 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 for weather
	jsTVNZ.Common().yourWeatherClick();
	jsTVNZ.Common().initWeatherLocation();
	jsTVNZ.Common().initYourWeatherSelect();
	jsTVNZ.Common().initYourWeather();
	
	// tab control for On Demand module
	jsTVNZ.Common().initOnDemand();

	// tab control for Watch Episodes module
	jsTVNZ.Common().initWatchEpisodes();

	// tab control for Most Popular module
	jsTVNZ.Common().initMostPopular();
	
	// 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();
	}
	
	// initialise the TVSchedule for special events
	jsTVNZ.Common().initTVScheduleSpecialEvent();
	jsTVNZ.Common().initQuickTabModule();
	jsTVNZ.Common().initImageGallerySpecialEvents();
	
	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);
		}
	}

	// 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").trigger('animate');
				}
			});
		}
	}
	
	var stopAnimation = function() {
		stopAnimationLoop = true;
	}
	
	var startAnimation = function() {
		stopAnimationLoop = false;
	}
	
	/***************************************************************
	* 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                                  *
	***************************************************************/
	/*
	Breakfast Live stream ? 1319543 (Breakfast) http://tvnz.co.nz/breakfast-news/breakfast-live-stream-video-1319543
	Midday News Live stream ? 1397073 (national) http://tvnz.co.nz/national-news/one-news-midday-live-stream-video-1397073
	Te Karere Live ? 2792573 (Te Karere ? programme cos it is under Televsion NOT News) http://tvnz.co.nz/te-karere/live-stream-video-2792573
	4.30pm news live stream ? 1397074 (National) http://tvnz.co.nz/national-news/one-news-4-30pm-live-stream-video-1397074
	6pm news live stream ? 1319542 (National) http://tvnz.co.nz/national-news/one-news-6pm-live-stream-video-1319542
	Close Up Live stream.- 1319544 (National) http://tvnz.co.nz/close-up/live-stream-video-1319544
	TVNZ 7 News at 8pm Live stream ? 1667847 (News on TVNZ 7 - programme cos it is under Televsion NOT News) http://tvnz.co.nz/news-on-tvnz-7/tvnz-news-8-live-stream-video-1667847
	Breakfast Business ? 1319541 (Business) http://tvnz.co.nz/business-news/breakfast-business-live-stream-video-1319541
	Q+A ? 1667847 (Q and A) http://tvnz.co.nz/news-on-tvnz-7/tvnz-news-8-live-stream-video-1667847
	 */
	var liveNewsDB = {
		weekdays: [
	   		{
	   			// news in a minute
	   			title: "Daily News in a Minute",
	   			url: "http://tvnz.co.nz/national-news/one-news-minute-informed-quickly-video-1664522",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
	   			hours: 0,
	   			minutes: 0
	   		}, {
	   			// NZI business
	   			title: "NZI Business",
	   			url: "http://tvnz.co.nz/business-news/breakfast-business-live-stream-video-1319541",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/nzi_business_live.jpg",
	   			hours: 6,
	   			minutes: 0
	   		}, {
	   			// breakfast
	   			title: "Watch Breakfast",
	   			url: "http://tvnz.co.nz/breakfast-news/breakfast-live-stream-video-1319543",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/breakfast_live.jpg",
	   			hours: 6,
	   			minutes: 30
	   		}, {
				// news in a minute
				title: "Daily News in a Minute",
				url: "http://tvnz.co.nz/national-news/one-news-minute-informed-quickly-video-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/national-news/one-news-midday-live-stream-video-1397073",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_live.jpg",
	   			hours: 12,
	   			minutes: 0
	   		}, {
	   			// news in a minute
	   			title: "Daily News in a Minute",
	   			url: "http://tvnz.co.nz/national-news/one-news-minute-informed-quickly-video-1664522",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
	   			hours: 12,
	   			minutes: 30
	   		}, {
	   			// te karere live
	   			title: "Watch Te Karere",
	   			url: "http://tvnz.co.nz/national-news/te-karere-live-stream-video-2792573",
	   			//url: "http://tvnz.co.nz/te-karere/live-stream-video-2792573",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/te_karere_live_stream.jpg",
	   			hours: 16,
	   			minutes: 0
	   		}, {
	   			// one news live
	   			title: "Watch ONE News",
	   			url: "http://tvnz.co.nz/national-news/one-news-4-30pm-live-stream-video-1397074",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_live.jpg",
	   			hours: 16,
	   			minutes: 30
	   		}, {
	   			// news in a minute
	   			title: "Daily News in a Minute",
	   			url: "http://tvnz.co.nz/national-news/one-news-minute-informed-quickly-video-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/national-news/one-news-6pm-live-stream-video-1319542",
	   			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/close-up/live-stream-video-1319544",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/closeup_live.jpg",
	   			hours: 19,
	   			minutes: 0
	   		}, {
	   			// news in a minute
	   			title: "Daily News in a Minute",
	   			url: "http://tvnz.co.nz/national-news/one-news-6pm-live-stream-video-1319542",
	   			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/national-news/tvnz-news-8-live-stream-video-1667847",
	   			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/national-news/one-news-minute-informed-quickly-video-1664522",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
	   			hours: 21,
	   			minutes: 0
	   		}
	   	],
	   	weekends: [
         	{
         		title: "Daily News in a Minute",
	   			url: "http://tvnz.co.nz/national-news/one-news-minute-informed-quickly-video-1664522",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
         		hours: 0,
         		minutes: 0,
         		day: "both"
         	}, {
				// Q + A
				title: "Q and A",
				url: "http://tvnz.co.nz/q-and-a-news/q-live-stream-video-2566686",
				img: "http://images.tvnz.co.nz/tvnz_site_images/images/q+a_live.jpg",
				hours: 9,
				minutes: 0,
				day: "Sunday"
         	}, {
         		title: "Daily News in a Minute",
	   			url: "http://tvnz.co.nz/national-news/one-news-minute-informed-quickly-video-1664522",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
         		hours: 10,
         		minutes: 0,
         		day: "both"
         	}, {
         		// one news live
         		title: "Watch ONE News",
         		url: "http://tvnz.co.nz/national-news/one-news-6pm-live-stream-video-1319542",
         		img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_live.jpg",
         		hours: 18,
         		minutes: 0,
         		day: "both"
         	}, {
         		title: "Daily News in a Minute",
	   			url: "http://tvnz.co.nz/national-news/one-news-minute-informed-quickly-video-1664522",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
         		hours: 19,
         		minutes: 0,
         		day: "both"
    		}, {
    			// tvnz news
    			title: "Watch TVNZ News",
    			url: "http://tvnz.co.nz/national-news/tvnz-news-8-live-stream-video-1667847",
    			img: "http://images.tvnz.co.nz/tvnz_site_images/images/tvnz7_live.jpg",
    			hours: 20,
    			minutes: 0,
    			day: "both"
    		}, {
    			title: "Daily News in a Minute",
	   			url: "http://tvnz.co.nz/national-news/one-news-minute-informed-quickly-video-1664522",
	   			img: "http://images.tvnz.co.nz/tvnz_site_images/images/onenews_minute.jpg",
    			hours: 21,
    			minutes: 0,
    			day: "both"
    		}
     	]
	}
	
	var initLiveNews = function() {
		var selectedShow;
		var showTheShow = true;
		if (www.serverTime.dayText == "Saturday" || www.serverTime.dayText == "Sunday") {
			$(liveNewsDB.weekends).each(function(i) {
				if (this.day == "both" || (www.serverTime.dayText == "Sunday" && this.day == "Sunday") || (www.serverTime.dayText == "Saturday" && this.day == "Saturday") ) {
					if ((this.hours < www.serverTime.hours) || ((this.hours == www.serverTime.hours) && (this.minutes <= www.serverTime.minutes))) {
						selectedShow = this;
					}
				}
			});
		} else {
			$(liveNewsDB.weekdays).each(function(i) {
				if ((this.hours < www.serverTime.hours) || ((this.hours == www.serverTime.hours) && (this.minutes <= www.serverTime.minutes))) {
					selectedShow = this;
				}
			});
		}
		
		if (selectedShow.noshow != undefined) {
			if ((typeof(selectedShow.noshow) == "boolean" && selectedShow.noshow == true)) {
				showTheShow = false;
				$("#oneNews .videoLightbox").attr("title", selectedShow.title);
				$("#oneNews .videoLightbox").css({display: "block", alt: selectedShow.title});
				$("#oneNews .videoLightbox img").attr("src", selectedShow.img);
				$("#oneNews .videoLightbox").unbind('click').removeAttr("href").removeAttr("class");
			} else {
				$(selectedShow.noshow).each(function(i) {
					if (www.dateWithin(this.from, this.to, www.serverTime.date) == true) {
						showTheShow = false;
					}
				});
			}
			if (showTheShow) {
				$("#oneNews .videoLightbox").attr({title: selectedShow.title, href: selectedShow.url});
				$("#oneNews .videoLightbox").css({display: "block", alt: selectedShow.title});
				$("#oneNews .videoLightbox img").attr("src", selectedShow.img);
			} 
		} else {
			$("#oneNews .videoLightbox").attr({title: selectedShow.title, href: selectedShow.url});
			$("#oneNews .videoLightbox").css({display: "block", alt: selectedShow.title});
			$("#oneNews .videoLightbox img").attr("src", selectedShow.img);
		}
	}
	
	/*
	 * Initialises the appropriate video player for this page. 
	 * If the user is watching a live stream they will get WMV unless they are using Windows 7 then they'll get SilverLight.
	 * For pre-recorded streams they will be presented with a choice of Flash or WMV, defaulting to their last used option (or flash if they don't have a cookie set)
	 */
	var initVideoPlayer = function() {
		if ($("#vidPlayer").is(":visible")) {
			document.cookie = 'cookiesEnabled';
			var cookiesEnabled = document.cookie.length > 0;
			var videoQualityFLV = jsTVNZ.Common().readCookie("videoTVNZ-Quality-FLV");
			var videoQualityWMP = jsTVNZ.Common().readCookie("videoTVNZ-Quality-WMP");
			var isWindows7User = navigator.userAgent.indexOf("Windows NT 6.1") >= 0;
			var isLiveStream = $("#vidPlayer").hasClass("liveStream");
			var videoType = isLiveStream ? 
					isWindows7User ? "silverlight" : "wmp" : 
					jsTVNZ.Common().readCookie("videoTVNZ-Type");
			if (videoType == "silverlight") {
				jsTVNZ.Common().changeSilverlightPlayer();
			} else if (((videoQualityWMP != null) || (videoQualityFLV != null)) && (videoType != null)) {
				switch (videoType) {
					case "wmp":
						switch(videoQualityWMP) {
							case "low":
								if (wmpURL != "") {
									jsTVNZ.Common().changeWinMedia("low");	
								} else {
									changeVideoSize("low");
								}
							break;
							default:
								if (wmpURL != "") {
									jsTVNZ.Common().changeWinMedia("mid");
								} else {
									changeVideoSize("mid");
								}
							break;
						}
					break;
					default:
						switch(videoQualityFLV) {
							case "mid":
								if (vpChapters != "") {
									changeVideoSize("mid");
								} else {
									jsTVNZ.Common().changeWinMedia("mid");
								}
							break;
							case "low":
								if (vpChapters != "") {
									changeVideoSize("low");
								} else {
									jsTVNZ.Common().changeWinMedia("low");
								}
							break;
							default:
								if (vpChapters != "") {
									changeVideoSize("high");
								} else {
									jsTVNZ.Common().changeWinMedia("mid");
								}
							break;
						}
					break;
				}
			} else if (cookiesEnabled) {
				jsTVNZ.Common().detectVideoBandwidth();
			} else {
				changeVideoSize("high");
			}			
		}
	}
	
	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;
	}

	var changeSilverlightPlayer = function() {
		jsHTML.silverlightPlayer($("#vidPlayer").attr("id"));
	}
	
	var onSilverLightPlayerLoaded = function (sender, args) {
		var silverlightControl = sender.getHost();
		var arg = new Object();
		arg.ad = getDCURL().replace('flashprogressive','wmstreaming');

		arg.low = wmpContent + "_low.asx";
		arg.med = wmpContent + "_med.asx";
		arg.high = wmpContent + "_high.asx";

		arg.dav0 = "http://secure-nz.imrworldwide.com/cgi-bin/m?ci=nz-nzoom-dav&cg=" + wmpCG + "&rp=" + document.location + "&tl=dav0-" + encodeURIComponent(wmpTL) + "&si=" + document.location + "&cc=1";
		arg.dav1 = "http://secure-nz.imrworldwide.com/cgi-bin/m?ci=nz-nzoom-dav&cg=" + wmpCG + "&rp=" + document.location + "&tl=dav1-" + encodeURIComponent(wmpTL) + "&cc=1";
		arg.dav2 = "http://secure-nz.imrworldwide.com/cgi-bin/m?ci=nz-nzoom-dav&cg=" + wmpCG + "&rp=" + document.location + "&tl=dav2-" + encodeURIComponent(wmpTL) + "&cc=1";
		arg.freq = 30;
		
		silverlightControl.Content.media.SL4DC(arg);
	}

	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);
			
			var isLiveStream = $("#vidPlayer").hasClass("liveStream");
			switch (quality) {
				case "low":
					var wmpWidth = 400;/*200;*/
					var wmpHeight = 276;/*142;*/
					var wmpBW = isLiveStream ? "_low.asx" : "_56k.asx";
				break;
				default:
					var wmpWidth = 400;
					var wmpHeight = 276;
					var wmpBW = isLiveStream ? "_high.asx" : "_128k.asx";
			}
			// this ensures that if the last player was wide video it it now removed
			$("#colOne .article").removeClass("wideVideo");
			$("#vidPlayer").removeClass("high");
			
			// 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);
			}
			
			var _rsdURL = '//secure-nz.imrworldwide.com/';
			var _rsdCI = 'nz-nzoom-dav';
			var _rsdFreq = 30;
			// both worked out automatically? No! Encoded automatically? Yes!
			var _rsdSI = document.location;
			var _rsdRP = document.location;
			var _rsdCG = wmpCG;
			// TC must be zero no matter what Nielsen says, it means that IE and all other
			// browsers report the same TL, otherwise (only) IE pulls it out of WMP directly 
			var _rsdTC = 0; 
			// PCG must be one so that "ad" is prefixed on CG for ads (so long as they are trafficked right
			var _rsdPCG = 1;
			var _rsdTL = wmpTL;
			var _rsdCustom = null; //['pa,TVNZ','br,TVNZ','ourl,http://tvnz.co.nz/'+_rsdCG];
			// below is called within the JS at time of media player load.
	        davInitStream(document[wmpName], _rsdURL, _rsdCI, _rsdTL, _rsdFreq, _rsdCG, _rsdSI, _rsdTC, _rsdPCG, _rsdCustom);
		
		    function setDavImg(ScImgSrc) {
		        // this is needed to be done in .js rather than c# in Silverlight as
		        // the Javascript escape() method and HttpUtility.UrlEncode in c# produce different results
		        // by using the javascript escape call we ensure that we are sending exactly the same data
		        // to Nielsen via the Silverlight player as we do via the WMP calls.
		        var davImgSrc = '';
		        for (count = 0; count < ScImgSrc.length; count++) {
		            if (ScImgSrc[count] != '') {
		                if (ScImgSrc[count] == 1) { // the next element in the array should be escaped
		                    davImgSrc += escape(ScImgSrc[++count]);
		                }
		                else {
		                    davImgSrc += ScImgSrc[++count];
		                }
		            }
		        }
		        
		        var davImg = new Image();
		        davImg.src = davImgSrc;
		    }
			
			// sets the add's to the window media player adds
			if (typeof(DartPlayer) == 'function') {
				var dartPlayer = null;
				dartPlayer = new DartPlayer(document[wmpName], function () {
					dartPlayer.setClickThroughWindowFeatures("status,toolbar,menubar,location");
					dartPlayer.attachOnAdStart(function () {
						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 {
					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").fadeIn(1500, function() {
			if (delay != undefined) {
				setTimeout(function() {
					jsTVNZ.Common().closePushDown();
				}, (delay * 1000));
			}
		});
	}

	
	var closePushDown = function() {
		if ($.browser.mozilla) {
			var version = parseInt($.browser.version.split(".")[0]) + (parseInt($.browser.version.split(".")[1]) / 10);
			if (version < 2) {
				setTimeout(jsTVNZ.Common().fireFoxRemovePushDownSWF, 250);
			} else {
				$("#pushDownAd").fadeOut();//animate({height: 'hide'}, 1500, "easeout");
			}
		} else {
			$("#pushDownAd").fadeOut();//animate({height: 'hide'}, 1500, "easeout");
		}
	}

	var fireFoxRemovePushDownSWF = function() {
		$("#pushDownAd").html("<!-- -->");
		$("#pushDownAd").css({display: "none"});
	}

	
	// 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).find(".cfbNav").css({display: 'block'});
				$(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(e) {
					if ($(this).parent().find(".cfbCurrent").html() == 1) {
						$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).fadeOut();//animate({opacity:'hide'}, 1500, "easeout");
						$(this).parent().find(".cfbCurrent").html($(this).parent().find(".cfbMax").html());
						$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).fadeIn();//animate({opacity:'show'}, 1500, "easeout");
					} else {
						$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).fadeOut();//animate({opacity:'hide'}, 1500, "easeout");
						$(this).parent().find(".cfbCurrent").html(parseInt($(this).parent().find(".cfbCurrent").html()) - 1);
						$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).fadeIn();//animate({opacity:'show'}, 1500, "easeout");
					}
					e.stopPropagation(); // required to prevent propagation causing omniture roundtrips in animation loop 
					e.preventDefault();
					return false;
				});
				
				var clickFunc = function(e) {
					if ($(this).parent().find(".cfbCurrent").html() == $(this).parent().find(".cfbMax").html()) {
						$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).fadeOut();//animate({opacity:'hide'}, 1500, "easeout");
						$(this).parent().find(".cfbCurrent").html("1");
						$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).fadeIn();//animate({opacity:'show'}, 1500, "easeout");
					} else {
						$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).fadeOut();//animate({opacity:'hide'}, 1500, "easeout");
						$(this).parent().find(".cfbCurrent").html(parseInt($(this).parent().find(".cfbCurrent").html()) + 1);
						$("#" + thisPanelsID + $(this).parent().find(".cfbCurrent").html()).fadeIn();//animate({opacity:'show'}, 1500, "easeout");
					}
					e.stopPropagation(); // required to prevent propagation causing omniture roundtrips in animation loop 
					e.preventDefault();
					return false;
				};
				$(this).find(".cfbNext").click(clickFunc);
				$(this).find(".cfbNext").bind('animate',clickFunc); // do this as my animate action too 
			}

			// 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()).fadeOut();//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()).fadeIn();//animate({opacity:'show'}, 1500, "easeout");
					} else {
						$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).fadeOut();//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()).fadeIn();//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;
				});
				var clickFunc = function() {
					if ($(this).parent().parent().parent().find(".cfbCurrent").html() == $(this).parent().parent().parent().find(".cfbMax").html()) {
						$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).fadeOut();//animate({opacity:'hide'}, 1500, "easeout");
						$(this).parent().parent().parent().find(".cfbCurrent").html("1");
						$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).fadeIn();//animate({opacity:'show'}, 1500, "easeout");
					} else {
						$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).fadeOut();//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()).fadeIn();//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(clickFunc);
				$(this).find(".arrowRight").bind('animate',clickFunc);
				$(".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()).fadeOut();//animate({opacity:'hide'}, 1500, "easeout");
							
							$(this).parent().parent().parent().find(".cfbCurrent").html((i+1));
							$("#" + thisPanelsID + $(this).parent().parent().parent().find(".cfbCurrent").html()).fadeIn();//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() {
		$.each( $("#tv_shed #tv_navigation .nav li a"), function(i, l){
			//Set the 1st element to be selected
			if(i == 0){
				$(this).addClass('selected');
			}
			$(this).click(function() {
				var slectedItem = $("#tv_shed #tv_navigation .nav li").find('.selected');
				$(slectedItem).removeClass("selected");
				$(this).addClass('selected');
				$('.show_container').animate({"left": (i * 237 * -1)+"px"}, { "duration": "slow"});
				return false;
			});
			
		});
		
		$.each( $("#tv_shed .show_container .show_now"), function(i, l){

			var showTitle = $(this).find('.show_title a');
			
			if($(showTitle).attr("href") != undefined){
				$(this).css("cursor","pointer");
				
				$(this).mouseover(function(){
					$(this).css("background-image","url(http://images.tvnz.co.nz/tvnz_site_images/home_tv_shedule_content_hover_repeat.jpg)");
				});
				
				$(this).mouseout(function(){
					$(this).css("background-image","none");
				});
				
				$(this).click(function(){
					window.location.replace($(showTitle).attr("href"));
				});
			}

		});
		
		$.each( $("#tv_shed .show_container .show_next"), function(j, k){
			var showTitle = $(this).find('.show_title a');
			if($(showTitle).attr("href") != undefined){
				$(this).css("cursor","pointer");
				$(this).mouseover(function(){
					$(this).css("background-color","#fff");
				});
				$(this).mouseout(function(){
					$(this).css("background-color","transparent");
				});
			}

		});
	}
	
	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 initMostPopular = function() {
		//Most Popular tabbed panel
		$("#most_popular #mp_tabs a").each(function() {
			$(this).click(function() {
				if (!$(this).hasClass("selected")) {
					$("#most_popular #mp_tabs .selected").removeClass("selected");
					$("#most_popular .main .selected").removeClass("selected");
					$(this).addClass("selected");
					$("#most_popular .main ." + $(this).parent().attr("class")).addClass("selected");
					//$(this).removeAttr("style");
				}
				return false;
			}).css({cursor: "pointer"});
		});
	}
	
	// superClick makes the entire div clickable
	var initSuperClick = function() {
		$(".superClick").click(function() {
			if ($(this).find("a:first").length>0) {
				document.location = $(this).find("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, urlEncodedArticleTitle, productionUrl);
					$("#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.toLowerCase().replace("_", " ") + ": ");
				$("#header .weather img.icon").attr("src", "http://images.tvnz.co.nz/tvnz_site_images/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", "http://images.tvnz.co.nz/tvnz_site_images/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");
						}

						//alert($(this).attr("id"))

					});
				});
				// 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", "http://images.tvnz.co.nz/tvnz_site_images/images/weather/icon" + town_data[$("#changeMyLocation .groupAll .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().toUpperCase(), 365);
					jsTVNZ.Common().initYourWeather();
					jsTVNZ.Common().initYourWeatherSelect();
					$("#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 yourWeatherClick = function () {
		$("#yourWeather .changeLocation select").change(function() {
			jsTVNZ.Common().createCookie("weatherTVNZ", $("#selectTown :selected").val().toUpperCase(), 365);
			jsTVNZ.Common().initYourWeather();
			jsTVNZ.Common().initWeatherLocation();
		});
	}

	// should apply seleceted town
	var initYourWeatherSelect = function () {
		var myCookie = jsTVNZ.Common().readCookie("weatherTVNZ");
		if (myCookie != null) {
			// if it exsists, use the cookie to write the current weather
			if (typeof(towns) == "object") {
				var getTown = myCookie;
			}
		}
		$("#yourWeather .changeLocation select option").each(function() {
			var town = $(this).val()
			if (town == getTown){
				$(this).attr("selected", "SELECTED");
			}
		});
	}


	var initYourWeather = function() {
		var myCookie = jsTVNZ.Common().readCookie("weatherTVNZ");
		if (myCookie != null) {
			// if it exsists, use the cookie to write the current weather
			if (typeof(towns) == "object") {
				var getTown = myCookie;
			}
		} else {
			// else it will pick a random number and use that.
			if (typeof(towns) == "object") {
				var randomNo = Math.floor(Math.random() * towns.length);
				var getTown = towns[randomNo];
			}
		}

		$.ajax({
			type: "GET",
			url: "/content/iphone_weather_group/iphone_weather_area_feed_skin.xml?area=" + getTown,
			dataType: "xml",
			success: function(xml) {
				$(xml).find('entry:first').each(function(){
					var area_caption_full = $(this).find('caption').text();
					if ($(this).find('caption').text().length > 50) {
							var area_caption = $(this).find('caption').text().substr(0, 50) + "...";
						} else {
							var area_caption = $(this).find('caption').text();
					};
					var area_high = $(this).find('high').text();
					var area_low = $(this).find('low').text();
					var area_wind = $(this).find('wind').text();
					var area_image = $(this).find('image').text();
					if( getTown == 'TE_KUITI') { getTown = 'TE_kUITI'};
					$('#yourWeather .longRange a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather .state a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather .state a').attr("title", area_caption_full);
					$('#yourWeather p.location a').html(getTown.replace("_", " ") + " WEATHER TODAY");
					$('#yourWeather p.location a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather p.overview a').attr("title", area_caption_full);
					$('#yourWeather p.overview a').html(area_caption);
					$('#yourWeather p.overview a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather ul.temps:first li.high span').html(area_high);
					$('#yourWeather ul.temps:first li.high a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather ul.temps:first li.low span').html(area_low);
					$('#yourWeather ul.temps:first li.low a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather ul.temps:first li.wind span').html(area_wind);
					$('#yourWeather ul.temps:first li.wind a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather .state a img').attr("src", area_image.replace(".jpg", "_large.gif"));
				});

				$(xml).find('entry:eq(1)').each(function(){
					var area_caption = $(this).find('caption').text();
					var area_high = $(this).find('high').text();
					var area_low = $(this).find('low').text();
					var area_image = $(this).find('image').text();
					$('#yourWeather .longRange .area1 a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather .longRange .area1 a').attr("title", area_caption);
					$('#yourWeather .longRange .area1 ul.temps li.high span').html(area_high);
					$('#yourWeather .longRange .area1 ul.temps li.high a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather .longRange .area1 ul.temps li.low span').html(area_low);
					$('#yourWeather .longRange .area1 ul.temps li.low a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather .longRange .area1 a img').attr("src", area_image);
				});

				$(xml).find('entry:eq(2)').each(function(){
					var area_caption = $(this).find('caption').text();
					var area_high = $(this).find('high').text();
					var area_low = $(this).find('low').text();
					var area_image = $(this).find('image').text();
					$('#yourWeather .longRange .area2 a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather .longRange .area2 a').attr("title", area_caption);
					$('#yourWeather .longRange .area2 ul.temps li.high span').html(area_high);
					$('#yourWeather .longRange .area2 ul.temps li.high a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather .longRange .area2 ul.temps li.low span').html(area_low);
					$('#yourWeather .longRange .area2 ul.temps li.low a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather .longRange .area2 a img').attr("src", area_image);
				});

				$(xml).find('entry:eq(3)').each(function(){
					var area_caption = $(this).find('caption').text();
					var area_high = $(this).find('high').text();
					var area_low = $(this).find('low').text();
					var area_image = $(this).find('image').text();
					$('#yourWeather .longRange .area3 a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather .longRange .area3 a').attr("title", area_caption);
					$('#yourWeather .longRange .area3 ul.temps li.high span').html(area_high);
					$('#yourWeather .longRange .area3 ul.temps li.high a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather .longRange .area3 ul.temps li.low span').html(area_low);
					$('#yourWeather .longRange .area3 ul.temps li.low a').attr("href", '/cda/tvnz/weather_area_skin/' + getTown + '?area=' + getTown);
					$('#yourWeather .longRange .area3 a img').attr("src", area_image);
				});
			}
		});
	}
	
	var resizeFrame = function(f) {
		f.style.height = f.contentWindow.document.body.scrollHeight + "px";
	}
	
	var updateScoreboardHeight = function(num) {
		switch(num) {
			case 1:
				$("#").css({height: "63px"});
			break;
			case 2:
				$("#").css({height: "136px"});
			break;
		}
	}
	
	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 .seeCam .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 initImageGallerySpecialEvents = function() {
		$(".quickTabModule .seeCam .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 initQuickTabModule = 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(String($(this).css("padding-top")).substring(0, String($(this).css("padding-top")).length - 2)) + parseInt(String($(this).css("padding-bottom")).substring(0, String($(this).css("padding-bottom")).length - 2)) + parseInt(String($(this).css("margin-top")).substring(0, String($(this).css("margin-top")).length - 2)) + parseInt(String($(this).css("margin-bottom")).substring(0, String($(this).css("margin-bottom")).length - 2));
			if (($("#latestNews .articles").height() - x) < 0) {
				$(this).css({display: "none"});
			}
		});
	}
	
	var initTVScheduleSpecialEvent = function() {
		// TV Schedule
		if ( $("#specialEventSchedule").length > 0 ){
			$("#specialEventSchedule .dayNav a").each(function() {
				$(this).click(function() {
					if ($("#specialEventSchedule .dayNav a.selected").parent().attr("class") != $(this).parent().attr("class")) {
						var oldSelected = $("#specialEventSchedule .dayNav a.selected").parent().attr("class");
						$("#specialEventSchedule .dayNav a.selected").removeClass("selected");
						$(this).addClass("selected");
						$("#specialEventSchedule .dayScroller ." + oldSelected).animate({left: "-282px"}, 250, function() {
							$(this).css({left: "282px"}).removeClass("selected");
						});
						$("#specialEventSchedule .dayScroller ." + $(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
							$('.dayScroller').jScrollPane({
								scrollbarWidth:17,
								scrollbarMargin:4,
								showArrows:true,
								dragMinHeight: 36,
								dragMaxHeight: 36
							});
						});
					}
					return false;
				}).css({cursor: "pointer"});
			});
		}
		
		// add's the JS Scroll Bar to the TV Schedule
		$('.dayScroller').jScrollPane({
			scrollbarWidth:17,
			scrollbarMargin:4,
			showArrows:true,
			dragMinHeight: 36,
			dragMaxHeight: 36
		});
	}
	
	var initToggleAnimation = function() {
		$(".thickbox").each(function() {
			$(this).click(function() {
				jsTVNZ.Common().stopAnimation();
			});
		});
	}

	var setCurrentDate = function() {
		$("#hpDate").html(www.serverTime.dayText + " - " + www.serverTime.monthText + " " + www.serverTime.day + ", " + www.serverTime.year).css({visibility: "visible"});
		$("#tv_shed #tv_shed_title span.date").html(www.serverTime.dayText + " " + www.serverTime.day + " " + www.serverTime.monthText).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+"; domain=tvnz.co.nz; 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", "http://images.tvnz.co.nz/tvnz_site_images/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 {
		animateLoop : animateLoop,
		syncToFlash : syncToFlash,
		checkIfSync : checkIfSync,
		initLiveNews : initLiveNews,
		initVideoPlayer : initVideoPlayer,
		removeBandWidthDetection : removeBandWidthDetection,
		fireFoxRemoveSWF : fireFoxRemoveSWF,
		switchVideoAdds : switchVideoAdds,
		changeVideoSize : function (quality) {changeVideoSize(quality)}, // dirty dirty hack so that this 'global' file with non-global interfaces still compiles
		changeWinMedia : changeWinMedia,
		changeSilverlightPlayer : changeSilverlightPlayer,
		onSilverLightPlayerLoaded: onSilverLightPlayerLoaded,
		stopAnimation : stopAnimation,
		startAnimation : startAnimation,

		setGlobal : setGlobal,
		getGlobal : getGlobal,
		
		initPushDown : initPushDown,
		openPushDown : openPushDown,
		fireFoxRemovePushDownSWF : fireFoxRemovePushDownSWF,
		closePushDown : closePushDown,	
		initSlidePanel : initSlidePanel,
		initLatestNews : initLatestNews,
		initTabs : initTabs,
		initTVSchedule : initTVSchedule,
		initOnDemand : initOnDemand,
		initWatchEpisodes : initWatchEpisodes,
		initMostPopular : initMostPopular,
		initShareMore : initShareMore,
		initSetHomepage : initSetHomepage,
		yourWeatherClick : yourWeatherClick,
		initWeatherLocation : initWeatherLocation,
		initYourWeatherSelect : initYourWeatherSelect,
		initYourWeather : initYourWeather,
		initPoll : initPoll,
		initPageControls : initPageControls,
		initImageGallery : initImageGallery,
		initToggleAnimation : initToggleAnimation,
		detectVideoBandwidth : detectVideoBandwidth,
		initSuperClick : initSuperClick,

		setCurrentDate : setCurrentDate,
		disableTools : disableTools,
		resizeFrame : resizeFrame,
		createCookie : createCookie,
		readCookie : readCookie,
		eraseCookie : eraseCookie,

		initTVScheduleSpecialEvent : initTVScheduleSpecialEvent,
		initQuickTabModule : initQuickTabModule,
		initImageGallerySpecialEvents : initImageGallerySpecialEvents,
		
		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' );
}

// 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>');
}

/*
 * This function is used update the hero panel. 
 */
function heroPanel(){
	var maxItems = 3;
	var rotationTime = 6; // time in seconds
	var maxChars = 100; //max number of words appear in the summary
	var currentHeroIndex = 0;
	var heroElements = $('#heroPanel .heroPanelNav .hero');;
	var heroMiniElements = $('#heroPanel .heroPanelNav .heroMini');
	var heroImages = $('#heroPanel .heroPanelContent a');
	var heroLinks = $('#heroPanel .heroPanelContent .heroLink');
	var arrow = $('.selectedArrow');
	var arrowYPos = 0;
	var heroHeight = $('.hero').height();
	var heroMiniHeight = $('.heroMini').height();
	var timer;
	var pre;
	
	heroPanelInit();
	performRotation();
	setTimer();
	
	$('.hero, .heroPanelContent').mouseover(function() {
		window.clearInterval(timer);
	});
	
	$('.hero, .heroPanelContent').mouseout(function() {
		setTimer();
	});
	
	$('.heroMini').click(function(){
		
		var clickedId = $(this);
		$.each( heroMiniElements, function(i, l){
			if($(this).attr("id") == $(clickedId).attr("id")){
				currentHeroIndex = i;
				performRotation();		
			}
		});
		
		return false;
	});
	
	
	function heroPanelInit(){
		$(heroMiniElements[0]).css("display","block");
		$(heroMiniElements[0]).hide();
		
		//bind the links to the arrow
		$.each( arrow, function(i, l){
			$(this).click(function(){
				var link = $(heroImages[i]).attr("href");
				window.location.replace(link);
			});
		});
		
		

	}
	
	//update the hero panel after a set interval
	function performRotation(){

		$.each( heroElements, function(i, l){
			if(currentHeroIndex == i){
				
				arrowYPos = (currentHeroIndex * heroMiniHeight) + heroHeight/2 - $(arrow).height()/2;
				$(".heroarrow"+(i+1)).css("display","block");
				$(".heroarrow"+(i+1)).animate({"left": -20}, "fast");
				$(heroElements[i]).slideDown("fast");
				$(heroMiniElements[i]).slideUp("fast");	
				
			}else{
				$(".heroarrow"+(i+1)).animate({"left": 0}, "fast");
				$(".heroarrow"+(i+1)).css("display","none");
				$(heroElements[i]).slideUp("fast");
				$(heroMiniElements[i]).slideDown("fast");
			}
			
		});
		
		if(pre == null){
			pre = heroImages[0];
			$(pre).fadeIn();
		}else{
			$(pre).hide();
			var getimg = "url("+$(pre).find('img').attr('src')+")";
			$('.heroPanelContent').css("background",getimg);
			$(heroImages[currentHeroIndex]).fadeIn(800);
			pre = heroImages[currentHeroIndex];
		}
		currentHeroIndex++;
		if(currentHeroIndex == maxItems){ currentHeroIndex = 0; }
	}
	
	function setTimer(){
		timer = window.setInterval(function() {
			performRotation();
		}, rotationTime * 1000);
	}
}