/* 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 */

if(wng_pageInfo.containerId == "221341" || wng_pageInfo.containerId == "32770"){
	document.write('\x3Clink rel="stylesheet" href="http://ftpcontent.worldnow.com/kxnet/custom/homepage.css">');
	$wn(document).ready(function(){
		hbRestyle();
	});
}

/* LOCATION COOKIE */
var affConf = new Array();
affConf[0] = new Array("KXMC", "225352");
affConf[1] = new Array("KXMA", "225353");
affConf[2] = new Array("KXMB", "225354");
affConf[3] = new Array("KXMD", "225355");

var cookieNum;
var cookieLoc;

function setLocation(){
	cookieLoc = document.location.href;
	cookieLoc.toString();

	if ((cookieLoc.indexOf("http://") == -1) || (cookieLoc.indexOf("default.asp") != -1)){
		return false;
	}

	cookieLoc = cookieLoc.slice(cookieLoc.indexOf("category/") + 9, cookieLoc.length);
	cookieLoc = parseInt(cookieLoc);
	cookieLoc = cookieLoc.toString();
	
	for(var ii = 0; ii < affConf.length; ii++){
		if(affConf[ii][1].toString() == cookieLoc){
			cookieLoc = affConf[ii][1].toString();
			ii = affConf.length;
		}
	}

	return cookieLoc;
}

function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function deleteCookie(name,value) {
	var cookie_date = new Date();  // current date & time
	cookie_date.setTime(cookie_date.getTime() - 1);
	var date = new Date();
	date.setTime(date.getTime()-1);
	var expires = "; expires="+date.toGMTString();	
	document.cookie = name+"="+value+expires+"; path=/";
}

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 false;
}


/* CONTENT CLASSIFICATION & AFFILIATE INFO */
var wncc = "";
var wnaffName = "";

try {
	wncc = wng_pageInfo.contentClassification;
} catch(e) {
}

try {
	wnaffName = wng_pageInfo.affiliateName;
} catch(e) {
}

/* FIX WNDATE */
function fixDate($el){
	var tDate = $el;
	$wn(tDate).find("script").remove();
	$wn(tDate).find("noscript").remove();
	$wn(tDate).html($wn(tDate).text());
}

//Col. 2 Headline Boxes for Homepage
function hbRestyle(){
	var backupImg = "http://KXNET.images.worldnow.com/images/582325_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");
		});
	}
};

/* DYNAMIC STATIC NAV */
var navTx;
function wnNavReset(){
	clearTimeout(navTx);
	$wn(".wnLevel2:visible").hide();
	$wn(".cur").show();
}

function launchDynamicNav(){
	var h = 26;
	var t = 40;
	$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 = $wn(".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);
	});	
}

//Weather Branding Feature
function launchWx(){
	var wxStations = new Array();
	wxStations[0] = new Array("http://data.wp.myweather.net/eWxII/?data=*58701", "Minot");
	wxStations[1] = new Array("http://data.wp.myweather.net/eWxII/?data=*58601", "Dickinson");	
	wxStations[2] = new Array("http://data.wp.myweather.net/eWxII/?data=*58501", "Bismarck");	
	wxStations[3] = new Array("http://data.wp.myweather.net/eWxII/?data=*58801", "Williston");

	var wxbf = "<div id='wxBrandFeature'><div class='info'><div id='loc'><select id='wxLocation'><option value='0'>" + wxStations[0][1] + "</option><option value='1'>" + wxStations[1][1] + "</option><option value='2'>" + wxStations[2][1] + "</option><option value='3'>" + wxStations[3][1] + "</option></select></div><div id='temp'></div><div id='feel'></div></div><div class='radar' style='left:120px;'><a href='http://www.kxnet.com/category/226396/interactive-radar'><img title='Click For Interactive Radar' src='http://ftpcontent.worldnow.com/kxnet/custom/weather-minot/auto-update/CurrentRadarSatelliteND.jpg' height='85' width='64' /><div>Precision Radar</div></a></div><div class='radar' style='left:220px;'><a title='7 Day Forecast' href='http://www.kxnet.com/weather'><img src='http://ftpcontent.worldnow.com/kxnet/custom/weather-minot/7Day.jpg' height='85' width='64' /><div>7 Day Forecast</div></a></div><div class='links'><a title='Bismarck &amp; Dickinson Forecast' href='http://www.kxnet.com/story/16067339/bismarck-weather-radar-forecast-dickinson'>Bismarck Forecast</a><a title='Minot &amp; Williston Forecast' href='http://www.kxnet.com/category/226067/weather-minot-williston'>Minot Forecast</a><a title='Weather Closings &amp; Delays' href='http://www.kxnet.com/category/229573/weather-closingsdelays'>Closings</a><a title='Road Report' href='http://www.kxnet.com/link/523619/road-report'>Road Report</a></div></div>";
	
	var wxint;
	var wxCheckCookie = getCookie("kxnetlocation");
	if (wxCheckCookie != false){
		if (wxCheckCookie == affConf[0][1]){
			var stationNum = 0;
		} else if (wxCheckCookie == affConf[1][1]){
			var stationNum = 1;
		} else if (wxCheckCookie == affConf[2][1]){
			var stationNum = 2;
		} else if (wxCheckCookie == affConf[3][1]){
			var stationNum = 3;
		}
	} else {
		var stationNum = 0;	
	}
				
	var wxIconPath = "http://ftpcontent.worldnow.com/kxnet/custom/wx_icons/"
	
	var wxLength = wxStations.length;
	var br_url = wxStations[stationNum][0];
	
	function wxInit(){
		br_weatherFeed(this);
	}
	
	function br_weatherFeed(xml){
		$wn("#wxBrandFeature").hide();
		var fullFeed = xml.response.responseXML;
		var currentLoc = wxStations[stationNum][1];
		var currentTemp = $wn(fullFeed).find("currents location temp").text();
		if (!currentTemp){
			//stationChange();
		} else {
			$wn("#WNDS70").hide();
			var feelsLike = $wn(fullFeed).find("currents location feelslike").text();
			
			$wn("#wxBrandFeature .info #temp").html(currentTemp + "&deg;");
			$wn("#wxBrandFeature .info #feel").html("Feels like " + feelsLike + "&deg;");
			$wn("#wxBrandFeature .info #wxLocation option:eq(" + stationNum + ")").attr("selected", "selected");
			$wn("#wxBrandFeature").show();
		}
	}

	function stationChange(num){
		br_url = wxStations[num][0];
		stationNum = num;
		WNHttpRequestManager.makeRequest(br_url, { onSuccess: wxInit, onError: wxError});
	}
	
	function wxError(){}
 
	WNHttpRequestManager.makeRequest(br_url, { onSuccess: wxInit, onError: wxError});

	$wn("#WNBranding").append(wxbf);		
	
	$wn("#wxBrandFeature #wxLocation").change(function(){
		stationChange($wn(this).val());
	});	
		
}

/* MORE LINK REPOSITION */
function moreLinkReposition(){
	if($wn(".wnMoreLink").length){
		$wn("#WNCol23 .wnMoreLink, #WNCol2 .wnMoreLink, #WNCol3 .wnMoreLink, #WNCol4 .wnMoreLink").each(function(){
			$wn(this).text("More News").appendTo($wn(this).parents(".wnDSContainer-standard").find(".wnDSItems-standard"));
		});
	}
}

// Forms
function formRestyle(){
	$wn(".wnDVQuickForm, .wnDVQuickSignup").each(function(){
		$wn(this).find("h4:first").addClass("wnDSHeader").wrapInner("<h3><span style='display:inline;'></span></h3>");
	});
}

// Breaking News
function breakingNews(){
	var breakingNewsHeader = "http://KXNET.images.worldnow.com/images/582511_G.jpg";
	var swxHeader = "http://KXNET.images.worldnow.com/images/582512_G.jpg";
	if($wn("#DisplaySizeId-30").length){
		$wn("#DisplaySizeId-30 .feature").each(function(){
			$wn(this).find(".summaryImage").prependTo($wn(this));
			$wn(this).find(".timestamps").insertAfter($wn(this).find(".summary"));
		});
		var sectionTitle = ($wn("#DisplaySizeId-30 .feature:first .sectionTitle span").text()).toLowerCase();
		if(sectionTitle.indexOf("severe") != -1){
			$wn("#WNBrandingImage").attr("src", swxHeader);
		} else {
			$wn("#WNBrandingImage").attr("src", breakingNewsHeader);
		}
	}
}

$wn(document).ready(function(){
	launchDynamicNav();
	moreLinkReposition();
	formRestyle();
	breakingNews();
	if(wng_pageInfo.containerId != "221342"){
		launchWx();
	}	
});

$wn(document).ready(function(){
	try {
		var checkCookie = getCookie("kxnetlocation");
		if (checkCookie != false){
			var tempLoc = "http://www.kxnet.com/";
			// Redirect to local homepage
			if (document.location.href == tempLoc){
				if (checkCookie == affConf[0][1]){
				  document.location.href = tempLoc + "category/" + affConf[0][1] + "/news-kxmc-cbs13";
				} else if (checkCookie == affConf[1][1]){
				  document.location.href = tempLoc + "category/" + affConf[1][1] + "/news-kxma-cbs2";
				} else if (checkCookie == affConf[2][1]){
				  document.location.href = tempLoc + "category/" + affConf[2][1] + "/news-kxmb-cbs12";
				} else if (checkCookie == affConf[3][1]){
				  document.location.href = tempLoc + "category/" + affConf[3][1] + "/news-kxmd-cbs11";
				}
			} 
			// Redirect to local weather page
			else if (document.location.href == tempLoc + "weather"){
				if (checkCookie == affConf[0][1]){
				  document.location.href = tempLoc + "category/226067/weather-minot-williston";
				} else if (checkCookie == affConf[1][1]){
				  document.location.href = tempLoc + "story/16067339/bismarck-weather-radar-forecast-dickinson";
				} else if (checkCookie == affConf[2][1]){
				  document.location.href = tempLoc + "story/16067339/bismarck-weather-radar-forecast-dickinson";
				} else if (checkCookie == affConf[3][1]){
				  document.location.href = tempLoc + "category/226067/weather-minot-williston";
				}
			}
			// Redirect to local sports page
			else if (document.location.href == tempLoc + "category/221345/sports"){
				if (checkCookie == affConf[0][1]){
				  document.location.href = tempLoc + "category/226080/sports-minot-williston";
				} else if (checkCookie == affConf[1][1]){
				  document.location.href = tempLoc + "category/226079/sports-dickinson-bismarck";
				} else if (checkCookie == affConf[2][1]){
				  document.location.href = tempLoc + "category/226079/sports-dickinson-bismarck";
				} else if (checkCookie == affConf[3][1]){
				  document.location.href = tempLoc + "category/226080/sports-minot-williston";
				}
			}	
		}
	} catch(e) {}	
});
