/*
 * jQuery UI Autocomplete Select First Extension
 *
 * Copyright 2010, Scott Gonz‡lez (http://scottgonzalez.com)
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * http://github.com/scottgonzalez/jquery-ui-extensions
 */
/*       
(function( $ ) {


$( ".ui-autocomplete-input" ).live( "autocompleteopen", function() {



		var autocomplete = $( this ).data( "autocomplete" ),
			menu = autocomplete.menu;
	
		if ( !autocomplete.options.selectFirst ) {
			return;
		}
	
		menu.activate( $.Event({ type: "mouseenter" }), menu.element.children().first() );
		
});

}( jQuery ));
*/



function spreadGridItems()
{
	var mainDiv_w = parseFloat($(".browseProductsGrid_div").css('width'));
	var gridDiv_w = 186;
	var numOfColumns = parseInt(mainDiv_w / gridDiv_w);
	var remainingWidth = mainDiv_w % gridDiv_w;
	var leftMargin = parseInt(remainingWidth / (numOfColumns + 1));
	if(leftMargin < 10)
	{
		remainingWidth = remainingWidth + gridDiv_w;
		leftMargin = parseInt(remainingWidth / numOfColumns);
	}
	
/* 		alert(mainDiv_w + 'and' + gridDiv_w + 'and' + remainingWidth + 'and' + numOfColumns + 'and' + leftMargin); */
	
	$(".browseProductGridDiv").each(function(){
		$(this).css('margin-left', leftMargin + 'px')
	});
}
	

$(document).ready(function() {

 var fadeSpeed = 250;
 var clickEvent = false;
 
// SHOW MORE FACETS ON CLICK
	$(".moreFacets").live('click', function(){

			var facetName = $(this).attr('id');		
			$(this).addClass('activeLink');

			$("#" + facetName + 'Top').fadeOut(fadeSpeed, function(){
				$(this).hide();
				$("#" + facetName + 'All').each(function(){
					$(this).fadeIn(fadeSpeed);	
					$(this).parent().addClass('longFacet_div');
				});
				$(".activeLink").animate({ 'margin-left': '10px'},100,function(){		
					$(this).replaceWith('<a href="#" id="' + facetName + '" class="bold lessFacets" style="margin-left: 10px"> &laquo; less </a>');
				});
			});
			
			
		
		return false;
	});
	
// SHOW LESS FACETS ON CLICK
	$(".lessFacets").live('click', function(){

			var facetName = $(this).attr('id');
			$(this).addClass('activeLink');
			$("#" + facetName + 'All').each(function(){
					$(this).fadeOut(fadeSpeed,function(){
						$(this).hide();
						$(this).parent().removeClass('longFacet_div');
						$("#" + facetName + 'Top').fadeIn(fadeSpeed);
						$(".activeLink").animate({ 'margin-left': '0px'},100,function(){		
							$(this).replaceWith('<a href="#" id="' + facetName + '" class="bold moreFacets"> more &#187;</a>');
						});
					});
			});
			
			
		
		return false;
	});



	$("#search_input").each(function(){
	
		$(this).focus();
		if (this.setSelectionRange)
	    {
	    // (Doesn't work in IE)
	    // Double the length because Opera is inconsistent about whether a carriage return is one character or two.
	    var len = $(this).val().length * 2;
	    this.setSelectionRange(len, len);
	    }
	 	else
	    {
	    // ... otherwise replace the contents with itself
	    // (Doesn't work in Google Chrome)
	    $(this).val($(this).val());
	    }
	});
			/*
if (this.setSelectionRange)
        {
        // ... then use it
        // (Doesn't work in IE)

        // Double the length because Opera is inconsistent about whether a carriage return is one character or two. Sigh.
        var len = $(this).val().length * 2;
        this.setSelectionRange(len, len);
        }
        else
        {
        // ... otherwise replace the contents with itself
        // (Doesn't work in Google Chrome)
        $(this).val($(this).val());
        }
*/


	$(".popSubMenu").tooltip({position: "bottom right", offset: [-15,-2], effect: 'slide', delay: 200, predelay: 200});
	
	// css's 'last-child' selector doesn't work when a hidden item is after this item (for the menu pop)
	$(".vertMenuCats:last").css('border-bottom','none');
	
	
	spreadGridItems();
	$(window).resize(spreadGridItems);

	
	$(".secDepartments").click(function(){
		$("#homePageCategories_div").toggle();
		$(this).toggleClass('orange');
		return false;
	});
	
	

	
	// don't navigate away from the field on tab when selecting an item
			$("#search_input").bind( "keydown", function( event ) {
/* 				if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "autocomplete" ).menu.active ) { */
				if ( event.keyCode === $.ui.keyCode.TAB ) {
					event.preventDefault();
				}
			})
			.autocomplete({
/* 				appendTo: "#homeWrapper", */
        selectFirst: true,
				source: function( request, response ) {
					$.getJSON( "/dev/search/autosuggestajax/" + request.term, {
					}, response );
				}
			});
			
	
    
});
