/*
Deal with NID pages opened in a javascript popup by external sites
*/

if (window.opener && screen.availWidth >= 1024 && screen.availHeight >= 700) {

	var winInnerW = 0;
	var winInnerH = 0;
	
	checkWinInnerDimensions();
	
	if (winInnerW > 0 && winInnerH > 0) {
	
		// Work out whether window needs resized
		var resizeWidth = (winInnerW < 992) ? 992-winInnerW : 0;
		var resizeHeight = (winInnerH < 500) ? 500-winInnerH : 0;
		
		window.resizeBy(resizeWidth,resizeHeight);
	}
	
}


function checkWinInnerDimensions() {

  if(typeof(window.innerWidth) == 'number' ) {
  
    //Non-IE
    winInnerW = window.innerWidth;
	winInnerH = window.innerHeight;
	
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    
	//IE 6+ in 'standards compliant mode'
    winInnerW = document.documentElement.clientWidth;
	winInnerH = document.documentElement.clientHeight;
	
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    
	//IE 4 compatible
    winInnerW = document.body.clientWidth;
	winInnerH = document.body.clientHeight;
	
  }
  
  return true;
  
}


$(document).ready(function(){
	
	/* For IE ################################# */
	if($.browser.msie) {
		
		// Suckerfish dropdown support
		$('#topnav ul li.dropdown').bind('focus mouseenter',function(){
			$(this).addClass('sfhover');
		});
		
		$('#topnav ul li.dropdown').bind('blur mouseleave',function(){
			$(this).removeClass('sfhover');
		});
	}
	
	/* For All Browsers ################################# */
	
	// Make Suckerfish dropdown links keyboard accessible
	
	$('#topnav ul li.dropdown div a').focus(function(){
		$(this).parents('li.dropdown').addClass('sfhover');
	});
	
	$('#topnav ul li.dropdown div a').blur(function(){
		$(this).parents('li.dropdown').removeClass('sfhover');
	});
	
	// Show the hidden search label
	$('#qsearch form fieldset label').addClass('show');
	
	// Hide it when focus is on the search box
	$('#bannerqt').focus(function(){
			$('#qsearch form fieldset label').removeClass('show');						   
	});
	
	// Show search label when focus is off the search box if search box is empty
	$('#bannerqt').blur(function(){
			if (jQuery.trim($(this).val()) == '')
				$('#qsearch form fieldset label').addClass('show');						   
	});
	
	// Most popular numbered unordered list
	$("#mostpopular ol li").each(function (i) {
		i = i+1;
		$(this).prepend('<span class="itemnumber">'+i+'</span>');
	});
	
	// Sub banner numbered unordered list
	$("#sub-banner ol li").each(function (i) {
		i = i+1;
		$(this).prepend('<span class="itemnumber">'+i+'</span>');
	});
	
	// External Links ##################################### */
	
	$("#content-wrap a[href*='http://'],#nid-maincontent a[href*='http://']").filter(function() {
	    return this.hostname && this.hostname !== location.hostname;
	  }).after('<img src="external.png" class="elink" alt="external link" style="border:0;float:none;height:10px;width:10px;margin:0 0 0 3px;" />');
	
	$("#content-wrap a[href*='https://'],#nid-maincontent a[href*='https://']").filter(function() {
	    return this.hostname && this.hostname !== location.hostname;
	  }).after('<img src="external.png" class="elink" alt="external link" style="border:0;float:none;height:10px;width:10px;margin:0 0 0 3px;" />');
	  
});


/*
Add WebTrends tags to the 'Most Popular' links
*/

$(document).ready(function(){
	var $franchise = '';	

	if ($('meta[name=onsite-ad]').length == 0) {
		$franchise = 'Home';
	} else {
		$franchise = $.trim($('meta[name=onsite-ad]').attr("content"));
	}
	
	$('#mostpopular ol li a').each(function(index) {
		$(this).attr("href", function() {
			if (this.href.indexOf('javascript:') == -1 && this.href.indexOf('?') == -1) {
  				return this.href + '?WT.ac=Popular-' + $franchise + '-' + parseInt(index+1);
			}
		});
	});
});


/*
Add WebTrends tags to the 'Do It Online' links
*/

$(document).ready(function(){
	var $franchise = '';	

	if ($('meta[name=onsite-ad]').length == 0) {
		$franchise = 'Home';
	} else {
		$franchise = $.trim($('meta[name=onsite-ad]').attr("content"));
	}

	$('#doitonline ul li a').each(function(index) {
		$(this).attr("href", function() {
			if (this.href.indexOf('javascript:') == -1 && this.href.indexOf('?') == -1) {
  				return this.href + '?WT.ac=DoItOnline-' + $franchise + '-' + parseInt(index+1);
			}
		});
	});
});


/*
Add WebTrends tags to the campaign footer links
*/

$(document).ready(function(){
	$('#footer h3 a').each(function(index) {
		var $wtTag = $.trim($(this).text());
		$wtTag = $wtTag.replace(/ /g, "");
		
		$(this).attr("href", function() {
			if (this.href.indexOf('javascript:') == -1 && this.href.indexOf('?') == -1) {
  				return this.href + '?WT.ac=Footer-' + $wtTag;
			}
		});
	});
});
