﻿Rav.currentPopup = null;
Rav.showPopup = function Rav$showPopup(popupId, onShowing, src) {
	if(Rav.currentPopup && Rav.currentPopup != null) {
		Rav.closePopup();
	}
	var popup = $("#" + popupId);
	var frame = $("#" + popupId + "_frame");
	var prevSrc = "";
	if(frame) {
		prevSrc = frame.attr("src");
	}
	var setSource = true;
	if(onShowing) {
		var e = new Object();
		e.cancel = false;
		e.setSource = true;
		onShowing(e, Rav.getElement(popupId + "_frame"));
		if(e.cancel) {
			return;
		}
		setSource = e.setSource;
	}
	Rav.currentPopup = popupId;
	if(src && frame && setSource && src != prevSrc){
		frame.attr("src", src);
	}
	popup.show();
	Rav.moveToCenter(popupId);
}
Rav.closePopup = function Rav$closePopup(popupId, onClosed) {
	if(!popupId || popupId == null) {
		popupId = Rav.currentPopup;
	}
	if(popupId) {
		$("#" + popupId).hide();
	}
	if(onClosed) {
		onClosed();
	}
}
Rav.resizePopup = function Rav$resizePopup(width, height, popId) {
	if(!popId || popId == null) {
		popId = Rav.currentPopup;
	}
	if(!popId || popId == null) return;
	var pop = Rav.getElement(popId);
	pop.style.width = (width + 8) + "px";
//	pop.firstChild.firstChild.firstChild.style.width = width - 8;
//	pop.childNodes[1].firstChild.firstChild.style.width = width;
//	pop.childNodes[1].firstChild.firstChild.style.height = height;
//	pop.childNodes[1].firstChild.firstChild.firstChild.style.width = width;
//	pop.childNodes[1].firstChild.firstChild.firstChild.style.height = height;
//	pop.childNodes[2].firstChild.firstChild.style.width = width;
	Rav.getElement(popId + "_1").style.width = (width - 8) + "px";
	Rav.getElement(popId + "_2").style.width = width + "px";
	Rav.getElement(popId + "_2").style.height = height + "px";
	Rav.getElement(popId + "_frame").style.width = width + "px";
	Rav.getElement(popId + "_frame").style.height = height + "px";
	Rav.getElement(popId + "_3").style.width = width + "px";
	Rav.moveToCenter(popId);
}
Rav.openNewPopup = function Rav$openNewPopup(id, title, url, width, height, onShowing) {
	if(Rav.getElement(id)) {
		Rav.showPopup(id, onShowing);
		return;
	}
	var popDiv = document.createElement("div");
	$(popDiv).attr("id", id);
	$(popDiv).css("position", "absolute");
	$(popDiv).css("width", width + 8);
	$(popDiv).css("z-index", "100");
	$(popDiv).css("display", "none");
	document.body.insertBefore(popDiv, document.body.firstChild);
	var html = "<div class=\"PopupTitleLeft\">";
	html += "  <div class=\"PopupTitleRight\">";
	html += "    <div id='" + id + "_1' class=\"PopupTitleTile\" style=\"width:" + (width - 8) + "px\">";
	html += "      <div class=\"PopupTitleText\">" + title + "</div>";
	html += "      <div class=\"PopupCloseIcon\" onclick=\"Rav.closePopup('" + id + "');\"></div>";
	html += "    </div>";
	html += "  </div>";
	html += "</div>";
	html += "<div class=\"PopupBodyLeft\">";
	html += "   <div class=\"PopupBodyRight\">";
	html += "     <div id='" + id + "_2' class=\"PopupBodyTile\" style=\"width:" + width + "px;height:" + height + "px\">";
	html += "<iframe id=\"" + id + "_frame\" src=\"" + url + "\" frameborder=\"0\" scrolling=\"auto\" style=\"width:" + width + "px;height:" + height + "px;border:none;border-width:0px\"></iframe>";
	html += "   </div>";
	html += " </div>";
	html += "</div>";
	html += "<div class=\"PopupBottomLeft\">";
	html += "  <div class=\"PopupBottomRight\">";
	html += "    <div id='" + id + "_3' class=\"PopupBottomTile\" style=\"width:" + width + "px\"></div>";
	html += "  </div>";
	html += "</div>";
	$(popDiv).html(html);
	Rav.showPopup(id, onShowing);
}
