/* GLOBAL SCRIPT FILE INCLUDES */
document.write('\x3Cscript type="text/javascript" src="http://content.worldnow.com/global/interface/httprequest/httprequest.js">\x3C/script>');
/* END GLOBAL SCRIPT FILE INCLUDES */

var wncc = "";
var wnaffName = "";

try {
	wncc = wng_pageInfo.contentClassification;
} catch(e) {
}

try {
	wnaffName = wng_pageInfo.affiliateName;
} catch(e) {
}

if(wng_pageInfo.containerId == "215870" || wng_pageInfo.containerId == "32770"){
	document.write('\x3Clink rel="stylesheet" href="http://ftpcontent.worldnow.com/wowk/custom/homepage.css">');
	$wn(document).ready(function(){
		hbRestyle();
		//bfChange();
	});
}
/*
if(wng_pageInfo.containerType == "S"){
	document.write('\x3Clink rel="stylesheet" href="http://ftpcontent.worldnow.com/wtvg/custom/story.css">');
}
*/
/* FIX WNDATE */
function fixDate($el){
	var tDate = $el;
	$wn(tDate).find("script").remove();
	$wn(tDate).find("noscript").remove();
	$wn(tDate).html($wn(tDate).text());
}

/* DYNAMIC STATIC NAV */
var navTx;
function wnNavReset(){
	clearTimeout(navTx);
	$wn(".wnLevel2:visible").hide();
	$wn(".cur").show();
}

function launchDynamicNav(){
	var h = 26;
	var t = 39;
	$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);
	});	
}

/* MORE LINK REPOSITION */
function moreLinkReposition() {
	if ($wn(".wnMoreLink").length) {
		$wn("#WNCol23 .wnMoreLink, #WNCol2 .wnMoreLink, , #WNCol4 .wnMoreLink").each(function () {
			$wn(this).appendTo($wn(this).parents(".wnDSContainer-standard").find(".wnDSItems-standard"));
		});
		$wn("#WNCol3 .wnMoreLink").each(function () {
			$wn(this).appendTo($wn(this).parents(".wnDSContainer-standard").find(".wnDSItems-standard"));
		});
	}
	/* OPTIMUS UPDATE */
	if ($wn(".header .more").length) {
		$wn(".header .more.enabled").each(function () {
			$wn(this).addClass("wnMoreLink").appendTo($wn(this).parents(".contentGroup"));
		});
	}
}

//Col. 2 Headline Boxes for Homepage
function hbRestyle(){
	var backupImg = "http://wtvg.images.worldnow.com/images/535206_G.jpg";
	if($wn("#WNCol2 .wnDVHeadlineBox").length){
		$wn("#WNCol2 .wnDVHeadlineBox").each(function(){
			$wn(this).addClass("modded").find(".wnDVSummary:gt(0)").addClass("noDisplay");
			$wn(this).find(".wnDVSummary:last").addClass("last");	
		});
	}
	if($wn("#WNCol3 .wnDVHeadlineBox").length){
		$wn("#WNCol3 .wnDVHeadlineBox").each(function(){
			$wn(this).addClass("modded").find(".wnDVSummary:gt(0)").addClass("noDisplay");
			$wn(this).find(".wnDVSummary:last").addClass("last");
		});
	}
};

// Forms
function formRestyle(){
	$wn(".wnDVQuickForm, .wnDVQuickSignup").each(function(){
		$wn(this).find("h4:first").addClass("wnDSHeader").wrapInner("<h3><span style='display:inline;'></span></h3>");
	});
}

//Background Image
function bgChange(){
	if((!$wn("#WNAd41").length) && (!$wn("#WNAd46").length)){
		$wn("#WNAffWOWK").addClass("noAd");
		if (($wn.browser.msie) && ($wn.browser.version == "7.0")){
			$wn("#WNAffWOWK").addClass("ie7");
		}
	} else if ($wn("#WNAd41").length || $wn("#WNAd46").length){
		if (($wn.browser.msie) && ($wn.browser.version == "7.0")){
			$wn("#WNAd41, #WNAd46").addClass("ie7");
		}
	}
}

//Preload Radar Image
/*
var radarImg = ["http://ftpcontent.worldnow.com/wowk/weather/tempradar.jpg", "http://ftpcontent.worldnow.com/wowk/weather/WOWK_Radar2.jpg", "http://ftpcontent.worldnow.com/wowk/weather/WOWK_Radar3.jpg", "http://ftpcontent.worldnow.com/wowk/weather/WOWK_Radar4.jpg", "http://ftpcontent.worldnow.com/wowk/weather/WOWK_Radar5.jpg", "http://ftpcontent.worldnow.com/wowk/weather/WOWK_Radar6.jpg", "http://ftpcontent.worldnow.com/wowk/weather/WOWK_Radar7.jpg"];

var brRadarInt;
var brRadarIndex = 0;
$wn(radarImg).each(function() {
	$wn("<img>").attr("src", this);
});

function CSbrandAnimatedRadar(){
	clearTimeout(brRadarInt);
	brRadarInt = setTimeout(CSbrandAnimatedRadar, 500);
	brRadarIndex++;
	if(brRadarIndex >= radarImg.length){ brRadarIndex = 0; }
	$wn("#CSbrandAniRadar").attr("src",radarImg[brRadarIndex]);
}
*/
//Weather Branding Feature
var mainTimer;
function CSlaunchWx(){
	if($wn("#wxBrandFeatureOuter").length){
		$wn("#wxBrandFeatureOuter").remove();
	}
	var wxbf = "<div id='wxBrandFeatureOuter'><div id='wxBrandFeature'><a class='radar' href='/weather'><img id='CSbrandAniRadar'  src='http://ftpcontent.worldnow.com/wowk/weather/WOWK_Radar1.jpg' /></a><div class='info'><div id='city'></div><div id='temp'></div><div id='feel'></div><div id='hilo'></div></div><div class='cc'><div id='condition'></div></div><div class='buttons'><a class='button' href='/weather'>7 Day Forecast ></a><a id='closings' class='button' href='/category/223083/school-closings'><span></span> School Closings ></a><a class='button' href='/category/223076/predictor'>Predictor ></a></div></div></div>";
	
	$wn("#WNDS70").hide();
	var wxint;
	var stationNum = 0;
	var wxStations = [];
	wxStations[0] = new Array("http://data.wp.myweather.net/eWxII/?data=*25301", "Charleston");
	wxStations[1] = new Array("http://data.wp.myweather.net/eWxII/?data=*25701", "Huntington");								
	var wxLength = wxStations.length;			
	var wxIconPath = "http://ftpcontent.worldnow.com/wowk/custom/wx_icons/"
	var br_url = wxStations[stationNum][0];
	var wxSavedData = [];	
	
	for(dd=0;dd<wxLength;dd++){
		wxSavedData[dd] = [];
	}
	
	function CSwxInit(){
		CSbrWeatherFeed(this);
	}
	
	function CSbrWeatherFeed(xml){
		$wn("#wxBrandFeature").hide();
		var fullFeed = xml.response.responseXML;
		var testFeed = $wn(fullFeed).find("currents location temp").text();
		if (isNaN(parseInt(testFeed))){
			stationChange();
		} else {
		  wxSavedData[stationNum][0] = $wn(fullFeed).find("currents location temp").text();
		  wxSavedData[stationNum][1] = wxStations[stationNum][1];
		  wxSavedData[stationNum][2] = $wn(fullFeed).find("currents location feelslike").text();
		  wxSavedData[stationNum][3] = $wn(fullFeed).find("forecasts location days:first day:first forecast:first").attr("high");
		  wxSavedData[stationNum][4] = $wn(fullFeed).find("forecasts location days:first day:first forecast:first").attr("low");
		  wxSavedData[stationNum][5] = $wn(fullFeed).find("currents location sky").text();
		  wxSavedData[stationNum][6] = $wn(fullFeed).find("currents location icon").text();
		  
		  $wn("#WNDS70").hide();
		  $wn("#wxBrandFeature .info #city").html(wxSavedData[stationNum][1]);
		  $wn("#wxBrandFeature .info #temp").html(wxSavedData[stationNum][0] + "&deg;");
		  $wn("#wxBrandFeature .info #feel").html("Feels like " + wxSavedData[stationNum][2] + "&deg;");
		  $wn("#wxBrandFeature .info #hilo").html("Hi " + wxSavedData[stationNum][3] + " / Lo " + wxSavedData[stationNum][4]);
		  $wn("#wxBrandFeature #condition").html(wxSavedData[stationNum][5]);
		  $wn("#wxBrandFeature .cc").css("background-image", "url('" + wxIconPath + wxSavedData[stationNum][6] + ".png')");
		  $wn("#wxBrandFeature").show();
		}
	}
	
	function CSinputSavedData(num){
		$wn("#wxBrandFeature").hide();
		$wn("#wxBrandFeature .info #city").html(wxSavedData[num][1]);
		$wn("#wxBrandFeature .info #temp").html(wxSavedData[num][0] + "&deg;");
		$wn("#wxBrandFeature .info #feel").html("Feels like " + wxSavedData[num][2] + "&deg;");
		$wn("#wxBrandFeature .info #hilo").html("Hi " + wxSavedData[num][3] + " / Lo " + wxSavedData[num][4]);
		$wn("#wxBrandFeature #condition").html(wxSavedData[num][5]);
		$wn("#wxBrandFeature .cc").css("background-image", "url('" + wxIconPath + wxSavedData[num][6] + ".png')");
		$wn("#wxBrandFeature").show();
	}
 
	function CSstationChange(){
		clearTimeout(wxint);
		wxint = setTimeout(CSstationChange, 5000);
		stationNum++;
		if(stationNum >= wxLength){ stationNum = 0; }
		br_url = wxStations[stationNum][0];
		if(!wxSavedData[stationNum].length){
			WNHttpRequestManager.makeRequest(br_url, { onSuccess: CSwxInit, onError: CSwxError});
		} else {
			CSinputSavedData(stationNum);
		}
	}
	
	function CSwxError(){}
	
	WNHttpRequestManager.makeRequest(br_url, { onSuccess: CSwxInit, onError: CSwxError});
	if(wxLength>1){
		wxint = setTimeout(CSstationChange, 5000);
	}
	$wn("#WNBranding").append(wxbf);		
	WNscClosings("http://ftpcontent.worldnow.com/wowk/WOWK_closings.htm");		
	//WNscClosings("http://ftpcontent.worldnow.com/wowk/BTI/WOWK-SC4C.xml");
}

function WNGetAPI(blockTitle, numItems, apiURL, moreLink, containerID, icon){
	function apiInitContent(){
		apiParseXML(this);
	}
		
	function apiErrorFunction(e){}
	
	function makeRSSRequest(){
			WNHttpRequestManager.makeRequest(apiURL, { onSuccess: apiInitContent, onError: apiErrorFunction});
	};
	
	makeRSSRequest();

	function apiParseXML(xml){
		var buildCont = "<div id='#api" + containerID + "' class='apiBlock'><ul class='apiGroup'><li class='apiItem header'><h3><span class='text'>" + blockTitle + "</span><span class='icon' style='background-image:url(" + icon + ")'></span></h3></li>";
		var fullFeed = xml.response.responseXML;
		var totalItems = ($wn(fullFeed).find("story[displaysize='-20']").length);
		if(totalItems > numItems){
			totalItems = numItems;
		}
		var $items = $wn(fullFeed).find("story[displaysize='-20']:lt(" + numItems + ")");
		$items.each(function(xx){
			var baseurl = $wn(this).find("baseurl:first").text();
			if (baseurl == ""){
				baseurl = moreLink.slice(7,moreLink.length);
			}
			var id = $wn(this).find("id:first").text();
			var type = $wn(this).find("type:first").text();
			if (type == "S"){
				type = "story"
			} else if (type == "C"){
				type = "category"
			} else if (type == "V"){
				type = "clip"
			}
			var headline = $wn(this).find("headline:first").text();
			var pageURL = $wn(this).find("seo:first pageurl").text();
	
			if(xx == 0){
				var summary = $wn(this).find("abstract:first").text();
				var image = $wn(this).find("abstractimage:last filename").text();
				if(!image){
					buildCont += "<li class='apiItem feature " + type + " priority-1'><h4 class='apiContent headline'><a href='http://" + baseurl + "/" + type + "/" + id + "/" + pageURL + "' target='_blank'><span class='video'></span><span class='text'>" + headline + "</span><div class='apiClear'></div></a></h4><div class='apiContent summary'>" + summary + "<a href='http://" + baseurl + "/" + type + "/" + id + "/" + pageURL + "' class='summaryLink' target='_blank'>More&gt;&gt;</a></div><div class='apiClear'></div></li>";
				} else {
				buildCont += "<li class='apiItem feature " + type + " priority-1'><h4 class='apiContent headline'><a href='http://" + baseurl + "/" + type + "/" + id + "/" + pageURL + "' target='_blank'><span class='video'></span><span class='text'>" + headline + "</span><div class='apiClear'></div></a></h4><div class='apiContent summaryImage'><a href='http://" + baseurl + "/" + type + "/" + id + "/" + pageURL + "' target='_blank'><img src='" + image + "'></a></div><div class='apiContent summary'>" + summary + "<a href='http://" + baseurl + "/" + type + "/" + id + "/" + pageURL + "' class='summaryLink' target='_blank'>More&gt;&gt;</a></div><div class='apiClear'></div></li>";
				}
			} else {
				buildCont += "<li class='apiItem feature " + type + "'><h4 class='apiContent headline'><a href='http://" + baseurl + "/" + type + "/" + id + "/" + pageURL + "' target='_blank'><span class='video'></span><span class='text'>" + headline + "</span><div class='apiClear'></div></a></h4></li>";
			}
	
		});
		
		buildCont += "</ul><a href='" + moreLink + "' class='wnMoreLink'>More&gt;&gt;</a></div>";
		
		$wn("#wn" + containerID).html(buildCont);
	}
}

function WNscClosings(closingsURL){
	function scClosingsInitContent(){
		scClosingsParseXML(this);
	}
		
	function scClosingsErrorFunction(e){}
	
	function makeRequest(){
			WNHttpRequestManager.makeRequest(closingsURL, { onSuccess: scClosingsInitContent, onError: scClosingsErrorFunction});
	};
	
	makeRequest();
	
	/*
	function scClosingsParseXML(file){
		var fullFeed = file.response.responseXML;
		var totalItems = ($wn(fullFeed).find("Closing").length);
		if(totalItems){
			$wn("#wxBrandFeature #closings").css("background-color","#AC2424").find("span").html(totalItems);
		}
	}
	*/

	function scClosingsParseXML(file){
		var fullFeed = file.response.responseText;
		if(fullFeed.match(/No Closings Currently Reported/i) === null){
			$wn("#wxBrandFeature #closings").css("background-color","#AC2424");
		}
	}
}

$wn(document).ready(function(){
	//launchDynamicNav();
	if(wng_pageInfo.containerId != "215871"){
		CSlaunchWx();
	}	
	moreLinkReposition();
	formRestyle();
	bgChange();
});
/*
$wn(window).load(function(){
	brRadarInt = setTimeout(CSbrandAnimatedRadar, 500);
});
*/

