/*********************************************************************************************************************************/
/* DOCUMENT READY CALLS **********************************************************************************************************/
/*********************************************************************************************************************************/

$(document).ready(function(){

	
	/* SUGGESTIVE SEARCH start ***********************************************************/
	var $searchbox = $('input#search_stringzz');
	var $criteria = $('select#search_criteria');
	var $suggestions = $('div#search_suggestions');
	var $searchform = $('form#TextSearch');
	
	$searchbox.bind('keyup', function(){
		if (($.trim($searchbox.val()) != '') && ($searchbox.val().length >= 1))
		{
			$.post(
				'/products/ajax_search/' + escape($searchbox.val() + '/' + escape($criteria.val())),
				{ searchstring : $searchbox.val() },
				function(data){
					$suggestions
						.html(data)
						.show()
						/*
						.find('div.suggestion')
							.bind('click', function(){
								var $this = $(this);
								$suggestions.hide();
								$searchbox.val($this.text());
								$searchform.submit();
							}); */
				}
			);
		}
		else
		{
			$suggestions.html('').hide();
		}
	});
	
	//var $blah = $('select#search_criteria');
	
	$('#TextSearch button').click(function(){
		if ($criteria.val() == '0') {
			alert('Please choose the category of product you are looking for in the dropdown box. If you would like to search across all of our products, please select All Products.');
			return false;
		} else {
			return true;
		}
	});
	
	
		
	/* EXTERNAL LINKS start ***********************************************************/
	$('a[rel="external"]').click(function(){
		this.target = "_blank";
	});
	/* EXTERNAL LINKS end *************************************************************/
	
	$('html').addClass('js');
	
	/*search*/
	$('#search input[type="text"]').val('Quick Search');
	$('#search input[type="text"]').focus(function(){ 
		if(this.value=='Quick Search') { $(this).val('');} 
		$('#search-help').fadeIn();
	});
	$('#search input[type="text"]').blur(function(){ if(this.value =='') {
		$(this).val('Quick Search');}
		$('#search-help').fadeOut();
	});
	
	/*featured products*/
	if($("#featured-products .scroll").length >0){
		$reset = $("#featured-products .scroll").clone();
		/*
		var init_carousel = function(){
			var num_visible = Math.ceil($('#featured-products .display').width()/242);
			//alert(num_visible);
			$("#featured-products .scroll li").eq(num_visible-1).css({'border-right':'0'});
			$("#featured-products .scroll").jCarouselLite({ btnNext: ".next", btnPrev: ".prev", mouseWheel: true,auto:900, speed: 500, visible: num_visible, scroll: 1});
			//var autoTime = false;
			//var x = setInterval("$('.next').trigger('click');",autoTime); //don't kno y, but works to keep slider from going crazy on resize
		};
		*/
		init_carousel();
		//$(window).resize(function () { $("#featured-products .display").html($reset); init_carousel(); });
	}
	/*categories*/
	if($("#internal").length >0){
		var $handle = $('#nav-cat h2');
		$handle.click(function(){
			if(!$handle.hasClass('active')){
				$('.site > .l').css({'background-position':'-281px 0'});
				$handle.addClass('active').next('ul').show();
				$('#content-panel').hide();
			} else {
				$('.site > .l').css({'background-position':'0 0'});
				$handle.removeClass('active').next('ul').hide();
				$('#content-panel').show();
			}
		});
	}
	
	/* Product Images */
	var $large_images = $('div#images-large a');
	var $small_images = $('div#images-small a');
	var $lightbox = $('div#lightbox');

	$small_images.hover(
			function(){
				$large_images.eq($(this).index() + 1).show();
			},
			function(){
				$large_images.eq($(this).index() + 1).hide();
			}
		);
	var $fancy = $('a.fancybox');
	if($fancy.length > 0){
		$fancy.fancybox({'transitionIn' : 'elastic', 'transitionOut' : 'elastic'});
	}
	 
	/* Category Listings:start */
	$('#category-listings .product-listings > ul li').mouseover(
		function(){
			$(this).prepend('<span class="t"></span>');
		}
	).mouseout(
		function(){			
			$('.t',this).remove();
		}
	);
	/* Category Listings:end */
	
	/* Product Sort:start */
	$('#product-sort ul').mouseover(function(){ $(this).addClass('active'); }).mouseout(function(){ $(this).removeClass('active'); });
	/* Product Sort:end */
	
	/* Additional Info:start */
	$('#attributes .nav > ul a').click(
		function(){
			$li = $(this).parent();
			$display = $($(this).attr('href'));
			
			$('#attributes .nav > ul li').removeClass('active');
			$('#attributes .nav > ul .b').remove();
			$('#attributes .row').removeClass('active');
			
			$li.addClass('active').append('<span class="b"></span>');
			$display.addClass('active');
			return false;
		}
	);
	/* Additional Info:end */
		
}); //close document.ready

/*********************************************************************************************************************************/
/* DOCUMENT READY FUNCTIONS ******************************************************************************************************/
/*********************************************************************************************************************************/

function init_carousel(){
	//alert($('#featured-products .display').width());
	var num_visible = Math.ceil($('#featured-products .display').width()/242);
		$("#featured-products .scroll li").eq(num_visible-1).css({'border-right':'0'});
		$("#featured-products .scroll").jCarouselLite({ btnNext: ".next", btnPrev: ".prev", mouseWheel: true, speed: 1000, auto: 2000, visible: num_visible, scroll: 2});
}
