/* ***********************************************************************************************
file:		functions.js
project: 	Nachaltiges Celle
author: 	Jens Späth (jms) 
contact: 	mail@jens-spaeth.de
date: 		13.08.2007

credits:	
		
summary:	show Login form
          

usage: 		No aeterno utroque vel, sed eirmod nominavi tincidunt cu.
		
dependencies:	
				
functions:		

known problems: 
*********************************************************************************************** */
//alert('Jep, link to functions.js is working!');


// Adding Events after Page Load
EVENT.addLoadEvent(function() {

	//Login: Plugs in the Login Fields if the "Anmelden"-Button is clicked
	if($('login_link')) 
		login();
	if($('form_container'))
		mailform();
		
	if(navigator.appVersion.indexOf("MSIE 6.0") != -1) {
		hover_ie6('login_link');
	}
});

function login () {
	//alert('true');
	// substitute  "Anmelden" A-tag with div
	substituteLink('login_link');
	
	// show login form
	EVENT.addEvent('login_link', 'click', function() {
		showLoginForm('login_link');
		setCursor();
	});	
	EVENT.addMultipleEvents ($$('input'), 'focus', function(event) {
	  //alert('call');
		if(!event) {
			event = window.event;
			//alert("window.event");
		}
		if(!event.target) {
			event.target = event.srcElement;
			//alert("event.srcElement");
		}
		event.target.select();
	});
	
}
// 
function mailform() {	
	EVENT.addMultipleEvents ($$('input'), 'focus', function(event) {
		if(!event)
			event = window.event;
		if(!event.target)
			event.target = event.srcElement
		event.target.select();
	});
  
	EVENT.addMultipleEvents($$('textarea'), 'focus', function(event) {
	  if(!event)
		  event = window.event;
		if(!event.target)
			event.target = event.srcElement;
		event.target.select();
	});
}


function showLoginForm(id) {
	elemA = $(id);
	//alert(elemA.tagName+' '+elemA.getAttribute('id'));
	elemA.setAttribute('class', 'invisible');
	elemA.setAttribute('className', 'invisible');
	//alert(elemA.getAttribute('className'));
	
	elemB = $('login_form');
	//alert(elemB.tagName+' '+elemB.getAttribute('id'));
	elemB.setAttribute('class','visible');
	elemB.setAttribute('className', 'visible');
	//alert(elemB.getAttribute('className'));
}
//Substitute the link (<a id="login_link"> with a <div id="login_link">)
function substituteLink(id) {
	//alert('calling substituteLink with: '+id);
	var elem = $(id);
	var parent = elem.parentNode;
	parent.removeChild(elem);

	var temp = document.createElement('div');
	temp.setAttribute('id', 'login_link'); 
	temp.appendChild(document.createTextNode('Anmelden'));
	//alert(temp.getAttribute('id')+' : '+temp.firstChild.data);
	
	var headline = $('personal_greeting').getElementsByTagName('h2')[0];
	//alert(headline.tagName);
	parent.insertBefore(temp, headline);
}

function setCursor() {
	var login_form = $('personal_greeting').getElementsByTagName('form')[0];
	var first_field = login_form.getElementsByTagName('input')[0];
	first_field.focus();
}
function hover_ie6(id) {
	//alert('function: hover_ie6: '+id)
	
	EVENT.addEvent(id, 'mouseover', function() {
	  //alert('mouseover');
		//$(id).setAttribute('class', 'highlite');
		//$(id).setAttribute('className', 'highlite');
		$(id).style.cssText = 'background: white; color: RGB(102,102,102);; border-color: RGB(202,202,204);';
		//alert('mouseOver: '+$(id).getAttribute('class'));
	});
	EVENT.addEvent(id, 'mouseout', function() {
	  //alert('mouseout');
		//$(id).setAttribute('class', 'normal');
		//$(id).setAttribute('className', 'normal');
		$(id).style.cssText = 'background: RGB(168,199,82); color: white; border-color: white;';
		//alert('mouseOut: '+$(id).getAttribute('className'));
	});
	
}