var PortailSL = new Class({
	
	getOptions: function(){
		return {
			onOpen: false,
			onClose: Class.empty,
			openWidth: window.getWidth()-200,
			transition: Fx.Transitions.Sine.easeOut,
			duration:400,
			open: null,
			border: 0
		};
	},
	initialize: function(elements, options){
		this.setOptions(this.getOptions(), options);
		this.elements = $$(elements);
		this.widths = {};
		this.widths.closed = this.elements[0].getStyle('width').toInt();
		this.widths.openSelected = Math.round(window.getWidth()-200);
		this.widths.openOthers = Math.round(((this.widths.closed*this.elements.length) - (this.widths.openSelected+this.options.border)) / (this.elements.length-1))
		
		
		this.fx = new Fx.Elements(this.elements, {wait: false, duration: this.options.duration, transition: this.options.transition});
		
		this.elements.each(function(el,i){
			el.addEvent('mouseenter', function(e){
				new Event(e).stop();
				this.reset(i);
				
			}.bind(this));
			
			el.addEvent('mouseleave', function(e){
				new Event(e).stop();
				this.reset(this.options.open);
				
			}.bind(this));
			
			var obj = this;
			
			el.addEvent('click', function(e){

				if(obj.options.onOpen){
					new Event(e).stop();
					if(obj.options.open == i){
						obj.options.open = null;
						obj.options.onClose(this.href, i);
					}else{
						obj.options.open = i;
						obj.options.onOpen(this.href, i);
					}
					
					
				}
				
			})
			
		}.bind(this));
		
		if(this.options.open){
			if($type(this.options.open) == 'number'){
				this.reset(this.options.open);
			}else{
				this.elements.each(function(el,i){
					if(el.id == this.options.open){
						this.reset(i);
					}
				},this);
			}
		}
		
	},
	
	reset: function(num){
		if($type(num) == 'number'){
			var width = this.widths.openOthers;
			if(num+1 == this.elements.length){
				width += this.options.border;
			}
		}else{
			var width = Math.round((window.getWidth()/3)-2);
			
		}
		
		var obj = {};
		this.elements.each(function(el,i){
			var w = width;
			if(i == this.elements.length-1){
				w = width+5
			}
			obj[i] = {'width': w};
		}.bind(this));
		
		if($type(num) == 'number'){
			obj[num] = {'width': Math.round(window.getWidth()-200)};

		}
				
		this.fx.start(obj);
		




	}
	
});

PortailSL.implement(new Options);
PortailSL.implement(new Events);

function InitPage(){
	var screenHeight = window.getHeight();
	var screenWidth = window.getWidth();
	var OpenedLinkWidth = screenWidth-200;
	var ClosedLinkWidth = Math.round(screenWidth/3) ;
	$('PortailSL').setStyles({
		width: screenWidth,
		height: screenHeight-30
	});
	$('ulMenu').setStyles({
		width: Math.round(screenWidth*2),
		height: screenHeight
	});
	$('aSL').setStyles({
		width: ClosedLinkWidth,
		height: screenHeight
	});
	$('aEP').setStyles({
		width: ClosedLinkWidth,
		height: screenHeight
	});
	$('aSV').setStyles({
		width: ClosedLinkWidth,
		height: screenHeight
	});
	$('lSV').setStyle('width',ClosedLinkWidth-120);
	$('lEP').setStyle('width',ClosedLinkWidth-120);
	$('lSL').setStyle('width',ClosedLinkWidth-120);
	OpenedMorphOptions = {
		'width': Math.round((window.getWidth()-320)/2),
		'margin-top': 100,
		'margin-left': Math.round(OpenedLinkWidth/4)
	};
	ClosedMorphOptions = {
		'width': Math.round((window.getWidth()/3)-120),
		'margin-top': 300,
		'margin-left': Math.round(ClosedLinkWidth/5)
	};	
}
function checkTeaser(teaser){
	if(teaser['url']!=''){
		SqueezeBox.fromElement(window, {url:teaser['url'],handler:teaser['handler'],size:{x: teaser['width'], y: teaser['height']}} );
	}
}
function displayLabel(disp,elm){
	var alt = elm.replace('l','a');
	if(disp){
		$(alt).setStyle('background-position','0 -'+ Math.round(1500-window.getHeight()) +'px' );
	}else{
		$(alt).setStyle('background-position','left bottom');
	}
}
function checkBrowser(){
	var browser = navigator.appName;
	var version = navigator.appVersion;
	if(version.indexOf("MSIE")!=-1){
		var tmp = version.split("MSIE")
		var appVers = parseFloat(tmp[1]);
		if(appVers < 7){
			if(confirm('Votre navigateur internet est obsolète et ne permet pas un affichage correct de notre site,\n\nVeuillez installer une version plus récente\n\nou utilisez Mozilla Firefox, Safari, Opéra ou encore Chrome qui sont bien plus performants.\n\nSinon cliquez sur Ok pour mettre à jour Internet Explorer sur Windows Update, sur Annuler pour continuer sur Sessionlibre.com')){
				window.location = 'http://v4.windowsupdate.microsoft.com/fr/default.asp';
			}
		}
	}
}

window.addEvents({
	'domready': function(){
		checkBrowser();
		InitPage();
		$(document.body).getElements('a[id^=a]').each(function(el){
			el.addEvent('mouseenter',function(evt){
				new Event(evt).stop();
				var i = el.getElementsByTagName('img');
				var anotherEl = $(i[0].id);
				var myMorph = new Fx.Morph(anotherEl, {wait: false, duration: 450, transition: Fx.Transitions.Sine.easeOut,onStart:function(){displayLabel(true,i[0].id);}}).start(OpenedMorphOptions);
			});
			el.addEvent('mouseleave',function(evt){
				new Event(evt).stop();
				var i = el.getElementsByTagName('img');
				var anotherEl = $(i[0].id);
				var myMorph = new Fx.Morph(anotherEl, {wait: false, duration: 450, transition: Fx.Transitions.Sine.easeIn,onStart:function(){displayLabel(false,i[0].id);}}).start(ClosedMorphOptions);
			});
		});
		var myPortal = new PortailSL($$('#PortailSL a'),{border:2, onOpen:function(e,i){alert(e);}});
		var MorphSL = new Fx.Morph($('lSL'), {wait: false, duration: 450, transition: Fx.Transitions.Sine.easeIn}).start(ClosedMorphOptions);
		var MorphSV = new Fx.Morph($('lSV'), {wait: false, duration: 450, transition: Fx.Transitions.Sine.easeIn}).start(ClosedMorphOptions);
		var MorphEP = new Fx.Morph($('lEP'), {wait: false, duration: 450, transition: Fx.Transitions.Sine.easeIn}).start(ClosedMorphOptions);
 	},	
	'load': function(){
		InitPage();
// 		checkTeaser(teaser);
 	},
	'resize': function(){
		InitPage();
 	}
 });
