/*
// Site : http://coudert.tv
// Date : 2008-02-27
*/

//<![CDATA[

//
// Searchbox handler
window.addEvent('domready', function(e){
	$('rechercher_saisie').addEvent('click', function(){
		this.value = "";
	})
});


//
// Form Newsletter/Contact validation handler
window.addEvent('domready', function(e){
	$ES('input', 'content').addEvent('keyup', function(e){

		var field_to_check = this;
		var check_url      = "/check/"

		if(field_to_check.value.length > 2)
		{
			var field_check = new Ajax(check_url,
				{
					method: 'get',
					postBody: 'field_value=' + field_to_check.value + '&field_name=' + field_to_check.name,
					onComplete:  function(request)
						{
							eval(request) ? field_to_check.setAttribute("class", "ok") : field_to_check.setAttribute("class", "ko");
						}
				}).request();
		}else{
			field_to_check.removeAttribute("class");
		}
	})
});


//
// Form Newsletter/Contact validation handler
window.addEvent('domready', function(e){

	var forms = ['information', 'newsletter'];

	for(form in forms)
	{
		if($(forms[form]))
		{
			$(forms[form]).addEvent('submit', function(e)
			{
				e.preventDefault();

				var myFx = new Fx.Style('news_step_1', 'opacity', {duration: 600, wait: true}).start(1,0);
				var myFx = new Fx.Style('news_step_2', 'opacity', {duration: 600, wait: true}).start(0,1);
				var log;

				this.send({
					update: log,
					onComplete: function()
					{
						var myFx = new Fx.Style('news_step_2', 'opacity', {duration: 600, wait: true}).start(1,0);
						var myFx = new Fx.Style('news_step_3', 'opacity', {duration: 600, wait: true}).start(0,1);
					}
				});
				// alert("Check, Validation, Soumission...");
			})
		}
	}
});


//
// Products Pagination Handler
window.addEvent('domready', function(e){

	if(!$('produits')) return;

	var scroll = new Fx.Scroll('recherche_resultats', {
		wait: false,
		duration: 800,
		offset: {'x': 0, 'y': 0},
		transition: Fx.Transitions.Quad.easeInOut
	});

	var pages        = $$('#produits_pagination_page li a');
	var current      = null;
	var state_up     = '#BBBBB4';
	var state_down   = '#999';
	var state_effect =  {duration: 300, transition: Fx.Transitions.linear};

	pages.each(function(page, i)
	{
		page.addEvent('click', function(page, i){

			if(this === current) return;
			index = this.name.split("_")[1];
			scroll.toElement('line-' + index);

			if(current) current.effect('background-color', state_effect).start(state_up);
			this.effect('background-color', state_effect).start(state_down);

			current = this;
		});

		pages[0].effect('background-color', state_effect).start(state_down);
		current = pages[0];
	});
});


//
// Searchbox handler
window.addEvent('domready', function(e){
	$('bookmark').addEvent('click', function(e){

		var current_title = document.title;
		var current_href  = location.href + "?bookmark";
		var message       = "Fonctionnalité indisponible pour votre navigateur.\nVeuillez ajouter manuellement le site à vos favoris.\n\n- Safari (Pomme ou Ctrl + D).\n- FireFox (Pomme ou Ctrl + D).";

		if(window.sidebar){
			// Mozilla Firefox
			alert(message);
		}else if(window.external){
			// IExplorer
			window.external.AddFavorite(current_href, current_title);
		}else if(window.opera){
			// Opera
			return true;
		}else{
			alert(message);
		}

		e.preventDefault();
	})
});


//
// Google Maps handler
window.addEvent('domready', function(e){

	var gdir          = null;
	var geocoder      = null;
	var addressMarker = null;

	initialize();

	function initialize() {

		if(!$("coudert-map")) return;

		$("itineraire-validation").addEvent('click', function(){
			$("calcul-itineraire").addEvent('submit', function(event){
				new Event(event).stop();
				setDirections();
			});
			return false;
		});

		if(GBrowserIsCompatible())
		{
			var map  = new GMap2($("coudert-map"));
			var gdir = new GDirections(map, $("coudert-calcul-itineraire"));

			map.removeMapType(G_HYBRID_MAP);
			map.removeMapType(G_SATELLITE_MAP);

			map.enableScrollWheelZoom();

			GEvent.addListener(gdir, "load" , onGDirectionsLoad);
			GEvent.addListener(gdir, "error", handleErrors);

			map.setCenter(new GLatLng(43.7019626641646, 7.274665832519531), 16);

			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());

			// Create a base icon for the Coudert Icon.
			var baseIcon              = new GIcon();

			baseIcon.shadow           = "/images/contact/icone-magasin-coudert.png";
			baseIcon.iconSize         = new GSize(36, 49);
			baseIcon.shadowSize       = new GSize(36, 49);
			baseIcon.iconAnchor       = new GPoint(0, 49);
			baseIcon.infoWindowAnchor = new GPoint(9, 2);
			baseIcon.infoShadowAnchor = new GPoint(18, 25);

			// Creates a Gmarker.
			function create_icon(point, index, icone)
			{
				// Create the Coudert Icon.
				var coudert_icon      = new GIcon(baseIcon);
				coudert_icon.image    = icone;
				markerOptions         = {icon:coudert_icon};
				var marker            = new GMarker(point, markerOptions);

				return marker;
			}

			map.addOverlay(create_icon(new GLatLng(43.70159035858923, 7.273743152618408), 16, "/images/contact/icone-magasin-coudert.png"));
			map.addOverlay(create_icon(new GLatLng(43.70202471486905, 7.27595329284668), 16, "/images/contact/icone-parking-marshall.png"));

		}

		// Set direction
		function setDirections()
		{
			adresse_beg = $("adresse").value + " " + $("code_postal").value;
			adresse_end = '19 rue Tonduti de L\'escarène 06000 Nice France';

			gdir.load("from: " + adresse_beg + " to: " + adresse_end, {"locale": "fr_FR"});
		}

		// Handle Errors
		function handleErrors()
		{
			if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
				alert("Aucune adresse correspondate n'a pu être trouvé : " + gdir.getStatus().code);
			}else if (gdir.getStatus().code == G_GEO_SERVER_ERROR){
				alert("Erreur de communication réseau, veuillez réeesayer ultérieurement : " + gdir.getStatus().code);
			}else if (gdir.getStatus().code == G_GEO_MISSING_QUERY){
				alert("Requête incompléte, veuillez saisir votre adresse et votre code postal : " + gdir.getStatus().code);
			}else if (gdir.getStatus().code == G_GEO_BAD_KEY){
				alert("La clef utilisée est incorrecte ou ne correspond pas au domaine : " + gdir.getStatus().code);
			}else if (gdir.getStatus().code == G_GEO_BAD_REQUEST){
				alert("Votre adresse n'a pu être analysé : " + gdir.getStatus().code);
			}else{
				alert("Veuillez renseigner votre adresse et votre code postal.");
			}
		}

		function onGDirectionsLoad(){
		}
	}
});


//
// Categories/Products handler
window.addEvent('domready', function(e){

	var categories = $$('div.categorie');
	
	categories.each(function(element)
	{
		var categorie_effect       = new Fx.Styles(element, {duration:300, wait:false});
		var categorie_titre_effect = new Fx.Styles(element.getElement('h1'), {duration:300, wait:false});
		var sous_categorie_effect  = new Fx.Slide(element.getElement('ul'), {duration:300, wait:false}).hide();

		element.addEvent('mouseenter', function(e)
		{
			e = new Event(e);
			sous_categorie_effect.slideIn();
			e.stop();

			categorie_effect.start({
				'background-color' : '#1F1F1F'
			});

			categorie_titre_effect.start({
				'color'           : '#F60'
			});
		});
	
		element.addEvent('mouseleave', function(e)
		{
			e = new Event(e);
			sous_categorie_effect.slideOut();
			e.stop();

			categorie_effect.start({
				'background-color' : '#FFF'
			});

			categorie_titre_effect.start({
				'color'           : '#AAA'
			});

		});
	});	
});

//]]>
