window.addEvent('domready', function(){

	$$('.backToHome').addEvents({
		click: function () {
			document.location.href = '/';	
		}
	});
	
	var onclickSecondaryCollection = function(eventTo, pageTo) {
		if ( $(eventTo) ) {
			$(eventTo).addEvents({
				mouseover: function() { 
					new Request.HTML({
						url: pageTo,
						update: $('collec-ajax'),
						onRequest: function() {
							$('collec-ajax').set('html', '');
							$('collec-ajax').style.backgroundColor = '#eee';
							$('collec-ajax').style.backgroundImage = 'url(/images/website_2010/ajax-loader.gif)';
							$('collec-ajax').style.backgroundPosition = 'center center';
							$('collec-ajax').style.backgroundRepeat = 'no-repeat';
							$('collec-ajax').style.fontSize = '18px';
							$('collec-ajax').style.zIndex = '1000';
							$('collec-ajax').set('html', '<p class="tc" style="font-weight: bold; padding-top: 280px; height: 384px; margin-bottom: 10px;">Chargement en cours</p>');
						},
						onSuccess: function(responseHTML) {
							$('collec-ajax').style.background = 'none';
							// $('collec-ajax').adopt(responseHTML); 
						},
						onFailure: function() {
							$('collec-ajax').style.backgroundImage = 'url(/images/website_2010/ajax-loader-error.gif)';
							$('collec-ajax').set('html', '<p class="tc" style="color: red; font-weight: bold; padding-top: 280px; height: 384px; margin-bottom: 10px;">Erreur lors du chargement. Merci d\'essayer à nouveau</p>');
						}
					}).get();
				}
			});
		}
	}
	onclickSecondaryCollection('btn_basics', '/ajaxoutput.php?PageID=home-ajax&id_category=84');
	onclickSecondaryCollection('btn_streetlife', '/ajaxoutput.php?PageID=home-ajax&id_category=93');
	onclickSecondaryCollection('btn_design', '/ajaxoutput.php?PageID=home-ajax&id_category=77');
	onclickSecondaryCollection('btn_wrunglady', '/ajaxoutput.php?PageID=home-ajax&id_category=46');
	
	
	if ( $('btn_basics') ) {
		$('btn_basics').fireEvent('mouseover');
	}
	
	if ( $('debugMode') ) {
		
		var mdebugMode = new Fx.Slide($('debugMode'));
		mdebugMode.hide();
		$('showhideDebugMode').addEvent('mouseover', function(e){
			e = new Event(e);
			mdebugMode.show();
			e.stop();
		});
		$('showhideDebugMode').addEvent('mouseout', function(e){
			e = new Event(e);
			mdebugMode.hide();
			e.stop();
		});
	}
	
	var flip = $('page-flip');
	var flipImage = $('page-flip-image');
	var flipMessage = $('page-flip-message');
	
	flip.addEvents({
		mouseenter:function() {
			flipImage.set('morph',{ duration: 220 }).morph({
				width: 60,
				height: 62
			});
			flipMessage.set('morph',{ duration:220 }).morph({
				width: 60,
				height:58
			});
		},
		mouseleave:function() {
			flipImage.set('morph',{ duration: 220 }).morph({
				width: 30,
				height: 32
			});
			flipMessage.set('morph',{ duration:220 }).morph({
				width: 30,
				height:30
			});
		}	
	});
	
	getBackground();
});

function getBackground() {
	var c = getCookie('Wrung Background');
	var v = c.split(new RegExp("[ ,;]+", "g"));
	
	if(c != "") {
		tile = v[0];
		color = v[1];
		repeat = v[2];
		
		setBackground(tile, color, repeat);
	}
}

function setBackground(tile, color, repeat)
{
	var m = $(document.body);
	if(repeat == 'true') {
		var r = 'repeat';
	} else {
		var r = 'repeat-x';
	}		
	if(tile == null) {
		m.setStyle('background-image','none');
		m.setStyle('background-color', color);
	}
	else {
		m.setStyle('background-color', color);
		m.setStyle('background-image','url(' + tile + ')');
		m.setStyle('background-position','center 0');
		m.setStyle('background-repeat',r);
	}
	setCookie('Wrung Background', '', -1);
	setCookie('Wrung Background', new Array(tile, color, repeat), 360);
}

function changeBackground(tile, color, repeat)
{
	var m = $(document.body);
	if(repeat == 'true') {
		var r = 'repeat';
	} else {
		var r = 'repeat-x';
	}		
	var fx = new Fx.Tween(m,{
		duration: 500,
		onComplete: function(){ 
			if(tile == null) {
				m.setStyle('background-image','none');
				m.setStyle('background-color', color);
				m.fade('in');
			}
			else {
				m.setStyle('background-color', color);
				m.setStyle('background-image','url(' + tile + ')');
				m.setStyle('background-position','center 0');
				m.setStyle('background-repeat',r);
				m.fade('in');
			}
		}
	});
	fx.start('opacity',1,0);
	setCookie('Wrung Background', '', -1);
	setCookie('Wrung Background', new Array(tile, color, repeat), 360);
}

function getCookie(name) {
	start = document.cookie.indexOf(name + "=");
	if (start >= 0) {
		start += name.length + 1;
		end = document.cookie.indexOf(";",start);
		if (end < 0) end = document.cookie.length;
		return unescape(document.cookie.substring(start,end));
	}
	return "";
}

function setCookie(name, value, days) {
	var time = new Date();
	time.setTime(time.getTime() + (days * 24 * 3600 * 1000));
	document.cookie = name + "=" + escape(value) + ";expires=" + time.toGMTString() + "; path=/";
}

function emptyEmail () {
	if ( document.forms['f_identify'].IDLogin.value == 'email@domaine.com' ) {
		document.forms['f_identify'].IDLogin.value = '';
	}
}
function emptyPass () {
	if ( document.forms['f_identify'].IDPass.value == 'xxxxxxx' ) {
		document.forms['f_identify'].IDPass.value = '';
	}
}
function emptyNEmail () {
	if ( document.forms['form_newsletter'].ff_email.value == 'Insérez votre email' ) {
		document.forms['form_newsletter'].ff_email.value = '';
	}
}

function emptyNewsEmail () {
	if ( document.forms['n_subscribe'].n_mail.value == 'Insérez votre email' ) {
		document.forms['n_subscribe'].n_mail.value = '';
	}
}
function getPwdMd5 ( idLogin, idPass, idMd5, idForm ) {
	pwd = document.getElementById(idPass).value;
	document.getElementById(idPass).value = '';
	
	buf = hex_md5(pwd);
	
	document.getElementById(idMd5).value = buf;
	
	document.getElementById(idForm).submit();
	return true;
}

function ngetPwdMd5 ( idLogin, idPass, idMd5, idForm ) {
	pwd = document.getElementById(idPass).value;
	document.getElementById(idPass).value = '';
	
	buf = hex_md5(pwd);
	document.getElementById(idMd5).value = buf;
	
}
function submitForm ( id ) {
	document.getElementById(id).submit();
}
function changeAndSubmitForm ( change, id ) {
	document.getElementById(id).action = change;
	document.getElementById(id).submit();
}
