var id = 0;
var array;
var fond_arr = new Array('http://www.messlive.net/themes/MessLivePro/images/bg.png', 'http://www.messlive.net/themes/MessLivePro/images/bg1.jpg', 'http://www.messlive.net/themes/MessLivePro/images/bg2.jpg'); //, 'http://www.messlive.net/themes/MessLivePro/images/bg3.jpg'
var fond_arr2 = new Array('repeat', 'repeat-x', 'repeat-x'); //, 'repeat-x'

var MessLiveFond = new Class({
	initialize: function(id, color, bgImage, bgRepeat, bgPosition, pImg) {
		this.id = id;
		this.color = color;
		this.bgImage = bgImage;
		this.bgRepeat = bgRepeat;
		this.bgPosition = bgPosition;
		this.pImg = pImg;
		
		return true;
 },
	defineElementStyles: function(element) {
		try
		{
			element = $(element);
			if(this.bgImage != "")
			{
				element.setStyles({ 'background-color': this.color, 'background-image': 'url(' + this.bgImage + ')', 'background-repeat': this.bgRepeat, 'background-position': this.bgPosition });
			}
			else
			{
				element.setStyles({ 'background-color': this.color, 'background-image': 'none', 'background-repeat': this.bgRepeat, 'background-position': this.bgPosition });
			}
			
			return true;
		}
		catch(e)
		{
			alert(false);
			return false;
		}
	}
});

window.addEvent('domready', function() {
	array = new Array(new MessLiveFond(0, '', 'http://www.messlive.net/themes/MessLivePro/images/bg.png', 'repeat', '', '/images/bg0.png'), new MessLiveFond(1, '', 'http://www.messlive.net/themes/MessLivePro/images/bg1.jpg', 'repeat-x', '', '/images/bg1.png'), new MessLiveFond(2, '', 'http://www.messlive.net/themes/MessLivePro/images/bg2.jpg', 'repeat-x', '', '/images/bg2.png')); //, new MessLiveFond(3, '', 'http://www.messlive.net/themes/MessLivePro/images/bg3.jpg', 'repeat-x', '', '/images/bg3.png')
	affmlfc();
});

function changeFondLoad()
{
	var id = getId();
	if(id != false)
	{
		if(fond_arr[id] != "")
		{
			document.body.style.backgroundImage = 'url(' + fond_arr[id] + ')';
		}
		else
		{
			document.body.style.backgroundImage = 'none';
		}
		document.body.style.backgroundRepeat = fond_arr2[id];
	}
}

function changeFond()
{
	$(document.body).fade(0.5);
	var img = new Image;
	img.src = this.bgImage;
	if(img.complete == true)
	{
		this.defineElementStyles(document.body);
		$(document.body).fade(1);
		save(this.id);
	}
	else
	{
		$(img).addEvent('load', function() {
			this.defineElementStyles(document.body);
			$(document.body).fade(1);
			save(this.id);
		}.bind(this));
	}
}

function save(id)
{
	Cookie.write('mlfc', id, { duration: 364 });
}
  
function lit_cook(nom) {
	var deb,fin;
	deb = document.cookie.indexOf(nom + "=");
	if (deb >= 0) {
		deb += nom.length + 1;
		fin = document.cookie.indexOf(";",deb);
		if (fin < 0) fin = document.cookie.length;
		return unescape(document.cookie.substring(deb,fin));
	}
	return "";
}

function getId() {
	c=lit_cook('mlfc');
	if(c=="") return false;
	else return c;
}

function affmlfc()
{
	if($chk($('mlfc')) && $chk($('mlfc_over')))
	{
		var e = $('mlfc');
		
		if(Browser.Engine.trident == true)
		{
			var gauche = (document.body.clientWidth / 2) + (962 / 2) - 30 + 10;
		}
		else
		{
			var gauche = (window.innerWidth / 2) + (962 / 2) - 30;
		}
		$('mlfc').setStyle('left', gauche);
		$('mlfc_over').setStyle('left', gauche);
		
		$(window).addEvent('resize', function() {
			if(Browser.Engine.trident == true)
			{
				var gauche = (document.body.clientWidth / 2) + (962 / 2) - 30 + 10;
			}
			else
			{
				var gauche = (window.innerWidth / 2) + (962 / 2) - 30;
			}
			$('mlfc').setStyle('left', gauche);
			$('mlfc_over').setStyle('left', gauche);
		});
		
		
		$('mlfc_over').addEvent('mouseenter', function() {
			if(Browser.Engine.trident == true)
			{
				var gauche = (document.body.clientWidth / 2) + (962 / 2) + 10;
			}
			else
			{
				var gauche = (window.innerWidth / 2) + (962 / 2);
			}
			$('mlfc').tween('left', gauche);
		});
		$('mlfc_over').addEvent('mouseout', function() {
			if(Browser.Engine.trident == true)
			{
				var gauche = (document.body.clientWidth / 2) + (962 / 2) - 30 + 10;
			}
			else
			{
				var gauche = (window.innerWidth / 2) + (962 / 2) - 30;
			}
			$('mlfc').tween('left', gauche);
		});
		
		$('mlfc').addEvent('mouseover', function() {
			if(Browser.Engine.trident == true)
			{
				var gauche = (document.body.clientWidth / 2) + (962 / 2) + 10;
			}
			else
			{
				var gauche = (window.innerWidth / 2) + (962 / 2);
			}
			$('mlfc').tween('left', gauche);
		});
		$('mlfc').addEvent('mouseout', function() {
			if(Browser.Engine.trident == true)
			{
				var gauche = (document.body.clientWidth / 2) + (962 / 2) - 30 + 10;
			}
			else
			{
				var gauche = (window.innerWidth / 2) + (962 / 2) - 30;
			}
			$('mlfc').tween('left', gauche);
		});
		
		
		array.each(function(item, index) {
			var boundDestroy = changeFond.bind(item);
			var element = new Element('div', {
				'styles': {
					'margin-top': 5,
					'cursor': 'pointer'
				},
				'html': '<img src="' + item.pImg + '" style="border: 0;" />',
				'events': {
					'click': boundDestroy
				}
			});
			this.grab(element);
		}, e);
	}
}
