
function centerElement(name) {
    element = top.mediamarktframe.document.getElementById(name);

    elementWidth = element.offsetWidth;
    elementHeight = element.offsetHeight;

    // Get size of the "viewport"
    vps = getViewPort ();
    scrolling = getScrollXY ();
    
    topPos = Math.round((scrolling[1]+(vps[1]/2)) - (elementHeight/2));
    if (topPos < 0) topPos = 0;
    element.style.top = topPos + "px";

    leftPos = Math.round((scrolling[0]+(vps[0]/2)) - (elementWidth/2));
    element.style.left = leftPos + "px";
}


function getViewPort() {
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

    if (typeof top.mediamarktframe.innerWidth != 'undefined')
    {
        viewportwidth = top.mediamarktframe.innerWidth;
        viewportheight = top.mediamarktframe.innerHeight;
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof top.mediamarktframe.document.documentElement != 'undefined'
            && typeof top.mediamarktframe.document.documentElement.clientWidth !=
            'undefined' && top.mediamarktframe.document.documentElement.clientWidth != 0)
    {
        viewportwidth = top.mediamarktframe.document.documentElement.clientWidth,
        viewportheight = top.mediamarktframe.document.documentElement.clientHeight
    }

    // older versions of IE

    else
    {
        viewportwidth = top.mediamarktframe.document.getElementsByTagName('body')[0].clientWidth,
        viewportheight = top.mediamarktframe.document.getElementsByTagName('body')[0].clientHeight
    }
    return [viewportwidth, viewportheight];
}

function getPageSizeWithScroll(){
    if( window.innerHeight && window.scrollMaxY ) { // Firefox {
        pageWidth = window.innerWidth + window.scrollMaxX;
        pageHeight = window.innerHeight + window.scrollMaxY;
    }
    else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
    {
        pageWidth = document.body.scrollWidth;
        pageHeight = document.body.scrollHeight;
    }
    else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
    { 
        pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
        pageHeight = document.body.offsetHeight + document.body.offsetTop; 
    }

    vp = getViewPort();
    if (pageHeight < vp[1]) {
        pageHeight = vp[1];
    }
    if (pageWidth < vp[0]) {
        pageWidth = vp[0];
    }

    return Array(pageWidth, pageHeight);
/*
    if (window.innerHeight && window.scrollMaxY) {// Firefox
        alert('ff');
        yWithScroll = window.innerHeight + window.scrollMaxY;
        xWithScroll = window.innerWidth + window.scrollMaxX;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        alert('not ex, not max');
        yWithScroll = document.body.scrollHeight;
        xWithScroll = document.body.scrollWidth;
    } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        alert('ie6s, moz, saf');
        yWithScroll = document.body.offsetHeight;
        xWithScroll = document.body.offsetWidth;
    }
    arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
    return arrayPageSizeWithScroll;
    */
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [ scrOfX, scrOfY ];
}
function OpenPopup (filename) {
    OpenPopup (filename, null);
}
function OpenPopup(filename, callback) {

    dialog = document.getElementById('popup_div');


    if (!dialog) {
        // Replace background image:
        var sizes = top.mediamarktframe.getPageSizeWithScroll();
        var sizey = sizes[1];
        var sizex = sizes[0];
//        var sizey = (document.height !== undefined) ? document.height : document.body.offsetHeight;
//        var sizex = (document.width !== undefined) ? document.width : document.body.offsetWidth;

        //bgimage = document.createElement('img');
        bgimage = top.mediamarktframe.document.createElement('div');
        bgimage.style.backgroundImage = 'url("/design/DEFAULT/graphics/semi-transperent.png")';
//        bgimage.setAttribute('src', '/design/DEFAULT/graphics/semi-transperent.png');
        bgimage.id =  'popup_background_image';
        bgimage.style.position = 'absolute';
        bgimage.style.top = '0px';
        bgimage.style.left = '0px';
        bgimage.style.width = sizex + 'px';
        bgimage.style.height = sizey + 'px';
        bgimage.style.zIndex = 100;
        top.mediamarktframe.document.body.appendChild(bgimage);

        // Open dialog:
        dialog = document.createElement('div');
        dialog.setAttribute('id', 'popup_div');
        dialog.style.position = 'absolute';
        dialog.style.zIndex = 101;
        dialog.style.width = '';
        dialog.style.height = '';
        dialog.innerHTML = "Loading content...";
        top.mediamarktframe.document.body.appendChild(dialog);
    }
    centerElement('popup_div');
    
    // Get the content:
    if (filename.substr(0,1) == "/") {
        url = filename;
    }
    else {
        url = "ajax/" + filename;
    }
   
    jQuery.get(url, 
        function (data) {
             if (data.substring(0,10) == "Location: ") {
                document.location = data.substring(10);
            }
            else {
                dialog.innerHTML = data; 
                centerElement('popup_div');
            }
            if (callback != null) {
                eval(callback);
            }
        });
}

function ClosePopup() {
    // Remove background image:
    bgimg = top.mediamarktframe.document.getElementById('popup_background_image');
    if (bgimg) {
        top.mediamarktframe.document.body.removeChild(bgimg);
    }
    
    // Remove popup:
    pdiv = top.mediamarktframe.document.getElementById('popup_div');
    if (pdiv) {
        top.mediamarktframe.document.body.removeChild(pdiv);
    }
    return true;
}

