﻿//Minifiy with http://fmarcia.info/jsmin/test.html

function PGVInitialize(PGVArray, closeButtonUrl, productTitle)
{
    PGVUpdateImages(PGVArray);

    $("#PGVImageInfo_Label").html(productTitle);
    
    $("#PGVImageInfo_ZoomLink").lightBox({
        imageLoading: Agility.ResolveUrl("~/img/Plugins/lightbox/lightbox-ico-loading.gif"),
        imageBtnClose: closeButtonUrl,
        imageBtnPrev: Agility.ResolveUrl("~/img/Plugins/lightbox/lightbox-btn-prev.gif"),
        imageBtnNext: Agility.ResolveUrl("~/img/Plugins/lightbox/lightbox-btn-next.gif"),
        imageBlank: Agility.ResolveUrl("~/img/Plugins/lightbox/lightbox-blank.gif")
    });

    $(".ProductLink").click(function () {
        var productID = parseInt($(this).attr("productid"), 10);
        PGVLoadProduct(productID);
        return false;
    });
}

function PGVLoadProduct(productID) {
    $.ajax({
        type: "POST",
        url: '/Services/ProductData.asmx/GetRelatedGraphicCardProduct',
        data: "{seriesID:" + Product_SeriesID + ",modelID:" + Product_ModelID + ",productID:" + productID + ",type:" + Product_ProductType + ",specificationDisplayListRefName:\"" + SPECS_specificationDisplayListRefName + "\", groupByCategory:" + SPECS_groupByCategory + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            if (!result || !result.d) return;

            PGVUpdateImages(result.d.GalleryImages || []);
            $("#PGVImageInfo_Label").html(result.d.Title);
            $("#ProductTab_1").html(result.d.Overview);
            $("#ProductTab_2").html(result.d.Features);
            var specsTab = $("#ProductTab_3");
            if (!specsTab.hasTemplate())
            {
                specsTab.setTemplateURL("/ClientTemplates/ModelSpecs.htm", null, {filter_data: false});
            }
            specsTab.processTemplate(result.d.SpecCategories);
            PGVFixSpecDisplay();

            var requirementsCategory = $("#RequirementsCategory"),
                requirementsValue = $("#RequirementsValue");
            if (result.d.Requirements) {
                requirementsValue.html(result.d.Requirements);
                requirementsCategory.show();
            } else {
                requirementsCategory.hide();
            }
            var productTitle = $("#ProductList").find("li[productid=" + productID + "]").text();
            if (productTitle) {
                $("#SelectedProductTitle").text(productTitle);
            }

            if (result.d.Reviews !== null) {
                var reviewsTab = $("#ProductTab_4");
                if (!reviewsTab.hasTemplate()) {
                    reviewsTab.setTemplateURL("/ClientTemplates/ProductReviews.htm", null, {filter_data: false});
                }
                reviewsTab.processTemplate(result.d.Reviews);
            }
        }
    });
}

function PGVFixSpecDisplay() {
    $(".SpecRow:not(.HardcodedRow)").each(function () {
        var row = $(this),
            text = row.find(".DisplayText"),
            value = row.find(".SpecValue"),
            textString;
        if (value.text() == "False" || !value.text()) {
            row.hide();
        } else if (value.text() == "True") {
            value.hide();
            text.removeClass("DisplayText").addClass("SpecValue").css("display", "inline");
            textString = text.text().replace(/^\W+/g, "").replace(/:\s*$/, "");
            text.text(textString);
        }
        $(".Category").each(function () {
            var category = $(this),
                rows = category.find(".SpecRow"),
                hasVisibleRow = false;
            rows.each(function () {
                if ($(this).css("display") != "none") {
                    hasVisibleRow = true;
                    return false;
                }
            });
            if (!hasVisibleRow) {
                category.hide();
            }
        });
    });
}

function PGVUpdateImages(PGVArray, productTitle)
{
    var thumbnailMainDiv = document.getElementById("PGVThumbnailArea");
    $(thumbnailMainDiv).empty();

    if(PGVArray.length>1)
    {
        for(var i=0; i<PGVArray.length; i++)
        {
            var thumbDiv = document.createElement("div");
            thumbDiv.className = "ThumbnailItem";
            thumbDiv.setAttribute("ItemIndex", i);
            thumbDiv.onclick = function(){ PGVItemClick(this, PGVArray); };
            
            var thumb = document.createElement("img");
            thumb.setAttribute("src", PGVArray[i].ThumbURL || PGVGetThumbUrl(PGVArray[i].URL));
            thumb.setAttribute("alt", PGVArray[i].Label);
            thumb.setAttribute("width", "60");
            thumb.setAttribute("height", "54");

            
            thumbDiv.appendChild(thumb);
            
            thumbnailMainDiv.appendChild(thumbDiv);
        }
    }

    var mainImageDiv = document.getElementById("PGVMainImageArea");
    $(mainImageDiv).empty();
    if (PGVArray.length > 0) 
    {
        var mainImage = document.createElement("img");   
    
        mainImage.setAttribute("src", PGVGetFeatureImageUrl(PGVArray[0].ImageURL || PGVArray[0].URL));
        mainImage.setAttribute("alt", PGVArray[0].Label);
        mainImage.setAttribute("height", "300");
        mainImage.setAttribute("width", "400");
    
        mainImageDiv.appendChild(mainImage);
        PGVUpdateImageControls(PGVArray[0]);
    }
    else {
        $("#PGVImageInfo").hide();
    }
}

function PGVGetThumbUrl(fullUrl) {
    // don't process if images are from connect
    if (fullUrl.indexOf("connect.xfxforce.com") != -1)
        return fullUrl;

    var ecmsIndex = fullUrl.indexOf("ecms.");
    if (ecmsIndex != -1) {
        fullUrl = fullUrl.substring(ecmsIndex + 9);
    }
    return "/thumb.ashx/" + fullUrl + ",60,54";
}

function PGVGetFeatureImageUrl(fullUrl) {
    // don't process if images are from connect
    if (fullUrl.indexOf("connect.xfxforce.com") != -1)
        return fullUrl;

    var ecmsIndex = fullUrl.indexOf("ecms.");
    if (ecmsIndex != -1) {
        fullUrl = fullUrl.substring(ecmsIndex + 9);
    }
    return "/thumb.ashx/" + fullUrl + ",400,300";
}

function PGVGetZoomImageUrl(fullUrl) {
    // don't process if images are from connect
    if (fullUrl.indexOf("connect.xfxforce.com") != -1)
        return fullUrl;

    var ecmsIndex = fullUrl.indexOf("ecms.");
    if (ecmsIndex != -1) {
        fullUrl = fullUrl.substring(ecmsIndex + 9);
    }
    return "/thumb.ashx/" + fullUrl + ",800,600";
}

function PGVItemClick(sender, array)
{
   
    var mainImageDiv = document.getElementById("PGVMainImageArea");
    
    var mainImage = document.createElement("img");   
    var index = sender.getAttribute("ItemIndex");
    
    if(mainImageDiv.childNodes[0]!=null)
    {
       
        mainImage.setAttribute("src", PGVGetFeatureImageUrl(array[index].ImageURL || array[index].URL));
        mainImage.setAttribute("alt", array[index].Label); 
        mainImage.setAttribute("height", "300");
        mainImage.setAttribute("width", "400");
        PGVClearDiv("PGVMainImageArea");
        mainImageDiv.appendChild(mainImage);
        PGVUpdateImageControls(array[index]);
    }   
}

function PGVClearDiv(divName)
{
    var divToClear = document.getElementById(divName);
    while(divToClear.hasChildNodes())
    {
        divToClear.removeChild(divToClear.lastChild);
    }
}

function PGVUpdateImageControls(image) {
    var imageUrl = (image.ImageURL || image.URL);
    if (imageUrl.indexOf("/") == 0) {
        imageUrl = location.protocol + "//" + location.host + imageUrl;
    }
    $("#PGVImageInfo_DownloadLink").attr("href", "/Handlers/DownloadImage.ashx?url=" + encodeURI(imageUrl));
    $("#PGVImageInfo_ZoomLink").attr("href", PGVGetZoomImageUrl(imageUrl));
    $("#PGVImageInfo_ZoomLink").attr("title", image.Label);
    $("#PGVImageInfo").show();
}
