/*
** Evonux 2005-2008
** Evonux JavaScript Tools (EJST) - local version
**
** tested: IE 6.0/7.0, Firefox 2.0/3.0, Safari 2.0/3.0, Konqueror
**         Mac OS X, Windows XP/Vista, Linux
**
** All rights reserved
*/

if (Evonux && Evonux.IncludeRegister('THIS'))
  Evonux.Exit();

if (!Evonux) Evonux = {};

Evonux.Page = new Class({
    intro_full: true, // true if we are already on correct language page
    homepage_url: '',
    from_place_input: null,
    from_place_completer: null,
    to_place_input: null,
    to_place_completer: null,
    nb_passengers_input: null,
    nb_passengers_autocompleter: null,
    nb_luggage_input: null,
    nb_luggage_autocompleter: null,

    country_switch: null,
    country_switch_access: null,

    your_info_wrap: null,
    your_info: null,
    your_info_rank: 0,

    /*
    ** Inits complex display
    */
    initialize: function()
    {
	this.EnableIntro();
	this.TransformText();
	window.addEvent('resize', this.AdjustSizes.bind(this));
	// Places (viabooking)
	preloadTranslation('place_from_full', 'place_to_full');
	var from_place = $('from-place'),
	    to_place = $('to-place');
	if (from_place)
	{
	    from_place.addEvents({
		'focus': function () {
		    var from_place = $('from-place');
		    if (from_place.hasClass('unfilled'))
		    {
		      from_place.removeClass('unfilled');
		      from_place.setAttribute('value', '');
		    }
		},
		'blur': function () {
		    var from_place = $('from-place');
		    if (!from_place.getAttribute('value'))
		    {
		      from_place.addClass('unfilled');
		      from_place.setAttribute('value', $tc1('place_from_full'));
		    }
		}
	    });
	}
	if (to_place)
	{
	    to_place.addEvents({
		'focus': function () {
		    var to_place = $('to-place');
		    if (to_place.hasClass('unfilled'))
		    {
		      to_place.removeClass('unfilled');
		      to_place.setAttribute('value', '');
		    }
		},
		'blur': function () {
		    var to_place = $('to-place');
		    if (!to_place.getAttribute('value'))
		    {
		      to_place.addClass('unfilled');
		      to_place.setAttribute('value', $tc1('place_to_full'));
		    }
		}
	    });
	}
	// Date calendar
	if ($('date-tr'))
	  new Evonux.Calendar({parent:$('date-tr'), className:'evonux-calendar', hidden_className:'save', hiddenTimestamp:true, parent_className:'evonux-calendar'});
	// Autocompleter for viabooking 'from' and 'to' fields
	this.PlaceAutocomplete();
	// Country (language) switch
	this.CountrySwitch();
	// Home "Your info" box
	this.EnableYourInfo();
	// Make CO2 computer activated
	this.EnableCO2();
    },

    /*
    ** Transforms texts into images with nice font
    */
    TransformText: function()
    {
	// CONTENT
	//  - h1
	Evonux.Text.ToImg({node:$$('div#wrap div.content h1'), font:2});
	//  - h2
	Evonux.Text.ToImg({node:$$('div#wrap div.content h2'), font:2});
	//  - h3
	Evonux.Text.ToImg({node:$$('div#wrap div.content h3'), font:2});
    },

    /*
    ** Adjusts size of content so footer is at least at the bottom of the page
    */
    AdjustSizes: function()
    {
	
    },

    /*
    ** Adds event for introduction's page removing (after user click)
    */
    EnableIntro: function()
    {
	var body = document.getElement('body'),
	    intro = $('intro');
	if (!body.hasClass('intro')) return false;

	// For IE6 : sets intro height
	intro.setStyle('height', body.getStyle('height'));

	// Countries links
	var link = $$('div#intro div.content a'), l, url;
	for (var k = 0; k < link.length; k++)
	{
	  l = link[k]; url = l.getAttribute('href');
	  if (window.location == url)
	    l.evonux = {homepage_url:'', intro_full:true};
	  else
	    l.evonux = {homepage_url:l.getAttribute('href'), intro_full:false};
	  l.addEvent('click', function(e) {var e = new Event(e); e.stop(); this.RemoveIntro(e); return false;}.bind(this));
	}
    },
    /*
    ** Removes introduction page when user has clicked a country
    */
    RemoveIntro: function(e)
    {
	var e = new Event(e),
	    param = e.target.evonux;
	this.intro_full = param.intro_full;
	this.homepage_url = param.homepage_url;

	var intro = $('intro'),
	    //logo = document.getElement('div#intro div.logo'),
	    content = document.getElement('div#intro div.content'),
	    slogan = document.getElement('div#intro div.slogan'),
	    poster = document.getElement('div#intro div.poster');
	var anim_intro = new Fx.Styles(intro, {wait:false, duration: 500}),
	    //anim_logo = new Fx.Styles(logo, {wait:false, duration: 1000}),
	    anim_content = new Fx.Styles(content, {wait:false, duration: 1000}),
	    anim_poster = new Fx.Styles(poster, {wait:false, duration: 1000});
	/*
	anim_logo.start({
	    'opacity': [0.5, 0],
	    'margin-top': [59, -100]
	});
	*/
	anim_content.start({
	    'opacity': [0.5, 0]
	});
	anim_poster.start({
	    'opacity': [0.5, 0]
	});
	if (this.intro_full)
	{
	  anim_intro.start.delay(1000, anim_intro, {
	      'opacity': [1, 0]
	  });
	  setTimeout(this.RemoveIntro_do.bind(this), 1500);
	}
	else
	  setTimeout(function () {window.location = this.homepage_url}.bind(this), 800);
    },
    /*
    ** Removes introduction page from DOM
    */
    RemoveIntro_do: function()
    {
	var intro = $('intro'),
	    body = document.getElement('body');
	intro.remove();
	body.removeClass('intro');
    },

    /*
    ** Allows country switching in any page
    */
    CountrySwitch: function()
    {
	var access = $$('div#head div.banner a.change-country'),
	    list = $$('div#head div.banner ul.change-country');
	this.country_switch = list;
	this.country_switch_access = access;
	access.addEvent('mouseover', function (e) {
	    this.country_switch.setStyle('display', 'block');
	}.bind(this));
	list.addEvent('mouseleave', function (e) {
	    this.country_switch.setStyle('display', 'none');
	}.bind(this));
	window.addEvent('click', function (e) {
	    this.country_switch.setStyle('display', 'none');
	}.bind(this));
    },

    /*
    ** Enables places autocompletion
    */
    PlaceAutocomplete: function()
    {
	// From place
	this.from_place_input = $('from-place');
	if (this.from_place_input)
	{
	  this.from_place_completer = new Autocompleter.Ajax.Json(this.from_place_input, Evonux.SITE_ROOT +'/dyn.php?a=autocompleteplace', {
	      'url': Evonux.SITE_ROOT +'/dyn.php?a=autocompleteplace',
	      'postVar': 'q',
	      'inheritWidth': false,
	      'maxChoices': 20,
	      'onRequest': function(el) {
		  this.from_place_input.setStyle('background-image', 'url('+ Evonux.PIC_DIR +'/loading2.gif)');
	      }.bind(this),
	      'onComplete': function(el) {
		  this.from_place_input.setStyle('background-image', 'none');
	      }.bind(this),
	      'onSelect': function(el) {
		  evx_page.QuickBookEstimate();
	      }
	  });
	}
	// To place
	this.to_place_input = $('to-place');
	if (this.to_place_input)
	{
	  this.to_place_completer = new Autocompleter.Ajax.Json(this.to_place_input, Evonux.SITE_ROOT +'/dyn.php?a=autocompleteplace', {
	      'url': Evonux.SITE_ROOT +'/dyn.php?a=autocompleteplace',
	      'postVar': 'q',
	      'inheritWidth': false,
	      'maxChoices': 20,
	      'onRequest': function(el) {
		  this.to_place_input.setStyle('background-image', 'url('+ Evonux.PIC_DIR +'/loading2.gif)');
	      }.bind(this),
	      'onComplete': function(el) {
		  this.to_place_input.setStyle('background-image', 'none');
	      }.bind(this),
	      'onSelect': function(el) {
		  evx_page.QuickBookEstimate();
	      }
	  });
	}
	// Number of passengers
	this.nb_passengers_input = $('nb-passengers');
	if (this.nb_passengers_input)
	{
	  this.nb_passengers_completer = new Autocompleter.Ajax.Static(this.nb_passengers_input, '', {
	      'inheritWidth': true,
	      'maxChoices': 3,
	      'staticChoices': ['1','2','3'],
	      'onSelect': function(el) {
		  evx_page.QuickBookEstimate();
	      }
	  });
	}
	// Number of pieces of luggage
	this.nb_luggage_input = $('nb-luggage');
	if (this.nb_luggage_input)
	{
	  this.nb_luggage_completer = new Autocompleter.Ajax.Static(this.nb_luggage_input, '', {
	      'inheritWidth': true,
	      'maxChoices': 3,
	      'staticChoices': ['1','2','3'],
	      'onSelect': function(el) {
		  evx_page.QuickBookEstimate();
	      }
	  });
	}
    },

    /*
    ** Displays estimated price (if possible) into Quickbook field
    */
    QuickBookEstimate: function()
    {
	// Builds options
	//  - get
	var place1 = $('from-place').value,
	    place2 = $('to-place').value;
	if (!place1 || !place2) return false;
	var query_string = 'place1='+ place1 +'&place2='+ place2 +'&nb_passengers='+ $('nb-passengers').value +'&nb_luggage='+ $('nb-luggage').value,
	    ajax_option = {
		method: 'get',
		onSuccess: this.QuickBookEstimate_do.bind(this)
	    };
	new Ajax(Evonux.SITE_ROOT +'/dyn.php?a=estimateprice&'+ query_string, ajax_option).request();
    },
    QuickBookEstimate_do: function(text, xml)
    {
	if (text)
	  $$('div#wrap div.leftpart div.quickform div.results').setHTML(text);
    },

    /*
    ** Grabs data from "your info" and sort it into motioned data
    */
    EnableYourInfo: function()
    {
	var wrap = $('yourinfocontent');
	if (!wrap) return false;

	// Cuts data
	var re_file_cut = /[\n]{2,}/i,
	    text = wrap.innerHTML;
	text = text.replace(/<br\/?>/gi, "\n");
	var elt = text.split(re_file_cut);

	this.your_info = elt;
	this.your_info_wrap = wrap;

	this.YourInfoNext();
    },
    YourInfoNext: function(e)
    {
	var p = this.your_info_wrap,
	    anim_p = new Fx.Styles(p, {wait:false, duration:500});
	anim_p.start.delay(3500, anim_p, {
	    'opacity': [1, 0]
	});
	anim_p.start.delay(4000, anim_p, {
	    'opacity': [0, 1]
	});

	var data = this.your_info[this.your_info_rank];
	this.your_info_rank++;
	if (this.your_info_rank > this.your_info.length - 1) this.your_info_rank = 0;
	
	var re_cut = /^([^\n]+)[\n]{1}([^\r]+)$/i;
	re_cut.exec(data);
	this.your_info_wrap.setHTML('<strong>'+ RegExp.$1 +'</strong><br/>'+ RegExp.$2.replace(/\n/g, '<br/>'));

	setTimeout(this.YourInfoNext.bind(this), 4000);
    },

    /*
    ** Sends contact form
    */
    ContactFormSend: function()
    {
	var form = $('contactform');
	form.send({
	    onSuccess: this.ContactFormSend_do.bind(this)
	});
	return false;
    },
    ContactFormSend_do: function(text, xml)
    {
	var xml = new Evonux.XML(xml).GetArray();
	alert(xml['msg'][0]);
    },

    EnableCO2: function()
    {
	var co2 = $('co2comp');
	if (!co2) return false;
	var input = co2.getElement('input.km');
	input.addEvent('keyup', this.CO2comp.bind(this));

	if (input.value) this.CO2comp();
    },
    CO2comp: function()
    {
	var co2 = $('co2comp'),
	    res = co2.getElement('div.res'),
	    i = co2.getElement('input.km'),
	    nb_trees = 0;
	nb_trees = (parseInt(i.value) * 199) / 39000;
	res.setHTML(isNaN(nb_trees) ? 0 : Math.floatRound(nb_trees, 1) +' '+ $t('planted_trees'));
    },

    /*
    ** Opens input to send to a friend by email
    */
    SendToFriend: function(e)
    {
	var a = $('send-to-friend'),
	    div = $('send-friend-do');
	a.setStyle('display', 'none');
	div.setStyle('display', 'inline');
    },
    /*
    ** Asks for server to send an email
    */
    SendToFriendSubmit: function()
    {
	var email = $('friend-email').value,
	    ajax_option = {
		method: 'get',
		onSuccess: this.SendToFriendSubmit_do.bind(this)
	    };
	new Ajax(Evonux.SITE_ROOT +'/dyn.php?a=friendemail&email='+ email, ajax_option).request();
    },
    SendToFriendSubmit_do: function(text, xml)
    {
	alert(text);
    }
});

var evx_page;
window.addEvent('domready', function () {evx_page = new Evonux.Page();});

// ------------------------------------------------

Evonux.IntroSelect = new Class({
    input_obj: null,
    box: null,

    initialize: function (input_obj, box)
    {
	this.input_obj = $(input_obj);
	this.box = $(box);
	var i = this.input_obj,
	    b = this.box;
	i.addEvent('click', this.Toggle.bind(this));
	i.setAttribute('readonly', 'readonly');
	//b.addEvent('mouseup', this.Close.bind(this));
    },

    Toggle: function ()
    {
	this.box.setStyle('display',
			   this.box.getStyle('display') != 'none'
			   ? 'none'
			   : 'block')
    },
    Open: function ()
    {
	this.box.setStyle('display', 'block');
    },
    Close: function ()
    {
	this.box.setStyle('display', 'none');
    }
});

