﻿// JScript File

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
var currentlyOpenWindow = null;
function openLanguageSelector() {
    var langSelectionPanel = document.getElementById('languageSelectionPanel');
    
    if(currentlyOpenWindow) {
        currentlyOpenWindow.style.display = "none";
        currentlyOpenWindow = null;
    }
    
    //Element from which to get offset position.
    var elemSource = document.getElementById("divLangaugeSelection"); //arguments[0] != undefined && arguments[0].target != undefined ? arguments[0].target : event.srcElement;
    var posArr = getElementPosition(elemSource);
    langSelectionPanel.style.display = "block";
    langSelectionPanel.style.top = (elemSource.offsetHeight - 11 + posArr[1]) + "px";
    langSelectionPanel.style.left = (posArr[0] - (elemSource.offsetWidth + 56)) + "px";
    
    if(window.attachEvent) {
        document.documentElement.attachEvent("onclick",function(e) {
            closeSelectorWindow(e);
        } );
        event.cancelBubble = true;
    }
    else {
        arguments[0].stopPropagation();
        arguments[0].preventDefault();
    }
    currentlyOpenWindow = langSelectionPanel;
    return false;
}

function openCurrencySelector() {
    
    var currencySelectionPanel = document.getElementById('currencySelectionPanel');
    if(currentlyOpenWindow) {
        currentlyOpenWindow.style.display = "none";
        currentlyOpenWindow = null;
    }
    
    //Element from which to get offset position.
    var elemSource = document.getElementById("divCurrencySelection");
    var posArr = getElementPosition(elemSource);
    currencySelectionPanel.style.display = "block";
    currencySelectionPanel.style.top = (elemSource.offsetHeight - 11 + posArr[1]) + "px";
    currencySelectionPanel.style.left = (posArr[0] - (elemSource.offsetWidth + 50)) + "px";
    
    if(window.attachEvent) {
        document.documentElement.attachEvent("onclick",function(e) {
            closeSelectorWindow(e);
        } ); 
        event.cancelBubble = true;
    }
    else {
        arguments[0].stopPropagation();
        arguments[0].preventDefault();
    }
    currentlyOpenWindow = currencySelectionPanel;
    return false;
    
}

function closeSelectorWindow(e) {
    e = !e ? event : e;
    if(currentlyOpenWindow) {
        currentlyOpenWindow.style.display = "none";
        currentlyOpenWindow = null;
    } else {
        return false;
        
    }
    if(e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    } else {
        e.cancelBubble = true;
    }
    
    return false;
}

function getElementPosition(elem) {
    var parent = elem;
    var oLeft = 0;
    var oTop = 0;
    while(parent != undefined && parent != null) {
        if(parent.offsetLeft != undefined && parent.offsetLeft != "NaN" && parent.tagName != "DIV" && parent.tagName != "TBODY" && parent.tagName != "TR" && parent.tagName != "SPAN") {
            oLeft += parent.offsetLeft;
            oTop += parent.offsetTop;
        } else {
            if(parent.style && parent.style.position == "absolute") {
                oLeft += parseInt(parent.style.left);
                oTop += parseInt(parent.style.top);
                break;
            } else if (parent.tagName == "DIV") {
                oLeft += parent.offsetLeft;
                oTop += parent.offsetTop;
            }
        }
        parent = parent.offsetParent;
    }
    var retArr  = new Array();
    retArr.push(oLeft);
    retArr.push(oTop);
    
    return retArr;
}


function getPosition(e, topNodeId){
    
    var left = 0;
    var top = 0;
    while (e.offsetParent) {
        if(e.offsetParent.id == topNodeId) { break; }
        left += parseInt(e.offsetLeft);
        top += parseInt(e.offsetTop);
        e = e.offsetParent;
        
    }
    left += e.offsetLeft;
    top += e.offsetTop;
    return {x:left, y:top};
}

function fixMyPosition(arrowId, refButtonId, topNodeId, posXYAndWidth) {
    var elemArrow   = document.getElementById(arrowId);
    if(elemArrow) {
        var elemRef     = document.getElementById(refButtonId);
        elemArrow.style.position = "absolute";
        var posArr = posXYAndWidth.split("x");
            
        if(posArr.length == 3) {
            elemWidth = posArr[2];
            elemArrow.style.left = elemWidth-15 + parseInt(posArr[0]) + "px";
            elemArrow.style.top = 7 + parseInt(posArr[1]) + "px";
        } else {
            var pos = getPosition(elemRef, topNodeId);
            elemArrow.style.left = elemRef.offsetWidth + - 15 + pos.x + "px";
            elemArrow.style.top = 7 + pos.y + "px";
        }
    }
}

function storeLastClickedHeaderPosition(refButtonId, topNodeId, storageFieldID) {
    var elemRef = document.getElementById(refButtonId);
    var pos = getPosition(elemRef, topNodeId);
    var elemWidth = elemRef.offsetWidth;
    document.getElementById(storageFieldID).value = parseInt(pos.x) + "x" + parseInt(pos.y) + "x" + elemWidth;
}

var calcPosition = function (t, b, c, d) {
    if ((t/=d/2) < 1) {
        return c/2*t*t + b;
    } else {
        return -c/2 * ((--t)*(t-2) - 1) + b;
    }
}

var imageDisplayContainer;

function showImage(imgId,srcElem) {
    var imageDisplay = document.getElementById("imageDisplay");
    var imageDisplayContainer = !imageDisplayContainer ? document.getElementById("imageDisplayContainer") : imageDisplayContainer;
    imageDisplayContainer.style.display = "block";
    imageDisplay.src= "http://www.b2bmarked.dk/FileStash/view/" + imgId + ".ashx?small=";
    var posObj = getPosition(srcElem);
    imageDisplayContainer.style.position = "absolute";
    imageDisplayContainer.style.left = (parseInt(posObj.x) - imageDisplayContainer.offsetWidth - 5)  + "px";
    imageDisplayContainer.style.top = (parseInt(posObj.y) - 40) + "px";
}

function hideImage() {
    var imageDisplayContainer = !imageDisplayContainer ? document.getElementById("imageDisplayContainer") : imageDisplayContainer;
    imageDisplayContainer.style.display = "none";
}


function addAnEvent(elemId,eventName,eHandler) {
    var elem = document.getElementById(elemId);
    if(window.attachEvent) {
        elem.attachEvent(eventName,eHandler);
    } else {
        elem.addEventListener("change",eHandler,false);
    }
}
