$(document).ready(function()
{

	$(function()
	{
		$('.nav').droppy();
	});

	$('.suff_img_change li').mouseover(function()
	{
		change_product_image_suf(this);
	});

	function change_product_image_suf(elem)
	{
		var id = $(elem).attr('id');
		id = id.substr(id.indexOf('_')+1);

		$('#product-photo img').attr('src', $('#image_' + id).val());
	}

	function megaHoverOver()
	{
		$(this).find(".sub").stop().fadeTo('fast', 1).show();

		//Calculate width of all ul's
		(function($)
		{
			jQuery.fn.calcSubWidth = function()
			{
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function()
				{
					rowWidth += $(this).width();
				});
			};
		})(jQuery);

		if ( $(this).find(".row").length > 0 )
		{ //If row exists...
			var biggestRow = 0;
			//Calculate each row
			$(this).find(".row").each(function()
			{
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow)
				{
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});

		}
		else
		{ //If row does not exist...

			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});

		}
	}

	function megaHoverOut()
	{
	  $(this).find(".sub").stop().fadeTo('fast', 0, function()
	  {
		  $(this).hide();
	  });
	}


	var config =
	{
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		 interval: 100, // number = milliseconds for onMouseOver polling interval
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		 timeout: 500, // number = milliseconds delay before onMouseOut
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);
	$("ul#topnav li a.products").click(function(event){
		event.preventDefault();
	});


//SLIDE ROTATOR //
	$.featureList(
		$("#tabs li a"),
		$("#output li"), {
			start_item	:	0
		}
	);

	$(".defaultText").focus(function() {
        if ($(this).val() == $(this)[0].title) {
            $(this).addClass("defaultTextActive");
            $(this).val("");
        }
    });

    $(".defaultText").blur(function() {
        if ($(this).val() == "")
        {
            $(this).removeClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });

    $(".defaultText").blur();

					/*

					// Alternative


					$('#tabs li a').featureList({
						output			:	'#output li',
						start_item		:	1
					});

					*/
	$(function() {
		// Change cursor style on the li, begin the function when trigger is clicked
		$(".filter_trigger").attr('style','cursor:pointer;').click(function() {
			// Each time the li is clicked, toggle the filter from unseleceted to selected.If the class is
			// filter_selected, append the filter to the selected_filters ul, otherwise append it to the available filters ul.
			$(this).toggleClass('filter_unselected filter_selected').appendTo($( '#' + ( $(this).hasClass('filter_selected') ? 'selected_filters' : 'available_filters' ) ));
			// Call the function apply_filters
			apply_filters();
			// Find spans within the filter_trigger li's and add the pointer attribute to them as well.
		}).find('span').attr('style','cursor:pointer;');
	});

	// For each li within the available_filters container,
	$('#available_filters').children('li').each(function(i,e){
		// set product_count variable equal to the number of product-rows with each filter.
		var product_count = $('.product-row.' + $(e).attr('rel') + ':visible').length;
		// If there is a product count for that filter, display the filter in the available filters.
		if( product_count ) {
			$(e).show().children('div').children('span.filter-product-count').html( '(' + product_count + ')' );
		} else {
			// If there are no products available for a filter, don't display it.
			$(e).hide();
		}
	});

});

// Applies filters to products
function apply_filters() {
	// Show all product rows
	$('.product-row').show();
	// If selected_filters has any children,
	if( $('#selected_filters').children('li').length  ) {
		// show the selected_filters container. For each child, hide the product rows that do not have the same class
		// as the selected filters' rel attributes.
		$('#selected_filters').show().children('li').each( function(i,e){ $( '.product-row' ).not( '.' + $(e).attr('rel') ).hide(); } );
	}

	// Hide the selected_filters container if it has no children.
	else {
		$('#selected_filters').hide();
	}

	// For each li within the available_filters container,
	$('#available_filters').children('li').each(function(i,e){
		// set product_count variable equal to the number of product-rows with each filter.
		var product_count = $('.product-row.' + $(e).attr('rel') + ':visible').length;
		// If there is a product count for that filter, display the filter in the available filters.
		if( product_count ) {
			$(e).show().children('div').children('span.filter-product-count').html( '(' + product_count + ')' );
		} else {
			// If there are no products available for a filter, don't display it.
			$(e).hide();
		}
	});
	
}







