// GLOBAL SCRIPT FILE INCLUDES
document.write('\x3Cscript type="text/javascript" src="http://content.worldnow.com/global/interface/httprequest/httprequest.js">\x3C/script>');
/* DYNAMIC STATIC NAV */
var navTx;
function wnNavReset(){
	clearTimeout(navTx);
	$wn(".wnLevel2:visible").hide();
	$wn(".cur").show();
}

function launchDynamicNav(){
	var h = 26;
	var t = 35;
	$wn(".wnMainMenu .wnHorizontal .wnStatic .wnSublevel").css({"z-index": "1000"});

	$wn(".wnStatic .wnLevel1 li:first").attr("rel", "1");
	$wn(".wnStatic .wnLevel1 li:first").siblings("li").each(function(){
		$wn(this).attr("rel", "1");
		if(!$wn(this).find("ul").length){
			$wn(this).append("<ul class='wnLevel2 wnSublevel' style='height: "+ h +"px; top: "+ t +"px; display: none;'><li class='wnFirstNode wnOn'><span><a href=''>&nbsp;</a></span></li></ul>");
		}
	});
	$wn(".wnStatic .wnLevel2 li:first").attr("rel", "2");
	$wn(".wnStatic .wnLevel2 li:first").siblings("li").each(function(){
		$wn(this).attr("rel", "2");
	});
	
	var subnavPos = $(".wnMenuSpacer").offset();
	if($wn(".wnLevel2:visible").attr("class") != null){
		//IF A SUB NAV IS PRESENT FOR THE CURRENT CATEGORY
		$wn(".wnLevel2:visible").addClass("cur");
		$wn(".wnLevel2").css({"height": h + "px", "top": t +"px"});
	}else{
		//IF A SUB NAV IS NOT PRESENT FOR THE CURRENT CATEGORY
		$wn("#WNMenuCol1 .wnMenuSpacer").height(h);
		$wn(".wnLevel2").css({"height": h +"px", "top": subnavPos.top +"px", "left": subnavPos.left +"px", "width": ($wn(".wnMenuSpacer").width()-2) +"px", "overflow": "hidden"});
	}
	//$wn("ul.wnLevel1 li.wnFirstNode .wnLeft").css("padding-left","3px");
	$wn("ul.wnLevel1 li[rel='1']").bind("mouseenter", function(){
		clearTimeout(navTx);
		$wn(".wnLevel2:visible").hide();
		$wn(this).find(".wnLevel2").addClass("wnOn").css({"display": "block"});
	});
	$wn("ul.wnLevel1 li[rel='2']").bind("mouseenter", function(){
		clearTimeout(navTx);
	});
	
	$wn("#WNMenuCol1").bind("mouseleave", function(){
		clearTimeout(navTx);
		navTx = setTimeout(function(){
			wnNavReset();
		}, 500);
	});	
}

function wxBrandingSetup(){
	var wxBrandContent = "<div id='wxBrandFeatureOuter'><div id='wxBrandFeature'><div class='current'><div id='loc' class='text'></div><div id='temp' class='text'></div><div id='image' class='text'></div></div><div class='location'><select id='wxLocation'><option value='0' selected='selected'>Traverse City</option><option value='1'>Sault Ste. Marie</option><option value='2'>Mt. Pleasant</option><option value='3'>Cadillac</option><option value='4'>Ludington</option><option value='5'>Houghton Lake</option><option value='6'>Gaylord</option><option value='7'>Rogers City</option><option value='8'>Manistee</option><option value='9'>Grayling</option><option value='10'>Big Rapids</option><option value='11'>Charlevoix</option><option value='12'>Harbor Springs</option><option value='13'>Bellaire</option></select></div><div class='default'><input type='checkbox' id='wxCity' name='wxCity' /><label id='wxCityLabel' for='wxCity'></label></div></div>";
	
	$wn("#WNBranding").prepend(wxBrandContent);
	
	var stationNum = 0;
	var wxStations = new Array();
	wxStations[0] = new Array("http://data.wp.myweather.net/eWxII/?data=*49684", "Traverse City");
	wxStations[1] = new Array("http://data.wp.myweather.net/eWxII/?data=*49783", "Sault Ste. Marie");	
	wxStations[2] = new Array("http://data.wp.myweather.net/eWxII/?data=*48858", "Mt. Pleasant");	
	wxStations[3] = new Array("http://data.wp.myweather.net/eWxII/?data=*49601", "Cadillac");	
	wxStations[4] = new Array("http://data.wp.myweather.net/eWxII/?data=*49431", "Ludington");	
	wxStations[5] = new Array("http://data.wp.myweather.net/eWxII/?data=*48629", "Houghton Lake");	
	wxStations[6] = new Array("http://data.wp.myweather.net/eWxII/?data=*49735", "Gaylord");	
	wxStations[7] = new Array("http://data.wp.myweather.net/eWxII/?data=*49779", "Rogers City");	
	wxStations[8] = new Array("http://data.wp.myweather.net/eWxII/?data=*49660", "Manistee");	
	wxStations[9] = new Array("http://data.wp.myweather.net/eWxII/?data=*49738", "Grayling");	
	wxStations[10] = new Array("http://data.wp.myweather.net/eWxII/?data=*49307", "Big Rapids");	
	wxStations[11] = new Array("http://data.wp.myweather.net/eWxII/?data=*49720", "Charlevoix");	
	wxStations[12] = new Array("http://data.wp.myweather.net/eWxII/?data=*49740", "Harbor Springs");	
	wxStations[13] = new Array("http://data.wp.myweather.net/eWxII/?data=*49615", "Bellaire");
	var wxIconPath = "http://ftpcontent.worldnow.com/wwtv/custom/wx_icons/"
	
	var wxLength = wxStations.length;
	var br_url = wxStations[stationNum][0];
	var selNum = $wn("#wxBranding #wxLocation").find("option:selected").val();
	var tempNum;
	
	function wxInit(){
		br_weatherFeed(this);
	}
	
	function br_weatherFeed(xml){
		var fullFeed = xml.response.responseXML;
 		var currentTemp = $wn(fullFeed).find("currents location temp").text();
		var currentIcon = $wn(fullFeed).find("currents location icon").text();
		if(tempNum){
			$wn("#wxBrandFeature .current #loc").html(wxStations[tempNum][1]);
		} else {
			$wn("#wxBrandFeature .current #loc").html(wxStations[stationNum][1]);
		}
		$wn("#wxBrandFeature .current #temp").html(currentTemp + "&deg;");
		$wn("#wxBrandFeature .current #image").css("background", "url('"+ wxIconPath + currentIcon + ".png') top center no-repeat");
	}
 
	function stationChange(num){
		tempNum = num;
		br_url = wxStations[num][0];
		WNHttpRequestManager.makeRequest(br_url, { onSuccess: wxInit, onError: wxError});
	}
	
	function wxError(){}
	
	$wn("#wxBrandFeature #wxLocation").change(function(){
		stationChange($wn(this).val());
	});

	$wn("#wxBrandFeature #wxCity").click(function(){
		if($wn("#wxBrandFeature #wxCity:checked").length){
			wxValue = $wn("#wxBrandFeature #wxLocation").find("option:selected").val();
			setCookie("wxLoc", wxValue, 365);
			$wn("#wxBrandFeature #wxCityLabel").text(wxStations[wxValue][1]);
		}
		else {
			setCookie("wxLoc", wxValue, 0);
			$wn("#wxBrandFeature #wxCityLabel").text("Make Default City");
		}
	});	
	
	function setCookie(c_name,value,expiredays) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
	}
	
	function getCookie(c_name)	{
		if (document.cookie.length>0) {
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1) {
				c_start=c_start + c_name.length+1;
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			}
		}
		return "";
	}		
	
	function checkCookie(){
		wxValue = getCookie("wxLoc");
		if (wxValue!=null && wxValue!=""){
			document.getElementById("wxCity").checked = true;
			stationNum = wxValue;
			$wn("#wxBrandFeature #wxCityLabel").text(wxStations[wxValue][1]);
			$wn("#wxLocation option").removeAttr("selected");
			$wn("#wxLocation option:eq(" + wxValue + ")").attr("selected", "selected");
			WNHttpRequestManager.makeRequest(wxStations[wxValue][0], { onSuccess: wxInit, onError: wxError});	
		} else {
			$wn("#wxBrandFeature #wxCityLabel").text("Make Default City");
			WNHttpRequestManager.makeRequest(br_url, { onSuccess: wxInit, onError: wxError});	
		}
	}
	checkCookie();			
}

function bnRestyle(){
	if($wn("#DisplaySizeId-30").length){
		$wn("#DisplaySizeId-30 .feature:gt(0)").hide();
		$wn("#DisplaySizeId-30 .feature:first .summaryImage").insertBefore($wn("#DisplaySizeId-30 .feature:first .sectionTitle"));
		$wn("#DisplaySizeId-30 .feature:first .summaryImage").nextAll().wrapAll("<div class='wrap'></div>");
		$wn("#DisplaySizeId-30 .feature:first .wrap").append("<a class='liveStream' href='#'>Live Streaming Video &raquo;</a><a class='email' href='#'>Sign up for Email &raquo;</a>");
		$wn("#DisplaySizeId-30 .feature:first .wnClear").appendTo($wn("#DisplaySizeId-30 .wnItem:first"));
		$wn("img#WNBrandingImage").attr("src", "http://wwtv.images.worldnow.com/images/552178_G.jpg");
		$wn("#WNBranding #WNDS70").addClass("break");
	}
}

function moreLink(){
	$wn("a.wnMoreLink").html("More &raquo;");
}

function oddEven(){
	$wn("#WNCol2 .wnDS-20.wnDSContainer-standard .wnDVSummary:odd, .wnDVHeadlineBox.wnDSContainer-standard .wnDVSummary:odd, .wnDS-7 .wnDVSummary:odd, .wnDS-4 .wnDSItemsRow:odd, .wnDVPromoBox.wnDSContainer-standard .wnDSItemsRow:odd").addClass("even");
}

function addCategoryHeader(){
	if(wng_pageInfo.containerId != "217904" && wng_pageInfo.containerType == "C"){
		var tempTxt = $wn("#WNHeader h1:first").text();
		var tempHeader = "<div id='catHeader' class='wnDSContainer-standard'><div class='wnDSHeader' style='border-bottom:none;'><h3><span>" + tempTxt + "</span><div style='clear:both'></div></h3></div>";
		$wn("#WNCol23Top").prepend(tempHeader);
	}
}

$wn(document).ready(function(){
	//if(wng_pageInfo.containerId != "217905"){
		wxBrandingSetup();
	//}
	//addCategoryHeader();
	launchDynamicNav();
	bnRestyle();
	moreLink();
	oddEven();
});
