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

if(wng_pageInfo.containerId == "215830"){
	document.write('\x3Clink rel="stylesheet" href="http://ftpcontent.worldnow.com/wvill/custom/homepage.css">');
	$wn(document).ready(function(){
		hbRestyle();
	});
}

function fixDate($el){
	var tDate = $el;
	$wn(tDate).find("script").remove();
	$wn(tDate).find("noscript").remove();
	$wn(tDate).html($wn(tDate).text());
}

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

// Move header ads (#WNAd41/46) deeper into the branding
function moveHdrAd() {
  var $ad41 = $wn('#WNHeader #WNAd41 iframe');
  var $ad46 = $wn('#WNHeader #WNAd46 iframe');
  
  if ($ad41.length > 0) {
    $ad41.addClass('csHeaderAd');
    $wn('#WNHeader #WNBranding').append($ad41);
  }
  
  if ($ad46.length > 0) {
    $ad46.addClass('csHeaderAd');
    $wn('#WNHeader #WNBranding').append($ad46);
  }
}

// Add social media, etc buttons to the branding area
function addHdrButtons() {
  $div = $wn('<div id="csBrandingIcons"></div>');
  $div.append('<a class="csBrandingIcon csEmail" href="/story/15359938/contact-us"></a>');
  $div.append('<a class="csBrandingIcon csMobile" href="/story/15360694/wvillustratedcom-mobile-sports-app"></a>');
  $div.append('<a class="csBrandingIcon csRSS" href="/category/215830/home?clienttype=rss"></a>');
  $div.append('<a class="csBrandingIcon csFacebook" href="http://www.facebook.com/wvillustrated"></a>');
  $div.append('<a class="csBrandingIcon csTwitter" href="http://www.twitter.com/wvillustrated"></a>');
  $wn('#WNHeader #WNBranding').append($div);
}

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

//Headline Boxes for Homepage
function hbRestyle(){
	var backupImg = "http://wvill.images.worldnow.com/images/531042_G.jpg";
	if($wn("#WNCol2 .wnDVHeadlineBox").length){
		$wn("#WNCol2 .wnDVHeadlineBox").each(function(){
			var $fHeadline = $wn(this).find(".wnDVSummary:eq(0)");
			if($fHeadline.find(".wnDate").length){
				fixDate($fHeadline.find(".wnDate"));
			}
			if(!$fHeadline.find(".wnImage").length){
				var genericImg = "<div class='wnImage'><a href='" + $fHeadline.find("h4 a").attr("href") + "'><img border='0' src='" + backupImg + "'></a></div>";
				$fHeadline.prepend(genericImg);
			}
			$fHeadline.find(".wnImage").append($fHeadline.find("h4"), $fHeadline.find(".wnDate"));
			$wn(this).addClass("modded").find(".wnDVSummary:gt(0)").addClass("noDisplay");
		});
	}
}

// Weather Branding Social Icons
function imgSwap(xx,url,type){
	var $tmp = $("#" + xx + " a");
	$tmp.each(function(){
		$(this).hover(function(){
			$(this).find("img").attr("src", url + $(this).attr("rel") + "_on." + type);
		},	function(){
			$(this).find("img").attr("src", url + $(this).attr("rel") + "_off." + type);
		})			
	});
}

/*
// Weather Branding
function wxBrandingSetup(){
	var wxbf = "<div id='wxBrand'><div class='location'><select id='wxLocation'><option value='0' selected='selected'>Salisbury</option><option value='1'>Ocean City</option></select></div><div id='socialTools'><a class='icon' rel='email' href='#'><img src='http://ftpcontent.worldnow.com/wvill/custom/social/email_off.png' /></a><a class='icon' rel='mobile' href='#'><img src='http://ftpcontent.worldnow.com/wvill/custom/social/mobile_off.png' /></a><a class='icon' rel='rss' href='#'><img src='http://ftpcontent.worldnow.com/wvill/custom/social/rss_off.png' /></a><a class='icon' rel='fb' href='#'><img src='http://ftpcontent.worldnow.com/wvill/custom/social/fb_off.png' /></a><a class='icon last' rel='twitter' href='#'><img src='http://ftpcontent.worldnow.com/wvill/custom/social/twitter_off.png' /></a><div class='clear'></div></div><div class='current'><div id='temp' class='text'></div><div id='image' class='text'></div><div id='location' class='text'></div></div><div id='sponsoredAd'><span id='adCall'></span></div></div>";
	
	$wn("#WNBranding").append(wxbf);
	
	var stationNum = 0;
	var wxStations = new Array();
	wxStations[0] = new Array("http://data.wp.myweather.net/eWxII/?data=*21804", "Salisbury");
	wxStations[1] = new Array("http://data.wp.myweather.net/eWxII/?data=*21842", "Ocean City");
	var wxIconPath = "http://ftpcontent.worldnow.com/wvill/custom/wx_icons/"
	
	var wxLength = wxStations.length;
	var br_url = wxStations[stationNum][0];
	var selNum = $wn("#wxBranding #wxLocation").find("option:selected").val();
	
	function wxInit(){
		br_weatherFeed(this);
	}
	
	function br_weatherFeed(xml){
		var fullFeed = xml.response.responseXML;
		var currentLocation = wxStations[stationNum][1];
 		var currentTemp = $wn(fullFeed).find("currents location temp").text();
		if (currentTemp){
			var currentIcon = $wn(fullFeed).find("currents location icon").text();
		
			$wn("#wxBrand .current #temp").html(currentTemp + "&deg;");
			$wn("#wxBrand .current #image").css("background-image", "url('"+ wxIconPath + currentIcon + ".png')");
			$wn("#wxBrand .current #location").html(currentLocation);
		}
	}
 
	function stationChange(num){
		br_url = wxStations[num][0];
		stationNum = num;
		WNHttpRequestManager.makeRequest(br_url, { onSuccess: wxInit, onError: wxError});
	}
	
	function wxError(){}
	
	$wn("#wxBrand #wxLocation").change(function(){
		stationChange($wn(this).val());
	});
	
	WNHttpRequestManager.makeRequest(br_url, { onSuccess: wxInit, onError: wxError});		
}

// Weather Branding Sponsored Ad
function addSponsoredAd(){
	var ord = Math.random()*10000000000000000;
	var wncc = '';
	var ad_wncc = '';
	try {
		wncc = wng_pageInfo.contentClassification;
		ad_wncc = wncc.toLowerCase();
		while (ad_wncc.indexOf(" ") != -1){
			ad_wncc = ad_wncc.replace(" ", "");
		}
	} catch(e) {}
	
	var affNameCaps = '';
	var affName = ''
	try {
		affNameCaps = wng_pageInfo.affiliateName;
		affName = affNameCaps.toLowerCase();
	} catch(e) {}	
	
	$wn("#sponsoredAd #adCall").append("<iframe id='headerAd' width='145' height='40' marginwidth='0' marginheight='0' frameborder='0' scrolling='no'></iframe>");
	var adcall = "http://ad.doubleclick.net/adi/wn.loc."+affName+"/"+ ad_wncc +";sz=120x60;ord=" + ord + "?";
	$wn("#sponsoredAd #adCall #headerAd").attr("src", adcall);
}
*/

// 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[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 bnRestyle(){
	if($wn("#WNDS-30").length){
		//$wn("#WNDS-30 .wnDVSummary:eq(0) a span").prepend("<span class='ribbonText'>BREAKING NEWS: </span>");
		$wn("#WNDS-30 .wnDVSummary:gt(0)").each(function(){
			$wn(this).addClass("other");
		});
	}
}

/* Weather Branding Sponsored Ad */
function add728x90Ad(){
	var ord = Math.random()*10000000000000000;
	var wncc = '';
	var ad_wncc = '';
	try {
		wncc = wng_pageInfo.contentClassification;
		ad_wncc = wncc.toLowerCase();
		while (ad_wncc.indexOf(" ") != -1){
			ad_wncc = ad_wncc.replace(" ", "");
		}
	} catch(e) {}
	
	var affNameCaps = '';
	var affName = ''
	try {
		affNameCaps = wng_pageInfo.affiliateName;
		affName = affNameCaps.toLowerCase();
	} catch(e) {}	
	
	var adcall = "http://ad.doubleclick.net/adi/wn.loc."+affName+"/"+ ad_wncc +";sz=728x90;ord=" + ord + "?";
	$wn("#WNCols23-4").append("<div id='WNAd146' class='wnMultiAd wnIframeAd' style='width:980px;text-align:center;'><iframe id='bottom728x90Ad' width='728' height='90' marginwidth='0' marginheight='0' frameborder='0' scrolling='no' src='" + adcall + "'></iframe></div>");
}

$wn(document).ready(function(){
	//bnRestyle();
  moveHdrAd();
  addHdrButtons();
	formRestyle();
});

$wn(window).load(function(){
	//launchDynamicNav();
	
	/*
	if($wn("#socialTools").length){
		imgSwap("socialTools","http://ftpcontent.worldnow.com/wvill/custom/social/","png");
	}
	*/
	add728x90Ad();
	//bgChange();
})

if(wng_pageInfo.containerType === 'S'){
	PLATFORM.EventMan.registerToEvent(function(){
		//ROS 300x100 - Bottom of Col. 4 #1
		<!-- begin ad tag-->
		if (typeof ord=='undefined') {ord=Math.random()*10000000000000000;}
		document.write('<iframe id="300x100A" src="http://ad.doubleclick.net/adi/wn.loc.wvill;sz=300x100;ord=' + ord + '?" width="300" height="100" marginwidth="0" marginheight="0" frameborder="0" scrolling="no">');
		document.write('</iframe>');
		
		//ROS 300x100 - Bottom of Col. 4 #2
		if (typeof ord=='undefined') {ord=Math.random()*10000000000000000;}
		document.write('<iframe id="300x100B" src="http://ad.doubleclick.net/adi/wn.loc.wvill.ext;sz=300x100;ord=' + ord + '?" width="300" height="100" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" style="margin-top:15px">');
		document.write('</iframe>');
		<!-- End ad tag -->
	}, 'WNCol4Done');
	
	$wn(document).ready(function(){
		$wn("#WNAd52").appendTo("#WNCol4");
		$wn("#300x100A").appendTo("#WNCol4");
		$wn("#300x100B").appendTo("#WNCol4");
	});
}
