var zoom = new Class({
	initialize:function(pics,labels){
		this.morph_list = [];
		this.pics = $(pics).getChildren('li');
		$(pics).setStyle('height','148px');
		this.pics.setStyles({
			'position':'absolute',
			'top':0,
			'opacity':0
		});
		this.pics[0].setStyle('opacity',1);
		this.labels = $(labels).getElements('li');
		this.construct();
		this.display(0);	
	},
	
	construct:function(){
		this.pics.each(function(el,index){
			el.set('morph',{ duration: 'long', transition: Fx.Transitions.Quad.easeOut	});
		}.bind(this));
		this.labels.each(function(el,index){
			el.addEvent('mouseenter',function(e){
				this.display(index);
			}.bind(this));
		}.bind(this));
	},	
	
	display:function(i){
		this.pics.each(function(el,index){
			if(i!=index) el.morph({'opacity':[el.getStyle('opacity'),0]});
		});
		this.pics[i].morph({'opacity':[this.pics[i].getStyle('opacity'),1]});
	}
});

function fInitAccount(){
	if($$('.account_capture').length==2){
		$$('.account_capture').setStyle('display','none');
		$('capture').set({
			'styles':{
				'display':'block'
			},
			'events':{
				'mouseenter':function(e){
					this.setStyle('display','none');
					$('video').setStyle('display','block');
				}
			}
		});
	}
}
 
function fInitPopup(){
	$$('.popthis').each(function(el,index){
		el.addEvent('click',function(e){
			var e = new Event(e).stop();
			var redir = WR+'pop/'+(el.get('href').replace(WR,''));
			Mediabox.open(redir,'pop','400 440');
		}.bind(this));
	});
}

var htmlquest = null;
function fInitTips(){
	if($$('.totips').length>0){
		uri = 'js/pages/get_zoom_description.js.php?ids=0';
		$$('.totips').each(function(el,index){
			var tipid = el.get('id').split('_');
			uri+=','+tipid[1];
		});			
		new Request.JSON({
			url:uri,
			onComplete:function(resp){
				$$('.totips').each(function(el,index){
					var tipid = el.get('id').split('_');
					el.store('tip:title',el.get('text'));
					el.store('tip:text',resp[tipid[1]]);	
				});
			}
		}).send();
		var tips = new Tips($$('.totips'),{'className':'tips'});
	}
}

function fInitSlides(){
	if($('boxes')){
		var size = $('boxes').getFirst('div').getSize();
		new noobSlide({
			mode: 'horizontal',
			box: $('boxes'),
			items: $('boxes').getElements('div'),
			size: size.x,
			autoPlay: true,
			fxOptions: {
				duration: 1000,
				wait: true
			}
		});
	}
}

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 initMenuHori(){
	if ($('menuHori'))
		var hMenu = new mooMenu($('menuHori'));
}
	
function initMenuVert(){
	if($('menuvert')){
		var vMenu = new jAccordion($('menuvert'), '.togglers', '.elements');
	}
}
 
function fInitAdvertising(){
	if($('mousemove')){
		var myRunner = new Runner('mousemove', {area: 40, velocity: 1, run:{active:true,mode:'V'}});
		myRunner.start();
	}
}

function initLimitValue(){
	if($$('.limited').length>0)
		new limitValues ($$('.limited'));
}

function fInitZoom(){
	if($('zoom_labels')){
		var sZoom = new zoom('zoom_pics','zoom_labels');
	}	
}
var request = null;
var tips = [];
function fInitAlbums(){
	if((typeof(window['data']) !== "undefined")){
		 var myShow =new Slideshow.KenBurns('show',data,{captions:true,controller:true,delay:5000,duration:1000,hu:'content/albums/elements/',thumbnails:true,zoom:50});
		 var thumbs = $('show').getElement('div.slideshow-thumbnails').getElements('a');
		 var uri = 'js/pages/get_element_description.js.php?ids=0'; 
		 thumbs.each(function(el,index){
			var tid = el.get('href').replace(/\.jpeg/,'').split('_');
			uri += ','+tid[2];		
		});
	
		 new Request.JSON({
			url:uri,
			onComplete:function(elements){
			 	thumbs.each(function(ti,index){
					var tid2 = ti.get('href').replace(/\.jpeg/,'').split('_');
					var title = ti.get('title');
					var desc = elements[tid2[2]];
					ti.store('tip:title',title);
					ti.store('tip:text',desc);		
				});
				var tips = new Tips(thumbs,{'className':'tips'});
				[myShow.a,myShow.b].each(function(el,index){
					el.removeEvents('click');
					el.getParent('a').removeEvents('click').addEvent('click',function(e){
						var e = new Event(e).stop();
						var tid = el.get('src').replace(/\.jpeg/,'').split('_');
						var ahref = el.get('src').replace(/_slide_/,'_');
						var el_title = el.getParent('a').get('title');
						new Request.JSON({
							url:'js/pages/get_element_sizes.js.php?id='+tid[2],
							onComplete:function(sizes){
								var rel = (parseInt(sizes.x)+16)+' '+(parseInt(sizes.y)+16);
								Mediabox.open(ahref,el_title,rel);
								$('mbCaption').set('html',elements[tid[2]]);
								setTimeout("$('mbCenter').setStyle('height','auto');",300);
							}
						}).send();									
					}.bind(this));
				}.bind(this));
			}
		}).send();	
	}
}

function fInitHiddenTitles(){
	var hTitles = new hiddenTitles($$('.hiddentitles'));
}

/*Menu avec onglet*/
function multiClass(eltId) {
	return;
	arrLinkId = new Array('ong_0','ong_1','ong_2');
	intNbLinkElt = new Number(arrLinkId.length);
	arrClassLink = new Array('on','off');
	strContent = new String();
	
	for (i=0; i<intNbLinkElt; i++) {
		
		strContent = "content_"+arrLinkId[i];
		
		if ( arrLinkId[i] == eltId ) {
			document.getElementById(arrLinkId[i]).className = arrClassLink[0];
			document.getElementById(strContent).className = 'display';
		} else {
			document.getElementById(arrLinkId[i]).className = arrClassLink[1];
			document.getElementById(strContent).className = 'no_display';
		}
		
	}	
	
}
var Tabs = new Class({
	initialize:function(togglers,elements){
		this.togglers = $$(togglers);
		this.elements = $$(elements);
		this.togglers.each(function(el,index){ el.addEvent('click',function(){this.display(index)}.bind(this));	}.bind(this));
		this.display(0);	
	},
	display:function(i){
		this.togglers.removeClass('on').addClass('off');
		this.elements.setStyle('display','none');
		this.elements[i].setStyle('display','');
		this.togglers[i].addClass('on');
	}	
});
function fInitMC(){
	if($$('.tab_title').length>0)
		var laureTabs = new Tabs('.tab_title','.tab_content');
}

function fAccordion () {
	var accordion = new Accordion('h4.accordion_er', 'div.accordion_e', {
		opacity: false,
		show: -1,
		onActive: function(toggler, element){
			toggler.addClass('active');
		},
		onBackground: function(toggler, element){
			toggler.removeClass('active');
		}
	});
}

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;
	}
});

function fEqualize() {
	var listCat = new Equalizer('.list-cat .product h3').equalize('height');
	var listCatPic = new Equalizer('.list-cat .product .pict').equalize('height');
	var listProd = new Equalizer('.list .product h3').equalize('height');
	var listProdPic = new Equalizer('.list .product .pict').equalize('height');
	var listMEA = new Equalizer('.list .product h5').equalize('height');
	var CMS = new Equalizer('.cms-wrap .list-promo p').equalize('height');
	var CMSList = new Equalizer('.cms-wrap .list-promo li').equalize('height');
}

function fInfoTips() {
	$$('.afficheDiv div').fade('out');   
	$$('.afficheDiv').each( function(elem) {
		elem.addEvents( {
			'mouseenter' : function(e) {
				elem.getElement('div').setStyle('display', 'block');
				elem.getElement('div').fade(1);
			},
			'mouseleave' : function(e) {
				elem.getElement('div').fade(0);
			},
			'click' : function(e) {
				elem.getElement('div').fade(0);
			}
		});
	});

	$$('.close-tip').each( function(elem){
		var tips = elem.getParent('div');
			elem.addEvents({
				'click' : function(){
					tips.fade('0');
				}
			});
	})	
}

function fInit(){
	fInitMC();
	//fInitAdvertising();
	initMenuHori();
	initMenuVert();
	fInitZoom();
	fInitSlides();
	fInitTips();
	fInitAccount();
	fInfoTips();
	fInitPopup();
	fInitAlbums();
	fInitHiddenTitles();
	fAccordion();
	fEqualize();
}

window.addEvent('domready', fInit);
