var popupWindow = null;

function openPopupWindow(url, width, height, resizable, status) {
    //alert("openPopupWindow: " + url);
    closePopupWindow();
    if ((!popupWindow) || (popupWindow.closed)) {
        var rs = (resizable) ? "yes" : "no";
        var st = (status)    ? "yes" : "no";
        var params = "left=20,top=20,resizable="+rs+",scrollbars="+rs+",status="+st;
        if ( parseInt(width) && parseInt(height) ) {
            params = "height="+height+",width="+width+"," + params;
        }
        popupWindow = window.open(url, "mwm_popup", params);
        popupWindow.opener = self;
        popupWindow.focus();
    } else {
        //popupWindow.focus();  // fails in firefox
    }
}

function closePopupWindow() {
    //alert("closePopupWindow");
    if ((popupWindow) && (!popupWindow.closed)) {
        popupWindow.close();
    }
    popupWindow = null; 
}

function scrollFlashBy(delta) {
    window.scrollBy(  0, delta);
}

function scrollTopPageStart() {
	window.scrollTo( 0, 0 );
}


function getRect(obj) {
    var x   = 0;
    var tmp = obj;
    if ( obj.offsetParent ) {
        while ( obj.offsetParent ) {
            x  += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    } else if ( obj.x ) {
        x += obj.x;
    }
    obj   = tmp;
    
    var y = 0;
    if ( obj.offsetParent ) {
        while ( obj.offsetParent ) {
            y  += obj.offsetTop;
            obj = obj.offsetParent;
        }
    } else if ( obj.y ) {
        y += obj.y;
    }
    obj = tmp;
    
    var width = 0;
    if ( obj.offsetWidth ) {
        width += obj.offsetWidth;
    }    
    
    var height = 0;
    if ( obj.offsetHeight ) {
        height += obj.offsetHeight;
    }
    
    return {
        x      : x,
        y      : y,
        width  : width,
        height : height
    };
}

function getViewport() {

    var x = 0;
    var y  = 0;
    if ( window.pageXOffset != undefined ) {
        x = window.pageXOffset;
        y  = window.pageYOffset;
    } else {
        if ( document.body.scrollLeft != undefined ) {
            x += document.body.scrollLeft;
            y  += document.body.scrollTop;
        }
        if ( document.documentElement && document.documentElement.scrollLeft != undefined ) {
            x += document.documentElement.scrollLeft;
            y  += document.documentElement.scrollTop;
        }
    }
    
    var width = 0;
    var height = 0;
    if ( window.innerWidth != undefined ) {
        width  = window.innerWidth;
        height = window.innerHeight;
    } else {
        if ( document.body.clientWidth != undefined ) {
            width  += document.body.clientWidth;
            height += document.body.clientHeight;
        }
        if ( document.documentElement && document.documentElement.clientWidth != undefined ) {
            width  += document.documentElement.clientWidth;
            height += document.documentElement.clientHeight;
        }
    }
    
    return {
        x      : x,
        y      : y,
        width  : width,
        height : height
    };
}

function getViewportData() {

    var viewport = getViewport();
    var viewer   = getRect(document.getElementById("MWM_main"));
    var x        = Math.max( viewer.x, viewport.x );
    var y        = Math.max( viewer.y, viewport.y );
    var width    = Math.min( viewer.x + viewer.width, viewport.x + viewport.width ) - x;
    var height   = Math.min( viewer.y + viewer.height, viewport.y + viewport.height ) - y;
    x = x - viewer.x;
    y = y - viewer.y;

    return {
		x      : x,
		y      : y,
		width  : width,
		height : height
    };
}

function getCurrentPageId() {
	var url = document.location.href;
	url = url.split("#");
	var id = url[1];
	return id;
}

function setSiteSize( width, height ) {
	var viewer = document.getElementById("MWM_main");
	viewer.style.width = width;
	viewer.style.height = height;
}
