/** 
 * Initiate pages
 */
 function initiate_page() {
 	initiate_menu();
 	language_selection();
 }
/** 
 * Initiate the homepage
 */
 function initiate_homepage() {
 	initiate_page();
 	random_motto();
 	initiate_twitter();
 }
/** 
 * Initiate the thumbnail pages
 */
 function initiate_thumbnail_page() {
 	initiate_page();
 	initiate_thumbnails();
 }
/** 
 * Initiate the accordion pages
 */
 function initiate_accordion_page() {
 	initiate_page();
 	initiate_accordion();
 }
/** 
 * Initiate the quote page
 */
 function initiate_quote_page() {
 	initiate_page();
 	initiate_quote_form();
 }
 /** 
 * Initiate the main menu
 */
 function initiate_menu() {
 	var main_menu   = $('nav-main');
   	var effectInExecution=null;

	main_menu.setStyle({height:'30px'});
   	main_menu.observe('mouseover',function(){
   		if(effectInExecution) effectInExecution.cancel();
   		effectInExecution=new Effect.Morph(this,{style:'height:210px;', duration: 0.5 });
   	});
   	main_menu.observe('mouseout',function(){
   		if(effectInExecution) effectInExecution.cancel();
   		effectInExecution=new Effect.Morph(this,{style:'height:30px;', duration: 0.3 });
   	});
}
 /** 
 * Display a random motto
 */
 function random_motto() {
 	var mottos = $$('div.home-motto span');
 	var index  = Math.floor(Math.random() * mottos.size());
 	// hide all
 	mottos.each(function(motto) {
 		motto.setStyle({display:'none'});
 	});
 	// display random one
 	mottos[index].setStyle({display:'inline'});
}
/**
 * Create language selection list
 */
 function language_selection() {
 	var list_dropped   = false;
 	var language_items = $$('ul.select li');
 	var item_count     = language_items.size();
 	var first_item     = language_items.shift(); // set first item and remove it from array

 	first_item.observe('click',function() {
 		list_dropped ? list_dropped = language_selection_hide(language_items) : list_dropped = language_selection_show(language_items);
 	});
	language_items.each(function(item) {
		item.observe('mouseover',function() {
			list_dropped = language_selection_show(language_items);
		});
		item.observe('mouseout',function() {
			list_dropped = language_selection_hide(language_items);
		});
		item.observe('click',function() {
			var date = new Date();
			date.setTime(date.getTime()+(365*24*60*60*1000));
			document.cookie = 'hhce_preferred_lang='+ item.down('a').href.toQueryParams().lang +'; expires='+ date.toGMTString() +'; path=/';
		});
	});
}
function language_selection_show(items) {
	items.invoke('setStyle','display:block');
	return true;
}
function language_selection_hide(items) {
	items.invoke('setStyle','display:none');
	return false;
}
/**
 * Initiate the mouse event of the thumbnails
 */
 function initiate_thumbnails() {
 	 var thumbnails = $$("div.thumbnails div");
 	 var tooltips   = [];
 	 
 	 thumbnails.each(function(thumbnail,index) {
 	 	if (thumbnail.readAttribute('rel')) {
 	 		thumbnail.setStyle({ cursor:'pointer'}).observe('click',function() {
 	 			window.location = thumbnail.readAttribute('rel');
 	 		});
 	 	}
 	 	tooltips[index] = new Control.ToolTip(thumbnail,thumbnail.down().innerHTML+' - '+thumbnail.down(1).innerHTML,{
 	 		className: 'tooltip',
 	 		offsetLeft: function () { return (tooltips[index].container.getWidth() + 5) * -1; }
 	 	});  
 	 });
 }
/**
 * Initiate accordion
 */
 function initiate_accordion() {
 	 var a = new accordion('accordion');
 	 a.activate($$('#accordion .accordion_toggle')[0]);
 }
/**
 * Initiate the quote form
 */
 function initiate_quote_form() {
 	var fields = $('quote-form').getInputs('text');
 	var validation_fields = [];
 	var required_msg = $('msg-required_field').innerHTML;
 	var valid_email_msg = $('msg-invalid_email').innerHTML;
 	var othersource_field = $('othersource');
 	var othertarget_field = $('othertarget');

 	// Prepare all the input boxes
 	fields.each(function(field,index) {
 		validation_fields[index] = new LiveValidation(field, {onlyOnSubmit: true, validMessage: "", insertAfterWhatNode: field.up().next().down('span') });
		validation_fields[index].add(Validate.Presence, { failureMessage: required_msg });

 		switch(field.id) {
		case 'othersource':
			validation_fields[index].disable();
			othersource_field.hide();
			break;
		case 'othertarget':
			validation_fields[index].disable();
			othertarget_field.hide();
			break;
		case 'email':
			validation_fields[index].add(Validate.Email, { failureMessage: valid_email_msg });
			break;
		}
 	});
 	$('sourcelang').observe('change',function() {
 		if ($F(this) == 'other') {
 			othersource_field.enable().show().focus();
 		} else {
 			Form.Element.focus(othersource_field).hide().disable();
			this.focus();
 		}
 	});
 	$('targetlang').observe('change',function() {
 		if ($F(this) == 'other') {
 			othertarget_field.enable().show().focus();
 		} else {
 			Form.Element.focus(othertarget_field).hide().disable();
			this.focus();
 		}
 	});
 	$('reset').observe('click',function() {
 		fields.each(function(field) {
 			field.focus();
 		});
 		fields[0].focus();
 	});
 }
 /**
  * Scroll the tweets on the home page
  */
 function	initiate_twitter() {
	 var feed   = $('twitter-feed');
	 var current_effect = null;
	 var tweet;

	 feed.select('div[id|=tweet]').each(function(t) {
		t.setStyle({cursor:'pointer'});
		t.observe('click',function() {
			var classes = t.className.split(" ");
			window.location = classes[1].gsub("_","/");
		});
	 });

	 new PeriodicalExecuter(function(pe) {
		 if (current_effect) {
			 current_effect.cancel();
			 current_effect = null;
			 var e = tweet.remove();
			 e.show();
			 feed.insert(e);
		 }
		 tweet = feed.firstDescendant();
		 current_effect = new Effect.BlindUp(tweet, { duration: 1.5 });
	 },5);
 }

