var currentModalElement;
function handleOnResize(){
	if(currentModalElement != undefined && currentModalElement.style.display == '')
	{
		window.centerToWindow(currentModalElement);
		resetBgSize();
	}
}
function showModal(elementId, width) {
	hideSelects();
	if ( typeof(width) == 'number' ) {
		$(elementId).style.width = width + "px";
	}
	blurBg();
	currentModalElement = elementId;
	window.centerToWindow($(elementId));
	try{
		new Draggable(elementId,
			{
				scroll:window,
				handle:'frameHandle',
				revert: function(element){return false;},
				snap: function(x,y,draggable) {
					function constrain(n, lower, upper) {
						if (n > upper) return upper;
						else if (n < lower) return lower;
						else return n;
					}
			     
					element_dimensions = Element.getDimensions(draggable.element);
					parent_dimensions = Element.getDimensions($('bgBlur'));//draggable.element.parentNode);
					return[
						constrain(x, 0, parent_dimensions.width - element_dimensions.width),
						constrain(y, 0, parent_dimensions.height - element_dimensions.height)];
				}
			}
		);
	} catch (e){
		//do nothing
	}
	return false;
}
function closeModal() {
	showSelects();
	if(currentModalElement != undefined)
	{
		$(currentModalElement).style.display = 'none';
	}
	$(bgBlur).style.display = 'none';	
}

function blurBg(){
	$(bgBlur).style.display = '';
	resetBgSize();
}
function resetBgSize(){
	if ( typeof( window.innerWidth ) == 'number' ) {
        $(bgBlur).style.width  = window.innerWidth;
    } else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        $(bgBlur).style.width  = document.documentElement.clientWidth;
    } else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        $(bgBlur).style.width  = document.body.clientWidth;
    }
    if (! isIE()) {
		$(bgBlur).style.height = (document.documentElement.scrollHeight > document.documentElement.clientHeight ? document.documentElement.scrollHeight: document.documentElement.clientHeight) + "px";
	}
}

window.centerToWindow = function( oNode ) {
    if ( typeof(oNode) == 'string' ) {
        oNode = $(oNode);
    }
	currentModalElement = oNode;
    var iDocX  = 0;
    var iWinY = 0;

    if ( typeof( window.innerWidth ) == 'number' ) {
        iDocX  = window.innerWidth;
        iWinY = window.innerHeight;
    } else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        iDocX  = document.documentElement.clientWidth;
        iWinY = document.documentElement.clientHeight;
    } else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        iDocX  = document.body.clientWidth;
        iWinY = document.body.clientHeight;
    }

	if ( oNode && oNode.style ) {
		with ( oNode.style ) {
		    position = 'absolute';
		    zIndex = 99;
		}
	}

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ) {
        scrollY = document.documentElement.scrollTop;
    } else if ( document.body && document.body.scrollTop ) {
        scrollY = document.body.scrollTop;
    } else if ( window.pageYOffset ) {
        scrollY = window.pageYOffset;
    } else if ( window.scrollY ) {
        scrollY = window.scrollY;
    }

    var oElementDimensions = Element.getDimensions(oNode);

    var iSetX = ( iDocX - oElementDimensions.width  ) / 2;
    var iSetY = ( iWinY - oElementDimensions.height ) / 2 + scrollY;

    iSetX = ( iSetX < 0 ) ? 0 : iSetX;
    iSetY = ( iSetY < 0 ) ? 0 : iSetY;
    
	if ( oNode && oNode.style ) {
		with ( oNode.style ) {
		    left = iSetX + "px";
		    top  = iSetY + "px";
			display = '';
		}
	}
};

function getDocHeight(){
	var height;
    if ( typeof( window.innerHeight ) == 'number' ) {
        height = window.innerHeight;
    } else if ( document.documentElement && document.documentElement.clientHeight) {
        height = document.documentElement.clientHeight;
    } else if ( document.body && document.body.clientHeight) {
        height = document.body.clientHeight;
    }
    
    if ( document.documentElement && document.documentElement.scrollTop ) {
        height += document.documentElement.scrollTop;
    } else if ( document.body && document.body.scrollTop ) {
        height += document.body.scrollTop;
    } else if ( window.pageYOffset ) {
        height += window.pageYOffset;
    } else if ( window.scrollY ) {
        height += window.scrollY;
    }
    return height;
}
function showImageInElem(e,pi_imageUrl) {
	hideSelects();
	var l_pos = getMouseXY(e);
	var l_elem = $("hoverImageContainer");
	l_elem.innerHTML = "<span class=\"processing\">Loading...</span>";
	l_elem.style.left = l_pos.X + 20;
	l_elem.style.top = l_pos.Y;
	l_elem.style.display = "";
	l_elem.innerHTML = "<img src=\"" + pi_imageUrl + "\" />";
	if (getDocHeight() < (l_pos.Y + l_elem.clientHeight - 20)) {
		l_elem.style.top = getDocHeight() - l_elem.clientHeight - 20;
	}
}
function hideImage() {
	var l_elem = $("hoverImageContainer");
	l_elem.innerHTML = "";
	l_elem.style.display = "none";
}