// Color management
function changeBackgroundColor(selector, color) { a = $$(selector); for(var i=0;i< a.length;i++) {  a[i].style.backgroundColor = color; }}
function changeTextColor(a, color) { for(var i=0;i< a.length;i++) {  a[i].style.color = color; }}
function changeColor(color_position) {
	document.getElementById("page_header").style.backgroundPosition = color_position;
	document.getElementById("page_footer").style.backgroundPosition = color_position;
	if (color_position == "top")
	{	changeBackgroundColor( ".c1", "#366");				
		changeBackgroundColor( ".c2", "#699");				
		changeTextColor( $$("h1,h2,h3"), "#366");				
	} else if (color_position == "center")
	  { changeBackgroundColor( ".c1", "#060");				
		changeBackgroundColor( ".c2", "#090");				
		changeTextColor( $$("h1,h2,h3"), "#060");				
	} else if (color_position == "bottom")
	{   changeBackgroundColor( ".c1", "#C60");				
		changeBackgroundColor( ".c2", "#F60");				
		changeTextColor( $$("h1,h2,h3"), "#C60");				
	}
	Cookie.write("color", color_position, {duration:30});
}

// Login validation
function validatePresenceOf(title, value) { if (value.length < 1) { alert(title+" should not be blank."); return false; } else return true; }

function setTimeZone()
{  document.getElementById('timezone_offset').value = (new Date()).getTimezoneOffset();
   return true;
}

// 	Initialization
window.addEvent('domready', 
function() 
{	
	// Implement hide or show
	Element.implement({show: function() {this.setStyle('display','');},hide: function() {this.setStyle('display','none');}});
	
	// 	set default color
	var color = Cookie.read("color"); 
	if (color) changeColor(color);

	// Load tips only for non-IE6 browsers
	if  (!(Browser.Engine.trident && Browser.Engine.version <= 4 ))
	{  var textTip = new Tips('.textTip',{ className: 'tooltip', fixed: true, forInput: true });
	   var tooltip = new Tips('.toolTip',{ className: 'tooltip', fixed: true});
	}
	
	// Setup login scripts
	$("account").value = Cookie.read("account_name") || "";
	$("login_name").value = Cookie.read("login_name") || $("account").value ;	
	$('login_form').addEvent('submit', function(e) {
		if (  !( validatePresenceOf( "Account id", $("account").value, 5 ) &&
			  validatePresenceOf( "Email or login name", $("login_name").value, 5 ) &&  			   
			  validatePresenceOf( "Password", $("password").value, 5 ) &&  setTimeZone() ) 			  
			)  e.stop(); });
	$('member').addEvent('click', function(e) {	e.stop(); $("login_box").show();  
		if ($("account").value == "") $("account").focus(); else $("password").focus(); });		
		
	$('login_close').addEvent('click', function(e) { e.stop(); $("login_box").hide(); });		
	if ($("account").value  != "")  $("login_box").show();
} );		


