var current = 0;

var Site = {
	
	start: function(){
		if ((navigator.appName).toLowerCase()!="microsoft internet explorer") $('side_bar_top').style.top = '255px'; //temporaire
		if ($('kwick')) Site.parseKwicks();
		Site.lastWorkInit();
	},
	
	parseKwicks: function(){
		var kwicks = $$('#kwick .kwick');
		var fx = new Fx.Elements(kwicks, {wait: false, duration: 200, transition: Fx.Transitions.quadOut});
		kwicks.each(function(kwick, i){
			kwick.addEvent('mouseenter', function(e){
				var obj = {};
				obj[i] = {
					'width': [kwick.getStyle('width').toInt(), 124]
				};
				kwicks.each(function(other, j){
					if (other != kwick){
						var w = other.getStyle('width').toInt();
						if (w != 62) obj[j] = {'width': [w, 62]};
					}
				});
				fx.start(obj);
			});
		});
		
		$('kwick').addEvent('mouseleave', function(e){
			var obj = {};
			kwicks.each(function(other, j){
				obj[j] = {'width': [other.getStyle('width').toInt(), 80]};
				if (j==3) obj[j] = {'width': [other.getStyle('width').toInt(), 70]};
			});			
			fx.start(obj);
		});
	},

	setOpacity: function(elem){
		if(elem.xOpacity>.99){
			elem.xOpacity = .99;
			return;
		}	
		elem.style.filter     = 'alpha(opacity=' + (elem.xOpacity*100) + ')';	
		elem.style.MozOpacity = elem.xOpacity;
		elem.style.opacity    = elem.xOpacity;	
	},
	
	lastWorkInit: function (){	
		css = document.createElement('link');
		css.setAttribute('href','../../style/style2.css');
		css.setAttribute('rel','stylesheet');
		css.setAttribute('type','text/css');
		document.getElementsByTagName('head')[0].appendChild(css);
		
		var	lastworks = document.getElementById('last_works').getElementsByTagName('div');
		for(i=1;i<lastworks.length;i++) lastworks[i].xOpacity = 0;
		lastworks[0].style.display = 'block';
		lastworks[0].xOpacity = .99;
	
		setTimeout(function() { Site.lastWorkFade(); },6000);	
	},
	
	lastWorkFade: function (){
		var	lastworks = document.getElementById('last_works').getElementsByTagName('div');
		
		cOpacity = lastworks[current].xOpacity;
		nIndex = lastworks[current+1]?current+1:0;
		nOpacity = lastworks[nIndex].xOpacity;
	
		cOpacity-=.05;
		nOpacity+=.05;
	
		lastworks[nIndex].style.display = 'block';
		lastworks[current].xOpacity = cOpacity;
		lastworks[nIndex].xOpacity = nOpacity;
	
		Site.setOpacity(lastworks[current]);
		Site.setOpacity(lastworks[nIndex]);
	
		if(cOpacity<=0)
		{
			lastworks[current].style.display = 'none';
			current = nIndex;
			setTimeout(function() { Site.lastWorkFade(); },6000);	
		}
		else
		{
			setTimeout(function() { Site.lastWorkFade(); },50);	
		}	
	}
};

function SendMail()
{
	if(document.getElementById('email').value!='' && document.getElementById('email').value!='email')
	{
		document.getElementById("notify").innerHTML  = "Envoie du mail en cours...";
		document.getElementById("notify").className  = "display";

		var xhr=null;
    
		if (window.XMLHttpRequest){ 
			xhr = new XMLHttpRequest();
		}
		else if (window.ActiveXObject){
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xhr.open("GET", "mail.php?" + $('contact_form').toQueryString(), true);
		xhr.send(null);
		
		setTimeout( 'document.getElementById("notify").innerHTML  = "Le mail &agrave; &eacute;t&eacute; envoyé avec succ&egrave;s.";', 500 );
		setTimeout( 'document.getElementById("notify").className  = "hide";', 2500 );
	}
	else
	{
		alert('Veuillez indiquer votre adresse e-mail');
	}
}

window.addEvent('domready', Site.start);