/*
*****************************************************************************
Copyright (c)2005 Hennes & Mauritz AB, Stockholm
All rights reserved.
*****************************************************************************
FILE
    corporate_common.js

DESCRIPTION
    common javascript functions.

HISTORY
    2004-12-22 hbehrn Converted from shop gen3 and inter gen2.
*****************************************************************************
*/


function getX(obj) {
    //return (obj.offsetParent == null ? obj.offsetLeft : obj.offsetLeft + getX(obj.offsetParent));
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            if (obj.style && obj.style.position == "absolute")
            {
                return curleft;
                break;
            }
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function getY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent) {

            if (obj.style && obj.style.position == "absolute") {
                return curtop;
                break;
            } else {
                curtop += obj.offsetTop
            }
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function displayEditDiv(obj, divId, editUrl) {
    var myX = getX(obj);
    var myY = getY(obj);
    this.window.name = 'site';
    var divHtml = "<div style=\"color: #ffffff; cursor: pointer; background-color: #cc0066\" onMouseOver=\"javascript:disableParentAtag(this)\" onMouseOut=\"javascript:restoreParentAtag(this)\" onClick=\"javascript: openEditPopup('" + editUrl + "');\">EDIT</div>";
    var editdiv = getEditDivObj(divId);
    editdiv.innerHTML = divHtml;
    editdiv.style.top = myY + "px"; //Added px suffix for Firefox compatibility;
    editdiv.style.left = myX + "px"; //Added px suffix for Firefox compatibility;
}

function getEditDivObj(divId) {

    if (document.getElementById(divId) != null) {
        return document.getElementById(divId);
    }

    var newElement = document.createElement("div");
    newElement.id = divId;
    newElement.style.position = "absolute";
    newElement.style.zIndex = 999;
    newElement.style.visiblity = "visible"
    document.getElementsByTagName("body")[0].appendChild(newElement);
    return document.getElementById(divId);
}

function disableParentAtag(obj) {
    var parentAElement = findParentAElement(obj);
    if (parentAElement != null && parentAElement.href != "javascript:") {
        var namedItem = document.createAttribute("oldHref");
        namedItem.value = parentAElement.href + "";
        parentAElement.attributes.setNamedItem(namedItem);
        parentAElement.href = "javascript:";
    }
}

function findParentAElement(element) {
    if (element && element.parentElement) {
        if (element.parentElement.tagName == "A")
            return element.parentElement;
        if (element.parentElement.tagName != "BODY")
            return findParentAElement(element.parentElement);
    }
    return null;
}

function restoreParentAtag(obj) {
    var parentAElement = findParentAElement(obj);
    if (parentAElement != null && parentAElement.attributes) {
        var oldHrefAtt = parentAElement.attributes.getNamedItem("oldHref");
        if (oldHrefAtt && oldHrefAtt.value) {
            parentAElement.href = oldHrefAtt.value;
        }
    }
}

var hideEditDivTimeOutHashmap = new Object();
var restoreParentATimeOutHashmap = new Object();

function hideEditDiv(divId) {
    divIdGlob = divId;
    if (hideEditDivTimeOutHashmap[divId])
        window.clearTimeout(hideEditDivTimeOutHashmap[divId]);
    hideEditDivTimeOutHashmap[divId] = setTimeout("hideDiv('" + divId + "')", 3000);

    if (restoreParentATimeOutHashmap[divId])
        window.clearTimeout(restoreParentATimeOutHashmap[divId]);
    restoreParentATimeOutHashmap[divId] = setTimeout("restoreParentAtag(document.getElementById('" + divId + "'))", 3000);


}

function hideDiv(divId) {
    var divHtml = "";
    document.getElementById(divId).innerHTML = divHtml;
}

function openEditPopup(url) {
    openPopupFallback(url, 'editpopup', 1010, 650, 0, 0, 0, 1, 'yes');
}

function openPopupFallback(ppage, pname, pwidth, pheight, pt, pl, ptol, pr, ps, ploc) {
    newwin = window.open(ppage, pname, "toolbar=" + ptol + ",location=" + ploc + ", resizable=" + pr + ",scrollbars=" + ps + ",width=" + pwidth + ",height=" + pheight + ",top=" + pt + ",left=" + pl)
    if (newwin.opener == null) {
        newwin.opener = self;
    }
    newwin.focus();
}
;

function openEditPopupQuestion(url) {
    if (confirm("Do you want to open the edit-form for this button instead of posting the form?")) {
        openEditPopup(url)
        return false;
    }
    return true;
}

