var chatterAdTimeout;
var pageScroll;
var chatterScroll;
var usa_debugFlag = false;
var randDARTNumber=0;
var agent = navigator.userAgent.toLowerCase();
var scrWidth = screen.width;
var scrHeight = screen.height;
// The document.documentElement dimensions seem to be identical to
// the screen dimensions on all the mobile browsers I've tested so far
var elemWidth = document.documentElement.clientWidth;
var elemHeight = document.documentElement.clientHeight;
// We need to eliminate Symbian, Series 60, Windows Mobile and Blackberry
// browsers for this quick and dirty check. This can be done with the user agent.
var otherBrowser = (agent.indexOf("series60") != -1) || (agent.indexOf("symbian") != -1) || (agent.indexOf("windows ce") != -1) || (agent.indexOf("blackberry") != -1);
// If the screen orientation is defined we are in a modern mobile OS
var mobileOS = typeof orientation != 'undefined' ? true : false;
// If touch events are defined we are in a modern touch screen OS
var touchOS = ('ontouchstart' in document.documentElement) ? true : false;
// iPhone and iPad can be reliably identified with the navigator.platform
// string, which is currently only available on these devices.
var iOS = ((navigator.platform).toLowerCase().indexOf("iphone") != -1) ||
        ((navigator.platform).toLowerCase().indexOf("ipad") != -1) || 
        ((agent).toLowerCase().indexOf("iphone") != -1) ||
        ((agent).toLowerCase().indexOf("ipad") != -1) ? true : false;
// If the user agent string contains "android" then it's Android. If it
// doesn't but it's not another browser, not an iOS device and we're in
// a mobile and touch OS then we can be 99% certain that it's Android.
var android = (agent.indexOf("android") != -1) || (!iOS && !otherBrowser && touchOS && mobileOS) ? true : false;

var usa_homeVideoWidth = 300;
var usa_homeVideoHeight = 187;

var usa_omniturePageName;
var usa_omnitureSprop3;
var usa_omnitureSprop4;

var EchoRiverClient;

var ad_section = '';

var usa_freewheel = {
	networkId : 169843,
	cb_profile : "169843:nbcu_live_as3",
	siteSectionNetworkId : 169843,
	videoAssetNetworkId : 169843,
	fw_server : "http://29773.v.fwmrm.net/ad/p/1",
	amLocation : "http://adm.fwmrm.net/p/nbcu_live/AdManager.swf",
	videoAssetFallbackId : "Fallback36750133"
}

// default fw_config for show pages
function fw_config()
{
	return { 
		networkId : usa_freewheel.networkId,
		cb_profile : usa_freewheel.cb_profile,
		siteSectionNetworkId : usa_freewheel.siteSectionNetworkId,
		videoAssetNetworkId : usa_freewheel.videoAssetNetworkId,
		fw_server : usa_freewheel.fw_server,
		siteSection : ((typeof fwSiteSection != 'undefined' && fwSiteSection != '') ? fwSiteSection : 'usa_home_'+ad_section+'_0_short'),
		autoplay : false,
		amLocation : usa_freewheel.amLocation,
		videoAssetFallbackId : usa_freewheel.videoAssetFallbackId
	};
}

//cookie functions
if (typeof usa_createCookie == 'undefined')
{
	usa_createCookie = function (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=/";
	}
}

if (typeof usa_readCookie == 'undefined')
{
	usa_readCookie = function (name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
}

if (typeof usa_eraseCookie == 'undefined')
{
	usa_eraseCookie = function (name) {
		usa_createCookie(name,"",-1);
	}
}

/* prefs obj and instance */
var usa_sitePrefsObj = function(chatter) {
	this.chatter = chatter;
};
var usa_sitePrefs = new usa_sitePrefsObj("1");
usa_loadPrefs();

function usa_savePrefs()
{
	var cookieValue = '';
	cookieValue = usa_sitePrefs.chatter;
	usa_createCookie('usa_prefs', cookieValue, 365);
}

function usa_loadPrefs()
{
	var loadedPrefs = usa_readCookie('usa_prefs');
	if (loadedPrefs != null)
	{
		if (loadedPrefs.indexOf(',') != -1)
		{
			loadedPrefs = loadedPrefs.split(',');
		}
		
		usa_sitePrefs.chatter = loadedPrefs;
	}
}

function genSetRandDARTNumber()
{
	randDARTNumber = Math.round(Math.random()*1000000000000);
}
genSetRandDARTNumber();

function usa_debugOut(msg)
{
	if (typeof console != 'undefined' && usa_debugFlag == true)
	{
		console.log(msg);
	}
	return;
}


function usa_handleOrientationChange()
{
	usa_debugOut('fn: usa_handleOrientationChange()');
	return;
	/*window.orientation returns a value that indicates whether iPhone is in portrait mode, landscape mode with the screen turned to the
    left, or landscape mode with the screen turned to the right. */
  var orientation = window.orientation;
  switch(orientation) {
    case 0:
        /* Add a descriptive message on "Handling iPhone or iPod touch Orientation Events"  */
        break;  
    case 90:
        /* If in landscape mode with the screen turned to the left, sets the body's class attribute to landscapeLeft. In this case, all style definitions matching the
           body[class="landscapeLeft"] declaration in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
        break;
    case -90:  
        /* If in landscape mode with the screen turned to the right, sets the body's class attribute to landscapeRight. Here, all style definitions matching the 
           body[class="landscapeRight"] declaration in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
    	
    	break;
    case 180:
    	/* Add a descriptive message on "Handling iPhone or iPod touch Orientation Events"  */
        break;  
  }
}

window.onorientationchange = function() {
	usa_handleOrientationChange();
	if (typeof pageScroll != 'undefined')
	{
		pageScroll.refresh();
	}
	usa_expandHeightOfContent();
}

function usa_loaded() {
	usa_debugOut('fn: usa_loaded()');
	usa_setFooter();
	if (iOS || android)
	{
		if (!$('body').hasClass('noiscroll'))
		{
			pageScroll = new iScroll('usa_outerContainer', { zoom: true, hScrollbar: false, vScrollbar: false });
			usa_handleOrientationChange();
		}
	}
	usa_expandHeightOfContent();
	if (document.getElementById('usa_chatter')) $('#usa_chatter').fadeIn('slow');
}

function usa_setFooter()
{
	usa_debugOut('fn: usa_setFooter()');
	//if (usa_readCookie('device_detect') == 'desktop' || !usa_readCookie('device_detect'))
	if (!android && !iOS && !touchOS && !mobileOS && !otherBrowser)
	{
		$('#usa_desktop').addClass('active');
		$('#usa_desktop').show();
	}
	$('#usa_headFootContainer').show().slideDown();
}



/* ADDITIONAL CHATTER FUNCTIONS */
var usa_lastChatterView = 'condensed';

function usa_refreshChatterScroll()
{
	usa_debugOut('fn: usa_refreshChatterScroll()');
	if (typeof chatterScroll != 'undefined')
	{
		setTimeout(function () {
			chatterScroll.refresh();
		}, 500);
		//setTimeout("usa_refreshChatterScroll()", 5000);
	}
	
	if (typeof usa_chatterPageType != 'undefined')
	{
		if (usa_chatterPageType == 'FULL')
		{
			if (typeof pageScroll != 'undefined')
			{
				setTimeout(function () {
					pageScroll.refresh();
				}, 500);
			}
		}
	}
}

function usa_setChatterDisplay(type)
{
	usa_debugOut('fn: usa_setChatterDisplay("'+type+'")');
	if (type == 'condensed')
	{
		$('#usa_chatter').removeClass('expanded');
		$('#usa_chatter').removeClass('closed');
		$('#usa_chatter').addClass('open');
		
		$('#usa_chatterBody').css('height', 80);
		$('#usa_outerContainer').css('bottom', 180);
		
		if (document.getElementById('chatterAd300x250iframe'))
		{
			document.getElementById('chatterAd300x250iframe').src = "javascript:'<html></html>'";
		}
		clearTimeout(chatterAdTimeout);
	}
	else if (type == 'expanded')
	{
		$('#usa_chatter').removeClass('closed');
		$('#usa_chatter').removeClass('open');
		$('#usa_chatter').addClass('expanded');

		var newHeight = $(window).height() - $('#usa_header').height() - $('#usa_headFootContainer').height() - 40;
		$('#usa_chatterBody').css('height', String(newHeight) + 'px');
		$('#usa_outerContainer').css('bottom', 180);
		
		var setChatterScroll = true;
		if (typeof usa_chatterPageType != 'undefined')
		{
			if (usa_chatterPageType == 'FULL')
			{
				setChatterScroll = false;
			}
		}
		
		if ((iOS || android) && setChatterScroll)
		{
			chatterScroll = new iScroll('usa_chatterBodyWrapperOuter', {  zoom: true, hScrollbar: false, vScrollbar: false  });			
			//usa_refreshChatterScroll();
			setTimeout("usa_refreshChatterScroll()", 1000);
		}
		
		if (document.getElementById('chatterAd300x250iframe'))
		{
			document.getElementById('chatterAd300x250iframe').src = '/_inc/ad.html?size=300x250';
		}
		
		if (typeof usa_chatterRefreshAd == 'function')
		{
			chatterAdTimeout = setTimeout(usa_chatterRefreshAd, 1000 * 60);
		}
		else if (typeof usa_refreshAd == 'function')
		{
			chatterAdTimeout = setTimeout(usa_refreshAd, 1000 * 60);
		}
	}
	
	$('#usa_chatterFooter').css('height', 5);
	if (typeof pageScroll != 'undefined')
	{
		pageScroll.refresh();
	}
}

function usa_expandChatter()
{
	usa_debugOut('fn: usa_expandChatter()');
	if ($('#usa_chatter').hasClass('open') || usa_lastChatterView == 'expanded')
	{
		if (typeof s_gi != 'undefined')
		{
			var s=s_gi('nbcuglobal,nbcuusanetworkd,nbcuusanetbu');
			usa_omnitureSprop3 = s.prop3;
			usa_omnitureSprop4 = s.prop4;
			usa_omniturePageName = s.pageName;
			s.prop3="Exclusives";
			s.prop4="Chatter";
			s.pageName = "Chatter";
			void (s.t());
		}
		
		usa_setChatterDisplay('expanded');
		
		usa_sitePrefs.chatter = 2;
		usa_savePrefs();
		
		if (typeof EchoRiverClient == 'undefined')
		{
			usa_initChatter('expanded');
		}
		else
		{
			EchoRiverClient.activities.paused = false;
			EchoRiverClient.config.set("maxBodyCharacters", "2000");
			usa_chatterShowAllNoRefresh();
		}
	}
	else
	{
		usa_setChatterDisplay('condensed');
		
		usa_sitePrefs.chatter = 1;
		usa_savePrefs();
		
		if (typeof EchoRiverClient == 'undefined')
		{
			usa_initChatter('condensed');
		}
		else
		{
			EchoRiverClient.activities.paused = false;
			EchoRiverClient.config.set("maxBodyCharacters", "60");
			EchoRiverClient.refresh();
		}
		
		if (typeof s_gi != 'undefined')
		{
			var s=s_gi('nbcuglobal,nbcuusanetworkd,nbcuusanetbu');
			if (typeof usa_omnitureSprop3 == 'undefined')
			{
				usa_omnitureSprop3 = s.prop3;
				usa_omnitureSprop4 = s.prop4;
				usa_omniturePageName = s.pageName;
			}
			s.prop3=usa_omnitureSprop3;
			s.prop4=usa_omnitureSprop4;
			s.pageName = usa_omniturePageName;
		}
	}
	
	usa_lastChatterView = '';
}

function usa_collapseChatter()
{
	usa_debugOut('fn: usa_collapseChatter()');
	if (typeof EchoRiverClient != 'undefined')
	{
		EchoRiverClient.activities.paused = true;
		EchoRiverClient.rerender("state"); 
	}
	
	if ($('#usa_chatter').hasClass('expanded'))
	{
		usa_lastChatterView = 'expanded';
		
		var s=s_gi('nbcuglobal,nbcuusanetworkd,nbcuusanetbu');
		if (typeof usa_omnitureSprop3 == 'undefined')
		{
			usa_omnitureSprop3 = s.prop3;
			usa_omnitureSprop4 = s.prop4;
			usa_omniturePageName = s.pageName;
		}
		s.prop3=usa_omnitureSprop3;
		s.prop4=usa_omnitureSprop4;
		s.pageName = usa_omniturePageName;
	}
	else
	{
		usa_lastChatterView = 'condensed';
	}
	
	$('#usa_chatter').removeClass('expanded');
	$('#usa_chatter').removeClass('open');
	$('#usa_chatter').addClass('closed');
	
	$('#usa_chatterBody').css('height', 0);
	$('#usa_chatterFooter').css('height', 0);
	usa_expandHeightOfContent();
	
	if (document.getElementById('chatterAd300x250iframe'))
	{
		document.getElementById('chatterAd300x250iframe').src = "javascript:'<html></html>'";
	}
	clearTimeout(chatterAdTimeout);
	
	var newHeight = 100 + $('#usa_chatterContent').height();
	$('#usa_outerContainer').css('bottom', newHeight);
	
	if (typeof pageScroll != 'undefined')
	{
		pageScroll.refresh();
	}
	
	usa_sitePrefs.chatter = 0;
	usa_savePrefs();
}

var usa_totalUsaStreamItems;
var usa_currentStreamTop = 0;
var usa_streamScrollHeight = -100;
var usa_currentStreamPage = 1;
var usa_streamPageSize = 3;
var usa_streamLoading = true;
var usa_waitingForStream = false;

function usa_updateStreamScroll()
{
	usa_debugOut('fn: usa_updateStreamScroll()');
	if (usa_waitingForStream)
	{
		var nextScroll = 5 + (usa_currentStreamPage * usa_streamScrollHeight);
		$('.echo-stream-body').css('top', String(nextScroll) + 'px');
		usa_currentStreamPage++;
		usa_waitingForStream = false;
	}
}

function usa_streamBack()
{
	usa_debugOut('fn: usa_streamBack()');
	var eb = $(".echo-stream-body");
	usa_totalUsaStreamItems = eb.find(".echo-item-content").length;

	if (usa_totalUsaStreamItems > 3 && usa_currentStreamPage >= 1)
	{
		usa_currentStreamPage--;
		var nextScroll = 5 + (usa_currentStreamPage * usa_streamScrollHeight);
		$('.echo-stream-body').css('top', String(nextScroll) + 'px');
	}
}

function usa_streamNext()
{
	usa_debugOut('fn: usa_streamNext()');
	var eb = $(".echo-stream-body");
	usa_totalUsaStreamItems = eb.find(".echo-item-content").length;

	if (usa_totalUsaStreamItems > 3 && usa_totalUsaStreamItems > (usa_currentStreamPage * usa_streamPageSize) && !usa_streamLoading)
	{
		var nextScroll = 5 + (usa_currentStreamPage * usa_streamScrollHeight);
		$('.echo-stream-body').css('top', String(nextScroll) + 'px');
		usa_currentStreamPage++;
	}
	else
	{
		usa_waitingForStream = true;
		$('.echo-stream-more').click();
	}
}

function usa_initChatter(type)
{
	usa_debugOut('fn: usa_initChatter("'+type+'")');
	var maxChars = 60;
	var query = usa_chatterObj.allQueryCondensed;

	if (typeof usa_chatterPageType == 'undefined')
	{
		if (type == 'condensed')
		{
			maxChars = 60;
			query = usa_chatterObj.allQueryCondensed;
		}
		else if (type == 'expanded')
		{
			maxChars = 2000;
			query = usa_chatterObj.allQuery;
		}
	}
	else if (usa_chatterPageType == 'FULL')
	{
		maxChars = 2000;
		query = usa_chatterObj.allQuery;
	}
	
	if (usa_goto == 'liveEvent' && usa_liveChat)
	{
		query = usa_chatterObj.questionsQuery;
	}
	else if (usa_goto == 'fanChat')
	{
		query = usa_chatterObj.chatQuery;
	}
	else if (usa_goto == 'curated')
	{
		query = usa_chatterObj.curatedQuery;
	}
	
   EchoRiverClient = new Echo.Stream({
          "target": document.getElementById("echo-stream"),
          "appkey": "prod.usanetwork",
		  "query": query,
          "maxBodyCharacters": maxChars,
          "viaLabel": {"icon": true,"text": true},
          "reTag": false,
          "streamStateLabel": {"icon": true,"text": true},
          "aggressiveSanitization": false,
          "plugins": [
			{
            	"name": "Whirlpools",
                "after": 2,
                "clickable": true
          	},
          	{
            	"name": "CommunityFlag"
    	  	},
    	  	{
    			"name": "SourceIconTweaks",
    			"icons": {
    				"usanetwork": "http://www.usanetwork.com/favicon.png"
    			}
    		},
        	{
    	      	"name": "Reply",
    	      	"nestedPlugins": [{
    	              "name": "FormAuth",
    	              "identityManagerLogin": identityManager, 
    	              "identityManagerSignup": identityManager,
    	              "identityManagerEdit": identityManager,
    	              "submitPermissions": "forceLogin"
    	          }]
    	    }
          ]
  });

   Echo.Broadcast.subscribe("User.onInit",
		    function(topic, data, contextId) {
				usa_checkFacebook();
				usa_checkTwitter();
				EchoSubmit.rerender("shareContainer");
		    }
		);

		Echo.Broadcast.subscribe("User.onInvalidate",
		    function(topic, data, contextId) {
				EchoSubmit.rerender("shareContainer");
		    }
		);
   
   if (typeof usa_chatterPageType == 'undefined')
   {
	  	Echo.Broadcast.subscribe("Stream.onReady",
			function(topic, data, contextId) {
	  			usa_streamLoading = false;
	  			if ($('#usa_chatter').hasClass('open') && typeof usa_chatterPageType == 'undefined')
				{
	  				$('.echo-stream-more').css('display', 'none');
				}
	  			usa_updateStreamScroll();
	  			setTimeout("usa_refreshChatterScroll()", 1000);
			}
		);
	
		Echo.Broadcast.subscribe("Stream.onMoreButtonPress",
			function(topic, data, contextId) {
				usa_streamLoading = true;
				$('.echo-stream-more').css('display', 'block');
			}
		);
   }
   else
   {
	   Echo.Broadcast.subscribe("Stream.onReady",
				function(topic, data, contextId) {
		   			setTimeout("usa_refreshChatterScroll()", 1000);
				}
			);
   }
}



function usa_expandHeightOfContent()
{
	usa_debugOut('fn: usa_expandHeightOfContent()');
	//return;
	var heightOfContent = ($('#usa_outerContainer').height() + $('#usa_chatter').height() - 18);
	if ($(window).height() > heightOfContent)
	{
		var newHeight = $('#usa_body').height() + ($(window).height() - heightOfContent);
		$('#usa_body').css('height', newHeight + 'px');
	}
}

function usa_refreshBannerAd()
{
	usa_debugOut('fn: usa_refreshBannerAd()');
	// get all IFRAME ads on the page
	var adRoot = $('body');
	adRoot.find(".usa_adIframe.refresh").each(function(index) {
		var iFrameID = $(this).attr('id');
		document.getElementById(iFrameID).src = document.getElementById(iFrameID).src;
	});
	
	// omniture
	if (typeof s_gi != 'undefined')
	{
		var s=s_gi('nbcuglobal,nbcuusanetworkd,nbcuusanetbu'); 
		void (s.t());
	}
}

var usa_adRefreshTO;
function usa_timedAdRefreshes(time)
{
	usa_debugOut('fn: usa_timedAdRefreshes("'+time+'")');
	usa_refreshBannerAd();
	usa_adRefreshTO = setTimeout("usa_timedAdRefreshes("+time+")", time);
}
function usa_setupTimedAdRefreshes(time)
{
	usa_debugOut('fn: usa_setupTimedAdRefreshes("'+time+'")');
	usa_adRefreshTO = setTimeout("usa_timedAdRefreshes("+time+")", time);
}

function usa_buildCharactersModule(options)
{
	usa_debugOut('fn: usa_buildCharactersModule("'+options+'")');
	// setup main defaults
	var default_args = {
		'target' : '',
		'basePath' : '',
		'data' : ''
	}
	
	// override defaults with arguments
	for (var index in default_args)
	{
		if (typeof options[index] == "undefined") options[index] = default_args[index];
	}
	
	if (typeof options['data'] == 'object')
	{
		var html = '';
		for (var i=0 ; i<options['data'].character.length ; i++)
		{
			var name = options['data'].character[i].attributes.name;
			var thumb = options['basePath'] + options['data'].character[i].attributes.thumb;
			var url = options['basePath'] + options['data'].character[i].attributes.url;
			
			html += '<div class="character"><a href="'+url+'"><img src="'+thumb+'" /><span>'+name+'</span></a></div>';
		}
		html += '<div class="clear"></div>';
		$(options['target']).html(html);
	}
}

function usa_buildScheduleModule(options)
{
	usa_debugOut('fn: usa_buildScheduleModule("'+options+'")');
	// setup main defaults
	var default_args = {
		'target' : '',
		'data' : '',
		'max' : 4
	}
	
	// override defaults with arguments
	for (var index in default_args)
	{
		if (typeof options[index] == "undefined") options[index] = default_args[index];
	}
	
	var max = (options['data'].item.length < options['max']) ? options['data'].item.length : options['max'];
	
	if (typeof options['data'] == 'object')
	{
		var html = '';
		for (var i=0 ; i<max ; i++)
		{
			var showDate = options['data'].item[i].showdate;
			var showDay = options['data'].item[i].showday;
			var showDescription = options['data'].item[i].showdescription;
			var showTime = options['data'].item[i].showtime;
			
			html += '<div class="showing"><div class="date">'+showDay+', '+showDate+', '+showTime+'</div><div class="episode">'+showDescription+'</div></div>';
		}
		html += '<div class="clear"></div>';
		$(options['target']).html(html);
	}
}

function usa_columnAlign()
{
	usa_debugOut('fn: usa_columnAlign()');
	var contentBody = $('#usa_body');
	var tallestColumn = 0;
	var tallestColumnID = '';
	contentBody.find(".usa_column").each(function(index) {
		var columnHeight = $(this).height();
		if (columnHeight > tallestColumn)
		{
			tallestColumn = columnHeight;
			tallestColumnID = $(this).attr('id');
		}
	})
	
	contentBody.find(".usa_column").each(function(index) {
		if ($(this).attr('id') != tallestColumnID)
		{
			columnHeight = $(this).height();
			columnDifference = tallestColumn - columnHeight;
			moduleMargin = parseInt($("#"+$(this).attr('id')+" .mod:last").css('margin-bottom'));
			moduleCurrentHeight = parseInt($("#"+$(this).attr('id')+" .mod:last").height());
			newHeight = columnDifference + moduleCurrentHeight;
			$("#"+$(this).attr('id')+" .mod:last").css('height', String(newHeight) + 'px');
		}
	})
}

$(document).ready(function(){
	
	$("ul.topnav li").click(function() { //When trigger is clicked...
		// hide all open so far
		$("ul.topnav").find("ul.subnav").hide();
	
		if ($(this).hasClass("subhover"))
		{
			$(this).removeClass("subhover");
			$(this).find("ul.subnav").slideUp('slow');
			return;
		}
		else
		{
			$(this).addClass("subhover");
		}
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).hover(function() {
		}, function(){
			$(this).find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});
	
	$("ul.topnav li").mouseenter(function() { //When trigger is clicked...

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).hover(function() {
		}, function(){
			$(this).find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

});

$(document).ready(function() {
	usa_loaded();
	usa_columnAlign();
});

$(window).resize(function() {
	usa_expandHeightOfContent();
});
function usa_showTabFeature(id)
{
	usa_debugOut('fn: usa_showTabFeature('+id+')');
	usa_debugOut(id);
	
	usa_nextHomeFeatureId = id;
	$('#tabFeatureContainer_' + usa_currentHomeFeatureId).fadeOut('fast', function() {
		$('#tabFeatureContainer_' + usa_currentHomeFeatureId).removeClass('selected');
		$('#tabFeatureNav_' + usa_currentHomeFeatureId).removeClass('selected');
		
		$('#tabFeatureContainer_' + usa_nextHomeFeatureId).fadeIn('fast');
		usa_currentHomeFeatureId = usa_nextHomeFeatureId;
		$('#tabFeatureContainer_' + usa_currentHomeFeatureId).addClass('selected');
		$('#tabFeatureNav_' + usa_currentHomeFeatureId).addClass('selected');
	});
}

function usa_buildHomeTabFeature(options)
{
	usa_debugOut('fn: usa_buildHomeTabFeature()');
	usa_debugOut(options);
	
	// setup main defaults
	var default_args = {
		'target' : '',
		'data' : ''
	}
	
	// override defaults with arguments
	for (var index in default_args)
	{
		if (typeof options[index] == "undefined") options[index] = default_args[index];
	}
	
	if (typeof options['data'] == 'object')
	{
		var navHtml = '';
		var tabHtml = '';
		
		navHtml += '<div class="tabFeatureNav">';
		
		if (typeof options['data'].tabFeature.length == 'undefined')
		{
			// we have one tab
			var cssClass = ' selected';
			navHtml += '<a href="javascript:usa_showTabFeature(0);" id="tabFeatureNav_0" class="'+cssClass+'"><span class="l"></span><span class="m">'+options['data'].tabFeature.attributes.title+'</span><span class="r"></span><span class="arrow"></span></a>';
			
			if (typeof options['data'].tabFeature.feature.length != 'undefined')
			{
				tabHtml += '<div id="tabFeatureContainer_0" class="tabFeatureContainer tabFeature'+options['data'].tabFeature.feature.length+cssClass+'">';
				
				for (var j=0 ; j<options['data'].tabFeature.feature.length ; j++)
				{
					tabHtml += '<a href="'+options['data'].tabFeature.feature[j].attributes.link+'" class="tabFeatureItem'+(j+1)+'"><span class="tabFeatureImage"><img src="'+options['data'].tabFeature.feature[j].attributes.src+'" /></span><span class="tabFeatureText">'+options['data'].tabFeature.feature[j].title+'</span></a>';
				}
			}
			else
			{
				// we have just one item
				tabHtml += '<div id="tabFeatureContainer_0" class="tabFeatureContainer tabFeature1'+cssClass+'">';
				tabHtml += '<a href="'+options['data'].tabFeature.feature.attributes.link+'" class="tabFeatureItem1"><span class="tabFeatureImage"><img src="'+options['data'].tabFeature.feature.attributes.src+'" /></span><span class="tabFeatureText">'+options['data'].tabFeature.feature.title+'</span></a>';
			}
			
			tabHtml += '</div>';
		}
		else
		{
			// we have multiple tabs
			for (var i=0 ; i<options['data'].tabFeature.length ; i++)
			{
				var cssClass = (i==0) ? ' selected' : '';
				navHtml += '<a href="javascript:usa_showTabFeature('+i+');" id="tabFeatureNav_'+i+'" class="'+cssClass+'"><span class="l"></span><span class="m">'+options['data'].tabFeature[i].attributes.title+'</span><span class="r"></span><span class="arrow"></span></a>';
				
				if (typeof options['data'].tabFeature[i].feature.length != 'undefined')
				{
					tabHtml += '<div id="tabFeatureContainer_'+i+'" class="tabFeatureContainer tabFeature'+options['data'].tabFeature[i].feature.length+cssClass+'">';
					
					for (var j=0 ; j<options['data'].tabFeature[i].feature.length ; j++)
					{
						tabHtml += '<a href="'+options['data'].tabFeature[i].feature[j].attributes.link+'" class="tabFeatureItem'+(j+1)+'"><span class="tabFeatureImage"><img src="'+options['data'].tabFeature[i].feature[j].attributes.src+'" /></span><span class="tabFeatureText">'+options['data'].tabFeature[i].feature[j].title+'</span></a>';
					}
				}
				else
				{
					// we have just one item
					tabHtml += '<div id="tabFeatureContainer_'+i+'" class="tabFeatureContainer tabFeature1'+cssClass+'">';
					tabHtml += '<a href="'+options['data'].tabFeature[i].feature.attributes.link+'" class="tabFeatureItem1"><span class="tabFeatureImage"><img src="'+options['data'].tabFeature[i].feature.attributes.src+'" /></span><span class="tabFeatureText">'+options['data'].tabFeature[i].feature.title+'</span></a>';
				}
				
				tabHtml += '</div>';
			}
		}
		tabHtml += '<div class="clear"></div>';
		
		navHtml += '<div class="clear"></div></div>';
		$(options['target']).html(navHtml + tabHtml);
	}
}
