var currentValue = 0;

function setHomePageBannerOpacity(id, sValue, eValue, mSec) {
    var speed = Math.round(mSec / 100), timer = 0;
    if (sValue > eValue) {
        for (var i = sValue; i >= eValue; i--) {
            currentValue = timer * speed;
            setTimeout("doFading('" + id + "', " + i + ")", currentValue);
            timer++;
        }
    }
    else if (sValue < eValue) {
        for (var i = sValue; i <= eValue; i++) {
            currentValue = timer * speed;
            setTimeout("doFading('" + id + "', " + i + ")", currentValue);
            timer++;
            if (sValue == 20 && currentValue >= 100) {
                var banner = $get('<%= imgBanner.ClientID %>');
                if (banner) banner.style.display = 'none';
            }
        }
    }
}

function doFading(id, opacity) {
    var element = $get(id);

    if (element) {
        var _style = element.style;
        _style.opacity = (opacity / 100);
        _style.MozOpacity = (opacity / 100);
        _style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacity + ")";
        _style.KhtmlOpacity = (opacity / 100);
    }
}


function showImage(normal, zoomed, rel, name) {
    var Img = document.getElementById("PreviewImage");
    var Lnk = document.getElementById("zoomLink");
    var Lnk2 = document.getElementById("imageLink");
    var IsValid = Lnk && Img && Lnk2;

    try {
        //image.src.replace(/medium/gi, "large");

        Lnk.style.visibility = IsValid && zoomed != "" ? "visible" : "hidden";
        Lnk.setAttribute("rel", rel);
        Lnk.href = IsValid ? zoomed : "javascript:void(0)";
        Lnk2.setAttribute("rel", rel);
        Lnk2.href = IsValid ? zoomed : "javascript:void(0)";
        Img.src = IsValid ? normal : "/gfx/pictures/noimage.gif";
        Img.setAttribute("alt", unescape(name));
    }
    catch (e) {

    }
}

function RenderCount() {
    var div = document.getElementById("Counter");
    var nslide = document.getElementById("NextSlide");

    if (div) {
        div.innerHTML = jQuery(".item").length;
    }

    if (nslide) {
        nslide.style.display = jQuery(".item").length <= 2 ? "none" : "";
    }
}

function xget(id) {
	if(document.getElementById) return document.getElementById(id);
	if(document.all) return document.all[id];
	return null;
}

function clientActiveTabChanged(sender, args) {
	if(sender.get_id()=='ctl00_MainContent_ImageVideoViewer1_TabContainer2_tp360')
	{
		openPreview2('/Gfx/view360/'+xget('url360').value, 600, 800);
	}
    var Div1 = document.getElementById("slideshowcontainer1");
    var Div2 = document.getElementById("slideshowcontainer2");
    if (Div1 && Div2) {
        Div1.style.display = sender.get_activeTabIndex() == 0 ? "" : "none";
        Div2.style.display = Div1.style.display;
    }
}

function bookmarkPage(title, url) {
    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
    else if (document.all) {// ie
        window.external.AddFavorite(url, title);
    }
    else {
        alert('In order to bookmark this site you need to do so manually ' +
        'through your browser.');
    }
}

function createPrinatableAreas(w, h) {
    createContainer();
    var e = document.getElementsByTagName("div");
    var content = "", container = null;
    for (var i = 0; i < e.length; i++) {
        if (e[i].getAttribute("printable") == "Print") {
            container = document.getElementById("PrintableAreasContainer");
            if (container) {
                if (e[i].id == "tab_overview") {
                    var spans = e[i].getElementsByTagName("span");
                    for (var j = 0; j < spans.length; j++) {
                        if (spans[j].parentNode && spans[j].parentNode.className == "td100") {
                            spans[j].style.fontWeight = "bold";
                        }
                    }
                }

                var html = e[i].innerHTML;

                if (e[i].id != "tab_image" &&
                e[i].id != "tab_title" &&
                e[i].id != "tab_specs" &&
                e[i].id != "tab_features" &&
                e[i].id != "tab_overview" &&
                e[i].id != "tab_access") {
                    html = e[i].innerHTML.replace(/(<{1}.+?>{1}){1}/gi, '');
                }
                if (e[i].id == "tab_overview") {
                    if (!e[i].firstChild || (e[i].firstChild && e[i].firstChild.id != "OverviewBlock")) {
                        html = "<div id='OverviewBlock'>" + html + "</div><div class='LineContainer'></div>";
                    }
                }
                if (e[i].id == "tab_features") {
                    if (!e[i].firstChild || (e[i].firstChild && e[i].firstChild.id != "FeaturesBlock")) {
                        html = "<div id='FeaturesBlock'><div id='FeaturesTitle'>Features</div>" + html + "</div>";
                    }
                }
                if (e[i].id == "tab_specs") {
                    if (!e[i].firstChild || (e[i].firstChild && e[i].firstChild.id != "SpecsBlock")) {
                        html = "<div id='SpecsBlock'><div id='SpecsTitle'>Specifications</div>" + html + "</div>";
                    }
                }
                if (e[i].id == "tab_access") {
                    if (!e[i].firstChild || (e[i].firstChild && e[i].firstChild.id != "AccessBlock")) {
                        html = "<div id='AccessBlock'><div id='AccessTitle'>Accessories</div>" + html + "</div>";
                    }
                }
                container.innerHTML = "<div class='ContentArea'>" + html + "</div>";
                content = content + container.innerHTML;
            }
        }
    }
    openPreview(content, w, h);
    if (container) container.innerHTML = "";
}

function openPreview(content, w, h) {
    var l = (screen.width - w) / 2;
    var t = (screen.height - h) / 2;
    PreviewPage = window.open('', 'PreviewPage', 'width=' + w + ',height=' + h + ',top=' + t + ',left=' + l + ',menubar=0,toolbar=0,status=0,scrollbars=1,resizable=0');
    PreviewPage.document.open("text/html", "replace");
    PreviewPage.document.writeln('<html><head><title>Print Preview</title>'
    + '<link href="/css/products.css" media="screen" rel="stylesheet" type="text/css" /><link href="/css/PreviewStyle.css" media="screen" rel="stylesheet" type="text/css" /><link href="/css/PrintStyle.css" media="print" rel="stylesheet" type="text/css" />'
    + '</head><body align="left" onload="self.focus()"><div style="width:100%;text-align:right;"><img align="right" id="PrintButton" src="/Images/print.png" onclick="javascript:window.print();window.close();" /></div>'
    + content + '</body></html>');
    PreviewPage.document.close();
}

function openPreview2(content, w, h) {
    var l = (screen.width - w) / 2;
    var t = (screen.height - h) / 2;
//    PreviewPage2 = window.open(content, 'PreviewPage2', 'width=' + w + ',height=' + h + ',top=' + t + ',left=' + l + ',menubar=0,toolbar=0,status=0,scrollbars=1,resizable=0');
}

function createContainer() {
    var vc = document.createElement("div");
    vc.id = "PrintableAreasContainer";
    vc.style.width = "600px";
    vc.style.height = "200px";
    document.body.appendChild(vc);
}

function excludeTags(textControlID) {
    var result = "";
    var re = new RegExp("<([^!>]([^>]|\\n)*)>", "g");

    var control = $get(textControlID);
    if (control) {
        control.value = control.value.replace(re, "");
        result = control.value;
    }

    return result;
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
        num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

function modifyCart(self, priceId, itemPriceId, subTotalId, containerId, buttonId) {
    if (priceId != "" && itemPriceId != "" && subTotalId != "") {
        var priceControl = $get(priceId);
        var itemPriceControl = $get(itemPriceId);
        var subTotalControl = $get(subTotalId);
        var containerControl = $get(containerId);

        if (priceControl && itemPriceControl &&
                subTotalControl && containerControl) {

            var Index = self.selectedIndex;
            var PriceValue = parseFloat(priceControl.innerHTML);
            var ItemPriceValue = parseFloat(itemPriceControl.value);
            var Lists = containerControl.getElementsByTagName("SELECT");
            var price = parseFloat(Index * PriceValue);

            itemPriceControl.innerHTML = Index == 0 ? "" : formatCurrency(price);

            var subtotal = 0;

            for (var i = 0; i < Lists.length; i++) {
                var list = Lists[i];
                if (list.value != 0) {
                    subtotal += parseFloat(list.selectedIndex *
                        parseFloat($get(list.getAttribute("PriceControl")).innerHTML));
                }
            }

            subTotalControl.innerHTML = formatCurrency(subtotal);
        }
    }
}

function trim(str) {
    var str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
    while (ws.test(str.charAt(--i)));
    return str.slice(0, i + 1);
}


function NotifyCart(controlId) {

    var control = $get(controlId);
    if (control && control.innerHTML == "$0.00") {
        return false;
    }

    var textControl = $get("ResultId");
    if (textControl) {
        textControl.innerHTML = "";
    }
}


function divclose() {
    var control = $get("dshow");
    if (control) {
        control.style.display='none';
    }
}

