window.addEvent('domready', function() {

	var cookie = readCookie("style");
  	var title = cookie ? cookie : getPreferredStyleSheet();
  	setActiveStyleSheet(title);
  	

	var szNormal = 230, szSmall  = 200, szFull   = 290;
	 
	var kwicks = $$("#kwicks .kwick");
	var fx = new Fx.Elements(kwicks, {wait: false, duration: 300, transition: Fx.Transitions.Back.easeOut});
	kwicks.each(function(kwick, i) {
		kwick.addEvent("mouseenter", function(event) {
			var o = {};
			o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
			kwicks.each(function(other, j) {
				if(i != j) {
					var w = other.getStyle("width").toInt();
					if(w != szSmall) o[j] = {width: [w, szSmall]};
				}
			});
			fx.start(o);
		});
	});
	 
	$("kwicks").addEvent("mouseleave", function(event) {
		var o = {};
		kwicks.each(function(kwick, i) {
			o[i] = {width: [kwick.getStyle("width").toInt(), szNormal]}
		});
		fx.start(o);
	})


	var rightFx = new Fx.Tween($('right-icon'), {duration:1000, wait:false, transition:'expo:in:out'});

	// Styles pour menu principal
	var list = $$('#menu li a');
	list.each(function(element) {

		var fx = new Fx.Morph(element, {duration:200, wait:false});
	 
		element.addEvent('mouseenter', function(){
			fx.start({
				'opacity': 0.1
			});
		});
	 
		element.addEvent('mouseleave', function(){
			fx.start({
				'opacity': 1
			});
		});
	 
	});
	
	// Styles pour menu de droite
	var list = $$('#menu-right li a');
	list.each(function(element) {

		var fx = new Fx.Morph(element, {duration:400, wait:false});
	 
		element.addEvent('mouseenter', function(){
			if (!Browser.Engine.trident) {
				fx.start({
					'opacity': 1
				});
			}
		});
	 
		element.addEvent('mouseleave', function(){
			if (!Browser.Engine.trident) {
				fx.start({
					'opacity': 0.1
				});
			}
		});
	 
	});

	// Gestion des tips
	var list = $$('.tips');
	list.each(function(element) {
		var myTips = new Tips('.tips', {className:'tooltip'});
	
		myTips.addEvent('show', function(tip){
	    	tip.fade('0.95');
		});
	
		myTips.addEvent('hide', function(tip){
		    tip.fade('out');
		});
	});	
	
//	$('tip1').store('tip:title', 'Origin\'elle Esthétique');
	if ($('tip1') != null) {
		$('tip1').store('tip:title', 'Origin\'elle Esth&eacute;tique');
		$('tip1').store('tip:text', 'Esth&eacute;ticienne &agrave; domicile dans la r&eacute;gion de Saint Nazaire.<br/>D&eacute;veloppement et charte graphique.<br/><img src="gfx/ref-originelle.jpg"/>');
	}
	if ($('tip2') != null) {
		$('tip2').store('tip:title', 'Amplinks Evolution');
		$('tip2').store('tip:text', 'Pr&eacute;sentation de la soci&eacute;t&eacute; de services informatiques et de ses produits.<br/><img src="gfx/ref-amplinks.jpg"/>');
	}
	if ($('tip3') != null) {
		$('tip3').store('tip:title', 'Manerik Surf Club');
		$('tip3').store('tip:text', 'Site communautaire d\'une association de Surf en Loire Atlantique.<br/><img src="gfx/ref-manerik.jpg"/>');
	}
	if ($('tip4') != null) {
		$('tip4').store('tip:title', 'Logo du Manerik Surf Club');
		$('tip4').store('tip:text', '<br/><img src="gfx/ref-logo-manerik.png"/>');
	}
	if ($('tip5') != null) {
		$('tip5').store('tip:title', 'Logo Origin\'elle esth&eacute;tique');
		$('tip5').store('tip:text', '<br/><img src="gfx/ref-logo-originelle.jpg"/>');
	}
	if ($('tip6') != null) {		$('tip6').store('tip:title', 'Flyer Origin\'elle esth&eacute;tique');		$('tip6').store('tip:text', '<img src="gfx/ref-flyers-originelle.jpg"/>');	}	
	// Affichage progressif du logo droit
	$('right-icon').fade('hide');
	rightFx.start('opacity', 1);
		
	// Sélection des champs du formulaire
	var list = $$('#table-form td .form-input');
	list.each(function(element) {

		var fx = new Fx.Morph(element, {duration:300, wait:false});
	 
		element.addEvent('focus', function(){
			fx.start({
				'background-color': '#c9f398'
			});
		});
	 
		element.addEvent('blur', function(){
			fx.start({
				'background-color': '#ffffff'
			});
		});
	 
	});
	
	// Gestion de l'envoi du formulaire
	$('myForm').addEvent('submit', function(e) {
		//Prevents the default submit event from loading a new page.
		e.stop();

		//Set the options of the form's Request handler. 
		this.set('send', {onComplete: function(response) { 
			var result = response.split('|');
			alert(result[1]);
			if (result[0] == 1) {
				//Fermer le slider
				mySlide.slideOut();
			}
			
			//log.removeClass('ajax-loading');
			//log.set('html', response);
		}});
		//Send the form.
		this.send();
	});
});


function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function getActiveStyleSheet() {
var i, a;
 for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  if(a.getAttribute("rel").indexOf("style") != -1
  && a.getAttribute("title")
  && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

