var Szene1PopUpGenerator = {

	loadingDots :
		'<div id="myPopUpContainerWait">'
		+ '<center>'
		+ '<img src="/images/icons/ajax-loader-white.gif" '
		+ 'border="0"> '
		+ Szene1Translator.translate('loadingDots',null)
		+ '</center>'
		+ '</div>',

	popUpInstance : null,

	popUpContainer : 'myPopUpContainer',

	jsResponse : null,

	open : function(url, modus, options, optionalData)
	{
		var callback = null;

		try
		{
			options['popupId'] = Szene1PopUpGenerator.popUpContainer;
			callback = options['callback'];
		}
		catch(e)
		{
			options = { popupId : Szene1PopUpGenerator.popUpContainer };
		}

		Szene1PopUpGenerator.popUpInstance =
			new Szene1PopUp(
				Szene1PopUpGenerator.loadingDots,
				options
			);

		var tmpData =
			{
				onCancel	: 'Szene1PopUpGenerator.popUpInstance.close',
				jsResponse	: 'Szene1PopUpGenerator.jsResponse',
				onSubmit	: 'Szene1PopUpGenerator.serializedSend',
				url			: url,
				mode		: modus,
				optional	: (((typeof optionalData) == 'object')
								? optionalData
								: {})
			}

		var postArgs =
			'mode='
			+ modus
			+ '&data='
			+ encodeURIComponent(Object.toJSON(tmpData));

		Szene1PopUpGenerator.doAjaxReq(url, postArgs);
	},

	doAjaxReq : function(url, postArgs)
	{
		new Ajax.Request(
			url,
			{
				method: 'post',
				postBody: postArgs,
				requestHeaders:
					['Pragma', 'no-cache', 'Cache-Control',
					'must-revalidate', 'If-Modified-Since',
					'Sat, 1 Jan 2000 00:00:00 GMT'],
				onSuccess: Szene1PopUpGenerator.show,
				onFailure: function(t)
				{
					$(Szene1PopUpGenerator.popUpContainer).innerHTML =
						Szene1Translator.translate('errorLoadingForm', null);
					alert(t.responseText);
				}
			}
		);
	},

	show : function(response)
	{
		var json = unescape(response.responseText);

		if(json.substring(0,1)!='{')
		{
			$(Szene1PopUpGenerator.popUpContainer).innerHTML =
				Szene1Translator.translate('errorLoadingForm', null);
			//alert(json);
			return;
		}

		var responseObj = eval("(" + json + ")");

		$(Szene1PopUpGenerator.popUpContainer).appendChild(
			Builder.node(
				'div',
				{
					'id':		Szene1PopUpGenerator.popUpContainer + '_temp',
					'style':	'display:none'
				}
			));

		$(Szene1PopUpGenerator.popUpContainer + '_temp').innerHTML = '';

		new Insertion.Top(
			$(Szene1PopUpGenerator.popUpContainer + '_temp'),
			responseObj['html']
		);

		Szene1PopUpGenerator.popUpInstance.reCenterByHiddenObject(
			$(Szene1PopUpGenerator.popUpContainer + '_temp')
		);
	},

	serializedSend : function()
	{
		var form	= $(Szene1PopUpGenerator.popUpContainer).down('form');

		if(form)
		{
			var formData = form.serialize(true);
		}
		else
		{
			var formData = new Object();
		}

		Object.extend(
			Szene1PopUpGenerator.jsResponse,
			formData
		);

		Szene1PopUpGenerator.jsResponse['isPost'] = true;

		$(Szene1PopUpGenerator.popUpContainer).innerHTML =
			Szene1PopUpGenerator.loadingDots;

		Szene1PopUpGenerator.popUpInstance.reCenterByHiddenObject(
			$('myPopUpContainerWait')
		);

		var postArgs =
			'mode='
			+ Szene1PopUpGenerator.jsResponse['mode']
			+ '&data='
			+ encodeURIComponent(
				Object.toJSON(Szene1PopUpGenerator.jsResponse)
			);

		window.setTimeout(
			'Szene1PopUpGenerator.doAjaxReq(\''
			+ Szene1PopUpGenerator.jsResponse['url']
			+ '\', "'
			+ postArgs
			+ '");',
			Szene1PopUpGenerator.popUpInstance.resizeDuration * 3 * 1000
		);

		Szene1PopUpGenerator.jsResponse = '';
	}
}

/**
 *
 * SZENE1 - CONTENT RELOADER
 *
 * --------------------------------------------------------------------------
 *
 * @author    Thomas Pillmayr <t.pillmayr@szene1.at>
 * @since     01.07.2008
 *
 * --------------------------------------------------------------------------
 *
 */

var Szene1ContentReloader = {

	container : null,

	reload : function(container, contentUrl, contentMode, contentObj)
	{
		Szene1ContentReloader.container = $(container);

		if(!Szene1ContentReloader.container ||
			!contentUrl)
		{
			return false;
		}

		var postAttach = '';
		if((typeof contentObj) == 'object')
		{
			postAttach = '&info=' +  Object.toJSON(contentObj);
		}

		new Insertion.Top(
			Szene1ContentReloader.container,
			'<div style="position:absolute; width: 600px; text-align: center;" '
			+ 'id="contentReloaderBox">'
			+ '<img src="/images/icons/ajax-loader-big.gif" border="0" />'
			+ '</div>'
		);

		new Ajax.Request(
			contentUrl,
			{
				method: 'post',
				postBody: 'mode=' + contentMode + postAttach,
				onSuccess: Szene1ContentReloader.show
			}
		);
	},

	show : function(response)
	{
		if(!Szene1ContentReloader.container)
		{
			return false;
		}

		var json = unescape(response.responseText);

		if(json.substring(0,1)!='{')
		{
			$('contentReloaderBox').remove();
			return;
		}

		var responseObj = eval("(" + json + ")");

		if(responseObj['valid'])
		{
			Szene1ContentReloader.container.innerHTML = '';
			new Insertion.Top(
				Szene1ContentReloader.container,
				responseObj['html']
			);
		}
		else
		{
			$('contentReloaderBox').remove();
		}
	}
}


/**
 *
 * SZENE1 - POPUP
 *
 * --------------------------------------------------------------------------
 *
 * @author    Thomas Pillmayr <t.pillmayr@szene1.at>
 * @since     03.06.2008
 *
 * --------------------------------------------------------------------------
 *
 */

var Szene1PopUp = Class.create();

Szene1PopUp.prototype = {

	initialize : function(content, options)
	{
		this.keyboardAction = this.keyboardAction.bindAsEventListener(this);

		var defaults = {
			enableEscape		: true,
			quickQuit			: true,
			overlayElementClass	: 'szene1_overlay',
			overlayElementId	: 'overlay',
			overlayDuration		: 0.5,
			overlayOpacity		: 0.5,
			popupId				: 'popupContainer',
			popupClass			: 'szene1_popup',
			reloadUrl			: null,
			reloadMode			: null,
			reloadContainer		: null,
			reloadInfo			: null
		}

		var settings			= Object.extend(defaults, options || {});

		this.overlayOpacity		= settings.overlayOpacity;
		this.overlayDuration	= settings.overlayDuration;
		this.overlayElementId	= settings.overlayElementId;
		this.overlayElementClass= settings.overlayElementClass;
		this.moveDuration		= 0.5;
		this.resizeDuration		= 0.5;
		this.quickQuit			= (this.overlayOpacity > 0) ? settings.quickQuit : false; // Close PopUp with click on overlay-Layer
		this.enableEscape		= settings.enableEscape;

		if(settings.reloadUrl &&
			settings.reloadMode &&
			settings.reloadContainer)
		{
			this.reloadUrl			= settings.reloadUrl;
			this.reloadMode			= settings.reloadMode;
			this.reloadContainer	= settings.reloadContainer;
			this.reloadInfo			= settings.reloadInfo
		}
		else
		{
			this.reloadUrl			= null;
			this.reloadMode			= null;
			this.reloadContainer	= null;
			this.relaodInfo			= null;
		}

		this.addOverlay			= (this.overlayOpacity > 0) ? true : false;

		this.popupId			= settings.popupId;
		this.popupClass			= settings.popupClass;
		this.popupBorder		= 10;

		if(!content)
		{
			content = 'There is no content yet!';
		}

		this.htmlContent		= content;

		this.start();
	},

	start : function()
	{
		var elmBody = $$('body')[0];
		var arrayPageSize = this.getPageSize();
		var windowPageSize = this.getPageSize(true);


		if(this.addOverlay)
		{
			if($(this.overlayElementId)){
				$(this.overlayElementId).remove();
			}

			elmBody.appendChild(
				Builder.node(
					'div',
					{
						'id':		this.overlayElementId,
						'style':	'display:none',
						'class':	this.overlayElementClass
					}
				));
		}

		if($(this.popupId)){
			$(this.popupId).remove();
		}

		elmBody.appendChild(
			Builder.node(
				'div',
				{
					'id':		this.popupId,
					'class':	this.popupClass
				}
			)
		);

		$(this.popupId).innerHTML = this.htmlContent;

		this.currentWidth	= $(this.popupId).getWidth();
		this.currentHeight	= $(this.popupId).getHeight();

		var topOffset = 0;

		if(document.documentElement &&
			document.documentElement.scrollTop)
		{
			topOffset = document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			topOffset = document.body.scrollTop;
		}

		$(this.popupId).setStyle({
			left: ((windowPageSize[0] - this.currentWidth) / 2) + 'px',
			top: (((windowPageSize[1] - this.currentHeight) / 2) + topOffset) + 'px'
		});

		$$('select', 'object', 'embed').each(
			function(node)
			{
				node.style.visibility = 'hidden';
			}
		);

		if(this.addOverlay)
		{
			$(this.overlayElementId).setStyle({
				width: arrayPageSize[0] + 'px',
				height: arrayPageSize[1] + 'px'
			});

			new Effect.Appear(
				$(this.overlayElementId),
				{
					duration:	this.overlayDuration,
					from:		0.0,
					to:			this.overlayOpacity
				}
			);
		}

		this.attachObservers();
	},

	close: function()
	{
		this.detachObservers();

		if($(this.popupId))
		{
			$(this.popupId).remove();
		}

		if(this.addOverlay &&
			$(this.overlayElementId))
		{
			new Effect.Fade(
				$(this.overlayElementId),
				{
					duration: this.overlayDuration,
					afterFinish:(function(){
						$(this.overlayElementId).remove()
					}).bind(this)
				}
			);
		}

		$$('select', 'object', 'embed').each(
			function(node)
			{
				node.style.visibility = 'visible';
			}
		);

		if(this.reloadUrl &&
			this.reloadMode &&
			this.reloadContainer)
		{
			Szene1ContentReloader.reload(
				this.reloadContainer,
				this.reloadUrl,
				this.reloadMode,
				this.reloadInfo
			);
		}
	},

	attachObservers : function()
	{
		if(this.enableEscape)
		{
			Event.observe(
				document,
				'keydown',
				this.keyboardAction
			);
		}

		if(this.quickQuit)
		{
			$(this.overlayElementId).hide().observe(
				'click',
				(function()
				{
					this.close();
				}
			).bind(this));
		}
	},

	detachObservers : function()
	{
		if(this.enableEscape)
		{
			Event.stopObserving(
				document,
				'keydown',
				this.keyboardAction
			);
		}
	},

	keyboardAction : function(event)
	{
		var keycode = event.keyCode
		var escapeKey;

		if(event.DOM_VK_ESCAPE)
		{
			escapeKey = event.DOM_VK_ESCAPE;
		}
		else
		{
			escapeKey = 27;
		}

		var key = String.fromCharCode(keycode).toLowerCase();

		if(keycode == escapeKey)
		{
			try
			{
				event.stop();
			}
			catch(e)
			{
			}
			this.close();
		}
	},

	reCenterByHiddenObject : function(hiddenContainer, avoidContentReplacing)
	{
		$(this.popupId).setStyle({'height' : this.currentHeight + 'px'});

		hiddenContainer = $(hiddenContainer);

		var widthNew  = hiddenContainer.getWidth();
		var heightNew = hiddenContainer.getHeight();

		var xScale = (widthNew  / this.currentWidth)  * 100;
		var yScale = (heightNew / this.currentHeight) * 100;

		var wDiff = this.currentWidth - widthNew;
		var hDiff = this.currentHeight - heightNew;

		if (wDiff != 0)
		{
			new Effect.Scale(
				this.popupId,
				xScale,
				{
					scaleY: false,
					scaleFromCenter: true,
					scaleContent: false,
					duration: this.resizeDuration,
					scaleMode: {originalHeight : this.currentHeight, originalWidth : this.currentWidth }
				}
			);
		}

		if (hDiff != 0)
		{

			new Effect.Scale(
				this.popupId,
				yScale,
				{
					scaleX: false,
					scaleFromCenter: true,
					scaleContent : false,
					duration: this.resizeDuration,
					delay: this.resizeDuration,
					scaleMode: {originalHeight : this.currentHeight, originalWidth : this.currentWidth },
					afterFinish: function(effect){
						effect.element.setStyle({'height' : 'auto'});
					}
				}
			);
		}

		this.currentWidth = widthNew;
		this.currentHeight = heightNew;

		if(!avoidContentReplacing)
		{
			window.setTimeout(
				'var tmp = $(\'' + $(hiddenContainer).id + '\').innerHTML;'
				+ '$(\'' + $(hiddenContainer).id + '\').remove();'
				+ '$(\'' + this.popupId + '\').innerHTML = \'\';'
				+ 'new Insertion.Top($(\'' + this.popupId + '\'), tmp);',
				this.resizeDuration * 2 * 1000
			);
		}
	},

	reCenter : function()
	{
		var arrayPageSize = this.getPageSize(true);

		var popupWidth = $(this.popupId).getWidth();
		var popupHeight = $(this.popupId).getHeight();

		new Effect.Move(
			$(this.popupId),
			{
				x: ((arrayPageSize[0] - popupWidth) / 2),
				y: ((arrayPageSize[1] - popupHeight) / 2),
				mode: 'absolute',
				duration: this.moveDuration
			}
		);

		return;
	},

	getPageSize: function(windowedMode)
	{
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY)
		{
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else if (document.body.scrollHeight > document.body.offsetHeight)
		{
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		else
		{
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		if(windowedMode &&
			$('page'))
		{
			windowWidth = $('page').getWidth();
			
			if (self.innerHeight)
			{
				windowHeight = self.innerHeight;
			}
			else if (document.documentElement && document.documentElement.clientHeight)
			{
				windowHeight = document.documentElement.clientHeight;
			}
			else if (document.body)
			{
				windowHeight = document.body.clientHeight;
			}
		}
		else
		{
			if (self.innerHeight)
			{
				if(document.documentElement.clientWidth)
				{
					windowWidth = document.documentElement.clientWidth;
				}
				else
				{
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			}
			else if (document.documentElement && document.documentElement.clientHeight)
			{
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			}
			else if (document.body)
			{
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}
		}
		
		if(windowedMode)
		{
			if(yScroll > windowHeight)
			{
				pageHeight = windowHeight;
			}
			else
			{
				pageHeight = yScroll;
			}

			if(xScroll > windowWidth)
			{
				pageWidth = windowWidth;
			}
			else
			{
				pageWidth = xScroll;
			}
		}
		else
		{
			if(yScroll < windowHeight)
			{
				pageHeight = windowHeight;
			}
			else
			{
				pageHeight = yScroll;
			}

			if(xScroll < windowWidth)
			{
				pageWidth = xScroll;
			}
			else
			{
				pageWidth = windowWidth;
			}
		}
		
		return [pageWidth,pageHeight];
	}
}