﻿/****************************************************************************************	
	[GLOBAL JQUERY FUNCTIONS]
*****************************************************************************************/

$(document).ready(function() {
    //$(".product-category li:nth-child(4n)").addClass('fourth');
	$(".style-this-with li:nth-child(3n)").addClass('third');
});

// Enable Forms to be Submitted via ENTER key
var AreaSelector = "#wrapper fieldset,#main-content,.search,.main-content,#promo";
var ButtonSelector = "input[type='submit'],input[type='image'],button";
$(document).ready(function() {
	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 = [];
$(document).ready(function() {
    jQuery.each($("input[type='text'].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')]);
            }
        }); 
    });
});

// Open the cart
$(function(){
    $('li#cart>span>a').click(function(event){
        event.preventDefault();
		$('li#cart').hasClass('open') ? $('li#cart').removeClass('open') : $('li#cart').addClass('open');
    });
});

// Products Carousel + Zoomer
$(document).ready(function() {
    var zoomed = false;
    var img = new Image();
    $('div.product-detail-img #controls').show();
    $('div.product-detail-img').carousel(500, '#prev', '#next', false);
    $('div.product-detail-img #zoomin').live('click', function(event) {
        event.preventDefault();
        if (!zoomed) {
            $('#overlay').show();
            zoomed = true;
            $(img).load(function() {
                $(this).hide();
                $('#overlay').append(this);
                $(this).fadeIn();
            }).attr('src', $('div.product-detail-img li.visible img').attr('name'));
        }
    });
    $('div.product-detail-img #zoomout').live('click', function(event) {
        event.preventDefault();
        if (zoomed) {
            $('#overlay').fadeOut(function() { $('#overlay img').hide(); });
            zoomed = false;
        }
    });
    $('div.product-detail-img #next, div.product-detail-img #prev').click(function() {
        return false;
        $('div.product-detail-img #zoomout').trigger('click');
	});
    $('div.product-detail-img #overlay').live('mousemove', function(e) {
        var offset = $(this).offset();
        var xpercent = Math.round((Math.round(e.pageX - offset.left) / $(this).width()) * 100);
        var ypercent = Math.round((Math.round(e.pageY - offset.top) / $(this).height()) * 100);
        var xpos = Math.round((($(this).children('img').width() - $(this).width()) / 100) * xpercent) * -1;
        var ypos = Math.round((($(this).children('img').height() - $(this).height()) / 100) * ypercent) * -1;
        $(this).children('img').css('left', xpos).css('top', ypos);
    });
	// Lookbook specific
	$('#TB_overlay, .TB_overlayBG').live('mousedown', function() { zoomed = false; });

    // Credit card movey
    var ccInput = $('.ccInput');
    $(ccInput).keyup(function() {
        var thisIndex = $(this).parent().find('.ccInput').index($(this));
        $(this).val().length == 4 ? $(this).parent().find('.ccInput').eq(thisIndex + 1).focus() : $(this);
    });

});

/****************************************************************************************	
	[JQUERY PLUG-INS]
*****************************************************************************************/
//jQuery Infinite Carousel - Copyright 2009 Stéphane Roucheray (CUSTOMISED FOR MARCS)
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;for(i=0;i<shownInViewport;i++){jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment+increment+"px");jQuery(sliderList).append(jQuery(elmnts[i]).clone());} function setVisible(){var pos=jQuery(sliderList).position();var found=Math.round((pos.left*-1)/sizeFirstElmnt);jQuery(sliderList).children('.visible').removeClass('visible');jQuery(sliderList).children().eq(found).addClass('visible');};setVisible();jQuery(previous).click(function(){if(!isAnimating){if(firstElementOnViewPort==1){jQuery(sliderList).css('left',"-"+numElmts*sizeFirstElmnt+"px");firstElementOnViewPort=numElmts;}
else{firstElementOnViewPort--;} jQuery(sliderList).animate({left:"+="+increment,y:0,queue:true},speed,"swing",function(){isAnimating=false;setVisible();});isAnimating=true;}});jQuery(next).click(function(){if(!isAnimating){if(firstElementOnViewPort>numElmts){firstElementOnViewPort=2;jQuery(sliderList).css('left',"0px");} else{firstElementOnViewPort++;} jQuery(sliderList).animate({left:"-="+increment,y:0,queue:true},speed,"swing",function(){isAnimating=false;setVisible();});isAnimating=true;}});if(autoscroll){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);});}}};
