$('head').append('<style>ul.level-3, dd{display:none;}</style>')

$(document).ready(function(){
    
	$('#all_cats:not(.keepopen)').hover(
	  function () {
		$('#NavigationLevel1').show();
		$('#all_cats h3.expand').addClass('open');
	  }, 
	  function () {
		$('#NavigationLevel1').hide();
		$('#all_cats h3.expand').removeClass('open');		
	  }
	);    

	(function($){$.belowthefold=function(element,settings){var fold=$(window).height()+$(window).scrollTop();return fold<=$(element).offset().top-settings.threshold;};$.abovethetop=function(element,settings){var top=$(window).scrollTop();return top>=$(element).offset().top+$(element).height()-settings.threshold;};$.rightofscreen=function(element,settings){var fold=$(window).width()+$(window).scrollLeft();return fold<=$(element).offset().left-settings.threshold;};$.leftofscreen=function(element,settings){var left=$(window).scrollLeft();return left>=$(element).offset().left+$(element).width()-settings.threshold;};$.inviewport=function(element,settings){return!$.rightofscreen(element,settings)&&!$.leftofscreen(element,settings)&&!$.belowthefold(element,settings)&&!$.abovethetop(element,settings);};$.extend($.expr[':'],{"below-the-fold":function(a,i,m){return $.belowthefold(a,{threshold:0});},"above-the-top":function(a,i,m){return $.abovethetop(a,{threshold:0});},"left-of-screen":function(a,i,m){return $.leftofscreen(a,{threshold:0});},"right-of-screen":function(a,i,m){return $.rightofscreen(a,{threshold:0});},"in-viewport":function(a,i,m){return $.inviewport(a,{threshold:0});}});})(jQuery);
	
	$('a.print').click(function(){
		window.print();
		return false;
	});
	
	// rotate banner in header
	$('#banner').innerfade({
		speed: 'slow',
		timeout: 4000,
		type: 'sequence',
		containerheight: '43px'
	});	
	// rotate banner in header
	$('#feat_ads').innerfade({
		speed: 'slow',
		timeout: 4000,
		type: 'sequence',
		containerheight: '300px'
	});	
	
	/* PRODUCT DETAIL */
	$('.cust_bought ul').innerfade({
		speed: 'slow',
		timeout: 4000,
		type: 'sequence',
		containerheight: '180px'
	});		
	$('.btn-backorder').click(function(){
		$(this).hide();
		$('#backorder-confirm').show('fast');
		return false;
	});
	
	$('#backorder-confirm .btn-cancel').click(function(){
		$('#backorder-confirm').hide();
		$('.btn-backorder').show();
		return false;
	});	

	// Review Antics
	$('a.showReviewForm').click(function(){
		$('form[name=ProductReview]').show();
		$(this).remove();
		return false;
	});
	
	$("#starify").children().not(":input").hide();
	
	// Create stars from :radio boxes
	if($('#starify').length){
		$("#starify").stars({
			cancelShow: false
		});
	};
    
	// PRODUCT DETAIL TABS    
	// get all tab containers
    var tabContainers = $('div.tabs > div');
    
    // when a tab is clicked    
	$('ul.idTabs li a').click(function () {
        
        // hide all containers and show the one we clicked
        tabContainers.hide().filter(this.hash).show();
        
        // deselect all tabs and select the one that was clicked
        $('ul.idTabs li a').removeClass('selected');
        $(this).addClass('selected');
        
        return false;
        
    });
    
	// on load get the hash and show the correct tab
    function showTabs(){
    
       var thisHash = window.location.hash;
       
       // if there is a hash in the url, get it and select the proper tab
       if(thisHash.length){
			$(tabContainers).hide().filter(thisHash).show();
			$('ul.idTabs a').removeClass('selected');
			$('a[href='+thisHash+']').addClass('selected');
       }
       // show the first tab by default
       else{
	    $(tabContainers).hide().filter(':first').show();       
       }
       
    }; showTabs();
	
	// when a link w/ class of tabJump is clicked -- show tab and jump to that content
    $('a.tabJump').click(function(){
        $(tabContainers).hide().filter(this.hash).show();
        $('ul.idTabs a').removeClass('selected');
        $('a[href='+this.hash+']').addClass('selected');
        
        // if this is a click to show reviews, show the form as well
        if($(this).attr('rel') == 'showForm'){
        	$('a.showReviewForm').trigger('click');
        };
    });		
	
	// if the menu is present
	if( $('dl#side-nav').length ){

		// hide all level-3
		$('ul.level-3').hide();
		
		// show any level 3s that have active children
		$('li.nav-active-2').parent().show();
		$('li.nav-active-2').parent().parent().addClass('current');
		
		// find a list item of nav-active and give the parent dd an id of active (to keep it open)
		$('li.nav-active').parent().parent('dd').attr('id','active');
		$('.nav-active-2').parent().parent().parent().parent().attr('id','active');
		
		// give the child anchor tag a class of current
		$('dd#active').prev('dt').children('a').addClass('current');
		
		// hide all inactive dds -- show all active dds
		$("dl#side-nav dd:not(#active)").hide();
		$("dl#side-nav dd#active").show();		
	
		$("dd:not(#active)").hide();
		$("dd#active").show();	
		
		// make sure we have menus present
		if( $('ul.level-2').length && $('ul.level-3').length ){
		
			// use hoverintent to prevent firing of the moveMenu function when the curser incidentally hits another dt a because of expanding/contracting		
			$("ul.level-2 > li:not(.current)").hoverIntent({
				interval: 100,
				over: moveMenu, 
				timeout: 500, 
				out: moveMenuDone
			});		
			
			function moveMenu(){
				//event.stopPropagation();
				$('li:not(.current) ul.level-3:visible').slideUp();
				$(this).find('ul:hidden').css('background-color', 'red').slideDown();
			}function moveMenuDone(){};

		
		};
		
	};	
	
	// LOAD
	$(window).bind("scroll", function(event) {

		// load more when the li#page-more is in the viewport
		if( $('li#page-more').is(':in-viewport') ){
					
			// set whether it's loading or not
			var isLoading = $('li#page-more').hasClass('loading');
			
			// if it is not loading, run the function to load it
			if (!isLoading){
				
				// run function to load content
				loadCatalogs();
				
			};
					
		};	
	
	});	

	// load content
	function loadCatalogs(){
	
		// show loading
		$('li#page-more').addClass('loading').empty().append('<img src="/images/layout/page_loading.gif" /> Loading...');
		
		var navigation_url_path = $('form[name=filterForm] input[name=navigation_url_path]').val();
		var nid = $('form[name=filterForm] input[name=nid]').val();
		var cid = $('form[name=filterForm] select[name=cid]').val();
		
		var sort = $('form[name=filterForm] select[name=sort]').val();
		var show = $('form[name=filterForm] select[name=show]').val();
		var show = parseInt(show);

		var offset = $('li#page-more').attr('data-start');
		var limit = show;
		var limit = parseInt(limit);
		var newOffset = parseInt(offset);				
		var newOffset = newOffset + limit;
				
		$.ajax({
			type: "GET",
			url: "/api/catalog.cfm?method=getCatalogs",
			data: { limit: limit, offset: offset, nid: nid, cid: cid, sort: sort, show: show, navigation_url_path: navigation_url_path },
			cache: false,
			success: function(data) {
			
				if( data.length ){
					$('li#page-more').before(data).removeClass('loading').attr('data-start',newOffset).empty().text('MORE');
				}
				else{
					$('li#page-more').remove();
				};
								
			},
			error: function(msg){
				
				
			}
		});		
		
	};		
	
	// when the filter form changes redirect
	$('form[name=filterForm]').change(function(){
		var formData = $('form[name=filterForm] :not(input[name=nid],input[name=navigation_url_path])').serialize();
		var path = $('form[name=filterForm]').attr('action');
		var url = path + '?' + formData;
		window.location = url;
	});
	
	// when the filter form changes redirect
	$('form[name=filterForm1]').change(function(){
		var formData = $('form[name=filterForm1]').serialize();
		var path = $('form[name=filterForm1]').attr('action');
		var url = path + '?' + formData;
		window.location = url;
	});	
	
	// CART
	// if .cartUpdated exists, that means the cart has been updated (seesion set on insert_cart_line and insert_cart_line_bulk
	if( $('div.cartUpdated').length ){
		
		// wait a second
		setTimeout(function() { flashCartdiv() }, 1000);
		
		// flash the div
		function flashCartdiv(){
			$('div.cartUpdated').css("background-color","yellow").fadeTo("slow", 1).fadeTo("fast", 0.2).fadeTo("fast", 0.5).fadeTo("fast", 1,function(){
				setTimeout(function() { $('div.cartUpdated').css("background-color","#FFFFFF") }, 2000);
			});
		};
		
	}
	else if( $('li.cartUpdated').length ){
		
		// wait a second
		setTimeout(function() { flashCartli() }, 1000);
		
		// flash the div
		function flashCartli(){
			$('li.cartUpdated').css("background-color","yellow").fadeTo("slow", 1).fadeTo("fast", 0.2).fadeTo("fast", 0.5).fadeTo("slow", 1,function(){
				setTimeout(function() { $('li.cartUpdated').css("background-color","#F5F5F5") }, 3000);
			});
		};		
	
	};	

	// remove messages
	if( $('div.message').length ){
		
		// wait a second
		setTimeout(function() { removeMessage() }, 5000);
		
		// flash the div
		function removeMessage(){
			$('.message').slideUp('fast');
		};
		
	};
	
	// search
	$('input.search_input').focus(function(){
	
		if(this.value == "Search ..."){
			$(this).val('');
		};
	
	});
	
	$('input.search_input').blur(function(){
	
		if(this.value == ""){
			$(this).val('Search ...');
		};
	
	});	
	
	
});

