
(function ($) { var c = /:(nth)-last-child(?:\((even|odd|[\dn+-]*)\))?/, a = $.expr, b = a.filter.CHILD; a[":"]["nth-last-child"] = function (h, g, e, k) { var j = e[0].match(c), f = $(h.parentNode).children(), d; j = a.preFilter.CHILD(j); b(h, j); d = f.eq(f.length - h.nodeIndex)[0]; return b(d, j) } })(jQuery);

$(document).ready(function () {

    $(".setskus div:nth-child(3n+3)").addClass("threenthree");
    $("#container .layout_default ol.breadcrumb li:last-child").addClass("lastchild");
    $("#container .layout_default ol.breadcrumb li:nth-last-child(2)").addClass("nthlastchildtwo");



    $("body.collectionaspx div.figure_list figure a").fancybox({ 'width': '902' });


    $("#random-quote").load("/AJAX/RandomQuote.aspx?r=" + Math.random());

    BindThickBox();
    BindProductDetailsSubImage();
    BindMouseOverEditors();


    //Show shopping spree popin if variable 'ss' is set.
    if (getQS('ss') != "") {
        showShoppingSpree();
    }

});

function showWishList() {
    showPopin("/WishList.aspx?layout=AJAX&r=" + Math.random(), 649, 651, "wishlist");
}

function showSpecialOffers() {
    showPopin("/SpecialOffers.aspx?layout=AJAX&r=" + Math.random(), 649, 500, "special_offers");
}

function showShoppingSpree() {
    showPopin("/ShoppingSpree.aspx?layout=AJAX&r=" + Math.random(), 649, 500, "shopping_spree");
}

function addToWishList(sku, quantity, category) {
    try { pageTracker._trackPageview("/AddToWishList/" + sku); } catch (e) { }
    var ajaxURL = "/AJAX/WishList.aspx?method=ADD&sku=" + sku + "&quantity=" + quantity + "&category=" + category + "&r=" + Math.random();

    $.ajax({
        url: ajaxURL,
        success: function (data, status, request) { showWishList(); },
        error: function (request, status, error) { window.open(ajaxURL, "_blank"); }


    });

}

function updateWishList(sku, quantity, category) {
    try { pageTracker._trackPageview("/UpdateWishList/" + sku); } catch (e) { }
    var ajaxURL = "/AJAX/WishList.aspx?method=UPDATE&sku=" + sku + "&quantity=" + quantity + "&category=" + category + "&r=" + Math.random();

    $.ajax({
        url: ajaxURL,
        error: function (request, status, error) { alert("There was an error saving the quantity."); }


    });

}


function showSendProductViaEmail(sku, isset) {
    showPopin("/SendProductViaEmail.aspx?sku=" + sku + "&isset=" + isset + "&layout=AJAX&r=" + Math.random(), 649, 651, "product_details");
}

function showRequestPricing(sku, isset) {
    showPopin("/RequestPricing.aspx?sku=" + sku + "&isset=" + isset + "&layout=AJAX&r=" + Math.random(), 649, 630, "product_details");
}

document.onkeyup = function (e) {
    if (e == null) { // ie
        keycode = event.keyCode;
    } else { // mozilla
        keycode = e.which;
    }
    if (keycode == 27) { // close
        try {
            window.parent.hidePopin('product_details');
            hidePopin('product_details');
        } catch (err) { }
    }
};


function BindMouseOverEditors() {

    $(".FCKData").each(function () {

        if ($(this).attr("BindMouseOverEditors_effect_loaded") == "true") return;
        $(this).attr("BindMouseOverEditors_effect_loaded", "true");
        $(this).hover(function () {
            $(this).children(".FCKEditorButtons").fadeIn(300);
        }, function () {
            $(this).children(".FCKEditorButtons").fadeOut(300);
        });
    });
}

function BindProductDetailsSubImage() {

    $(".pd_sub_img").each(function () {

        if ($(this).attr("pd_sub_img_effect_loaded") == "true") return;
        $(this).attr("pd_sub_img_effect_loaded", "true");
        $(this).bind("click", function () {

            var newSrc = $(this).attr("href");
            var oldSrc = $("#pd_medium_img").attr("src");
            var oldRel = $("#pd_medium_img").attr("rel");
            var newRel = $(this).children("img:first").attr("src");
            var srcImg = $(this).children("img:first");
            var target = $(this);
            $(srcImg).fadeOut(300);
            $("#pd_medium_img").fadeOut(300, function () {
                $("#pd_medium_img").attr("src", newSrc);
                $(target).attr("href", oldSrc);
                $(srcImg).attr("src", oldRel);
                $("#pd_medium_img").attr("rel", newRel);
                $(srcImg).fadeIn(300);
                $("#pd_medium_img").fadeIn(300);
            });
            return false;
        });
    });
}



function BindThickBox() {
    $(".thickbox").each(function () {
        if ($(this).attr("thickbox_effect_loaded") == "true") return;
        $(this).attr("thickbox_effect_loaded", "true");
        $(this).bind("click", function () {
            showPopin($(this).attr("href"), 649, 849, "product_details");
            return false;
        });
    });
}

function hideShowAdvancedSearch() {
    if ($('#advanced_search_box').is(":visible")) {
        $('#advanced_search_box').slideUp();
    } else {
        $('#advanced_search_box').slideDown();
    }
}


//Get querystring function
function getQS(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
