/**
 * JavaScript popup
 * @param position - depends of type:
 *   object - positions window by given object
 *   if nothing is given - positions to the center of screen
 */
function popup(url,width,height,position) {
    var properties = "scrollbars=1,toolbar = 0, location = 0, height = " + height;
    var name = 'boo';
    properties = properties + ", width=" + width;
    var leftprop, topprop, screenX, screenY, cursorX, cursorY, padAmt;
    if(navigator.appName == "Microsoft Internet Explorer") {
        screenY = document.body.offsetHeight;
        screenX = window.screen.availWidth;
    }
    else {
        screenY = window.outerHeight;
        screenX = window.outerWidth;
    }

    if( typeof position == 'object' )	{ // position popup window by given element
        cursorX = getOffsetLeft(position);
        cursorY = getOffsetTop(position);
        padAmtX = 10;
        padAmtY = 10;
        if((cursorY + height + padAmtY) > screenY) {
            // make sizes a negative number to move left/up
            padAmtY = (-30) + (height * -1);
            // if up or to left, make 30 as padding amount
        }
        if((cursorX + width + padAmtX) > screenX)	{
            padAmtX = (-30) + (width * -1);	
            // if up or to left, make 30 as padding amount
        }
        if(navigator.appName == "Microsoft Internet Explorer") {
            leftprop = cursorX + padAmtX;
            topprop = cursorY + padAmtY;
        }
        else {
            leftprop = (cursorX - pageXOffset + padAmtX);
            topprop = (cursorY - pageYOffset + padAmtY);
        }
    }
    else{
        leftvar = (screenX - width) / 2;
        rightvar = (screenY - height) / 2;
        if(navigator.appName == "Microsoft Internet Explorer") {
            leftprop = leftvar;
            topprop = rightvar;
        }
        else {
            leftprop = (leftvar - pageXOffset);
            topprop = (rightvar - pageYOffset);
        }
    }
    properties = properties + ", left = " + leftprop;
    properties = properties + ", top = " + topprop;
    popupHandle = open(url,name,properties);
}


function getOffsetTop (o) {
	var curoffset = 0;
	if (o.offsetParent) {
		while (o.offsetParent) {
			curoffset += o.offsetTop;
			o = o.offsetParent;
		}
	}
	else if (o.y) curoffset += o.y;
	return curoffset;
}
function getOffsetLeft (o) {
	var curoffset = 0;
	if (o.offsetParent) {
		while (o.offsetParent) {
			curoffset += o.offsetLeft;
			o = o.offsetParent;
		}
	}
	else if (o.x) curoffset += o.x;
	return curoffset;
}


