function fDelete(){
	$$('.fDelete').each(function(el){
		el.addEvent('click', function(e){
			e = new Event(e);
			if( !confirm(el.title ? el.title : 'Etes vous sûr de vouloir supprimer cet élément ?')){
				e.stop();
			}
		});
	});
}

function del( Obj ) {
	var label = ( Obj.title ) ? Obj.title : 'Etes vous sûr de vouloir supprimer cet élément ?';
	return confirm( label );	
}
function publ( Obj ) {
	var label = ( Obj.title ) ? Obj.title : 'Etes vous sûr de vouloir publier la référence et le produit dont cette référence fait partie ?';
	return confirm( label );	
}
function popup( href, name, h, l ) {
	var hauteur = Math.round( ( screen.availHeight - h ) / 2 );
	var largeur = Math.round( ( screen.availWidth - l ) / 2 );
	var popup = window.open( href, name, 'toolbar=0,location=0,directories=0,status=0, scrollbars=1,resizable=1,menubar=0,top='+hauteur+',left='+largeur+',width='+l+',height='+h);
	popup.focus();
	return false;	
}

function popupOrder( Obj ) {
	return popup( Obj.href, 'order', 600, 580 );
}

function fFlashMessage() {
	new mooFlashMessage($$('.flash'));
}

function fBlink(){
	$$('.blink1').each(function(el, i){
		var effect = el.effect('opacity', {duration: 600});
		$$('.blink2').setOpacity(0);
		
		effect.start('0').chain(function() {
			effect.start('1').chain(function() {
				effect.start('0').chain(function() {
					effect.start('1').chain(function() {
						effect.start('0').chain(function() {
							effect.start('1');
							
							$$('.blink2').each(function(el, i){
								var effect = el.effect('opacity', {duration: 600});
								effect.start('1').chain(function() {
									effect.start('0').chain(function() {
										effect.start('1').chain(function() {
											effect.start('0').chain(function() {
												effect.start('1');
											});
										});
									});
								});
							});
							
						});
					});
				});
			});
		});
	});
	
}


function initHome(){
	// Mousemove
	/*
	var scrollLeft = new Scroller('moveleft', {area: 13, velocity: 1, onChange: function(x, y){ console.log(x, y); }});
	$('moveleft').addEvent('mouseenter', scrollLeft.start.bind(scrollLeft));
	$('moveleft').addEvent('mouseleave', scrollLeft.stop.bind(scrollLeft));	
	
	var scrollRight = new Scroller('moveright', {area: 13, velocity: 1, onChange: function(x, y){ console.log(x, y); }});
	$('moveright').addEvent('mouseenter', function(){ scroll.start.bind(scrollRight); console.log('moveright start'); } );
	$('moveright').addEvent('mouseleave', function(){ scroll.stop.bind(scrollRight); console.log('moveright stop'); });
	*/
	
	var scroll = new Scroller('mousemove', {area: 40, velocity: 1});
	$('mousemove').addEvent('mouseenter', scroll.start.bind(scroll));
	$('mousemove').addEvent('mouseleave', scroll.stop.bind(scroll));	

	var fxs = [];
	$$('.imgmac').each(function(div, i){
		fxs[i] = new Fx.Styles(div.getElements('img').shift(), {duration: 300, transition: Fx.Transitions.linear, wait: false});
		div.addEvents({
			mouseenter: function(){
				fxs[i].start({
					'margin-top': 0,
				    'height': 110,
				    'width': 160
				});
			},
			mouseleave: function(){
				fxs[i].start({
					'margin-top': 15,
				    'height': 80,
				    'width': 130
				});	
			}
		});
	});
}

function marqueeInit(){
	if($('marqueebox')){
		new mooMarquee($('marqueebox'), $$('#marqueebox .marquee'), {'waitDuration': 7000});
	}
	if($('marqueepromo')){
		new mooMarquee($('marqueepromo'), $$('#marqueepromo .marquee'), {'waitDuration': 7000});
	}
} 

function disp_delai(year,month,day,hour,min,elementId){
	document.getElementById(elementId).value=delai(year,month,day,hour,min);
	setTimeout("disp_delai("+year+","+month+","+day+","+hour+","+min+",'"+elementId+"')",1000);
}
function delai(annee,mois,jour,heure,min){
	var date_fin=new Date(annee,mois-1,jour,heure,min)
	var date_jour=new Date();
	var tps=(date_fin.getTime()-date_jour.getTime())/1000;
	var j=Math.floor(tps/3600/24); tps=tps % (3600*24);
	var h=Math.floor(tps / 3600); tps=tps % 3600;
	var m=Math.floor(tps/60);
	tps=tps % 60
	var s=Math.floor(tps);
	//var txt=j+" j "+h+" h "+m+" min et "+s+" sec";
	var txt=j+" j "+h+" h "+m+" min";
	date_fin=don_date(date_fin);
	return txt;
}
function don_date_jour(){
   var date_jour=new Date();
	date_jour=don_date(date_jour);
	return date_jour;
}
function don_date(une_date){
	var la_date;
	var months=new Array(12);
	months[1]="Janvier";
	months[2]="Février";
	months[3]="Mars";
	months[4]="Avril";
	months[5]="Mai";
	months[6]="Juin";
	months[7]="Juillet";
	months[8]="Aout";
	months[9]="Septembre";
	months[10]="Octobre";
	months[11]="Novembre";
	months[12]="Décembre";
	var days=new Array(7);
	days[1]="Lundi";
	days[2]="Mardi";
	days[3]="Mercredi";
	days[4]="Jeudi";
	days[5]="Vendredi";
	days[6]="Samedi";
	days[7]="Dimanche";
	var month=months[une_date.getMonth() + 1];
	var day=days[une_date.getDay()];
	var date=une_date.getDate();
	var year=une_date.getYear();
}

function initMenu(){
	//new mooMenu($('menu'));
	if($('menu')) {
		$$('#menu li:last-child').addClass('last');
		
		// sk 2011-03-16 : class "right" pour aligner les menus déroulants
		$$('#menu li').each(function(element){
			//console.log('new element : ' + element.getProperty('class') + ' ' + element.offsetLeft + ' ' + element.offsetTop );
			if(element.offsetTop == 0 &&  element.offsetLeft > 780) {
				element.getChildren('ul').addClass('right');
			}
			if(element.offsetTop > 0) {
				element.addClass('sub-level');
			}
		});
	}
}
window.addEvent('domready', initMenu);
sfHover = function() {
	var sfEls = document.getElementById("menu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function InitSwapImage(){
	//var lb_glass = new LbGlass('mbImage');
	//lb_glass.start();
	var list = $$('.thumblist');
	$$('.imageswap').each(function(el){
		el.addEvent('click',function(e){
		
			// sk 2011-05-30 : gestion du 360
			if(el.hasClass('image360') || el.hasClass('spanimage360')  ) {
				$('showimg').addClass('image360');
				if($('specialpromo') && $('specialpromo').hasClass('image360')) {
					$('specialpromo').setStyle('display', 'inline');
				}
					
				if( $('image_360') ) {
					// sk 2011-05-31 on affiche l'anim flash 360
					$('image_360').setStyle('display','inline');
					$('showimg').setStyle('display', 'none');
				}				
				
			} else {
				$('showimg').removeClass('image360');
				if($('specialpromo') && $('specialpromo').hasClass('image360')) {
					$('specialpromo').setStyle('display', 'none');
				}
				
				if( $('image_360') ) {
					// sk 2011-05-31 on masque l'anim flash 360
					$('image_360').setStyle('display','none');
					$('showimg').setStyle('display', 'inline');
				}				
			}

			// sk 2011-05-30 ajustements pour le click sur le span360
			if( this.hasClass('spanimage360') ) {
				var miniature = this.getNext();
				el = miniature;
			} else {
				var miniature = this;
			}			
			var rel = miniature.getProperty('rel').split(' ');
			$('showimg').setProperty('src',miniature.getProperty('src').replace('thumbnail',rel[0]));
			
			var withflash = (rel[3])?rel[3]:'';
			if(withflash!='' && list[0].src==miniature.getProperty('src')){
				$$('.ahreflink').each(function(el){
					el.setProperty('rel','milkbox');
					el.setProperty('href','catalogue/productzoom-'+withflash+'.html');
				});
				//MediaGlass.init();
			}else{
				var srcahreflink = miniature.src.replace('thumbnail','highdef')
				$$('.ahreflink').each(function(el){
					el.addClass("hasglass");
					el.setProperty('rel','milkbox['+rel[1]+' '+rel[2]+']');
					el.setProperty('href',srcahreflink);
					//console.log(el);
				});
			}				
		});
	});
}
 

function clearInput(inputid,inputvalue) {
	if (document.getElementById(inputid).value==inputvalue)
		document.getElementById(inputid).value="";
}

function fInit(){
	marqueeInit();
	fFlashMessage();
	fBlink();
	fDelete();
	//MediaGlass.init();
	setTimeout("InitSwapImage();",500);
}
window.addEvent('domready',fInit);

/*
---
description:     Equalizer

authors:
  - David Walsh (http://davidwalsh.name)

license:
  - MIT-style license

requires:
  core/1.2.1:   '*'

provides:
  - Equalizer
...
*/
var Equalizer = new Class({
	initialize: function(elements) {
		this.elements = $$(elements);
	},
	equalize: function(hw) {
		if(!hw) { hw = 'height'; }
		var max = 0,
			prop = (typeof document.body.style.maxHeight != 'undefined' ? 'min-' : '') + hw; //ie6 ftl
			offset = 'offset' + hw.capitalize();
		this.elements.each(function(element,i) {
			var calc = element[offset];
			if(calc > max) { max = calc; }
		},this);
		this.elements.each(function(element,i) {
			element.setStyle(prop,max - (element[offset] - element.getStyle(hw).toInt()));
		});
		return max;
	}
});

/* equalize heights */
function equalizeColumn() {
	var columnizer = new Equalizer('.column-details').equalize('height'); //call .equalize() as often as you want! 
	var columnizer = new Equalizer('.slider-product').equalize('height'); //call .equalize() as often as you want! 
	var columnizer = new Equalizer('.equal').equalize('height'); //call .equalize() as often as you want! 
	var columnizer = new Equalizer('.equal1').equalize('height'); //call .equalize() as often as you want! 
	var columnizer = new Equalizer('.equal2').equalize('height'); //call .equalize() as often as you want! 
	var columnizer = new Equalizer('.equal3').equalize('height'); //call .equalize() as often as you want! 
	var columnizer = new Equalizer('.equal4').equalize('height'); //call .equalize() as often as you want! 
	var columnizer = new Equalizer('.equal5').equalize('height'); //call .equalize() as often as you want! 
	var columnizer = new Equalizer('.equal6').equalize('height'); //call .equalize() as often as you want! 
	//var columnizer = new Equalizer('.img-prod').equalize('height'); //call .equalize() as often as you want! 
	
}
window.addEvent('domready', equalizeColumn);

//window.addEvent('domready', function(){
	/* thumbnails example , links only */
	/*if ($('gallery_container') != null) {
		new SlideItMoo({itemsVisible:5, // the number of thumbnails that are visible
						currentElement: 0, // the current element. starts from 0. If you want to start the display with a specific thumbnail, change this
						thumbsContainer: 'thumbs',
						elementScrolled: 'thumb_container',
						overallContainer: 'gallery_container'});
	}
})*/
window.addEvent('domready', function(){
	
	// fiche produit
	if( $('slider-cont') && $('slider') && $('slider-items') ) {
		new SlideItMoo({
			overallContainer: 'slider-cont', 
			elementScrolled: 'slider', 
			thumbsContainer: 'slider-items', 
			itemsVisible:5, 
			elemsSlide:1, 
			duration:300, 
			itemsSelector: '.slider-product-img', 
			itemWidth: 114, 
			showControls:1, 
			startIndex:1, 
			onChange: function(index){}
		});
	}
	
	// initialisation des sliders pour la catalogue
	
	if( $('menu-catalog') ) {
		$$('div.categories-slider-cont').each(function(el) {
			var currentCategoryId = el.id.replace(/^categories-/,"");
			if( $('slider-'+ currentCategoryId) && $('slider-items-' + currentCategoryId ) ) {
				new SlideItMoo({
					overallContainer: 'categories-' + currentCategoryId, 
					elementScrolled: 'slider-' + currentCategoryId, 
					thumbsContainer: 'slider-items-' + currentCategoryId, 
					itemsVisible:3, 
					elemsSlide:1, 
					duration:300, 
					itemsSelector: '.slider-product', 
					itemWidth: 223, 
					showControls:1, 
					startIndex:1, 
					onChange: function(index){}
				});
			}
		});
	}
});

/* 2010-10-08 chargement de la loupe */
var pictureLouper = null;
function loadGlass(idImage, urlPicture) {
	//console.log(urlPicture);
	
	if ( pictureLouper == null ) {
		//console.log('init new louper');
		zoomUrlPicture = urlPicture.replace(/_highdef_/,"_fulldef_");
		loupeUrl = urlPicture.replace(/\/catalogue\/([0-9A-Za-z\-\/_\.]+)/,"/images/loupe.png");
		//console.log(loupeUrl);
		
		var loupe = {
			src: loupeUrl,
			x: 123,
			y: 125,
			radius: 120
		};
		pictureLouper = new Louper(idImage, {
			big: zoomUrlPicture,
			radius: 114,
			loupe: loupe,
			onReady: function(){
				this.loupeWrapper.setStyles({
					left: this.smallSize.width / 2 - this.loupeSize.width / 2,
					top: this.smallSize.height / 2 - this.loupeSize.width / 2
				});
			}
		});	
		
		//$(idImage).removeEvent('mouseover');
	}
}
/* init form newsletter  */
function initFormNewsletter()
{
	$$('.form-newsletter .mail').addEvents({
		'focus' : function(){
			this.set('value', '');
		}
	})
}
window.addEvent('domready',initFormNewsletter);
