﻿jQuery(function () {

    if (jQuery("body.productPage").length > 0) {
        setupProductPage();
    }
});

function setupProductPage() {

    // Slider
    var objSlider1 = jQuery("#productSlider1");
    var objSlider1Items = objSlider1.find("li");
    if (objSlider1Items.length > 0) {

        objSlider1Items.wrapInner("<div class='wrap'/>");

        hLib.enableSlider(objSlider1, {
            animationTime: 700,
            visibleItems: 3,
            itemHeight: 149,
            itemWidth: 137,
            direction: "vertical"
        });
    }
    else objSlider1.css("visibility", "hidden");

    // 360 degree display
    var objProductMedia = jQuery(".productContent .productImage");
    var obj360 = objProductMedia.find(".imageOptions a.rotate");
    var obj360Info = objProductMedia.find(".flashInfo");
    var objImgInfo = objProductMedia.find(".imageOptions");
    var objImagePlaceholder = objProductMedia.find(".imagePlaceholder");

    // Internal path
    if (obj360Info.find(".localPath").length == 1) {

        // Display flash on page
        obj360.click(function () {
            var htmFlash = jQuery('<div class="flash"><object width="323" height="470"><param name="360" value="' + obj360Info.find(".localPath").html() + '"><embed src="' + obj360Info.find(".localPath").html() + '" width="323" height="470"></embed></object></div>');
            objImagePlaceholder.addClass("displayFlash").append(htmFlash);
            objImgInfo.addClass("flashVisible");
        });

        // Enable auto hide of flash when click on slider images
        jQuery("#productSlider1 li a").click(function () { closeFlashView(); });
    }
    // External path
    else if (obj360Info.find(".externalPath").length == 1) {
        var objIframe = jQuery(obj360Info.find(".externalPath").text().replace("&gt;", ">").replace("&lt;", "<"));
        
        var strSrc = objIframe.attr("src");
        var intIframeWidth = parseInt(objIframe.attr("width"));
        var intIframeHeight = parseInt(objIframe.attr("height"));

        obj360.fancybox({
            'type': 'iframe',
            'transitionIn': 'none',
            'transitionOut': 'none',
            'autoScale': false,
            'width': intIframeWidth,
            'height': intIframeHeight,
            'content': strSrc,
            'onComplete': function () {
                jQuery("a#fancybox-close").css({ "top": "0", "right": "2px", "height": "19px", "width": "18px", "background-color": "#fff" });
            },
            'onClosed': function () {
                jQuery("a#fancybox-close").removeAttr("css");
            }
        });
    }
    // No 360 is available
    else obj360.remove();
    
    // Setting correct width for product colors container
    var objColors = jQuery(".productColors .productColorsInner");
    objColors.find("ul").width((objColors.find("ul li").length * objColors.find("ul li:eq(0)").outerWidth(true)) + 5);

    // Switch between tab content
    jQuery("div.productInfo div.description div.tabs a").click(function () {

        var objLink = jQuery(this);
        var objParent = objLink.parent();
        var intIndex = objParent.find("a").index(objLink);
        objParent.find("a").removeClass("selected");
        objLink.addClass("selected");

        var objTab1 = objParent.siblings("div.tab1");
        var objTab2 = objParent.siblings("div.tab2");

        if (intIndex == 0) {
            objTab2.hide();
            objTab1.show();
        }
        else if (intIndex == 1) {
            objTab1.hide();
            objTab2.show();
        }
        return false;
    });
}

function closeFlashView() {
    var objProductMedia = jQuery(".productContent .productImage");
    objProductMedia.find(".flash").hide().remove();
    objProductMedia.find(".imagePlaceholder").removeClass("displayFlash");
    objProductMedia.find(".imageOptions").removeClass("flashVisible");
}

function UpdatePriceProductPage(ProductSizeID) {
    if (!top.processingUpdate) {
        top.processingUpdate = true;

        jQuery.post(window.url_UpdatePriceOnProductPage, { ProductSizeID: ProductSizeID}, function (data) { HandleUpdatePriceOnProduct(data) }, 'json');
    }
}

function HandleUpdatePriceOnProduct(data) {

    var productItem = jQuery(".productInfo");

    jQuery(productItem).find(".price").html(data.Price);

    top.processingUpdate = false;

}

function getActiveProductImg() {
    return jQuery("#productSlider1 li.selected input").val();
}
