var userAgent = navigator.userAgent.toLowerCase();
var isIE = ((userAgent.indexOf("msie") != -1) ? true : false);
var isMac = ((userAgent.indexOf("mac") != -1) ? true : false);
var isIEMac = isIE && isMac;



function openPrintPopup(link) {
    openPopup(link, "winprint", "resizable,scrollbars,width=640,height=600");
    return false;
};

/**
 * Open a new window with either given or default params
 * @param winObj, winName, winProperties
 * @return false
 */
var openWindows = new Array;
function openPopup(winObj, winName, winProperties) {
    var targetUrl = null;
    var isValid = true;
    var defaultWinWidth = "650";
    var defaultWinHeight = "450";
    var monitorTop = (screen.width / 2) - (defaultWinWidth / 2);
    var monitorLeft = (screen.height / 2) - (defaultWinHeight / 2);
    var defaultWinName = "default";
    var defaultWinProperties = "resizable,scrollbars,width=" + defaultWinWidth + ",height= " + defaultWinHeight;
    // Check if all needed params are given
    if (winObj == null) {
        isValid = false;
    } else if (typeof winObj == "object") {
        // Extract href out of winObj ...
        targetUrl = winObj.href;
    } else {
        // ... or else winObj assumed as valid string.
        targetUrl = winObj;
    }
    if (winProperties == null) {
        winProperties = defaultWinProperties;
    }
    if (winName == null) {
        winName = defaultWinName;
    }
    //
    // Now open the window
    if (isValid == true) {
        /*if (openWindows[winName]) {
            openWindows[winName].close();
        }*/
        openWindows[winName] = window.open(targetUrl, winName, winProperties);
        openWindows[winName].focus();
    }
    return false;
}

function preparePopup() {
    //debug(document.links[0], "link");
    for (x = 0; x < document.links.length; x++) {
        document.links[x].href = "javascript:window.print()";
        document.links[x].onclick = "return false";
    }
    for (x = 0; x < document.forms.length; x++) {
        document.forms[x].action = "javascript:window.print()";
    }
    window.location.href = "javascript:window.print()"; // print order like this way does work on more browsers then simply window.print()
}
function resizebody(num) {
	document.getElementById("swfmap").setAttribute("height", num + 20);
}

function betaCFC_popup(aLink, aName, aOptions) {
    if (aLink.search("^/content") != -1) {
        aLink = "/" + window.location.pathname.split("/")[1] + aLink.substr(8) ;
    }
     window.open(aLink, aName, aOptions);
     //return true;
}

/*	Event Cache uses an anonymous function to create a hidden scope chain.
	This is to prevent scoping issues. */
var EventCache = function() {
    var listEvents = [];

    return {
        listEvents : listEvents,

        add : function(node, sEventName, fHandler, bCapture) {
            listEvents.push(arguments);
        },

        flush : function() {
            var i, item;
            for (i = listEvents.length - 1; i >= 0; i = i - 1) {
                item = listEvents[i];

                if (item[0].removeEventListener) {
                    item[0].removeEventListener(item[1], item[2], item[3]);
                }
                ;

                /* From this point on we need the event names to be prefixed with 'on" */
                if (item[1].substring(0, 2) != "on") {
                    item[1] = "on" + item[1];
                }
                ;

                if (item[0].detachEvent) {
                    item[0].detachEvent(item[1], item[2]);
                }
                ;

                item[0][item[1]] = null;
            }
            ;
        }
    };
}();

function addEventHandler(target, eventType, listenerFunction, useCapture) {
    var result;
    if (target.addEventListener) {
        // W3C DOM approach
        useCapture = (typeof useCapture == 'boolean') ? useCapture : false;
        target.addEventListener(eventType, listenerFunction, useCapture);
        result = true;
    } else if (target.attachEvent) {
        // IE/Win DOM approach
        var r = target.attachEvent('on' + eventType, listenerFunction);
        result = r;
    } else {
        // fallback approach (IE/Mac and anything else that gets this far)
        var onEv = 'on' + eventType;
        // if there's an existing event handler function
        if (typeof target[onEv] == 'function') {
            // store it
            var existing = target[onEv];
            // attach new onload handler
            target[onEv] = function(e) {
                // call existing function
                existing(e || window.event);
                // call given function
                listenerFunction(e || window.event);
            };
        } else {
            target[onEv] = function(e) {
                listenerFunction(e || window.event);
            };
        }
        return true;
        // do not use EventCash
    }
    EventCache.add(target, eventType, listenerFunction, useCapture);
    return result;
}

var interval;
function startFooterIEHotFix(e) {
    interval = window.setInterval("footerIEHotFix()", 50);
}
function stopFooterIEHotFix(e) {
    if (interval) { window.clearInterval(interval); }
}
function footerIEHotFix() {
    if (isIE && !isIEMac) {
        var f = document.getElementById("footer-inner");
        f.style.height = (f.offsetHeight == 20 ? "21px" : "20px");
    }
}

function calcFooterPosition() {
    var b = document.getElementsByTagName("body")[0];
    var f = document.getElementById("footer");
    var w = document.getElementById("wrapper");
    if (f && w) {
        var h = 0;
        if (window.innerHeight) {
            h = window.innerHeight;
        } else if (document.body && document.body.clientHeight) {
            h = document.body.clientHeight;
        }
        var wh = w.offsetHeight;
        var fih = 50; // #footer-inner height + margin-top
        if (h > (wh + fih)) {
            f.style.paddingTop = (h - (wh + fih)) + "px";
        }
        addEventHandler(document.getElementById("footer"), 'mouseover', startFooterIEHotFix);
        addEventHandler(document.getElementById("footer"), 'mouseout', stopFooterIEHotFix);
    }
}

addEventHandler(window, 'load', function(evt) {
    calcFooterPosition();
});
