var globalActions_boxStyle = '';

var globalActions = {

   /**
	* Create a message msg, below main menu
	*
	* @param msg
	* @author henhus
	**/
	msg: function(msg) {
		$('messageBox').empty();
		if($('errorBox')) {
			$('errorBox').remove();
		};
		new Element('div', {'class':'messageBox','id':'messageBox2'}).setStyle('opacity',0).setHTML(msg).injectInside('messageBox');
		new Fx.Style('messageBox2', 'opacity', {duration: 500} ).start(1);
		var scroll = new Fx.Scroll(window, { wait: false, duration: 500, transition: Fx.Transitions.Quad.easeInOut }); 
		scroll.toElement('topPage');
	},

   /**
	* Create a error message err, below main menu
	*
	* @param err, send false to remove
	* @author henhus
	**/
	err: function(err) {
		// remove errorbox
		if(err == false){
			$('messageBox').empty();
			if($('errorBox')) {
				$('errorBox').remove(); 
			}
			return;
		}
		$('messageBox').empty();
		if($('errorBox')) {
			$('errorBox').empty();
			var ErrEl = $('errorBox'); 
		} else {
			var ErrEl = new Element('div', {'class':'errors','id':'errorBox'}).injectAfter('messageBox');
		};
		ErrEl.setStyle('opacity',0);
		new Element('p').setHTML(langArray['one.or.more.errors']).injectInside(ErrEl);
		var ulEl = new Element('ul').injectInside(ErrEl);
		err.each(function(e){
			new Element('li').setHTML(e).injectInside(ulEl);
		});
		new Fx.Style('errorBox', 'opacity', {duration: 500} ).start(1);
		var scroll = new Fx.Scroll(window, { wait: false, duration: 500, transition: Fx.Transitions.Quad.easeInOut }); 
		scroll.toElement('topPage');
	},

   /**
	* Create popup box, with param html as content.
	*
	* @param html
	* @author henhus
	**/
	box: function(html,sendback) {
		if (html != null) {
			globalActions_boxStyle = new Fx.Style('isRam', 'opacity', {duration: 500});
			globalActions_boxStyle.start(.4);
			(function(){
				if($type(html) == 'element') {
					var newPopEl = new Element('div', {'id':'popupElement'});
					html.injectInside(newPopEl);
					newPopEl.injectAfter('isRam');
				} else {
					new Element('div', {'id':'popupElement'}).setHTML(html).injectAfter('isRam');
				};
			}).delay(500);
			$('isRam').addEvent('click', function(){globalActions.box(null,sendback)});
		} else {
			if($('popupElement')) {
				$('isRam').removeEvent('click', function(){globalActions.box(null)});
				sendback();
				$('popupElement').remove();
				globalActions_boxStyle.start(1);
			}
		}
	}
};

var infoBoxArray = Array();
var informationBox = {
   /**
	* Hide all infoboxes with given class. Setup FX effect.
	*
	* @param class
	* @author henhus
	**/
	hideAll: function(className) {
		var fullClassName = '.'+className;
		var i = 0;
		var fx = Array();
      		$$(fullClassName).each(function(el) {
      			el.setStyle('opacity', '0');
      			fx = el.effect('opacity', {duration: 300});
      			infoBoxArray[i] = Array(el, fx);
      			i+=1;
      		});
	},

   /**
	* Show infobox in infoBoxArray with given index.
	*
	* @param index
	* @author henhus
	**/
	showInfoBox: function(index) {
		for (u = 0; u < infoBoxArray.length; u++) {
			if(u != index) {
				infoBoxArray[u][1].start(0);
			};
		};
		infoBoxArray[index][1].start(1);
	},

   /**
	* Hide infobox with given index.
	*
	* @param index
	* @author henhus
	**/
	hideInfoBox: function(index) {
		infoBoxArray[index][1].start(0);
	}
};

window.addEvent('domready', function() {
	// Focus the first input on page.
	var focus_input = $$('#content .inputFocus');
	if(focus_input.length >= 1) {
		focus_input[0].focus();
	}

	// Only setup loginform actions if loginform is present
	if ($('loginForm')) {
		// Setup enter keystroke submit!
		nn=(document.layers)?true:false;
		ie=(document.all)?true:false;
		function checkForEnter(e) {
		    var evt=(e)?e:(window.event)?window.event:null;
		    if(evt){
		        var key=(evt.charCode)?evt.charCode:((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0));
		        if(key==13){
		            $('loginForm').submit();
		        };
		    };
		};
		// End: Setup enter kestroke submit

		//var LoginEmailOrigText=$('login_email').value;
		$('login_email').addEvent('focus', function() {
			this.value='';
		});
		$('login_password').addEvent('focus', function() {
			this.setProperty('type','password');
			this.value='';
			// If enter is pressed, submit form
			document.onkeydown=checkForEnter;
			if(nn) {
				document.captureEvents(Event.KEYDOWN);
			};
		});

		// Clear submit form on keypress enter when we leave the form
		$('login_email').addEvent('blur', function() {
			document.onkeydown=null;
		});
		$('login_password').addEvent('blur', function() {
			document.onkeydown=null;
		});
		// END: Clear submit form on keypress enter when we leave the form

		var loginPwdValue=$('login_password').getProperty('value');
		if (!window.ie) {
			if (loginPwdValue == '') {
				$('login_password').setProperty('type','text')
				$('login_password').setProperty('value',langArray['password']);
			} else{
				$('login_password').setProperty('type','password');
			};
		} else {
			if (loginPwdValue == '') {
				$('login_password').setProperty('value',langArray['password']);
			}
		};
		// pwd clear text fix?
		$('login_password').setProperty('autocomplete','off');
		$('actionLoginFormSubmit').onclick = function(e) {
			new Event(e).stop();
			if (!window.ie6) {
				$('loginForm').submit();
			} else {
				document.getElementById('loginForm').submit();
			}
		};
	};

});


