$(document).ready(function () {

	$('#btnSearch').hover(function() {
    	$(this).css({'cursor' : 'pointer'});
    });

	/* This is basic - uses default settings */
	$("a.single-img").fancybox({
		'overlayOpacity':0.9,
		'overlayColor':'#1a1a1a'
	});
	$("a.iframe").fancybox({
		'frameWidth':780,
		'frameHeight':760,
		'overlayOpacity':0.9,
		'overlayColor':'#1a1a1a'
	});
	
	
	$.viewMap = {
    	'0' : $([]),
    	'mtn1' : $('#mtn1'),
    	'mtn2' : $('#mtn2'),
    	'mtn3' : $('#mtn3'),
    	'mtn4' : $('#mtn4')
  	};
  	$.each($.viewMap,function(){ $(this).hide(); });
  	
  	$.viewMap['mtn1'].show();

	$('#viewSelector').change(function() {
	    // hide all
	    $.each($.viewMap, function() { $(this).hide(); });
	    // show current
		$.viewMap[$(this).val()].show();
	});
	
	if(document.getElementById("promos")){
	
		var promomods = $('#promos > div.promo');
			promomods.hide().filter('#promo-' + cdate).show();
		var promotabs = $('#promo-nav > li');
	    	promotabs.filter('#tab-' + cdate).addClass('selected');
	   
		$('ul#promo-nav li a').hover(function () {
			
			$('ul#promo-nav li').removeClass('hover');
			
			if(!$(this).parent('li').hasClass('selected')) {
	   			$(this).parent('li').addClass('hover');
	   		}
	    });
	    $('ul#promo-nav li a').mouseout(function () {		
	   			$(this).parent('li').removeClass('hover');
	    });
	    
		$('ul#promo-nav li a').click(function () {
			
			var activetab = this.hash;
				activetab = activetab.replace("#","promo-");
			
			//alert(activetab);
			
			promomods.hide().filter('#' + activetab).show();
			
			/*var qu = $(this).attr("id");
		    var an = "#" + qu.replace("gb","gid");
		    var gc = "#" + qu.replace("gb","gc");*/
			
			//alert(this.hash);
			// switch chart
			var seasonID = this.hash;
				seasonID = seasonID.replace("#","");
	   		switchchart(seasonID);
		
			//alert(this.hash);
			//chartdetails.hide().filter(this.hash + '-detail').show();
			
	   		// remove selected
	   		$('ul#promo-nav li').removeClass('hover');
	   		$('ul#promo-nav li').removeClass('selected');
	   		// add selected to this
	   		$(this).parent('li').addClass('selected');
	   		
	   		return false;
	   		
	    });
	    
	}
    
    function megaHoverOver(){
    
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}


	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 50, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 100, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);


});

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function popWindow(url, name, width, height, scrollbars) {
	var opts = "toolbar=no,status=no,location=no,menubar=no,scrollbars=no,resizable=no";
	opts += ",height=" + height + ",width=" + width + ",scrollbars=" + scrollbars;
	window.open(url, name, opts);
}

/* externalinterface */

function switchchart(path){
	//alert('switch chart');
	var swfchart = new Object;
		swfchart = getFlashMovie('swfslideshow');
		swfchart.updateText(path);
}
function getFlashMovie(movieName) {   
	var isIE = navigator.appName.indexOf("Microsoft") != -1;  
	if (isIE) {              
   		return window[movieName];              
	} else {  
    	return document[movieName];     
	}     
}
