/*======================================================================================*\
 * 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-2008, Bluewing, Florian Tschoerner
 * @link             http://www.bluewing.biz
 * @package          bluewing
 * @since            Bluewing v 2.0.1
 * @version          
 * @modifiedby       
 * @lastmodified     18.06.2008
 * @license          http://www.opensource.org/licenses/mit-license.php The MIT License
\*======================================================================================*/

	var bframe_guiAjaxLoader;



	/*==================================================================================*\
	 *	Function:	bframe_guiScreenInit()
	 *	Purpose:	initializes shaddow
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		none
	 *	Return:		none
	\*==================================================================================*/

	function bframe_guiScreenInit() {
		bframe_guiShaddowInit();

		bframe_guiDialogueInit();

		// check if java script is enabled
		document.getElementById('bframe_dlgJavaScript').style.display = 'none';

		// check if cookies are enabled
		if (navigator.cookieEnabled == true)
			document.getElementById('bframe_dlgCookies').style.display = 'none';

		// check if popups are enabled
		document.getElementById('bframe_dlgPopup').style.display = 'none';

		// hide help dialogue
		document.getElementById('bframe_dlgHelp').style.display = 'none';
		
		// hide ajax loader dialogue
		document.getElementById('bframe_dlgAjaxLoader').style.display = 'none';
		bframe_guiAjaxLoader = false;

		// hide alert dialogue
		document.getElementById('bframe_dlgAlert').style.display = 'none';

		// hide dialogue shaddow
		document.getElementById('bframe_dlgShaddow').style.display = 'none';
		document.getElementById('bframe_dialogues').style.display = 'none';

		bframe_guiAddOnClickEvent();

		if (document.getElementById('clock'))
			startClock();
	}



	/*==================================================================================*\
	 *	Function:	bframe_guiShaddowInit()
	 *	Purpose:	initializes shaddow
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		none
	 *	Return:		none
	\*==================================================================================*/

	function bframe_guiShaddowInit() {
		var elem = document.getElementById("bframe_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:	bframe_guiDialogueInit()
	 *	Purpose:	initializes dialogues
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		none
	 *	Return:		none
	\*==================================================================================*/

	function bframe_guiDialogueInit() {
		var parent = document.getElementById("bframe_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 == 'bframe_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:	bframe_guiAddOnClickEvent()
	 *	Purpose:	
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		none
	 *	Return:		none
	\*==================================================================================*/

	function bframe_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", "bframe_guiDlgLoader(); " + elements[i].getAttribute("onclick"));
				else
					elements[i].setAttribute("onclick", "bframe_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", "bframe_guiDlgLoader(); " + elements[i].getAttribute("onclick"));
				else if (!elements[i].getAttribute("onclick"))
					elements[i].setAttribute("onclick", "bframe_guiDlgLoader();");

			}
		}
	}



	/*==================================================================================*\
	 *	Function:	bframe_guiDlgHelp()
	 *	Purpose:	toggles the help dialogue
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		@text : string, element id
	 *	Return:		none
	\*==================================================================================*/

	function bframe_guiDlgHelp(text) {
		if (document.getElementById("bframe_dlgHelp"))
			if (document.getElementById("bframe_dlgHelp").style.display == 'block') {
				document.getElementById('bframe_dialogues').style.display = 'none';
				document.getElementById("bframe_dlgShaddow").style.display = 'none';
				document.getElementById("bframe_dlgHelp").style.display = 'none';
			} else {
				document.getElementById("bframe_dlgBodyHelp").innerHTML = text;
				document.getElementById('bframe_dialogues').style.display = 'block';
				document.getElementById("bframe_dlgShaddow").style.display = 'block';
				document.getElementById("bframe_dlgHelp").style.display = 'block';
			}
	}



	/*==================================================================================*\
	 *	Function:	bframe_guiDlgAlert()
	 *	Purpose:	toggles the alert dialogue
	 *	Author:		Florian Tschoerner
	 *	LastMod:	06.06.2009
	 *	Params:		@text : string, element id
	 *	Return:		none
	\*==================================================================================*/

	function bframe_guiDlgAlert(title, text) {
		if (document.getElementById("bframe_dlgAlert"))
			if (document.getElementById("bframe_dlgAlert").style.display == 'block') {
				document.getElementById('bframe_dialogues').style.display = 'none';
				document.getElementById("bframe_dlgShaddow").style.display = 'none';
				document.getElementById("bframe_dlgAlert").style.display = 'none';
			} else {
				document.getElementById("bframe_dlgBodyAlertTitle").innerHTML = title;
				document.getElementById("bframe_dlgBodyAlertText").innerHTML = text;
				document.getElementById('bframe_dialogues').style.display = 'block';
				document.getElementById("bframe_dlgShaddow").style.display = 'block';
				document.getElementById("bframe_dlgAlert").style.display = 'block';
			}
	}



	/*==================================================================================*\
	 *	Function:	bframe_guiDlgLoader()
	 *	Purpose:	toggles ajax loader dialogue
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		@elem : string, element id
	 *	Return:		none
	\*==================================================================================*/

	function bframe_guiDlgLoader() {
		if (document.getElementById("bframe_dlgAjaxLoader"))
			if (document.getElementById("bframe_dlgAjaxLoader").style.display == 'none') {
				document.getElementById('bframe_dialogues').style.display = 'block';
				document.getElementById("bframe_dlgShaddow").style.display = 'block';
				document.getElementById("bframe_dlgAjaxLoader").style.display = 'block';
				bframe_guiAjaxLoader = true;
			} else {
				document.getElementById('bframe_dialogues').style.display = 'none';
				document.getElementById("bframe_dlgShaddow").style.display = 'none';
				document.getElementById("bframe_dlgAjaxLoader").style.display = 'none';
				bframe_guiAjaxLoader = false;
			}
	}



	/*==================================================================================*\
	 *	Function:	bframe_guiDlgPosition()
	 *	Purpose:	toggles ajax loader dialogue
	 *	Author:		Florian Tschoerner
	 *	LastMod:	03.06.2009
	 *	Params:		@elem : string, element id
	 *	Return:		none
	\*==================================================================================*/

	function bframe_guiDlgPosition() {
		var topPos = 0;

		topPos+=(document.all?document.body.scrollTop:window.pageYOffset);
		
		document.getElementById("bframe_dialogues").style.top=topPos+"px";
	}



	/*==================================================================================*\
     *	Function:	ajaxLoad()
     *	Purpose:	
     *	Author:		Florian Tschoerner
	 *	Lastmod:	30.12.2008
     *	Params:		none
	 *	Return:		none
    \*==================================================================================*/

	function ajaxLoad(div, path, params) {
		if (bframe_guiAjaxLoader == false)
			bframe_guiDlgLoader();

		new Ajax.Updater(div, path+"?ajax=1&"+params, {
				evalScripts:true, 
				onComplete:function(){
						if (bframe_guiAjaxLoader == true)
							bframe_guiDlgLoader();
					},
				onFailure:function(){
						if (bframe_guiAjaxLoader == true)
							bframe_guiDlgLoader();
					}
				});
	}



	/*==================================================================================*\
     *	Function:	ajaxSubmit()
     *	Purpose:	
     *	Author:		Florian Tschoerner
	 *	Lastmod:	30.12.2008
     *	Params:		none
	 *	Return:		none
    \*==================================================================================*/

	function ajaxSubmit(div, params) {
		if (bframe_guiAjaxLoader == false)
			bframe_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 (bframe_guiAjaxLoader == true)
							bframe_guiDlgLoader();
					},
				onFailure:function(){
						if (bframe_guiAjaxLoader == true)
							bframe_guiDlgLoader();
					}
				});
	}
	
	
	
