﻿/****************************************************************************************	
	[GLOBAL JQUERY FUNCTIONS]
*****************************************************************************************/
$(document).ready(function() {
	
	// Dropdown menu
	$('#nav-main>li').each(function(){
		var headOffset = $('#header').offset();
		var dropOffset = $(this).children('div').offset();
		if (dropOffset != null && (dropOffset.left + $(this).children('div').outerWidth()) > (headOffset.left + $('#header').outerWidth())) {
			var difference = "-" + Math.round((dropOffset.left + $(this).children('div').outerWidth()) - (headOffset.left + $('#header').outerWidth())) + "px";
			$(this).children('div').css('left', difference);
		}
	});
	$('#nav-main>li').hover(function(){$(this).addClass('hover');},function(){$(this).removeClass('hover');});
	
	// Init Home Carousel
	$('#prev, #next').css('display', 'block');
	$('#promos li').css('margin-bottom', '0');
	$('#promos').carousel(750, '#prev', '#next', true, 5000);
	
	// Products Grid
	$('div.grid-row.products>div').hover(function(){$(this).addClass('expanded');},function(){$(this).removeClass('expanded');});
	
	// Products Carousel
	$('#sku-img div').carousel(250, '#prev', '#next', false);
		$('#sku-img div input').click(function(){
			$('#sku-img>img').not('.notification-overlay').attr('src',$(this).attr('name'));
		});
		
	// Products 'You may also like'
	$(".more-products").show().click(function(){
		$(this).prev('div').is(":hidden") ? $(this).html("<span>View less</span>") :  $(this).html("<span>View more</span>");
		$(this).prev('div').toggle();
		$(this).prev('div').find('div.first').hover();
	});
	
	// Product Description
	if (!$.browser.msie) {$('.more-products').prev('div').hide();}; // Hiding element as normal if not IE.
	$('#more-info').prev('div.wysiwyg').css('height','3.5em').css('overflow','hidden');
	$("#more-info").show().click(function(){
		$(this).prev('div.wysiwyg').css('overflow') == 'hidden' ? $(this).html("<span>Less</span>") :  $(this).html("<span>More</span>");
		$(this).prev('div.wysiwyg').css('overflow') == 'hidden' ? $(this).prev('div.wysiwyg').css('height','auto').css('overflow','visible') : $(this).prev('div.wysiwyg').css('height','3.5em').css('overflow','hidden');
	});
	
	// Product Swatches
	$('#sku-swatches li').hover(function() {
		$(this).append('<div><div class="inner"><em>'+$(this).children('img').attr('alt')+'</em></div></div>');
		$(this).children('img').clone().prependTo($(this).find('div.inner'));
		var pos = $(this).position();
		var left = pos.left + Math.round($(this).width()/2);
		var left = left - Math.round($(this).children('div').width()/2)
		$(this).children('div').css('left',left);
	}, function() {
		$(this).find('div').remove();
	});
	
	// Refine Search Hide Submit Button
	$('.btn-refine').hide();
		
	// Show Filters
	$('#filter').css('display','block');
	$('.my-freedom #filter dl').css('visibility','visible');
	
	// Enable Forms to be Submitted via ENTER key
	var AreaSelector = "#wrapper fieldset, #sign-in, #main-content, #nav-main div div, #wrapper #sidebar div.search, #footer div.inner div";
	var ButtonSelector = "input[type='submit'],input[type='image'],button";
	jQuery.each($(AreaSelector), function() {
        $(this).keypress(function(e) {
            if (e.which == 13 && e.target.type != 'textarea') {
                var arrItems = $(this).find(ButtonSelector);
                if (arrItems.length > 0) {
                    $(this).find(ButtonSelector)[0].click();
                } else {
                    eval($(this).find(".btn-submit")[0].href);
                }
                return false;
            }
        });
    });

	// Clear text input values
	var swap_text_boxes = [];
	jQuery.each($("input[type='text'].autoclear,input[type='password'].autoclear"), function() {
        swap_text_boxes[$(this).attr('id')] = $(this).attr('value');
        $(this).bind('focus', function() {
            if ($(this).val() == swap_text_boxes[$(this).attr('id')]) {
                $(this).val('');
            }
        });
        $(this).bind('blur', function() {
            if ($(this).val() == '') {
                $(this).val(swap_text_boxes[$(this).attr('id')]);
            }
        }); 
    });
});
$(window).load(function() {$('.more-products').prev('div').hide();}); // Element hiding for IE.


//Click events for the homepage top navigation searches
function findItByPage(btn, pageID) {
    var strURL = "/ProductSearch.aspx?SearchText=" + btn.prev().attr("value") + "&PageFilter=" + pageID;
    window.location.href = strURL
}

function findItByCategory(btn, productCategory) {
    var strURL
    window.location.href = "/ProductSearch.aspx?SearchText=" + btn.prev().attr("value") + "&Category=" + productCategory;
}


/****************************************************************************************	
	[JQUERY PLUG-INS]
*****************************************************************************************/
//jQuery Infinite Carousel - Copyright 2009 Stéphane Roucheray
jQuery.fn.carousel=function(speed,previous,next,autoscroll,duration,options){var sliderList=jQuery(this).children("ul")[0];if(sliderList){var increment=jQuery(sliderList).children().outerWidth("true"),elmnts=jQuery(sliderList).children(),numElmts=elmnts.length,sizeFirstElmnt=increment,shownInViewport=Math.round(jQuery(this).width()/sizeFirstElmnt),firstElementOnViewPort=1,isAnimating=false;if(numElmts<=shownInViewport){jQuery(previous).hide();jQuery(next).hide();jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment+increment+"px");}else{for(i=0;i<shownInViewport;i++){jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment+increment+"px");if(i<numElmts){jQuery(sliderList).append(jQuery(elmnts[i]).clone());};}};jQuery(previous).click(function(event){event.preventDefault();if(!isAnimating){if(firstElementOnViewPort==1){jQuery(sliderList).css('left',"-"+numElmts*sizeFirstElmnt+"px");firstElementOnViewPort=numElmts;} else{firstElementOnViewPort--;} jQuery(sliderList).animate({left:"+="+increment,y:0,queue:false},speed,"swing",function(){isAnimating=false;});isAnimating=true;}});jQuery(next).click(function(event){event.preventDefault();if(!isAnimating){if(firstElementOnViewPort>numElmts){firstElementOnViewPort=2;jQuery(sliderList).css('left',"0px");} else{firstElementOnViewPort++;} jQuery(sliderList).animate({left:"-="+increment,y:0,queue:false},speed,"swing",function(){isAnimating=false;});isAnimating=true;}});if(autoscroll&&numElmts>shownInViewport){var intervalId=window.setInterval(function(){jQuery(next).click();},duration);jQuery(this).mouseenter(function(){window.clearInterval(intervalId);});jQuery(this).mouseleave(function(){intervalId=window.setInterval(function(){jQuery(next).click();},duration);});}}};
