var $j = jQuery.noConflict(); 

$j(function(){
	
	// Top Nav ********************************************
	
    $j("ul#menu-navigation li").hover(function(){
    
        $j(this).addClass("hover");
        $j('ul:first',this).css('visibility', 'visible');
        $j('ul:first',this).fadeOut(0);
        $j('ul:first',this).fadeIn(300);
    
    }, function(){
    
        $j(this).removeClass("hover");
        $j('ul:first',this).fadeOut(300);
    
    });
    
    $j("ul#menu-navigation li ul li:has(ul)").find("a:first").append(" &raquo; ");
    
    $j("ul#menu-navigation ul").hover( function() {
    	
    	$j(this).parent().siblings().fadeTo(500, 0.35);
    	
    }, function() {
    
    	$j(this).parent().siblings().fadeTo(500, 1.0);
    
    });
    
    // Tabs ********************************************
    
    $j(".tab-content").hide(); // Hide all of the tab content
    $j("ul.tabs li:first").addClass("active").show(); // Activate first tab
    $j(".tab-container :first").show(); // Show first tab content
    
    // On Click Event
    $j("ul.tabs li").click(function() {
    	
    	$j("ul.tabs li").removeClass("active"); // Remove any .active class
    	$j(this).addClass("active"); // add .active class to selected tab
    	$j(".tab-content").hide(); // hide all tab tontent
    	
    	var activeTab = $j(this).find("a").attr("href");
    	$j(activeTab).fadeIn();
    	
    	return false;
    	
    });
   	
   	// More Views ********************************************
   	
   	$j("div.toggler-content").hide(); //Hide the extra views
   	
   	$j("a.toggler").click(function() {
   		var target = $j(this).attr("href");
   		
   		if( $j(target).is(":visible") ) { // if not hidden
   			$j(target).fadeOut(); // hide it
   		} else {
   			$j(target).fadeIn(); // show it
   		}
   		
   		return false;
   	});
   	
   	// Homepage Slider ********************************************
   	
   	$j(".slider-nav").show(); // Show the slider navigation elements
   	$j(".slider-nav a:first").addClass("active"); // Activate first
   	$j(".slider-nav a:first").siblings().fadeTo(250, 0.7);
   	
   	var windowWidth = $j(".window").width(); // Width of window
   	var slideSum = $j(".slides div").size(); // Number of slides
   	var slidesWidth = windowWidth * slideSum; // Width of the slides (all)
   	
   	$j(".slides").css({'width' : slidesWidth}); // Adjust width of div.slides
   	
   	// Paging and Slider Function
   	rotate = function () {
   	
   		var triggerID = $active.attr("rel") - 1; // Get number of times to slide
   		var slidesPosition = triggerID * windowWidth; // determine the distance to slide
   		
   		$j(".slider-nav a").removeClass('active').fadeTo(250, 0.7); // Remove all active classes
   		$active.addClass('active').fadeTo(500, 1.0); // Add active class to the $active
   		
   		// Slider Animation
   		$j(".slides").animate( { left: -slidesPosition }, 500 );
   	
   	};
   	
   	// Rotation and Timing Event
   	rotateSwitch = function() {
   		
   		play = setInterval(function() { // Set the timer (7 seconds)
   			$active = $j(".slider-nav a.active").next(); // Switch Active Nav Btn
   			if ($active.length === 0) { // If reaches the end
   				$active = $j(".slider-nav a:first"); // Go back to first
   			}
   			rotate(); // Trigger the Nav and Slider Function
   		}, 7000); // Timer speed in milliseconds (7 seconds)
   		
   	};
   	
   	rotateSwitch();
   	
   	// On Hover
   	$j("div.slide").hover(function() {
   		clearInterval(play); // Stop the rotation
   	}, function() {
   		rotateSwitch(); // Resume rotation timer
   	});
   	
   	// On Click
   	$j(".slider-nav a").click(function() {
   		$active = $j(this); // Activate the clicked nav btn
   		
   		//Reset Timer
   		clearInterval(play); // Stop the rotation
   		rotate(); // Trigger rotation immediately
   		rotateSwitch(); // Resume rotation timer
   		return false; // Prevent browser jump to link anchor
   	});
   	
	// PAYPAL THINGS ********************************************
	
	$j("p.paypal-logo").hide(); //hide paypal express chexkout buttons
		
	$j(".add-to-cart").hover( function() {
		$j("p.paypal-logo").fadeIn(300);
	}, function() {
		$j("p.paypal-logo").fadeOut(300);
	});
	
	// TESTIMONIAL BLOCK ********************************************
	
	$j(".testimonials ul li").css("display", "none"); // Hide all of the quotes
	
	$j(".testimonials ul li:first").fadeIn(300); // Fade in first quote
	
	setInterval(function(){ // Start the timer
		
		$j(".testimonials ul li:visible").fadeOut(5000, function(){
			if($j(this).next("li").size()){ // If there is a next quote
				$j(this).next().fadeIn(1000); // Fade in the next quote
			} else { 
				$j(".testimonials ul li:first").fadeIn(300); // Fade in first quote
			}	
		});
		
	}, 5000);
	
});
