



//CLEAR FORMS ON FOCUS
jQuery(document).ready(function(){

	var clearMePrevious = '';
    
    //sort by brand
    jQuery('.cat-drop').change(function() {
        jQuery('.cat-drop option:selected').each(function() {
            var val = jQuery('.cat-drop option:selected').val();
            
            window.location = jQuery(this).val();        
        });    
    });
        
    // clear input on focus
    jQuery('.clearMeFocus').focus(function()
    {
    	if(jQuery(this).val()==jQuery(this).attr('title'))
    	{
    		clearMePrevious = jQuery(this).val();
    		jQuery(this).val('');
    	}
    });
    // if field is empty afterward, add text again
    jQuery('.clearMeFocus').blur(function()
    {
    	if(jQuery(this).val()=='')
    	{
    		jQuery(this).val(clearMePrevious);
    	}
    });
    
    //TABBED BOX FUNCTIONS
	jQuery(function() {
	    jQuery('#river-tab').organicTabs({
			"speed": 400
		});
		
	    jQuery("#tab-box").organicTabs({
	        "speed": 400
	    });
	});
	
	jQuery(function() {
		jQuery('#river-tab-box').organicTabs({
			"speed": 400
		});
	});
	
	//HOMEPAGE SLIDESHOW SCRIPT
	jQuery(function(){
	    jQuery('#slider').cycle({
	    	cleartype: false,
	    	speed: 1600,
	    	timeout: 8000,
	    	pager: '#pager'
	    });
	});
	
        //TABBED BOX FUNCTIONS
	jQuery('#tabs div#tab1, #tabs div#tab2, #tabs div#tab3, #tabs div#tab4').hide(); // Hide all divs
		
		jQuery('#tabs div#tab1').show(); // Show the first div
		
		jQuery('#thetabs li a:first').addClass('active'); // Set the class for active state
		
		jQuery('#thetabs li a').click(function(){ // When link is clicked
		
			jQuery('#thetabs li a').removeClass('active'); // Remove active class from links
			
			jQuery(this).addClass('active'); //Set parent of clicked link class to active
			
			var currentTab = jQuery(this).attr('href'); // Set currentTab to value of href attribute
			
			jQuery('#tabs div#tab1, #tabs div#tab2, #tabs div#tab3, #tabs div#tab4').hide(); // Hide all divs
			
			jQuery(currentTab).show(); // Show div with id equal to variable currentTab
			
			return false;
		});
		
	jQuery(".scrollable").scrollable({
    	circular: true,
    	next: '.nextPage',
    	prev: '.prevPage'
    });
	

	//ACCORDIAN SCRIPT
	
    //Set default open/close settings
    jQuery('.acc_container').hide(); //Hide/close all containers
    jQuery('.acc_trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container
    
    //On Click
    jQuery('.acc_trigger').click(function(){
    	if( jQuery(this).next().is(':hidden') ) { //If immediate next container is closed...
    		jQuery('.acc_trigger').removeClass('active').next().slideUp(); //Remove all "active" state and slide up the immediate next container
    		jQuery(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
    	}
    	return false; //Prevent the browser jump to the link anchor
    });
    
    //DROPDOWN MENUS
   	function megaHoverOver(){
		jQuery(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function(jQuery) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				jQuery(this).find("ul").each(function() {					
					rowWidth += jQuery(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( jQuery(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			jQuery(this).find(".row").each(function() {							   
				jQuery(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			//jQuery(this).find(".sub").css({'width' :biggestRow});
			//jQuery(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			//jQuery(this).calcSubWidth();
			//Set Width
			//jQuery(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  jQuery(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  jQuery(this).hide(); 
	  });
	}


	var config = {    
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 250, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	jQuery("ul.menu li .sub").css({'opacity':'0'});
	jQuery("ul.menu li").hoverIntent(config);
	//END DROPDOWN MENUS
	
});


