/*
 * Katherinehooker Brand
 */

// Global Variables
var $slideSpeed = 400;
var $timer = '';
var $parent = '';

jQuery(function() {
	
	// Variables
	var $return = false;
	var $orig_page = '';
	var $funcSpeed = 250;
	var $topMenu = jQuery('#top-menu-wrapper');
	var $menuHeight = 38;
	// Slide the top menu up to start with
	jQuery('#slide-menu').hide();
	
	// Top menu
	jQuery('header#head-main .head-nav ul li a').bind('click', function() {
		// Allow the default action on the last menu item
		if( jQuery(this).parent().hasClass('menu-item-last') || jQuery(this).parent().attr('id') == 'menu-item-4847' ) {
			$return = true;
		// Slide down the sub menu
		} else if( !(jQuery(this).parent().hasClass('current-page-ancestor')) ) {
			// Retrieve the parent ID
			var $li = jQuery(this).parent().attr('id');
			var $ID = $li.split( '-', 3);
			// From the parent ID retrieve the children
			jQuery.ajax({
				type: 'POST',
				data: 'post_id='+$ID[2],
				url: '/wp-content/themes/kh-brand/includes/slidedown-menu.php',
				success: function( msg ) {
					if( msg != '' ) {
						// If the top menu is active slide up & display the new children
						if ( jQuery('.top-children').length > 0 ) {
							jQuery('.top-children').slideUp( $slideSpeed );
							jQuery('#slide-menu').html( msg ).slideDown( $slideSpeed );
							// Stop the top menu bouncing
							$topMenu.css({ 'height' : $menuHeight });
						} else {
							jQuery('#slide-menu').html( msg ).slideDown( $slideSpeed );
						}
					} else {
						
					}
				}
			});
			$return = false;
		} else {
			jQuery('#slide-menu').slideUp( $slideSpeed );
			jQuery('.top-children').slideDown( $slideSpeed );
			// Stop the top menu bouncing
			$topMenu.css({ 'height' : $menuHeight });
		};
		// return true or false based on the situation
		return $return;
	});
	
	// ----------------- SWATCHES --------------------
	
	// Variables
	var $swatches = jQuery('ul#swatches');
	var $swatch_wrapper = jQuery('#swatch_wrapper');
	
	jQuery('ul#swatches').wrap('<div id="swatch_wrapper"></div>');
	jQuery('#swatch_wrapper').before('<div class="swat_up"></div>').after('<div class="swat_down"></div>');
	
	// Move by 154px;
	
	$swat_up = jQuery('.swat_up');
	$swat_down = jQuery('.swat_down');
	
	
	var swatches = jQuery('ul#swatches').height();
	var swat_holder = jQuery('#swatch_wrapper').height();
	//var scroll_depth = parseInt( jQuery( 'ul#swatches' ).css( 'top' ) );
	var total_scroll = null;
	
	$swat_down.bind('click', function(s) {
					
		//alert( swatches );
		//alert( swat_holder ); //460
		//alert( scroll_depth );
				
		var scroll_depth = parseInt( jQuery( 'ul#swatches' ).css( 'top' ) );
		//alert( scroll_depth ); // 0
		
		
		total_scroll = swatches + scroll_depth;
		//alert( total_scroll );  // 770
			
		
		if ( total_scroll > (swat_holder + 2) ) {
			jQuery('#swatches').animate({ 'top' : '-=154px' }, 'fast');
		}
	});
	
	$swat_up.bind('click', function(s) {
		var scroll_depth = parseInt( jQuery( 'ul#swatches' ).css( 'top' ) );
		//alert( scroll_depth );
		if ( scroll_depth < 0 ) {
			jQuery('#swatches').animate({ 'top' : '+=154px' }, 'fast');
		}
	});
	
	
	// Make sure the gallery thumbnails are available
    if (jQuery('ul#product-thumbs').length > 0) {
		
		// Remove the lightbox if the product gallery exists
		if( jQuery('ul#product-gallery').length > 0 ) {
			jQuery('ul#product-thumbs li a').attr('rel', function( index ) {
				return( 're' + (index+1) );
			});
		}
		        
		// Find when a thumbnail is clicked
        jQuery('ul#product-thumbs li a').bind('click', function() {
    	
            // Get the ID
            var id = jQuery(this).attr('name');
            
            // Set the correct image to display
            jQuery('ul#product-gallery li').removeClass('active');
            jQuery('ul#product-gallery li#'+id).addClass('active');
			
			return false;
			
		});
       
	}
	
	// Homepage Gallery
	//	Only run the slideshow if there's more than two slide
	if( jQuery('#bg-front img').length >= 2) {
		var interval = setInterval("slideSwitch()", 5000);
	}
	
	// Remove empty <p>'s
	jQuery('p').filter(function() {
        return jQuery.trim(jQuery(this).text()) === '' && jQuery(this).children().length === 0;
    }).remove();
});

function slideSwitch() {
	
	if( jQuery('#bg-front img').length >= 2) {
		var $active = jQuery('#bg-front img.active');
    
		if($active.length == 0) {
			$active = jQuery('#bg-front img:last');
		}
		
		var $next = $active.next().length ? $active.next() : jQuery('#bg-front img:first');
		
		$active.addClass('last-active');
		
		$next.css({opacity: 0.0}).addClass('active').animate({ opacity: 1.0 }, 1000, function() {
			$active.removeClass('active last-active');
		});
	}
}

// Return the menu to its orginal state
function return_menu_state() {
	clearTimeout( $timer );
	jQuery('.top-children').slideDown( $slideSpeed );
	jQuery('#slide-menu').slideUp( $slideSpeed );	
}
