// if not found div "popup__id__", will be used POPUP_TPL
var POPUP_TPL = '<div id="popup__id__" class="popups popup_a" style="position:absolute; display:none;">'+
'  <a class="popup_close" href="#popup" onclick="document.getElementById(\'popup__id__\').style.display = \'none\'; return false;"><span class="bolder">&nbsp;</span></a>'+
'  <div class="mHeader">'+
'    <img src="images/general/boxsmall_header_left.jpg" class="floatLeft"/>'+
'     <div class="smHeaderTitle">__title__</div>'+
'     <img src="images/general/boxsmall_header_right.jpg" class="floatRight"/>'+
'  </div>'+
'   <div class="moduleContent" id="popup_content__id__" style="height: 100%; overflow: auto">__content__</div>'+
'</div>';
var POPUP_OBJ = false;
var POPUP_TPL_INNER = '';
if (typeof($) == "undefined") { // check prototype.js
	$ = function(id) {
		return document.getElementById(id);
	}
}
/**
* Creates popup window
* objWhere - id or obj of ANCHOR popup
* title - title string
* objContent - 1) id of DIV with content 2) string of content 3) ajax url
* width - popup width in pixel
* height - popup height in pixel
* shiftX, shiftY - offset from anchor, by default = 0,0
*/
function showPopup(objWhere, title, objContent, width, height, shiftX, shiftY) {
	if (!POPUP_OBJ) {
		if ((obj = $('popup__id__')) == null) {
			var obj = document.createElement("DIV");
			obj.innerHTML = POPUP_TPL;
			document.body.appendChild(obj);
			
		}
		POPUP_OBJ = $('popup__id__');
		POPUP_OBJ._busy = false;
		POPUP_TPL_INNER = POPUP_OBJ.innerHTML;
	}
	// set title
	var tpl = POPUP_TPL_INNER.replace(/__title__/,title);
	
	// set size
	if (typeof(width) != "undefined") {
		if (/^\d+$/.test(width)) {
			//tpl = tpl.replace(/__width__/,' width: '+width+'px');
			POPUP_OBJ.style.width = width + 'px';
		} else { // RP capability
			//tpl = tpl.replace(/popup_a/,width);
		}
	}
	if (typeof(height) != "undefined") {
		//tpl = tpl.replace(/__height__/,' height: '+height+'px');
		POPUP_OBJ.style.height = height + 'px';
		//POPUP_OBJ.style.overflow = 'auto';
	}
	if (typeof(POPUP_OBJ.cache) == "undefined") {
		POPUP_OBJ.cache = {};
	}
	// set content
	var content = '';
	if ($(objContent) != null) objContent = $(objContent);
	if (typeof(objContent) == "object") {
		if (typeof(POPUP_OBJ.cache[objContent.id]) != "undefined") {
			content = POPUP_OBJ.cache[objContent.id];
		} else {
			content = '<div id="'+objContent.id+'">'+objContent.innerHTML+'</div>';
			POPUP_OBJ.cache[objContent.id] = content;
			objContent.parentNode.removeChild(objContent);
		}
	} else if (typeof(objContent) == 'string') {
		if (/^http/i.test(objContent) || /^ajax::/i.test(objContent)) {
			objContent = objContent.replace(/^ajax::/i,'');
			ajax(objContent, _onLoadPopupContent)
			content = '<div style="margin: 5px; text-align: center">Please wait...</div>';
		} else if (typeof(POPUP_OBJ.cache[objContent]) != "undefined") {
			content = POPUP_OBJ.cache[objContent];
		} else {
			content = objContent;
		}
	}
	tpl = tpl.replace(/__content__/,content);
	//alert(tpl);
	POPUP_OBJ.innerHTML = tpl;
	// find anchor
	if (typeof(objWhere) == 'string') {
		objWhere = $(objWhere);
	}
	
	// shift from anchor point
	shiftX = typeof(shiftX) != "undefined" ? shiftX : 0;
	shiftY = typeof(shiftY) != "undefined" ? shiftY : 0;
	// to center ?
	if (objWhere == null) {
		popupMoveToCenter(POPUP_OBJ);
	} else {
		var pos = popupAbsolutePos(objWhere);
		pos.x += shiftX;
		pos.y += shiftY;
		POPUP_OBJ.style.left = pos.x+'px';
		POPUP_OBJ.style.top = pos.y+'px';
	}
	_showPopupEffect();
}
function _showPopupEffect() {
	// FIX IE
	if (/MSIE/.test(navigator.userAgent)) {
		POPUP_OBJ.style.display = ''; //alert(3);
		return;
	}
	if (typeof(Effects) != "undefined") { // effects_lib.js
		Effects.show(POPUP_OBJ); //alert(1);
	} else if (typeof(Effect) != "undefined") { // scriptaculous.js
		Effect.Apper(POPUP_OBJ); //alert(2);
	} else {
		POPUP_OBJ.style.display = ''; //alert(3);
	}
	
}
function _onLoadPopupContent(obj) {
	var content = '';
	if (typeof(obj.str) != "undefined") {
		content = obj.str;
	} else if (typeof(obj) == "object") { // may be xmlhttpreqest :?
		content = obj.responseText;
	} else if (typeof(obj) == "string") {
		conent = obj;
	}
	$('popup_content__id__').innerHTML = content;
	_showPopupEffect();
}

function popupAbsolutePos(el, scrollOff) {
	if (typeof(el) == "string") el = document.getElementById(el);
	var SL = 0, ST = 0;
	if (!scrollOff) {
		var is_div = /^div$/i.test(el.tagName);
		if (is_div && el.scrollLeft)
			SL = el.scrollLeft;
		if (is_div && el.scrollTop)
			ST = el.scrollTop;
	}
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) {
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
};
function popupMoveToCenter(obj) {
	if (typeof(obj) == "string") {
		obj = document.getElementById(obj);
	}
	var old_disp = obj.style.display;
	obj.style.display = "";
	var size = popupWindowSize(); // window size
	var br = popupPageScroll(); //scrolling
	var newX = Math.round((size.width - obj.offsetWidth) / 2) + br.x; 
	var newY = Math.round((size.height - obj.offsetHeight) / 2) + br.y;
	if (newX < 0) newX = 0;
	if (newY < 0) newY = 0;
	obj.style.position = "absolute";
	// alert("W:"+winW+' x '+winH+" C: "+w+' x '+h + " NEW: x="+newX+' y='+newY);
	obj.style.left =  newX + 'px';
	obj.style.top =  newY + 'px';
	obj.style.display = old_disp;
	// Fix IE
	/*
	var copy = obj.cloneNode(true);
	obj.parentNode.removeChild(obj);
	document.body.appendChild(copy);
	*/
}
function popupWindowSize() {
	var iWidth = 0;
	var iHeight = 0;

	if (document.compatMode && document.compatMode == 'CSS1Compat') {
	    // Standards-compliant mode
		if (window.opera) {
			iWidth = document.body.clientWidth || 0;
			iHeight = document.body.clientHeight || 0;
		} else {
			iWidth = document.documentElement.clientWidth || 0;
			iHeight = document.documentElement.clientHeight || 0;
		}
	} else {
	    // Non standards-compliant mode
		iWidth = window.innerWidth || document.body.clientWidth ||
			document.documentElement.clientWidth || 0;
		iHeight = window.innerHeight || document.body.clientHeight ||
			document.documentElement.clientHeight || 0;
	}

	return {
		width: iWidth,
		height: iHeight
	};
};
function popupPageScroll() {
	var s = {};
	s.y = window.pageYOffset ||
			document.documentElement.scrollTop ||
			(document.body ? document.body.scrollTop : 0) ||
			0;
	s.x = window.pageXOffset ||
			document.documentElement.scrollLeft ||
			(document.body ? document.body.scrollLeft : 0) ||
			0;
	return s;
};