
// jStepper 1.2

// A jQuery plugin by EmKay usable for making a numeric textfield value easy to increase or decrease.

function AddOrSubtractTwoFloats(fltValue1, fltValue2, bAddSubtract) {

	var strNumber1 = fltValue1.toString();
	var strNumber2 = fltValue2.toString();

	var strResult = "";

	if (strNumber1.indexOf(".") > -1 || strNumber2.indexOf(".") > -1) {

		// If no decimals on one of them, then put them on!
		if (strNumber1.indexOf(".") == -1) {
			strNumber1 = strNumber1 + ".0";
		}

		if (strNumber2.indexOf(".") == -1) {
			strNumber2 = strNumber2 + ".0";
		}

		// Get only decimals
		var strDecimals1 = strNumber1.substr(strNumber1.indexOf(".") + 1);
		var strDecimals2 = strNumber2.substr(strNumber2.indexOf(".") + 1);

		// Getting the integers...
		var strInteger1 = strNumber1.substr(0, strNumber1.indexOf("."));
		var strInteger2 = strNumber2.substr(0, strNumber2.indexOf("."));

		//Make sure that the two decimals are same length (ie .02 vs .001) and append zeros as necessary.
		var bNotSameLength = true;

		while (bNotSameLength) {

			if (strDecimals1.length != strDecimals2.length) {
				if (strDecimals1.length < strDecimals2.length) {
					strDecimals1 += "0";
				} else {
					strDecimals2 += "0";
				}
			} else {
				bNotSameLength = false;
			}
		}

		var intOriginalDecimalLength = strDecimals1.length;

		for (var intCharIndex = 0; intCharIndex <= strDecimals1.length - 1; intCharIndex++) {
			strInteger1 = strInteger1 + strDecimals1.substr(intCharIndex, 1);
			strInteger2 = strInteger2 + strDecimals2.substr(intCharIndex, 1);
		}

		var intInteger1 = Number(strInteger1);
		var intInteger2 = Number(strInteger2);
		var intResult;

		if (bAddSubtract) {
			intResult = intInteger1 + intInteger2;
		} else {
			intResult = intInteger1 - intInteger2;
		}

		strResult = intResult.toString();

		for (var intZerosAdded = 0; intZerosAdded < ((intOriginalDecimalLength - strResult.length) + 1); intZerosAdded++) {
			strResult = "0" + strResult;
		}

		if (strResult.length >= intOriginalDecimalLength) {
			strResult = strResult.substring(0, strResult.length - intOriginalDecimalLength) + "." + strResult.substring(strResult.length - intOriginalDecimalLength);
		}
		
	} else {
		if (bAddSubtract) {
			strResult = Number(fltValue1) + Number(fltValue2);
		} else {
			strResult = Number(fltValue1) - Number(fltValue2);
		}
		
	}

	return Number(strResult);
}

(function(jQuery) {

	jQuery.fn.jStepper = function(options) {

		var opts = jQuery.extend({}, jQuery.fn.jStepper.defaults, options);

		return this.each(function() {
			var $this = jQuery(this);

			var o = jQuery.meta ? jQuery.extend({}, opts, $this.data()) : opts;

			if (o.disableAutocomplete) {
				$this.attr("autocomplete", "off");
			}

			if (jQuery.isFunction($this.mousewheel)) {
				$this.mousewheel(function(objEvent, intDelta){
					if (intDelta > 0){ // Up
						MakeStep(o, 1, null, this);
						return false;
					}
					else if (intDelta < 0){ // Down
						MakeStep(o, 0, null, this);
						return false;
					}
				});
			}

			$this.keydown(function(e){
				var key = e.keyCode;

				if (key == 38){ // Up
					MakeStep(o, 1, e, this);
				}

				if (key == 40){ // Down
					MakeStep(o, 0, e, this);
				}

			});
			
			$this.keyup(function(e){

				CheckValue(o, this);

			});

		});
	};

	function CheckValue(o, objElm) {
	
		var $objElm = jQuery(objElm);
	
		var strValue = $objElm.val();
	
		if (o.disableNonNumeric) {
			strValue = strValue.replace(/[^\d\.,\-]/gi,"");
		}
		
		if (o.maxValue !== null) {
			if (strValue >= o.maxValue) {
				strValue = o.maxValue;
			}
		}
		
		if (o.minValue !== null) {
			if (strValue <= o.minValue && strValue != "") {
				strValue = o.minValue;
			}
		}
		
		$objElm.val(strValue);
	
	}

	function MakeStep(o, bDirection, keydown, objElm) {
	
		var $objElm = jQuery(objElm);
		
		var stepToUse;
		if(keydown) {

			if (keydown.ctrlKey) {
				stepToUse = o.ctrlStep;
			} else if (keydown.shiftKey) {
				stepToUse = o.shiftStep;
			} else {
				stepToUse = o.normalStep;
			}

		} else {
			stepToUse = o.normalStep;
		}

		var numValue = $objElm.val();
		
		var intSelectionStart = numValue.length - objElm.selectionStart;
		var intSelectionEnd = numValue.length - objElm.selectionEnd;

		numValue = numValue.replace(/,/g,".");
		numValue = numValue.replace(o.decimalSeparator,".");
		
		numValue = numValue + '';
		if (numValue.indexOf(".") != -1) {
			numValue = numValue.match(new RegExp("-{0,1}[0-9]+[\\.][0-9]*"));
		}

		numValue = numValue + '';
		if (numValue.indexOf("-") != -1) {
			numValue = numValue.match(new RegExp("-{0,1}[0-9]+[\\.]*[0-9]*"));
		}

		numValue = numValue + '';
		numValue = numValue.match(new RegExp("-{0,1}[0-9]+[\\.]*[0-9]*"));

		if (numValue === "" || numValue == "-" || numValue === null) {
			numValue = o.defaultValue;
		}


		if (bDirection == 1) {
			numValue = AddOrSubtractTwoFloats(numValue, stepToUse, true);
		} else {
			numValue = AddOrSubtractTwoFloats(numValue, stepToUse, false);
		}

		var bLimitReached = false;

		if (o.maxValue !== null) {
			if (numValue >= o.maxValue) {
			numValue = o.maxValue;
			bLimitReached = true;
			}
		}

		if (o.minValue !== null) {
			if (numValue <= o.minValue) {
			numValue = o.minValue;
			bLimitReached = true;
			}
		}

		numValue = numValue + '';

		if (o.minLength !== null) {
			var intLengthNow = numValue.length;
			if (numValue.indexOf(".") != -1) {
				intLengthNow = numValue.indexOf(".");
			}
			var bIsNegative = false;
			if (numValue.indexOf("-") != -1) {
				bIsNegative = true;
				numValue = numValue.replace(/-/,"");
			}
			
			if (intLengthNow < o.minLength) {
				for (var i=1;i<=(o.minLength - intLengthNow);i++) {
					numValue = '0' + numValue;
				}
			}
			
			if (bIsNegative) {
				numValue =  '-' + numValue;
			}
		
		}
		
		numValue = numValue + '';
		
		var intDecimalsNow;
		
		if (o.minDecimals > 0) {
			var intDecimalsMissing; 
			if (numValue.indexOf(".") != -1) {
				intDecimalsNow = numValue.length - (numValue.indexOf(".") + 1);
				if (intDecimalsNow < o.minDecimals) {
					intDecimalsMissing = o.minDecimals - intDecimalsNow;
				}
			}	else {
				intDecimalsMissing = o.minDecimals;
				numValue = numValue + '.';
			}
			for (var intDecimalIndex=1; intDecimalIndex<=intDecimalsMissing; intDecimalIndex++) {
				numValue = numValue + '0';
			}
		} 

		if (o.maxDecimals > 0) {
			intDecimalsNow = 0; 
			if (numValue.indexOf(".") != -1) {
				intDecimalsNow = numValue.length - (numValue.indexOf(".") + 1);
				if (o.maxDecimals < intDecimalsNow) {
					numValue = numValue.substring(0,numValue.indexOf(".")) + "." + numValue.substring(numValue.indexOf(".") + 1,numValue.indexOf(".") + 1 + o.maxDecimals);
				}
			}
		}
		
		if (!o.allowDecimals) {
			numValue = numValue + '';
			numValue = numValue.replace(new RegExp("[\\.].+"),"");
		}
		
		numValue = numValue.replace(/\./,o.decimalSeparator);

		$objElm.val(numValue);
		
		objElm.selectionStart = numValue.length - intSelectionStart;
		objElm.selectionEnd = numValue.length - intSelectionEnd;
		
		CheckValue(o, this);
		
		if (o.onStep) {
			o.onStep($objElm, bDirection, bLimitReached);
		}
		
		return false;
	
	}

	jQuery.fn.jStepper.defaults = {
		maxValue : null,
		minValue : null,
		normalStep : 1,
		shiftStep : 5,
		ctrlStep : 10,
		minLength : null,
		disableAutocomplete : true,
		defaultValue : 1,
		decimalSeparator : ",",
		allowDecimals : true,
		minDecimals : 0,
		maxDecimals : null,
		disableNonNumeric : true,
		onStep : null
	};

})(jQuery);


function showModalLoadingMessage(title) {
    $('#loadingtext').text(title);
    $('#loading').show();
}

function hideModalLoadingMessage() {
    $('#loading').fadeOut('slow');
}

function HandleDeleteCartItemButton(row) {
    row.find('input:first').val('0');
    row.fadeTo('slow', 0);
}


var clothesTimeout = null;
var designersTimeout = null;
var shoesTimeout = null;
var kidsTimeout = null;
var jewelryTimeout = null;

$(function () {

	$("<img>").attr("src", "/Content/Images/Backgrounds/Loading.gif");

    $('.nojavascript').remove();

    $('.requiresjavascript').show();

    $('.quantityinput').jStepper({ minValue: 1, maxValue: 6 });

    $('<div class="loading" id="loading"><div id="loadingbar"></div><div id="loadingtext"></div></div>').appendTo('body');

    $('.photozoom').fancybox();

    $('#sizingguide').fancybox();

    $('.galleryimage').fancybox({
        'cyclic': true
    });

    $('.enlargebutton').click(function (e) {
        $('.photozoom').trigger('click');
        return false;
    });

    if ($('#shippingAddressSame').attr('checked')) {
        $('#ShippingAddressDiv').before("<div class='note' id='ShippingAddressSameLabel'>Same as billing address</div>");
        $('#ShippingAddressDiv').hide();
    }

    $("#shippingAddressSame, #shippingAddressDifferent").click(function () {

        if ($("#ShippingAddressSameLabel").length == 0) {
            $('#ShippingAddressDiv').before("<div class='note' id='ShippingAddressSameLabel'>Same as billing address</div>");
        }
        if ($('#shippingAddressSame').attr('checked')) {
            $("#ShippingAddressSameLabel").stop(true, true).slideDown();
            $('#ShippingAddressDiv').stop(true, true).fadeOut(1000);
        } else {
            $("#ShippingAddressSameLabel").stop(true, true).slideUp();
            $('#ShippingAddressDiv').stop(true, true).fadeIn(1000);
        }
    });

    $('form').inputHintOverlay();

    $('#clothes-link').mouseenter(function () {
        $('#clothes-link').addClass('active');
        $('#clothes-link').parent().addClass('active');
        $('#clothes-hover').show();
    });

    $('#clothes-link').mouseleave(function () {
        clothesTimeout = setTimeout(function () { $('#clothes-hover').hide(); $('#clothes-link').removeClass('active'); $('#clothes-link').parent().removeClass('active'); }, 100);
    });

    $('#clothes-hover').mouseenter(function () {
        if (clothesTimeout != null) {
            clearTimeout(clothesTimeout);
            clothesTimeout = null;
        }
    });

    $('#clothes-hover').mouseleave(function () {
        $('#clothes-link').removeClass('active');
        $('#clothes-link').parent().removeClass('active');
        $('#clothes-hover').hide();
    });


    $('#designers-link').mouseenter(function () {
        $('#designers-link').addClass('active');
        $('#designers-link').parent().addClass('active');
        $('#designers-hover').show();
    });

    $('#designers-link').mouseleave(function () {
        designersTimeout = setTimeout(function () { $('#designers-hover').hide(); $('#designers-link').removeClass('active'); $('#designers-link').parent().removeClass('active'); }, 100);
    });

    $('#designers-hover').mouseenter(function () {
        if (designersTimeout != null) {
            clearTimeout(designersTimeout);
            designersTimeout = null;
        }
    });

    $('#designers-hover').mouseleave(function () {
        $('#designers-link').removeClass('active');
        $('#designers-link').parent().removeClass('active');
        $('#designers-hover').hide();
    });


    $('#shoes-link').mouseenter(function () {
        $('#shoes-link').addClass('active');
        $('#shoes-link').parent().addClass('active');
        $('#shoes-hover').show();
    });

    $('#shoes-link').mouseleave(function () {
        shoesTimeout = setTimeout(function () { $('#shoes-hover').hide(); $('#shoes-link').removeClass('active'); $('#shoes-link').parent().removeClass('active'); }, 100);
    });

    $('#shoes-hover').mouseenter(function () {
        if (shoesTimeout != null) {
            clearTimeout(shoesTimeout);
            shoesTimeout = null;
        }
    });

    $('#shoes-hover').mouseleave(function () {
        $('#shoes-link').removeClass('active');
        $('#shoes-link').parent().removeClass('active');
        $('#shoes-hover').hide();
    });


    $('#kids-link').mouseenter(function () {
        $('#kids-link').addClass('active');
        $('#kids-link').parent().addClass('active');
        $('#kids-hover').show();
    });

    $('#kids-link').mouseleave(function () {
        kidsTimeout = setTimeout(function () { $('#kids-hover').hide(); $('#kids-link').removeClass('active'); $('#kids-link').parent().removeClass('active'); }, 100);
    });

    $('#kids-hover').mouseenter(function () {
        if (kidsTimeout != null) {
            clearTimeout(kidsTimeout);
            kidsTimeout = null;
        }
    });

    $('#kids-hover').mouseleave(function () {
        $('#kids-link').removeClass('active');
        $('#kids-link').parent().removeClass('active');
        $('#kids-hover').hide();
    });

    $('#jewelry-link').mouseenter(function () {
        $('#jewelry-link').addClass('active');
        $('#jewelry-link').parent().addClass('active');
        $('#jewelry-hover').show();
    });

    $('#jewelry-link').mouseleave(function () {
        jewelryTimeout = setTimeout(function () { $('#jewelry-hover').hide(); $('#jewelry-link').removeClass('active'); $('#jewelry-link').parent().removeClass('active'); }, 100);
    });

    $('#jewelry-hover').mouseenter(function () {
        if (jewelryTimeout != null) {
            clearTimeout(jewelryTimeout);
            jewelryTimeout = null;
        }
    });

    $('#jewelry-hover').mouseleave(function () {
        $('#jewelry-link').removeClass('active');
        $('#jewelry-link').parent().removeClass('active');
        $('#jewelry-hover').hide();
    });


    $('.navlist dd').has('ul').hide();
    $('.navlist dd').has('ul').has('a.active').show();
    $('.navlist dt').click(function () { $(this).next().slideToggle(); });


    $('.navlist dd li').has('ul').hide();
    $('.navlist dd li').has('ul').has('a.active').show();
    $('.navlist span').click(function () { $(this).parent().next().slideToggle(); });

    $('#minicart').hide();

    $('.shoppingbag').click(function () {

        if ($('#minicart').children().size() == 0) {
            showModalLoadingMessage("Loading Cart");

            $.ajax({
                url: '/Cart/MiniCart',
                success: function (data) {
                    $('#minicart').html(data);
                    $('#minicart').slideDown();
                    hideModalLoadingMessage();
                }
            });

            return false;
        }


        if ($('#minicart').is(':visible'))
            $('#minicart').slideUp();
        else
            $('#minicart').slideDown();

        return false;
    });

    $('html').removeClass('jsloading');

    $('.search-autocomplete').autocomplete({
        source: "/Search/Autocomplete",
        select: function (event, ui) {
            $(".search-autocomplete").val(ui.item.value);
            $(".search-autocomplete-form").submit();
        }
    });

    $('.mainsearch-autocomplete').autocomplete({
        source: "/Search/Autocomplete",
        select: function (event, ui) {
            $(".mainsearch-autocomplete").val(ui.item.value);
            $(".mainsearch-autocomplete-form").submit();
        }
    });




});




