﻿var sCurrentProduct = "";
//var bPageNotLoaded = true;

function mp_onload() {
    if (window.body_onload != null)
        window.body_onload();
}

function mp_onunload() {
    if (window.body_onunload != null)
        window.body_onunload();
}

function mouseOverOut(obj, img) {
    var sEventButName = img.substring(0, img.indexOf('_'));
    if (sCurrentProduct != sEventButName) {
        obj.src = "Images/" + img + ".png";
    }
}

function SetCssClass(className, keyEntry, value)
{
    /*
    className = ".className"/"#className"/etc.
    keyEntry = "cssParam"
    value = "cssParamValue"
    */    

    var bAdded = false;
    var sCssRules;

    for (var iS = 0; iS < document.styleSheets.length; iS++) {

        if (document.styleSheets[iS]['rules']) {
            sCssRules = 'rules';
        } else if (document.styleSheets[iS]['cssRules']) {
            sCssRules = 'cssRules';
        } else {
            //ToDo: add another browser compatibilty
        }

        for (var iRule = 0; iRule < document.styleSheets[iS][sCssRules].length; iRule++) {
            if (document.styleSheets[iS][sCssRules][iRule].selectorText == className) {
                if (document.styleSheets[iS][sCssRules][iRule].style[keyEntry]) {
                    document.styleSheets[iS][sCssRules][iRule].style[keyEntry] = value;
                    bAdded = true;
                    break;
                }
            }
        }
        if (!bAdded) {
            if (document.styleSheets[iS].insertRule) {
                document.styleSheets[iS].insertRule(className + ' { ' + keyEntry + ': ' + value + '; }', document.styleSheets[iS][sCssRules].length);
            } else if (document.styleSheets[iS].addRule) {
                document.styleSheets[iS].addRule(className, keyEntry + ': ' + value + ';');
            }
        }
    }
}

function setMenuWidth(iWidth)
{
    SetCssClass(".menu", "width", iWidth.toString()+"px");
}

var FadeOutStartValue = 99;
var FadeInStartValue = 1;
var timeOut;
var interval;

function CreateImagesWithFadeIn() {
    DivImagesContainer = document.getElementById("ImagesContainer");
    ProdImage[0] = document.getElementById('ProdImage0');
    document.getElementById("LoadingImage").style.visibility = "hidden";
    FadeInOut(FadeInStartValue, null, ProdImage[0], null);
}

function FadeInOut(FadeInValue, FadeOutValue, CurImage, PrevImage) {
    CurImage.style.filter = "alpha(opacity=" + FadeInValue + ")";
    CurImage.style.opacity = FadeInValue / 100;
    if (PrevImage) {
        PrevImage.style.filter = "alpha(opacity=" + FadeOutValue + ")";
        PrevImage.style.opacity = FadeOutValue / 100;
        FadeOutValue -= 5;
    }
    FadeInValue += 5;
    if (FadeInValue < 100)
        (PrevImage ? (timeOut = setTimeout(function() { FadeInOut(FadeInValue, FadeOutValue, CurImage, PrevImage) }, 1)) :
            (timeOut = setTimeout(function() { FadeInOut(FadeInValue, null, CurImage, null) }, 1)));
    else {
        if (typeof (ProdImage[1]) == "undefined")
            for (i = 1; i < ImagesArray.length; i++) {
            ProdImage[i] = new Image();
            ProdImage[i].src = ImagesArray[i];
            ProdImage[i].style.zIndex = 1;
            ProdImage[i].id = "ProdImage" + i;
            ProdImage[i].style.position = 'absolute';
            ProdImage[i].style.filter = "alpha(opacity = 0)";
            ProdImage[i].style.opacity = 0;
            if (ProdImage[i].src.search("lamp6.jpg") != -1)
                ProdImage[i].style.top = "50px";
            DivImagesContainer.appendChild(ProdImage[i]);
        }
        CurImage.style.filter = "alpha(opacity=100)";
        CurImage.style.opacity = 1;
        if (PrevImage) {
            PrevImage.style.filter = "alpha(opacity = 0 )";
            PrevImage.style.opacity = 0;
        }
        clearTimeout(timeOut);
    }
}

function ButtonNextClick() {
    iPrevImage = iCurImage++;
    if (document.getElementById("ImageText")) 
        document.getElementById("ImageText").innerHTML = ImagesText[iCurImage];
    document.getElementById("CurImageNum").innerHTML = iCurImage + 1;
    if (document.getElementById("ButtonPrevImage").onclick == null) {
        document.getElementById("ButtonPrevImage").onclick = ButtonPrevClick;
        document.getElementById("ButtonPrevImage").style.color = "";
        document.getElementById("ButtonPrevImage").style.textDecoration = "";
        document.getElementById("ButtonPrevImage").style.cursor = "pointer";
    }
    if ((iCurImage + 1) == ImagesArray.length) {
        document.getElementById("ButtonNextImage").onclick = null;
        document.getElementById("ButtonNextImage").style.color = "Gray";
        document.getElementById("ButtonNextImage").style.textDecoration = "none";
        document.getElementById("ButtonNextImage").style.cursor = "default";
    }
    FadeInOut(FadeInStartValue, FadeOutStartValue, ProdImage[iCurImage], ProdImage[iPrevImage]);

}

function ButtonPrevClick() {
    iPrevImage = iCurImage--;
    if (document.getElementById("ImageText"))
        document.getElementById("ImageText").innerHTML = ImagesText[iCurImage];
    document.getElementById("CurImageNum").innerHTML = iCurImage + 1;
    if (document.getElementById("ButtonNextImage").onclick == null) {
        document.getElementById("ButtonNextImage").onclick = ButtonNextClick;
        document.getElementById("ButtonNextImage").style.color = "";
        document.getElementById("ButtonNextImage").style.textDecoration = "";
        document.getElementById("ButtonNextImage").style.cursor = "pointer";
    }
    if (iCurImage == 0) {
        document.getElementById("ButtonPrevImage").onclick = null;
        document.getElementById("ButtonPrevImage").style.color = "Gray";
        document.getElementById("ButtonPrevImage").style.textDecoration = "none";
        document.getElementById("ButtonPrevImage").style.cursor = "default";
    }
    FadeInOut(FadeInStartValue, FadeOutStartValue, ProdImage[iCurImage], ProdImage[iPrevImage]);
}

var curHash = "";
var prevImgBut = null;

function setButtons(imgBut) {
    if (prevImgBut)
        prevImgBut.src = prevImgBut.src.replace("_on", "_off");
    imgBut.src = imgBut.src.replace("_off", "_on");
    prevImgBut = imgBut;
}

function initFrameLoad(frameName) {
    if (top === self)
        parent.window.location.href = "Portfolio.aspx#" + frameName;
    parent.sCurrentProduct = frameName;
    setButtons(parent.document.getElementById('img' + frameName));
    setURL(frameName, window.parent);
}

function LoadPageInFrame(prodName, imgBut) {
    setButtons(imgBut);
    setURL(prodName, window);
    curHash = window.location.hash;
    loadFrame(prodName);
}

function setURL(sHash, winObj) {
    var browser = _browser();
    if ((browser != "Firefox") && (browser != "MSIE8") && (browser != "Chrome") && (browser != "MSIE7") && (browser != "Safari") && (browser != "MSIE6"))
        winObj.location.hash = sHash;
}

function loadFrame(Source) {
    var frame = document.getElementById("InnerPageFrame");
    if (Source != "")
        frame.src = Source + ".aspx";
    else
        frame.src = "";
}

function MonitorAddressBar() {
    if (curHash != window.location.hash) {
        var sProd = window.location.hash.substring(1, 20);
        //check if address entered manually with valid product page
        if (document.getElementById("img" + sProd) != null) {
            curHash = window.location.hash;
            document.getElementById("img" + sProd).onclick();
        }
        else {
            setURL("", window);
            curHash = window.location.hash;
            loadFrame("");
        }
    }
}

function _browser() {
    var browser = navigator.userAgent;
    if (browser.search("MSIE 6") != -1)
        return "MSIE6";
    if (browser.search("MSIE 7") != -1)
        return "MSIE7";
    if (browser.search("MSIE 8") != -1)
        return "MSIE8";
    if (browser.search("Chrome") != -1)
        return "Chrome";
    if (browser.search("Firefox") != -1)
        return "Firefox";
    if (browser.search("Safari") != -1)
        return "Safari";    
    if (browser.search("Opera") != -1)
        return "Opera";    
        
    return "UNKNOWN_BROWSER";    
}

function unloadFrame(sFrameName) {
    parent.sPreviousProduct = sFrameName;
    parent.document.getElementById("img" + sFrameName).src = "Images/" + sFrameName + "_off.png";
}

function $(text) {
    alert(text);
    
}