﻿/**
	Requirements:
		- jquery-1.4.2 or later (with my changes);
		- jqueryUI-1.8.1 or later (with my changes ([!important]auto resize));
		- jqueryUI css (with my changes);
		- jqueryUI images and popup header images;
 */

var openedPopupID = '';
var DialogTitle = '';
var iframeObjForResize = null;
var contentObjForResize = null;
var PopupOpened = false;

function AddClosePopupOnBlackAreaClick (id)
{
	if ($('#' + id).dialog('widget')[0])
	{
		if ($("#" + id).dialog("widget")[0].previousSibling)
		{
			$("#" + id).dialog("widget")[0].previousSibling.setAttribute('onclick', '$("#' + id + '").dialog("close");');
		}
		else if ($("#" + id).dialog("widget")[0].parentElement)
		{
			$("#" + id).dialog("widget")[0].parentElement.previousSibling.onclick = function() { $('#' + id).dialog('close'); };
		}
		else
		{
			setTimeout ("AddClosePopupOnBlackAreaClick ('" + id + "');", 100);
		}
	}
	else
	{
		setTimeout ("AddClosePopupOnBlackAreaClick ('" + id + "');", 100);
	}
}

function CreatePopupForDiv (id, title)
{
	$(function() {
		$("#" + id).dialog("destroy");
		
		$("#" + id).dialog({
			modal: true,
			autoOpen: true,
			closeOnEscape: true,
			draggable: true,
			resizable: false,
			dialogClass: 'defaultDialogClass',
			title: '<img src="/Temas/Global/JqueryUI/images/sapo.jpg" style="float:left;margin-top:-3px;" /><h1 id="PopupTitle" style="float:left;color:#fff;font-size:23px;">' + title + '</h1>',
			zIndex: 999999999,
			show: 'blind',
			//hide: 'explode',
			open: function(event, ui)
			{
				//Fix Header for ie
				if (window.navigator.systemLanguage)
				{
					var dialogNumber = ($(".ui-dialog").length - 1);
					var newHeaderSize = ($(".ui-dialog")[dialogNumber].offsetWidth - 33);
					var minHeaderSize = 0;
					for (var i = 0; i < $(".ui-dialog")[dialogNumber].childNodes[0].childNodes.length; i++)
					{
						minHeaderSize += $(".ui-dialog")[dialogNumber].childNodes[0].childNodes[i].offsetWidth;
					}
					if (newHeaderSize > minHeaderSize)
					{
						$(".ui-dialog")[dialogNumber].childNodes[0].style.width = newHeaderSize + "px";
					}
					else
					{
						$(".ui-dialog")[dialogNumber].childNodes[0].style.width = minHeaderSize + "px";
					}
				}
				
				PopupOpened = true;
				try
				{
					AddClosePopupOnBlackAreaClick (id);
				}
				catch (ex) { }
			},
			close: function(event, ui)
			{
				$("#" + id).dialog("destroy");
				PopupOpened = false;
			}
		});
	});
	
	try
	{
		$('#' + id).dialog('widget')[0].firstChild.lastChild.firstChild.title = '{GENERICS\CLOSE}';
	}
	catch (ex) { }
}

function OpenPopup (id, requestPath, title)
{
	openedPopupID = id;
	DialogTitle = title;
	RequestHttp (requestPath, AtribDadosElem, null, id);
}

function OpenLocalPopup (id, title)
{
	openedPopupID = id;
	DialogTitle = title;
	PopupFinishedLoading ();
}

function LoadIframeForPopup_OnClick (divID, hiddenID, iframeID, iframeSrc, title)
{
	$(document).ready(function()
	{
		var iframeDiv = document.getElementById(iframeID);
		if (iframeDiv)
		{
			OpenLocalPopupAfterLoad (divID, title, iframeID);
			iframeDiv.src = "";
			iframeDiv.src = iframeSrc;
		}
		else
		{
			setTimeout ("LoadIframeForPopup_OnClick ('" + divID + "', '" + hiddenID + "', '" + iframeID + "', '" + iframeSrc + "', '" + title + "');", 100);
		}
	});
}

function OpenLocalPopupAfterLoad (id, title, hiddenDivId)
{
	var hiddenDiv = document.getElementById (hiddenDivId);
	if (hiddenDiv && hiddenDiv.style)
	{
		//hiddenDiv.style.display = "block"; //fix resize for ie because display:none;
		openedPopupID = id;
		DialogTitle = title;
		PopupFinishedLoading ();
	}
	else
	{
		setTimeout ("OpenLocalPopupAfterLoad ('" + id + "', '" + title + "', '" + hiddenDivId + "');", 100);
	}
}

function OpenPopupAndFinishLoading (id, requestPath, title)
{
	openedPopupID = id;
	DialogTitle = title;
	RequestHttp (requestPath, EndOpenPopupAndFinishLoading, null, id);
}

function EndOpenPopupAndFinishLoading (request, param1)
{
	AtribDadosElem (request, param1);
	setTimeout ("PopupFinishedLoading ();", 1); //setTimeout to fix slow browsers (like ie)
}

//<Special Popups>
function CreatePopupImage360 (popupDivID, popup360Content, file, xml360CreatorPath, xml360Plugin, title)
{
	$(document).ready(function()
	{
		var hiddenDiv = document.getElementById (popupDivID);
		if (hiddenDiv) { hiddenDiv.style.display = 'block'; }
		OpenLocalPopup (popupDivID, title);
		setTimeout ("OpenImage360('" + popup360Content + "', '" + file + "', '" + xml360CreatorPath + "', '" + xml360Plugin + "');", 1000);
		setTimeout ("Fix360FF();", 1000);
		setTimeout ("Fix360FF();", 3000);
		setTimeout ("Fix360FF();", 5000);
	});
}

//fix 360 position for firefox
function Fix360FF ()
{
	var popupTitleElem = document.getElementById ("PopupTitle");
	if (popupTitleElem) { popupTitleElem.innerHTML = popupTitleElem.innerHTML + " "; }
}

function OpenImage360 (popup360Content, file, xml360CreatorPath, xml360Plugin)
{
	PopupInsertMovie360 (xml360CreatorPath + '?file=' + file, '100%', '100%', xml360Plugin, popup360Content);
}

function CreatePopupPDF (file, title)
{
	OpenLocalPopup ("PopUpDiv_pdf", title);
	setTimeout ("OpenPopupPDF ('" + file + "');", 500); //TODO: make it better
}

function OpenPopupPDF (file)
{
	PopupInsertPDF (file, '100%', '100%', 'DivPDFContent');
}
//</Special Popups>

function PopupFinishedLoading ()
{
	CreatePopupForDiv (openedPopupID, DialogTitle);
//	$('#' + openedPopupID).dialog('open');
}

//<Resize and Reposition Functions>
function MovePopupToCenter ()
{
	var popupDiv = $('#' + openedPopupID).dialog('widget')[0];
	var offsetHeight = 0;
	var offsetWidth = 0;
	var viewportWidth = 0;
	var viewportHeight = 0;
	
	if (self.pageYOffset !== undefined)
	{
		offsetHeight = self.pageYOffset;
		offsetWidth = self.pageXOffset;
	}
	else if (document.documentElement !== undefined && document.documentElement.scrollTop !== undefined)
	{
		offsetHeight = document.documentElement.scrollTop;
		offsetWidth = document.documentElement.scrollLeft;
	}
	else if (document.body !== undefined && document.body.scrollTop !== undefined)
	{
		offsetHeight = document.body.scrollTop;
		offsetWidth = document.body.scrollLeft;
	}
	
	if (window.innerWidth !== undefined)
	{
		viewportWidth = window.innerWidth;
		viewportHeight = window.innerHeight;
	}
	else if (document.documentElement !== undefined && document.documentElement.clientHeight !== undefined)
	{
		viewportWidth = document.documentElement.clientWidth;
		viewportHeight = document.documentElement.clientHeight;
	}
	else
	{
		viewportWidth = document.getElementsByTagName('body')[0].clientWidth;
		viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
	}
	
	popupDiv.style.top = (offsetHeight + (viewportHeight - popupDiv.offsetHeight) / 2) + "px";
	popupDiv.style.left = (offsetWidth + (viewportWidth - popupDiv.offsetWidth) / 2) + "px";
	
	if (parseInt (popupDiv.style.top.substr (0, (popupDiv.style.top.length - 2))) < 0) { popupDiv.style.top = 0; }
	if (parseInt (popupDiv.style.left.substr (0, (popupDiv.style.left.length - 2))) < 0) { popupDiv.style.left = 0; }
}

function ResizeIframeForPopup (iframeObj, contentObj)
{
	if (openedPopupID)
	{
		if (iframeObj) { iframeObjForResize = iframeObj; }
		if (contentObj) { contentObjForResize = contentObj; }
		
		if (iframeObjForResize && contentObjForResize)
		{
			//Fix for stupie
			try
			{
				var dialogHeaderDiv = $("#" + openedPopupID).dialog("widget")[0].childNodes[0];
			}
			catch (ex)
			{
				setTimeout ("ResizeIframeForPopup (null, null);", 100);
				return false;
			}
			
			if (dialogHeaderDiv && dialogHeaderDiv.style)
			{
				var newWidth = (contentObjForResize.clientWidth ? contentObjForResize.clientWidth : contentObjForResize.offsetWidth);
				var newHeight = (contentObjForResize.clientHeight ? contentObjForResize.clientHeight : contentObjForResize.offsetHeight);
				if (newWidth) { iframeObjForResize.width = newWidth; }
				if (newHeight) { iframeObjForResize.height = newHeight; }
				if (newWidth) { dialogHeaderDiv.style.width = newWidth; }
				
//				if (newWidth && newHeight)
//				{
//					dialogHeaderDiv.id = "dialogHeaderID";
//					$("#" + iframeObjForResize.id).animate({"width": newWidth, "height": newHeight}, 250);
//					$("#" + dialogHeaderDiv.id).animate({"width": newWidth}, 250);
//				}

				MovePopupToCenter ();
				//TODO: make this code better (without setTimeout hardcoded)
				setTimeout("MovePopupToCenter ();", 500); //setTimeout to fix slow browsers (like ie)
			}
		}
	}
}

function ResizeIframeForPopupByContent (contentObj)
{
	if (openedPopupID)
	{
		if (contentObj) { contentObjForResize = contentObj; }
		ResizeIframeForPopup ($("#" + openedPopupID).dialog("widget")[0].getElementsByTagName('iframe')[0], contentObj);
	}
	else
	{
		setTimeout ("ResizeIframeForPopupByContent (null);", 100);
	}
}
//</Resize and Reposition Functions>

function ChangePopupContent (newTitle, newContentObj)
{
	if (newTitle)
	{
		$('#' + openedPopupID).dialog('option', 'title', '<img src="/Temas/Global/JqueryUI/images/sapo.jpg" style="float:left;margin-top:-3px;" /><h1 id="PopupTitle" style="float:left;color: #fff;">' + newTitle + '</h1>');
	}
	
	if (newContentObj)
	{
		ResizeIframeForPopup ($("#" + openedPopupID).dialog("widget")[0].getElementsByTagName('iframe')[0], newContentObj);
	}
}

function CloseOpenedPopup ()
{
	$('#' + openedPopupID).dialog('close');
}

