/*======================================================================================*\
 * Copyright (c) 2008 BLUEWING.BIZ - All Rights Reserved.
 * ======================================================================================
 * No part of this file may be reproduced or transmitted in any form or by any means, 
 * electronic or mechanical, including photocopying and recording, for any purpose 
 * without the express written permission of the owner of copyright.
 * ======================================================================================
 * @filesource       _core/control/js/gui.js
 * @copyright        Copyright 2007-2010, Bluewing, Florian Tschoerner
 * @link             http://www.bluewing.biz
 * @package          bluewing
 * @since            Bluewing v 2.0.1
 * @version          
 * @modifiedby       
 * @lastmodified     25.03.2010
 * @license          http://www.opensource.org/licenses/mit-license.php The MIT License
\*======================================================================================*/

	var global_guiAjaxLoader;

	var pf = navigator.platform;
	var lng = navigator.language;
	
	if (navigator.cookieEnabled == true)
		var ce = 1;
	else
		var ce = 0;
	
	var av = navigator.appVersion;
	var an = navigator.appName;
	var cn = navigator.appCodeName;

	var sw = screen.width;
	var sh = screen.height;
	var ah = screen.availHeight;
	var aw = screen.availWidth;
	var cd = screen.colorDepth;
	var pd = screen.pixelDepth;

	// send environment vars
	document.write('<script type="text/javascript" src="../statistics/loggAll?log=stats&pf='+pf+'&lng='+lng+'&ce='+ce+'&av='+av+'&an='+an+'&cn='+cn+'&sw='+sw+'&sh='+sh+'&ah='+ah+'&aw='+aw+'&cd='+cd+'&pd='+pd+'"><\/script>');



	/*==================================================================================*\
	 *	Function:	global_guiScreenInit()
	 *	Purpose:	initializes shaddow
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		none
	 *	Return:		none
	\*==================================================================================*/

	function global_guiScreenInit() {
		global_guiShaddowInit();

		global_guiDialogueInit();

		// check if java script is enabled
		document.getElementById('global_dlgJavaScript').style.display = 'none';

		// check if cookies are enabled
		if (navigator.cookieEnabled == true)
			document.getElementById('global_dlgCookies').style.display = 'none';

		// check if popups are enabled
		document.getElementById('global_dlgPopup').style.display = 'none';

		// hide help dialogue
		document.getElementById('global_dlgHelp').style.display = 'none';
		
		// hide ajax loader dialogue
		document.getElementById('global_dlgAjaxLoader').style.display = 'none';
		global_guiAjaxLoader = false;

		// hide alert dialogue
		document.getElementById('global_dlgAlert').style.display = 'none';

		// hide dialogue shaddow
		document.getElementById('global_dlgShaddow').style.display = 'none';
		document.getElementById('global_dialogues').style.display = 'none';

//		global_guiAddOnClickEvent();

		if (document.getElementById('clock'))
			startClock('clock');
	}



	/*==================================================================================*\
	 *	Function:	global_guiShaddowInit()
	 *	Purpose:	initializes shaddow
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		none
	 *	Return:		none
	\*==================================================================================*/

	function global_guiShaddowInit() {
		var elem = document.getElementById("global_dlgShaddow").style;

		// style action blocker
		elem.width = screen.width+'px';
		elem.height = screen.height+'px';

		if (document.all && navigator.appVersion.indexOf("MSIE 7") > -1) { // ie 7
			elem.filter = 'alpha(opacity=80)';
		} else { // ff
			elem.opacity = '.8';
		}

	}



	/*==================================================================================*\
	 *	Function:	global_guiDialogueInit()
	 *	Purpose:	initializes dialogues
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		none
	 *	Return:		none
	\*==================================================================================*/

	function global_guiDialogueInit() {
		var parent = document.getElementById("global_dialogues");

		var screenWidth = parent.offsetWidth;
		var screenHeight = parent.offsetHeight;

		if (screenWidth > screen.width || screenHeight > screen.height) {
			screenWidth = screen.width;
			screenHeight = screen.height;
		}

		for (var i=0; i<parent.childNodes.length; i++) {
			if (parent.childNodes[i].className == 'global_dlgTable') {
				parent.childNodes[i].style.width = (screenWidth / 2)+'px';
				parent.childNodes[i].style.left = (screenWidth / 4)+'px';
				parent.childNodes[i].style.top = ((screenHeight / 2) - parent.childNodes[i].offsetHeight) + 'px';
			}
		}
	}



	/*==================================================================================*\
	 *	Function:	global_guiAddOnClickEvent()
	 *	Purpose:	
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		none
	 *	Return:		none
	\*==================================================================================*/

	function global_guiAddOnClickEvent() {
		var elements;
		var string = '';

		if (document.all) elements = document.all;
		else if (document.getElementsByTagName && !document.all) elements = document.getElementsByTagName("*");

		for(i=0;i<elements.length;i++) {
			// add to links
			if (elements[i].tagName.toLowerCase() == "a" 
					&& elements[i].href.indexOf('#') == -1
					&& elements[i].href.indexOf('download=') == -1
					&& elements[i].href.indexOf('javascript') == -1
					&& !elements[i].getAttribute("target")
					&& !elements[i].getAttribute("rel")
				) {

				if (elements[i].getAttribute("onclick"))
					elements[i].setAttribute("onclick", "global_guiDlgLoader(); " + elements[i].getAttribute("onclick"));
				else
					elements[i].setAttribute("onclick", "global_guiDlgLoader();");
/*
			// add to buttons
			} else if (elements[i].tagName.toLowerCase() == "button" && elements[i].getAttribute("type") != "reset"
				) {

				// create absolute href
				if (elements[i].getAttribute("onclick") && elements[i].getAttribute("onclick").indexOf("open(") == -1)
					elements[i].setAttribute("onclick", "global_guiDlgLoader(); " + elements[i].getAttribute("onclick"));
				else if (!elements[i].getAttribute("onclick"))
					elements[i].setAttribute("onclick", "global_guiDlgLoader();");
*/
			}
		}
	}



	/*==================================================================================*\
	 *	Function:	global_guiDlgHelp()
	 *	Purpose:	toggles the help dialogue
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		@text : string, element id
	 *	Return:		none
	\*==================================================================================*/

	function global_guiDlgHelp(text) {
		if (document.getElementById("global_dlgHelp"))
			if (document.getElementById("global_dlgHelp").style.display == 'block') {
				document.getElementById('global_dialogues').style.display = 'none';
				document.getElementById("global_dlgShaddow").style.display = 'none';
				document.getElementById("global_dlgHelp").style.display = 'none';
			} else {
				document.getElementById("global_dlgBodyHelp").innerHTML = text;
				document.getElementById('global_dialogues').style.display = 'block';
				document.getElementById("global_dlgShaddow").style.display = 'block';
				document.getElementById("global_dlgHelp").style.display = 'block';
			}
	}



	/*==================================================================================*\
	 *	Function:	global_guiDlgAlert()
	 *	Purpose:	toggles the alert dialogue
	 *	Author:		Florian Tschoerner
	 *	LastMod:	06.06.2009
	 *	Params:		@text : string, element id
	 *	Return:		none
	\*==================================================================================*/

	function global_guiDlgAlert(title, text) {
		if (document.getElementById("global_dlgAlert"))
			if (document.getElementById("global_dlgAlert").style.display == 'block') {
				document.getElementById('global_dialogues').style.display = 'none';
				document.getElementById("global_dlgShaddow").style.display = 'none';
				document.getElementById("global_dlgAlert").style.display = 'none';
			} else {
				document.getElementById("global_dlgBodyAlertTitle").innerHTML = title;
				document.getElementById("global_dlgBodyAlertText").innerHTML = text;
				document.getElementById('global_dialogues').style.display = 'block';
				document.getElementById("global_dlgShaddow").style.display = 'block';
				document.getElementById("global_dlgAlert").style.display = 'block';
			}
	}



	/*==================================================================================*\
	 *	Function:	global_guiDlgLoader()
	 *	Purpose:	toggles ajax loader dialogue
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		@elem : string, element id
	 *	Return:		none
	\*==================================================================================*/

	function global_guiDlgLoader() {
		if (document.getElementById("global_dlgAjaxLoader"))
			if (document.getElementById("global_dlgAjaxLoader").style.display == 'none') {
				document.getElementById('global_dialogues').style.display = 'block';
				document.getElementById("global_dlgShaddow").style.display = 'block';
				document.getElementById("global_dlgAjaxLoader").style.display = 'block';
				global_guiAjaxLoader = true;
			} else {
				document.getElementById('global_dialogues').style.display = 'none';
				document.getElementById("global_dlgShaddow").style.display = 'none';
				document.getElementById("global_dlgAjaxLoader").style.display = 'none';
				global_guiAjaxLoader = false;
			}
	}



	/*==================================================================================*\
	 *	Function:	global_guiDlgPosition()
	 *	Purpose:	toggles ajax loader dialogue
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		@elem : string, element id
	 *	Return:		none
	\*==================================================================================*/

	function global_guiDlgPosition() {
		var topPos = 0;

		topPos+=(document.all?document.body.scrollTop:window.pageYOffset);
		
		document.getElementById("global_dialogues").style.top=topPos+"px";
	}



	/*==================================================================================*\
	 *	Function:	cookie_create()
	 *	Purpose:	creates a cookie
	 *	Author:		Florian Tschoerner
	 *	LastMod:	25.03.2010
	 *	Params:		none
	 *	Return:		none
	\*==================================================================================*/

	function cookie_create(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		} else {
			expires = "";
		}

		document.cookie = name+"="+value+expires+";";
	}



	/*==================================================================================*\
	 *	Function:	cookie_read()
	 *	Purpose:	reads a cookie
	 *	Author:		Florian Tschoerner
	 *	LastMod:	25.03.2010
	 *	Params:		none
	 *	Return:		none
	\*==================================================================================*/

	function cookie_read(name) {
		var cookie_read = name + "=";
		var ca = document.cookie.split(';');

		for(var i=0;i < ca.length;i++) {
			var c = ca[i];

			while (c.charAt(0) == ' ')
				c = c.substring(1,c.length);

			if (c.indexOf(cookieRead) == 0)
				return c.substring(cookieRead.length,c.length);
		}
		return null;
	}



	/*==================================================================================*\
     *	Function:	ajax_load()
     *	Purpose:	
     *	Author:		Florian Tschoerner
	 *	Lastmod:	30.12.2008
     *	Params:		none
	 *	Return:		none
    \*==================================================================================*/

	function ajax_load(div, path, params) {
		if (global_guiAjaxLoader == false)
			global_guiDlgLoader();

		new Ajax.Updater(div, path+"?ajax=1&"+params, {
				evalScripts:true, 
				onComplete:function(){
						if (global_guiAjaxLoader == true)
							global_guiDlgLoader();

						return true;
					},
				onFailure:function(){
						if (global_guiAjaxLoader == true)
							global_guiDlgLoader();
					}
				});
	}



	/*==================================================================================*\
     *	Function:	ajax_submit()
     *	Purpose:	
     *	Author:		Florian Tschoerner
	 *	Lastmod:	30.12.2008
     *	Params:		none
	 *	Return:		none
    \*==================================================================================*/

	function ajax_submit(div, params) {
		if (global_guiAjaxLoader == false)
			global_guiDlgLoader();

		var inputs = document.form.getElementsByTagName("input");

		for (var i = 0; i < inputs.length; i++) {
		   params = params+'&'+inputs[i].name+"="+inputs[i].value;
		}

		new Ajax.Updater(div, params+"&ajax=1", {
				evalScripts:true, 
				onComplete:function(){
						if (global_guiAjaxLoader == true)
							global_guiDlgLoader();
					},
				onFailure:function(){
						if (global_guiAjaxLoader == true)
							global_guiDlgLoader();
					}
				});
	}



	/*==================================================================================*\
	 *	Function:	ajax_select()
	 *	Purpose:	
	 *	Author:		Florian Tschoerner
	 *	Lastmod:	26.02.2010
	 *	Params:		none
	 *	Return:		none
	\*==================================================================================*/

	function ajax_select(target, path, params) {
		if (document.getElementById(target)) {
			ajax_load(target, path, params);

			alert(document.getElementById(target).options[0].value);
		}
	}