$wn(document).ready(function() {
  $wn('#WNMemberCenter-cols2345top strong').text('Member Center | ');
	csLaunchDynamicNav();
  csSevereWeather();
  csAddFbWidget();
});

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

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

/* SEVERE WEATHER - BREAKING NEWS TAKEOVER */
function csSevereWeather() {
  // Do an EXTREMELY forgiving test of the first section title - ideally "severe weather"
  var testText = $wn('#DisplaySizeId-30 li.feature').first().find('h4.sectionTitle .text').text();
  if (testText.match(/severe[\s]*weather/i) === null) {
    // Section title is not about severe weather, abort
    return;
  }

  // Change Breaking box header, hide all but the first story
  $wn('#DisplaySizeId-30 li.header .text').text('Severe Weather Alert');
  $wn('#DisplaySizeId-30 li.feature').slice(1).hide();
  
  // Build and add the links box
  $weatherLinks = $wn('<div id="csWeatherLinks">');
  $weatherLinks.append('<a class="csWLink csClosings" href="#todo">Closing Alerts</a>');
  $weatherLinks.append('<a class="csWLink csRadar" href="#todo">Interactive Radar</a>');
  $weatherLinks.append('<a class="csWLink csForecast" href="#todo">7 Day Forecast</a>');
  $wn('#DisplaySizeId-30').prepend($weatherLinks);
}

/* ADD FACEBOOK WIDGET TO BOTTOM OF COL4 */
function csAddFbWidget() {
  var $ub = $wn('<iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fwten.albany&width=300&height=290&colorscheme=light&show_faces=true&border_color=%23182351+&stream=false&header=true&appId=225942364129894" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:290px;" allowTransparency="true" />');
  $wn('#WNCol4').append($ub);
  $ub.wrap('<div class="wnDSContainer-feature" id="csFacebookBlock"><div class="wnDSItems-feature"><div class="wnDVUtilityBlock"></div></div></div>');
}
