jQuery(document).ready(function() {
	// Set up a listener so that when anything with a class of 'tab' 
	// is clicked, this function is run.
	jQuery('#medias .tab').click(function () {
		// Remove the 'active' class from the active tab.
		jQuery('#medias > ul > li.active').removeClass('active');
	
		// Add the 'active' class to the clicked tab.
		jQuery(this).parent().addClass('active');
	
		// Remove the 'tab_contents_active' class from the visible tab contents.
		jQuery('#tabsContent > div.activeContent').removeClass('activeContent');
	
		// Add the 'tab_contents_active' class to the associated tab contents.
		jQuery(this.rel).addClass('activeContent');
	});

	// Set up a listener so that when anything with a class of 'tab' 
	// is clicked, this function is run.
	jQuery('#matches .tab').click(function () {
		// Remove the 'active' class from the active tab.
		jQuery('#matches > ul > li.active').removeClass('active');
	
		// Add the 'active' class to the clicked tab.
		jQuery(this).parent().addClass('active');
	
		// Remove the 'tab_contents_active' class from the visible tab contents.
		jQuery('#tabMatches > div.activeContent').removeClass('activeContent');
	
		// Add the 'tab_contents_active' class to the associated tab contents.
		jQuery(this.rel).addClass('activeContent');
	});

	// Set up a listener so that when anything with a class of 'tab' 
	// is clicked, this function is run.
	jQuery('#slider .control').hover(function () {
		var n = this.rel.split('-');
				
		// Remove the 'tab_contents_active' class from the visible tab contents.
		jQuery('#slider > div.active').removeClass('active');
		jQuery('#slide-'+n[1]).addClass('active');

		jQuery('#slider > div.controlPanel > div.active').removeClass('active');
		jQuery('#info-'+n[1]).addClass('active');
	});


	function megaHoverOver(){
		jQuery(this).find(".sub-menu").append('<span class="arrow"></span>');
    	jQuery(this).find(".sub-menu").stop().fadeTo('slow', 0.9); //Find sub and fade it in
	    (function($) {
    	    //Function to calculate total width of all ul's
        	jQuery.fn.calcSubWidth = function() {
            	rowWidth = 0;
	            //Calculate row
    	        jQuery(this).find("ul").each(function() { //for each ul...
        	        rowWidth += jQuery(this).width(); //Add each ul's width together
            	});
	        };
    	})(jQuery); 

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

    	    var biggestRow = 0;	

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

    	    jQuery(this).find(".sub-menu").css({'width' :biggestRow}); //Set width
        	jQuery(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

	    } else { //If row does not exist...
	
    	    jQuery(this).calcSubWidth();  //Call function to calculate width of all ul's
        	jQuery(this).find(".sub").css({'width' : rowWidth}); //Set Width
	    }
	}

	function megaHoverOut(){
		jQuery(this).find(".sub-menu").find(".arrow").remove();
		jQuery(this).find(".sub-menu").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
			jQuery(this).hide();  //after fading, hide it
		});
	}	
	
	jQuery("#menu-top-menu > li").hoverIntent({
		sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		interval: 100,   // number = milliseconds for onMouseOver polling interval
		over: megaHoverOver,     // function = onMouseOver callback (required)
		timeout: 500,   // number = milliseconds delay before onMouseOut
		out: megaHoverOut       // function = onMouseOut callback (required)
	});

	jQuery("a.myfancybox").fancybox({ 'zoomSpeedIn': 500, 'zoomSpeedOut': 500, 'overlayShow':true,'overlayOpacity':0.3 });
});
