﻿/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

	WebCore: Modal dialog scripts
	Abyss Studios Ltd. (c) 2004-2009

	history:
		070119 - creation -=AGi=-

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/

function ModalWindow(instanceVariableName, cssClassPrefix, resultElementId) {
	this.instanceVariableName = instanceVariableName;
	this.cssClassPrefix = (cssClassPrefix==null || cssClassPrefix=='') ? 'modalWindow' : cssClassPrefix;
	this.windowContentUrl = '';
	this.resultElementId = resultElementId;
	
	
	function GetScrollPosition() {
		if (document.documentElement) {
			return {x: document.documentElement.scrollLeft, y: document.documentElement.scrollTop };
		}
		else if (document.all) {
			return {x: document.body.scrollLeft, y: document.body.scrollTop };
		}
		else {
			return {x: window.pageXOffset, y: window.pageYOffset };
		}
	}

	function GetWindowSize() {
		if (document.documentElement) {
			return {x: document.documentElement.clientWidth, y: document.documentElement.clientHeight };
		}
		else if (document.all) {
			return {x: document.body.clientWidth, y: document.body.clientHeight };
		}
		else {
			return {x: window.innerWidth, y: window.innerHeight };
		}
	}


	function WindowScroll()
	{
		this.WindowScroll_FixPositions(true);
	}

	function WindowResize()
	{
		this.WindowScroll_FixPositions(false);
	}

	function WindowScroll_FixPositions(isScrolling)
	{
		scrollPos = this.GetScrollPosition();
		windowSize = this.GetWindowSize();
		
		midX = (windowSize.x / 2) + scrollPos.x;
		midY = (windowSize.y / 2) + scrollPos.y;
		if (document.all==null) {
			midY = (windowSize.y / 2);
		}
		
		eTrap = document.getElementById(this.instanceVariableName + 'Trap');
		eBorder = document.getElementById(this.instanceVariableName + 'Border');
		eContainer = document.getElementById(this.instanceVariableName + 'Container');
		
		if (eTrap!=null) {
			eTrap.style.left = (midX - (eTrap.offsetWidth / 2)) + 'px';
			eTrap.style.top = (midY - (eTrap.offsetHeight / 2)) + 'px';
		}

		if (eBorder!=null) {
			eBorder.style.left = (midX - (eBorder.offsetWidth / 2)) + 'px';
			eBorder.style.top = (midY - (eBorder.offsetHeight / 2)) + 'px';
		}

		if (eBorder!=null) {
			eContainer.style.left = (midX - (eContainer.offsetWidth / 2)) + 'px';
			eContainer.style.top = (midY - (eContainer.offsetHeight / 2)) + 'px';
		}
	}

	function Close(resultValue)
	{
		eBody = parent.document.body;
		if (eBody==null) return;
		
		eTrap = parent.document.getElementById(this.instanceVariableName + 'Trap');
		if (eTrap!=null) eBody.removeChild(eTrap);
		
		eBorder = parent.document.getElementById(this.instanceVariableName + 'Border');
		if (eBorder!=null) eBody.removeChild(eBorder);
		
		eContainer = parent.document.getElementById(this.instanceVariableName + 'Container');
		if (eContainer!=null) eBody.removeChild(eContainer);
		
		if (this.resultElementId != null && resultValue != null) {
			element = elementControl_SetValue(this.resultElementId, resultValue); 
			if (element != null && element.onchange != null) {
				element.onchange();
			}
		}
	}

	function WindowCrossFade(targetAlpha, timeStart, timeTotal)
	{
		now = new Date();
		if (timeStart==null || timeStart==0)
			timeStart = now.getTime();
		timeCurrent = now.getTime() - timeStart;
		fadePosition = timeCurrent / timeTotal;
		
		if (timeCurrent > timeTotal) timeCurrent = timeTotal;
		eBorder = document.getElementById(this.instanceVariableName + 'Border');
		eContainer = parent.document.getElementById(this.instanceVariableName + 'Container');

		if (eBorder!=null) {
			opacity = fadePosition * targetAlpha;
			if (document.all) {
				eBorder.style.filter = "alpha(opacity=" + Math.round(opacity*100) + ")";
			}
			else {
				eBorder.style.opacity = opacity;
			}
			if (timeCurrent < timeTotal) {
				setTimeout(this.instanceVariableName + ".WindowCrossFade(" + targetAlpha + ", " + timeStart + ", " + timeTotal + ")", 30);
			}
			else {
				eContainer.style.visibility = 'visible';
			}
		};
	}


	function DoModal() {	
		eBody = document.body; 

		eTrap = document.createElement('div');
		eTrap.id = this.instanceVariableName + 'Trap';
		eTrap.className = this.cssClassPrefix + 'Trap';
		
		eBorder = document.createElement('div');
		eBorder.id = this.instanceVariableName + 'Border';
		eBorder.className = this.cssClassPrefix + 'Border';
		
		eContainer = document.createElement('div');
		eContainer.id = this.instanceVariableName + 'Container';
		eContainer.className = this.cssClassPrefix + 'Container';
		
		frameName = this.instanceVariableName + 'Frame';
		eContainer.innerHTML = '<iframe src="' + this.windowContentUrl + '" style="width:100%; height:100%; background-color:transparent;" scrolling="no" frameborder="0" allowtransparency="true" id="' + frameName + '" name="' + frameName + '" width="100%" height="100%"></iframe>';
		
		
		eBody.appendChild(eTrap);
		eBody.appendChild(eBorder);
		eBody.appendChild(eContainer);	
		

		eContainer.style.visibility = 'hidden';
		
		if (document.all) {
			//window.attachEvent('onscroll', this.WindowScroll);
			//window.attachEvent('onresize', this.WindowResize);
			//var tmpOnScrollEventFunction;
			eval("tmpOnScrollEventFunction = function() { "  + this.instanceVariableName + ".WindowScroll(); }");
			window.attachEvent('onscroll', tmpOnScrollEventFunction);
			
			eval("tmpOnResizeEventFunction = function() { "  + this.instanceVariableName + ".WindowResize(); }");
			window.attachEvent('onresize', tmpOnResizeEventFunction);
		}
		else {
			eTrap.style.position = 'fixed';
			eBorder.style.position = 'fixed';	
			eContainer.style.position = 'fixed';
			
			eval("tmpOnResizeEventFunction = function() { "  + this.instanceVariableName + ".WindowResize(); }");
			window.onresize = tmpOnResizeEventFunction;
		}
				
		this.WindowScroll_FixPositions();
		this.WindowCrossFade(0.7, 0, 200);
		//this.WindowCrossFade(0.7, 0, 0);
	}
	
	function InitOnClick(e, url) // stops bubble
	{			
		this.windowContentUrl = url;
		this.DoModal();
		return elementControl_EventStopBubble(e);
	}	

	this.GetScrollPosition = GetScrollPosition;
	this.GetWindowSize = GetWindowSize;	
	this.WindowScroll = WindowScroll;
	this.WindowResize = WindowResize;	
	this.WindowScroll_FixPositions = WindowScroll_FixPositions;	
	this.WindowCrossFade = WindowCrossFade;
	this.DoModal = DoModal;
	this.Close = Close;	
	this.InitOnClick = InitOnClick;
}

function ModalWindowScroll()
{
	alert('jjj');
	alert(window.name);
}

function ModalWindowClose(resultValue)
{
	frameName = window.name;
	instanceName = frameName.substring(0, frameName.length - ('Frame').length);
	parent[instanceName].Close(resultValue);
}	

