
	function iCMS_dialog()
	{
		this.popupID = 0;
		this.registerEscapeCapture();
		
		this.popupContentError		= '<div class="popupError"></div>';
		this.popupContentProgress	= '<div class="popupProgress"></div>';
	}
	
	/********************* escape capture **************************/
	
	iCMS_dialog.prototype.registerEscapeCapture = function()
	{
		var dialog	= this;
		var handler = function( e )
		{
			if ( ! e ) var e = window.event;
			
			var kC	= e.keyCode;
			var Esc	= ( window.event ) ? 27 : e.DOM_VK_ESCAPE;
			
			if ( kC == Esc )
			{
				if ( dialog.popupID > 0 )
					dialog.hideLastPopup();
				
				return false;
			}
			
			return true;
		};
		
		setEvent ( ( window.addEventListener ) ? window : document.body, 'keydown', handler );
	};
	
	/*************************** AJAX & requests **************************/
	
	/**
	 * Vytvoreni a odeslani xmlHttp pozadavku
	 * 
	 * @param function obsluha Funkce zajistujici obsluhu pri zmene stavu pozadavku, dostane parametr s XMLHttp objektem
	 * @param string method GET|POST|...
	 * @param string url URL pozadavku
	 * @param string [content] Telo zpravy
	 * @param array [headers] Pole predanych hlavicek
	 * @return XMLHttpRequest Objekt XMLHttpRequest nebo null pri neuspechu
	 */
	iCMS_dialog.prototype.sendXmlHttpRequest = function ( handler, method, url, content, headers )
	{
		var xmlhttp = ( window.XMLHttpRequest
			? new XMLHttpRequest
			: ( window.ActiveXObject
				? new ActiveXObject ( "Microsoft.XMLHTTP" )
				: false
				)
			);
		
		if ( ! xmlhttp )
			return null;
		
		xmlhttp.open ( method, url );
		
		xmlhttp.onreadystatechange = function() { handler ( xmlhttp ); };
		
		if ( headers )
			for ( var key in headers )
				xmlhttp.setRequestHeader ( key, headers [ key ] );
		
		xmlhttp.send ( content );
		return xmlhttp;
	};

	iCMS_dialog.prototype.composeParameters = function ( parameters )
	{
		var parametersString = '';
		var sep = '';
		
		for ( var key in parameters )
		{
			parametersString += sep + key + '=' + encodeURIComponent ( parameters[key] );
			sep = '&';
		}

		return parametersString;
	};
	
	iCMS_dialog.prototype.sendGETRequest = function ( handler, url )
	{
		return this.sendXmlHttpRequest ( handler, 'get', url );
	};
	
	iCMS_dialog.prototype.sendPOSTRequest = function ( handler, url, parameters )
	{
		var params		= this.composeParameters( parameters );
		var headers		= { 'Content-type' : 'application/x-www-form-urlencoded', 'Content-length' : params.length, 'Connection' : 'close' };
		
		return this.sendXmlHttpRequest( handler, 'post', url, params, headers );
	};
	
	iCMS_dialog.prototype.createProgressElementHandler = function ( element, customFunction )
	{
		var dialog = this;
		return function ( xmlHttp )
		{
			if ( xmlHttp.readyState == 1 )
				element.innerHTML = dialog.popupContentProgress;
	
			else if ( xmlHttp.readyState == 4 )
			{
				if ( xmlHttp.status == 200 )
				{
					dialog.insertHTMLAndEvaluate ( element, xmlHttp.responseText );
					
					if ( customFunction )
						customFunction ( element );
				}
			
				else
					element.innerHTML = dialog.popupContentError;
			}
		};
	};

	/*********************** progress popup ****************************/
	
	/**
	 * Creates a new popup with the progress bar
	 */
	iCMS_dialog.prototype.createProgressPopupHandler = function()
	{
		var popupID;
		if ( ! ( popupID = this.displayPopup ( '' ) ) )
			return function() {};
	
		var dialogObject = this;
		return function ( xmlHttp ) { dialogObject.displayProgressPopup ( popupID, xmlHttp ); };
	};
	
	iCMS_dialog.prototype.displayProgressPopup = function ( popupID, xmlHttp, customSuccessHandler )
	{
		if ( xmlHttp.readyState == 1 )
			this.setPopupContentProgress( popupID );

		else if ( xmlHttp.readyState == 4 )
		{
			if ( xmlHttp.status == 200 )
			{
				this.setPopupContent ( popupID, xmlHttp.responseText );
				
				if ( customSuccessHandler != undefined )
					customSuccessHandler ( popupID );
			}
			else
				this.setPopupContentError ( popupID );
		}
	};
	
	/*********************** common popup ******************************/
	
	iCMS_dialog.prototype.displayPopup = function ( content, allowCloseButton )
	{
		if ( allowCloseButton == undefined )
			allowCloseButton = true;
		
		// Do not use innerHTML here as it resets form in Mozilla - we have to create elements "manually"
		
		// first the "main" DIV
		var div			= document.createElement( 'div' );
		var popupID		= ++this.popupID;
		var divID		= 'popup_' + popupID;
		
		div.className	= 'dialogPopup' + ( popupID > 1 ? ' dialogPopup2' : '' );
		div.id			= divID;

		document.body.appendChild ( div );
		
		// we use iframe here so IE6 covers selects & other stuff
		var isIE6				= /MSIE 6/i.test(navigator.userAgent);
		var iframeEl			= document.createElement ( isIE6 ? 'iframe' : 'div' );
		iframeEl.className		= 'grey';
		
		if ( isIE6 )
		{
			iframeEl.frameBorder = 0;
			iframeEl.setAttribute ( 'src', iCMS_adminBase.rootURL + 'vp_black.php' );
		}
		
		div.appendChild ( iframeEl );
		
		// create the content
		var contentEl		= document.createElement ( 'div' );
		contentEl.className	= 'content';
		
		div.appendChild ( contentEl );
		
		// this one is created to contain the content
		// if the content is added directly to #content,
		// the onclick for closeEl does not work
		var innerContentEl	= document.createElement ( 'div' );
		innerContentEl.id	= divID + '_innerContent';
		
		contentEl.appendChild ( innerContentEl );

		// close button
		if ( allowCloseButton )
		{
			var closeEl			= document.createElement ( 'a' );
			closeEl.href		= '#';
			closeEl.className	= 'closeButton';
			
			contentEl.appendChild ( closeEl );
	
			var dialog		= this;
			closeEl.onclick	= function() { dialog.hideLastPopup(); return false; };
		}
		
		// set the content
		if ( this.setPopupContent ( popupID, content ) )
			return popupID;
		
		// hide it on error
		this.hideLastPopup();
		return null;
	};

	iCMS_dialog.prototype.getPopupInnerContentEl = function ( popupID )
	{
		return document.getElementById ( 'popup_' + popupID + '_innerContent' );
	};
	
	iCMS_dialog.prototype.setPopupContent = function ( popupID, content )
	{
		// find the dialog
		if ( ! ( el = this.getPopupInnerContentEl( popupID ) ) )
			return false;

		// clean-it-up
		cleanUpContent ( el );
		
		this.insertHTMLAndEvaluate ( el, content );
		
		return true;
	};
	
	iCMS_dialog.prototype.insertHTMLAndEvaluate = function ( el, content )
	{
		el.innerHTML = content;
		this.evaluateScripts( el );
	};
	
	iCMS_dialog.prototype.evaluateScripts = function ( el )
	{
		var scripts	= el.getElementsByTagName ( 'script' );
		var headEl	= document.getElementsByTagName('head').item(0);
		
		for ( var i=0; i < scripts.length; i++ )
		{
			if ( scripts[i].src )
			{
				var script	= document.createElement ( 'script' );
				script.type	= 'text/javascript';
				script.src	= scripts[i].src;
				headEl.appendChild ( script );
			}
			
			eval ( scripts[i] . innerHTML );
		}
	};
	
	iCMS_dialog.prototype.hideLastPopup = function()
	{
		if ( this.popupID <= 0 )
			return false;
		
		if ( ! ( el = document.getElementById ( 'popup_' + this.popupID ) ) )
			return false;
		
		if ( ! el.parentNode )
			return false;
		
		cleanUpContent ( el );
		el.parentNode.removeChild ( el );
		
		this.popupID--;
		
		return true;
	};

	/*********************** special content of popup *************************/
	
	iCMS_dialog.prototype.setPopupContentError = function ( popupID )
	{
		this.setPopupContent ( popupID, this.popupContentError );
	};
	
	iCMS_dialog.prototype.setPopupContentProgress = function ( popupID )
	{
		this.setPopupContent ( popupID, this.popupContentProgress );
	};
	
	/************************ various types of popups *************************/
	
	iCMS_dialog.prototype.displayAlertPopup = function( message )
	{
		return this.displayInfoPopup( '<p class="error">' + htmlspecialchars ( message ) + '</p>' );
	};
	
	iCMS_dialog.prototype.displayInfoPopup = function ( content )
	{
		var popupID = null;
		popupID = this.displayPopup ( content );
		
		if ( popupID == null )
			return null;
	
		if ( el = this.getPopupInnerContentEl( popupID ) )
		{
			var newPEl			= document.createElement ( 'p' );
			newPEl.className	= 'center';
			
			el.appendChild ( newPEl );
			
			var dialog		= this;
			var newAEl		= document.createElement ( 'a' );
			newAEl.href		= '#';
			newAEl.onclick	= function() { dialog.hideLastPopup(); return false; };
			
			newPEl.appendChild ( newAEl );
			
			var newIEl		= document.createElement ( 'img' );
			newIEl.src		= iCMS_adminBase.resourceURL + 'img/popup/ok.png';
			newIEl.alt		= "OK";
			newIEl.title	= "OK";
			
			newAEl.appendChild ( newIEl );
		}
		
		return popupID;
	};
	
	/*************************** submitting form ******************************/
	
	iCMS_dialog.prototype.convertFormToJS = function ( formID )
	{
		var formEl;
		if ( ! ( formEl = document.getElementById ( formID ) ) )
			return false;
	
		var dialog		= this;
		var i			= 0;
		var buttons		= formEl.getElementsByTagName ( 'input' );
		formEl.onsubmit	= function() { dialog.submitFormInCurrentPopup( formEl ); return false; };
		
		for ( i = 0; i < buttons.length; i++ )
			if ( ( buttons[i].type == 'submit' ) || ( buttons[i].type == 'image' ) )
				buttons[i].onclick = (function ( button ) { return function() { dialog.submitFormInCurrentPopup( formEl, button ); }; } )(buttons[i]);
		
		return true;
	};
	
	iCMS_dialog.prototype.submitFormInCurrentPopup = function ( element, button )
	{
		var popupID	= this.popupID;
		var dialog	= this;
		var handler	= function ( xmlHttp ) { dialog.displayProgressPopup( popupID, xmlHttp); };
		
		this.submitFormByElement ( handler, element.action, element, button );
	};
	
	iCMS_dialog.prototype.submitForm = function ( handler, url, formID )
	{
		var formEl;
		if ( ! ( formEl = document.getElementById ( formID ) ) )
			return false;
	
		return this.submitFormByElement ( handler, url, formEl );
	};
	
	iCMS_dialog.prototype.submitFormByElement = function ( handler, url, formEl, button )
	{
		var params = new Object;
		var inputs = formEl.getElementsByTagName ( 'input' );
		var input;
		var inputKey;
		
		// text & hidden & radio & checkbox
		for ( inputKey = 0; inputKey < inputs.length; inputKey++ )
		{
			input = inputs[inputKey];
			
			if ( input.name )
			{	
				// text input & hidden
				if ( ( input.type == 'text' ) || ( input.type == 'hidden' ) )
					params [ input.name ] = input.value;
				
				// radio button & checkbox
				else if ( ( input.type == 'checkbox' ) || ( input.type == 'radio' ) )
				{
					if ( input.checked )
						params [ input.name ] = input.value;
				}
				
				// find the first input type submit or input type image, if not already set by function parameter
				else if ( ( button == undefined ) && ( ( input.type == 'submit' ) || ( input.type == 'image' ) ) )
					button = input;
			}
		}
		
		// selects
		inputs = formEl.getElementsByTagName ( 'select' );
		for ( inputKey = 0; inputKey < inputs.length; inputKey++ )
		{
			input = inputs[inputKey];
		
			if ( input.name )
			{
				if ( input.multiple )
				{
					var counter = 0;
					while ( input.selectedIndex != -1 )
					{
						params [ input.name + '[' + ( counter++ ) + ']' ] = input.options [ input.selectedIndex ].value;
						input.options [ input.selectedIndex ].selected = false;
					}
				}
				else if ( input.selectedIndex != -1 )
					params [ input.name ] = input.options [ input.selectedIndex ].value;
			}
		}
		
		// textareas
		inputs = formEl.getElementsByTagName ( 'textarea' );
		for ( inputKey = 0; inputKey < inputs.length; inputKey++ )
		{
			input = inputs[inputKey];
		
			if ( input.name )
				params [ input.name ] = input.value;
		}
		
		if ( ( button != undefined ) && button.name )
			params [ button.name ] = button.value;
		
		return this.sendPOSTRequest ( handler, url, params );
	};
	
	/****************************** misc *********************************/
	
	iCMS_dialog.prototype.blinkElements = function ( els, count, colorCode, speed )
	{
		if ( ! speed )
			speed = 150;
		
		this.blinkElementsHandler ( els, count, true, colorCode, speed );
	};

	iCMS_dialog.prototype.blinkElementsHandler = function ( els, count, switchOn, colorCode, speed )
	{
		if ( count == 0 )
			return;
		
		var targetColor;
		if ( switchOn )
		{
			targetColor = colorCode;
			switchOn = false;
		}
		else
		{
			targetColor = '';
			switchOn = true;
			count--;
		}
		
		var i;
		var el;
		
		for ( i = 0; i < els.length; i++ )
			els[i].style.backgroundColor = targetColor;
		
		var dialog = this;
		window.setTimeout ( function() { dialog.blinkElementsHandler ( els, count, switchOn, colorCode, speed ); }, speed );
	};
