﻿/// <reference path="~/js/jquery.js" />
$(document).ready(function() {
    //AddJavascriptToPage();
    AddValidationToInputs("validate_BarnBurner_EmailSubscribe");

    // Set up the larger image to go away when clicked
    $('#ViewLargerImage_TB_overlay').click(function() {
        $('#ViewLargerImage_TB_overlay').hide();
        $('#ViewLargerImage_TB_window').hide();
        return false;
    });
    $('#ViewLargerImage_TB_window').click(function() {
        $('#ViewLargerImage_TB_overlay').hide();
        $('#ViewLargerImage_TB_window').hide();
        return false;
    });

    $("textarea.myid_EmailFriend_Message").keyup(function() {
        LimitText("textarea.myid_EmailFriend_Message", "personalized_message_count", 300);
    });

    var VideoUrl = $("#VideoPopUp").attr("href");
    if (undefined !== VideoUrl && null !== VideoUrl && "" !== VideoUrl) {
        $("#divVideoSection").show();
        $("#VideoPopUp").click(function() {
            var win = window.open(VideoUrl, "PopupLarge", "scrollbars=yes,width=820,height=530,resizable=yes");
            win.focus();
            return false;
        });
    } else {
        $("#divVideoSection").hide();
    }
});

function ShowProductAltViewsTab() {
	SelectTab("ProductAltViewsTab");
	ShowTabDetails("ProductAltViews");
	DeselectTab("ProductColorsTab");
	HideTabDetails("ProductColors");
}
function ShowProductColorsTab() {
	SelectTab("ProductColorsTab");
	ShowTabDetails("ProductColors");
	DeselectTab("ProductAltViewsTab");
	HideTabDetails("ProductAltViews");
}
function ShowTabDetails(detailsName) {
    if (document.getElementById(detailsName)) {
        document.getElementById(detailsName).style.display = 'block';
	}
}
function HideTabDetails(detailsName) {
    if (document.getElementById(detailsName)) {
        document.getElementById(detailsName).style.display = 'none';
	}
}
function SelectTab(tabName) {
    if (document.getElementById(tabName)) {
        document.getElementById(tabName).className = 'active';
	}
}
function DeselectTab(tabName) {
    if (document.getElementById(tabName)) {
        document.getElementById(tabName).className = 'menu';
	}
}
function Redirect(newUrl) {
	self.setTimeout("self.location.href = '" + newUrl + "';", 0);
}

function ClearDropDownList(ddlToClear) {
    if (null == ddlToClear) { return; } // no list
    if (null == ddlToClear.options) { return; } // not a list
    for (var i = ddlToClear.options.length - 1; i >= 0; i--) {
        ddlToClear.options[i] = null;
    }
}

// Placeholder variables for lookup tables
var ColorCodeImageTable = {};
var ColorLookupTable = {};
var ColorNameTable = {};
var QuantityLookupTable = {};
function GetImageUrls(colorCode) {
    var ImageUrls = "";
    if (null != ColorCodeImageTable) {
        if (true == ColorCodeImageTable.hasOwnProperty(colorCode)) {
            ImageUrls = ColorCodeImageTable[colorCode];
        }
    }
    return ImageUrls;
}
function SetHiddenZoomVariable(colorCode) {
    var hdnCurrentBaseNumber = window.document.getElementById("hdnCurrentBaseNumber");
    var CurrentBaseNumber = hdnCurrentBaseNumber.value;
    if (null != CurrentBaseNumber && CurrentBaseNumber.length > 0) {
        var hdnZoomVariable = window.document.getElementById("hdnZoomVariable");
        if (null != hdnZoomVariable) {
            hdnZoomVariable.value = CurrentBaseNumber + "_" + colorCode;
        }
    }
}
function ViewSwatchImageBarnBurner(colorCode) {
    if ("-1" == colorCode) { return; }

    var urls = GetImageUrls(colorCode).split("|");
    if (2 != urls.length) { return; }
    
    var MediumImageUrl = urls[0];
    var FullSizeImageUrl = urls[1];
    SetSwatchImageUrls(MediumImageUrl, FullSizeImageUrl);
    SetHiddenZoomVariable(colorCode);
}
function SetSwatchImageUrls(mediumImageUrl, fullSizeImageUrl) {
    var imgProductImage = window.document.getElementById("imgProductImage");
	if (null != imgProductImage) {
	    imgProductImage.src = mediumImageUrl;
	}
	var imgLargerImagePopUp = window.document.getElementById("imgLargerImagePopUp");
	if (null != imgLargerImagePopUp) {
	    imgLargerImagePopUp.src = fullSizeImageUrl;
	}
}

function ClearColorList() {
    ClearDropDownList(document.getElementById("ddlColors"));
}
function RefillDefaultColorList(includeLabelItem) {
    ClearColorList();
    var ColorDropDownList = document.getElementById("ddlColors");
    if (null == ColorDropDownList) {
        return;
    }
    
    if (true == includeLabelItem) {
        ColorDropDownList.options[0] = new Option("Color", "-1", true);
    }
    for (var CurrentColorEntry in ColorNameTable) {
        if (true == ColorNameTable.hasOwnProperty(CurrentColorEntry)) {
            var CurrentColorCode = CurrentColorEntry;
            var CurrentColorName = ColorNameTable[CurrentColorCode];
            ColorDropDownList.options[ColorDropDownList.options.length] = new Option(CurrentColorName, CurrentColorCode);
        }
    }
}
function SetColorList(currentSize) {
    if ("-1" == currentSize) {
        RefillDefaultColorList(true);
        return;
    }
    var ColorDropDownList = document.getElementById("ddlColors");
    if (null == ColorDropDownList) {
        return;
    }
    var CurrentSelectedColor = ColorDropDownList.value;

    ClearColorList();
    var ColorsForCurrentSize = ColorLookupTable[currentSize];
    if (null == ColorsForCurrentSize) {
        return;
    }
    
    var CurrentColors = ColorsForCurrentSize.split("|");
    ColorDropDownList.options[0] = new Option("Color", "-1", true);
    for (var i = 0; i < CurrentColors.length; i++) {
        var CurrentColorCode = CurrentColors[i];
        var CurrentColorName = "";
        if (ColorNameTable.hasOwnProperty(CurrentColorCode)) {
            CurrentColorName = ColorNameTable[CurrentColorCode];
        }
        if (CurrentSelectedColor == CurrentColorCode) {
            ColorDropDownList.options[ColorDropDownList.options.length] = new Option(CurrentColorName, CurrentColorCode, true);
            //ColorDropDownList.options[0].selected = false;
        } else {
            ColorDropDownList.options[ColorDropDownList.options.length] = new Option(CurrentColorName, CurrentColorCode);
        }
    }
}

function DetermineCurrentSku() {
    var ColorDropDownList = document.getElementById("ddlColors");
    if (null == ColorDropDownList) {
        return "";
    }
    var CurrentSelectedColor = ColorDropDownList.value;
    if ("-1" == CurrentSelectedColor) {
        return "";
    }

    var SizeDropDownList = document.getElementById("ddlSizes");
    var CurrentSelectedSize = "";
    if (null != SizeDropDownList) {
        CurrentSelectedSize = SizeDropDownList.value;
    }
    if ("-1" == CurrentSelectedSize) {
        return "";
    }
    
    var hdnCurrentBaseNumber = document.getElementById("hdnCurrentBaseNumber");
    if (null == hdnCurrentBaseNumber) {
        return "";
    }
    var CurrentBaseNumber = hdnCurrentBaseNumber.value;
    var CurrentSku = CurrentBaseNumber;
    if ("" != CurrentSelectedColor) {
        CurrentSku += "-" + CurrentSelectedColor;
    }
    if ("" != CurrentSelectedSize) {
        CurrentSku += "-" + CurrentSelectedSize;
    }
    return CurrentSku;
}
function ClearQuantityList() {
    ClearDropDownList(document.getElementById("ddlQuantity"));
}
var MAX_QUANTITY = 5;
function RefillDefaultQuantityList(includeLabelItem, maxQuantity) {
    ClearQuantityList();
    var QuantityDropDownList = document.getElementById("ddlQuantity");
    if (null == QuantityDropDownList) {
        return;
    }
    
    if (true == includeLabelItem) {
        QuantityDropDownList.options[0] = new Option("Quantity", "-1", true);
    }
    for (var i = 1; i <= maxQuantity; i++) {
        QuantityDropDownList.options[i] = new Option(i, i);
    }
}
function SetQuantityList() {
    var CurrentSku = DetermineCurrentSku();
    if ("" == CurrentSku) {
        RefillDefaultQuantityList(true, MAX_QUANTITY);
        return;
    } else {
        var QuantityForCurrentSku = MAX_QUANTITY;
        if (QuantityLookupTable.hasOwnProperty(CurrentSku)) {
            QuantityForCurrentSku = QuantityLookupTable[CurrentSku];
            if (QuantityForCurrentSku > MAX_QUANTITY) {
                QuantityForCurrentSku = MAX_QUANTITY;
            }
        }
        RefillDefaultQuantityList(true, QuantityForCurrentSku);
    }
}

function LimitText(limitFieldMyId, limitCountId, limitNum) {
	if ($(limitFieldMyId).val().length > limitNum) {
		$(limitFieldMyId).val($(limitFieldMyId).val().substring(0, limitNum));
	}
    $("#" + limitCountId).html(limitNum - $(limitFieldMyId).val().length + "");
}
