var currentTab = '';
var fmklivenetcast = 0;
$(document).ready(function() {

	//On Hover Over
	function megaHoverOver(){
	    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
	    (function($) {
	        //Function to calculate total width of all ul's
	        jQuery.fn.calcSubWidth = function() {
	            rowWidth = 0;
	            //Calculate row
	            $(this).find("ul").each(function() { //for each ul...
	                rowWidth += $(this).width(); //Add each ul's width together
	            });
	        };
			jQuery.fn.adjustLeft = function(t,w) {
				var nleft = $('#topnavx').position().left;
				var mwidth = $('#topnavx').width();
				var mright = mwidth + nleft;
				// adjust so it doesn't blow off the main wrapper BN
				var nleftx = $(t).position().left;
				var mrightx =  nleftx + rowWidth;
				if(mrightx > mright) {
					var adjust = ((mrightx - mright) + 10) * -1;
					$(t).find(".sub").css({'left' :adjust}); //Set width
				}
			}
	    })(jQuery); 

	    if ( $(this).find(".row").length > 0 ) { //If row exists...

	        var biggestRow = 0;	

	        $(this).find(".row").each(function() {	//for each row...
	            $(this).calcSubWidth(); //Call function to calculate width of all ul's
	            //Find biggest row
	            if(rowWidth > biggestRow) {
	                biggestRow = rowWidth;
	            }
	        });

	        $(this).find(".sub").css({'width' :biggestRow}); //Set width
	        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
			$(this).adjustLeft(this,biggestRow);

	    } else { //If row does not exist...

	        $(this).calcSubWidth();  //Call function to calculate width of all ul's
	        $(this).find(".sub").css({'width' : rowWidth}); //Set Width
			$(this).adjustLeft(this,rowWidth);
	    }
	}
	//On Hover Out
	function megaHoverOut(){
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
	      $(this).hide();  //after fading, hide it
	  });
	}
	//Set custom configurations
	var config = {
	     sensitivity: 10, // number = sensitivity threshold (must be 1 or higher)
	     interval: 50, // number = milliseconds for onMouseOver polling interval
	     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 200, // number = milliseconds delay before onMouseOut
	     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	$(".fmktab").corner('tl tr');

	var checkLastTab = $.cookie('lasttab');


	if(checkLastTab)
		currentTab = checkLastTab;
	else
		currentTab = 'federal_sales_training';


	var $_GET = {};

	document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
	    function decode(s) {
	        return decodeURIComponent(s.split("+").join(" "));
	    }

	    $_GET[decode(arguments[1])] = decode(arguments[2]);
	});

	if($_GET["pt"]) {
		if($_GET["pt"] == 'prop') {
			currentTab = 'proposals';
		}
	}

	$("#" + currentTab).addClass('fliphover');

	$('#slidearea').html($('#' + currentTab + '_slide_hide').html());
	$('#slidearea').addClass(currentTab);
	//$('#slidearea').addClass('video');
	$('#switch_pane').html($('#' + currentTab + '_hide').html());
	$('#topnavx').html($('#' + currentTab + '_nav_hide').html());
	$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations

	if(currentTab == 'federal_sales_training') {
		// only do if we don't have a live cast going
			$('#pane1').jScrollPane({showArrows:true, scrollbarWidth: 17});
	}
	
	if(currentTab == 'gsa_schedules') {
		$('#slidearea').bind('click',function() { document.location = '/l/gsa_schedules/gsa_eproducts/gsa_eproposal/';});
		$('#slidearea').css('cursor','pointer');
	}
	else {
		$('#slidearea').css('cursor','auto');
		$('#slidearea').unbind('click');
	}

	$('.hpadf').hide();
	$('#' + currentTab + '_ad').show();
	
	/*
	if(fmklivenetcast == 1) {
		var seen = $.cookie("seenlive");
		var nseen = 0;
		if(seen) {
			nseen = parseInt(seen) + 1;
			$.cookie("seenlive", nseen, { expires: 1 });
		}
		else {
			$.cookie("seenlive", 1, { expires: 1 });
		}
		if(nseen < 10) { 
			var ev = $('#slidearea');
			$('#slidearea').addClass('video');

			var pos = $("#slidearea").offset();  
			var mwidth = $("#slidearea").width();
			var mleft = mwidth + pos.left - 260;
			//console.log('left='+pos.left+' : width=' + mwidth + ' : mleft=' + mleft);

			$('#video_feed').css('left',mleft + 'px');
			$('#video_feed').show();
			seenpop = setTimeout(fncltdc,120000);
		}
		else {
			fmklivenetcast = 0;
			$('#slidearea').removeClass('video');
			$('#video_feed').hide();
			$.cookie("nolive", 1, { expires: 1 });
			document.location = '/';
		}
	}
	*/
	
	var ads = '<div id="ads" class="ads" style="position: absolute; bottom: 8px; left: 20px;"></div>';
	$('#slidearea').append(ads);
	$('.hpad').each(function() {
		$('#ads').append($(this).html());
	});
	
	$('a[rel*=_hpad]').click(function() {
		var pn = $(this).attr('title');
		_gaq.push(['_trackEvent', 'Ad Click','Home Page Ad', pn]);
	});
	
	$("li .fmktab").click(
	   	function(){ 
				if(currentTab.length > 0) {
					$('#' + currentTab).removeClass('fliphover');
					if(fmklivenetcast == 1) {
						$('#slidearea').removeClass('video');
					}
					$('#slidearea').removeClass(currentTab);
				}
				$(this).addClass('fliphover');
				currentTab  = this.id;

				$.cookie("lasttab", currentTab, { expires: 7 });
				$('#switch_pane').html($('#' + this.id + '_hide').html());

					$('#slidearea').html($('#' + this.id + '_slide_hide').html());
					$('#slidearea').addClass(this.id);

				if(fmklivenetcast == 1) {
					$('#slidearea').addClass('video');
				}

				$('#topnavx').html($('#' + this.id + '_nav_hide').html());
				$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default

				$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations

				$(this).blur();
				if(currentTab == 'federal_sales_training') {
					// only do if we don't have a live cast going
					if(fmklivenetcast != 1)  {
						$('#allevents').datepicker('destroy');
						fmkapp.fscal();
					}
					$('#pane1').jScrollPane({showArrows:true, scrollbarWidth: 17});
	
				}
				
				
				if(currentTab == 'gsa_schedules') {
					$('#slidearea').bind('click',function() { document.location = '/l/gsa_schedules/gsa_eproducts/gsa_eproposal/';});
					$('#slidearea').css('cursor','pointer');
				}
				else {
					$('#slidearea').css('cursor','auto');
					$('#slidearea').unbind('click');
				}
				
				$('.hpadf').hide();
				$('#' + currentTab + '_ad').show();
				var ads = '<div id="ads" class="ads" style="position: absolute; bottom: 10px; left: 20px;"></div>';
				$('#slidearea').append(ads);
				$('.hpad').each(function() {
					$('#ads').append($(this).html());
				});
				$('a[rel*=_hpad]').click(function() {
					var pn = $(this).attr('title');
					_gaq.push(['_trackEvent', 'Ad Click','Home Page Ad', pn]);
				});
				$('a[rel*=_hpad]').click(function() {
					var pn = $(this).attr('title');
					_gaq.push(['_trackEvent', 'Ad Click','Home Page Ad', pn]);
				});
				_gaq.push(['_trackEvent', 'Home Page Tabs', 'Tab Click',currentTab]);
		},
        function() {
		 }
    );
});
