$(document).ready(function() {
	// Cycle plugin for slideshow	
	if($('.slider-content').length > 0){
		$('.slider-content')
			.after('<div class="slider-nav">')
			.cycle({ 
				fx:     'fade', 
				timeout: 6000, 
				delay:  -2000,
				pause:	true,
				pager:  '.slider-nav'
		});
	}
	
	// Set equal heights
	$.fn.setAllToMaxHeight = function(){
		return this.height( Math.max.apply(this, $.map( this , function(e){ 
		return $(e).height() }) ) );
	}
	$('div.equal-height').setAllToMaxHeight(); 	
	
	// Mega Menu
	function megaHoverOver(){
		$(this).find(".sub-tab").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.sub-tab-menu").each(function() { //for each ul...
					rowWidth += $(this).width() + $('ul.tabs li div.sub-tab span ul.sub-tab-menu').css('padding-left').replace("px","") * 2; //Add each ul's width together
				});
				
			};
		})(jQuery); 

		$(this).calcSubWidth();  //Call function to calculate width of all ul's
		xPos = $(this).find("a").position().left; // align megamenu with main meny tab
		$(this).find(".sub-tab").css({'width' : rowWidth,left:xPos}); //Set Width
		
	
	}
	//On Hover Out
	function megaHoverOut(){
	  $(this).find(".sub-tab").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
		  $(this).hide();  //after fading, hide it
	  });
	}
	
	//Set custom configurations
	var config = {
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		 interval: 25, // 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.tabs li div.sub-tab").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul.tabs li").hoverIntent(config); //Trigger Hover intent with custom configurations

	
	// Mega Menu Preview Screen
	
	function previewHoverOver(){
		el = $(this).find('a').attr('id');
		if(el){
			$('ul.sub-tab-preview li.'+el).show();
			(function($) {
				//Function to calculate total width of all ul's
				jQuery.fn.calcSubWidth = function() {
					rowWidth = 0;
					//Calculate row
					$(this).parent().parent().find("ul").each(function() { //for each ul...
						rowWidth += $(this).width() + $('ul.tabs li div.sub-tab span ul.sub-tab-menu').css('padding-left').replace("px","") * 2; //Add each ul's width together
					});
				};
			})(jQuery);
				
			$(this).parent().parent().calcSubWidth();  //Call function to calculate width of all ul's
			$(this).parent().parent().css({'width' : rowWidth}); //Set Width
			$('ul.sub-tab-preview li').height('0');
			$('ul.sub-tab-preview li.'+el).height($(this).parent().parent().height() - $('ul.sub-tab-menu').css('padding-left').replace("px","") * 2 - 1);
		}
	
	}
	
	//On Hover Out
	function previewHoverOut(){
		el = $(this).find('a').attr('id');
		if(el){
			$('ul.sub-tab-preview li.'+el).hide();
		}
		(function($) {
			//Function to calculate total width of all ul's
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).parent().parent().find("ul.sub-tab-menu").each(function() { //for each ul...
					rowWidth += $(this).width() + $('ul.tabs li div.sub-tab span ul.sub-tab-menu').css('padding-left').replace("px","") * 2; //Add each ul's width together
				});
			};
		})(jQuery);

		$(this).parent().parent().calcSubWidth();  //Call function to calculate width of all ul's
		$(this).parent().parent().css({'width' : rowWidth}); //Set Width
	}
	
	//Set custom configurations
	var config2 = {
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		 interval: 0, // number = milliseconds for onMouseOver polling interval
		 over: previewHoverOver, // function = onMouseOver callback (REQUIRED)
		 timeout: 0, // number = milliseconds delay before onMouseOut
		 out: previewHoverOut // function = onMouseOut callback (REQUIRED)
	};
	
	$("ul.tabs li div.sub-tab ul li").hoverIntent(config2); //Trigger Hover intent with custom configurations
	
	var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
	
	/* accordion */
	$('div.accordion dt').hover(
		function(){
			$(this).append('<span class="accordionPreview"> ' + $('p',$(this).next()).html().substr(0,30) + '...' + '</span>');
		},
		function(){
			$('.accordionPreview',$(this)).remove();
		}
	);
	$('div.accordion dt').click(
		function(){
			if(!$(this).hasClass('down')){
				$('div.accordion dt').removeClass('down');
				$('div.accordion dd').slideUp();
				$(this).addClass('down').next().slideDown();
			} else {
				$('div.accordion dt').removeClass('down');
				$('div.accordion dd').slideUp();
			}
		}
	);
	
	

});
