Event.observe(window, 'load', function(){
	initTimeLeft();
	setTimeout(function(){headerPhoto();}, 6000);
});

function initTimeLeft(){
	return false;
	if($$('.news').size()){
		var target = new Date(2011, 04, 19, 18, 00, 00);
		target = Math.round(target.getTime()/1000);
		
		var left = new Date();
		left = Math.round(left.getTime()/1000);
		left = target-left;
		
		var d = Math.floor(left/(60*60*24));
		if(d > 30) return false;
		
		var h = Math.floor((left/(60*60))-(d*24));
		var m = Math.floor((left/60)-(d*24*60+h*60));
		var s = Math.floor(left-(d*24*60*60+h*60*60+m*60));
		
		if(d < 10) d = '0'+d;
		if(h < 10) h = '0'+h;
		if(m < 10) m = '0'+m;
		if(s < 10) s = '0'+s;	
		
		var timeleft = new Element('div', {'id': 'timeleft'});
		var html = '	<h2 style="color: #FFFFFF;">Inauguration officielle du Casino dans...</h2>';
		html += '	<div style="margin-left: 47px;">'+d+'</div>';
		html += '	<div style="margin-left: 51px;">'+h+'</div>';
		html += '	<div style="margin-left: 55px;">'+m+'</div>';
		html += '	<div style="margin-left: 52px;">'+s+'</div>';
		timeleft.innerHTML = html;
		($$('.news').first()).insert({'before': timeleft});
		
		if(document.attachEvent){
			(timeleft.select('div')).collect(function(element){ element.setStyle({marginTop: '42px'}); });
		}

		function updateTimeLeft(target){
			var left = new Date();
			left = Math.round(left.getTime()/1000);
			left = target-left;
			
			var d = Math.floor(left/(60*60*24));
			var h = Math.floor((left/(60*60))-(d*24));
			var m = Math.floor((left/60)-(d*24*60+h*60));
			var s = Math.floor(left-(d*24*60*60+h*60*60+m*60));
			
			if(d < 10) d = '0'+d;
			if(h < 10) h = '0'+h;
			if(m < 10) m = '0'+m;
			if(s < 10) s = '0'+s;

			var day = $('timeleft').down(1);
			var hour = $('timeleft').down(2);
			var minute = $('timeleft').down(3);
			var second = $('timeleft').down(4);
			
			
			if(day.innerHTML != d) 		day.innerHTML = d;
			if(hour.innerHTML != h) 	hour.innerHTML = h;
			if(minute.innerHTML != m) 	minute.innerHTML = m;
			if(second.innerHTML != s) 	second.innerHTML = s;
			
		}
		new PeriodicalExecuter(function(timer){ updateTimeLeft(target) }, 1);
	}
}

function slide(imgs){
	var gallery = new Element('div', {'id': 'gallery'});
	imgs[0].insert({'before': gallery});
	
	var table = new Element('table');
	gallery.insert({'top': table});
	
	imgs.each(function(img){
		var td = new Element('td');
		td.insert({'top': img});
		table.insert({'bottom': td});
	});				
	
	var preview = new Element('div', {'id': 'preview'});
	imgs.each(function(img){
		var img = new Element('img', {'src': img.src});
		img.on('click', function(){
			this.lightbox();
		});
		preview.insert({'bottom': img});
	});
	gallery.insert({'after': preview});
	
	var way = 1;
	new PeriodicalExecuter(function(timer){
		var current_margin = parseInt(table.getStyle('marginLeft'));
		if(!current_margin) current_margin = 0;
		if(current_margin == 0 || Math.abs(current_margin) > table.getWidth()-gallery.getWidth()) way = -way;
		table.setStyle({marginLeft: (current_margin+(way*10))+'px'});
	}, 0.08);
}

function makeSubMenu(parent, links){
	//On recherche le parent
	var lis = $$('#menu li');
	var li_parent = null;
	lis.each(function(li){
		var href = li.getElementsByTagName('a')[0];
		if(href){
			href = href.toString();
			href = href.replace(/http:\/\/[a-z\.\-]+/, '', href);
			if(href == parent){
				li_parent = li;
			}
		}
	});	
	li_parent.innerHTML += '<div style="min-width: '+li_parent.offsetWidth+'px;">'+links+'</div>'; 
	li_parent.addClassName('parent');
}

function viewimage(url){
	var body = $$('body').first();
	
	//Ecran gris
	var grey = new Element('div');
	grey.setStyle({width: '100%', height: '100%', position: 'fixed', top: '0px', left: '0px', zIndex: '100', backgroundColor: '#000000'});
	grey.setOpacity(0);
	body.insert({top: grey});
	$R(0, 5).each(function(i){
		Element.setOpacity.delay(0.04*1, grey, (i/10));
	});
	
	//Ouverture de la zone d'affichage
	var mask = new Element('div');
	mask.setStyle({width: '0px', height: '0px', position: 'fixed', top: '50%', left: '50%',  zIndex: '101', backgroundColor: '#FFFFFF', border: '3px solid #FFFFFF'});
	body.insert({top: mask});
	
	//Loader
	var loader = new Element('img', {src : '/site/templates/casino/loader.gif'});
	loader.setStyle({position: 'fixed', top: '50%', left: '50%', marginLeft: '-30px', marginTop: '-30px',  zIndex: '101', backgroundColor: '#FFFFFF', border: '5px solid #FFFFFF', borderTop: '0px', borderLeft: '0px'});
	mask.insert({top: loader});

	var img = new Image();
	img.title = 'Fermer';
	img.on('load', function(){
		width = img.width;
		height = img.height;
		var max = document.viewport.getDimensions();
		max.width -= 80;
		max.height -= 80;
		if(width > max.width){
			height = Math.round((height*max.width)/width);
			width = max.width;
		}
		if(height > max.height){
			width = Math.round((width*max.height)/height);
			height = max.height;
		}
		img.setStyle({width: width+'px', height: height+'px', cursor: 'pointer'});
		img.setOpacity(0.5);
		
		//Action de fermeture
		img.on('click', function(){
			mask.remove();
			$R(0, 5).each(function(i){
				Element.setOpacity.delay(0.04*i, grey, ((5-i)/10));
			});
			Element.remove.delay(0.2, grey);
		});
		
		//Agrandissement de la zone d'affichage
		var time_log = 0;
		$R(0, 19).each(function(i){
			var current_width = ((width/20)*(i+1));
			var current_height = ((height/20)*(i+1));
			var current_margin_left = -(current_width/2);
			var current_margin_top = -(current_height/2);
			Element.setStyle.delay(0.02*i, mask, ({
					width: current_width+'px',
					marginLeft: current_margin_left+'px',
					height: current_height+'px',
					marginTop: current_margin_top+'px'
				}));
		});
		
		Element.update.delay(0.4, mask, img);
		$R(5, 10).each(function(j){
			Element.setOpacity.delay(0.4+((j-5)*0.01), img, (j+1)/10);
		});
	});
	img.src = url;
}

Element.observe(window, 'load', function(){
	var images = $$('#frame img');
	images.each(function(image){
		var parent = image.up();
		if(!parent.match('a')){
			var widthStyle = parseInt(image.getStyle('width'));
			var heightStyle = parseInt(image.getStyle('height'));
			image.setStyle({width: 'auto', height: 'auto'});
			var width = image.getWidth();
			var height = image.getHeight();
			if(width-widthStyle > 200){
				image.addClassName('clickable');
				image.on('click', function(){
					viewimage(image.src);
				});
			}
			image.setStyle({width: widthStyle+'px', height: heightStyle+'px'});
		}
	});
});

function headerPhoto(){
	var p = document.getElementById('header_photo');
	var p2 = document.createElement('img');
	var src = p.src.split('/');
	src = src[src.length-1];
	p2.src = p.src;
	p2.style.position = 'absolute';
	p2.style.left = findPos(p)[0]+'px';
	p2.style.top = findPos(p)[1]+'px';
	document.body.appendChild(p2);
	p.src = (src == 'header-photo.png' ? '/site/templates/casino/images/header-photo2.png' : '/site/templates/casino/images/header-photo.png');
	
	//Lancement de la transition
	for(var i=0; i<10; i++){
		var opacity = 100-((i+1)*10);
		setTimeout((function(opacity, p2, src){
			return function(){
				p2.style.opacity = (opacity/100);
				p2.style.filter = 'alpha(opacity='+opacity+')';
				if(!opacity){
					//Sauvegarde de l'image affichée pour le prochain chargement de page
					getContent('/site/specClass.php?himg='+src);
					document.body.removeChild(p2);
					setTimeout(function(){headerPhoto();}, 6000);
				}
			}
		})(opacity, p2, src), (i*120))
	}
}

function getContent(url, xml){
	var response = false;
	var XHR = false; 
	try{		XHR = new ActiveXObject("Microsoft.XMLHTTP");	}
	catch(e){	XHR = new XMLHttpRequest();	}
	XHR.open("GET", url, false);
	XHR.send(null);
	response = (xml ? XHR.responseXML : XHR.responseText);
	return response;
}

function findPos(obj){
	var curleft = curtop = 0;
	if(obj.offsetParent){
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while(obj = obj.offsetParent){
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
