/* Script client commun à toutes les pages du site chaudronnerie-ctm.fr */

/* Déclaration */

function IEFixPng(){
	 $$('img').each(function(e){
	    var src = e.getProperty('src');
	    if (src.toUpperCase().contains('.PNG')){
	       var element = new Element('span', {
         'styles': {
          'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+src+'\', sizingMethod=\'scale\')',
          'display': 'inline-block',
          'height': e.height,
          'width': e.width
         }});
	       element.replaces(e);
	    }
	 });
}

/* Initialisation */
window.addEvent('domready',function(){
	if (Browser.Engine.trident && Browser.Engine.version<5) IEFixPng();
	$$('.fade-in').each(function(element){
		element.setStyle('opacity',0);
		element.set('tween',{duration:3000});
		element.tween('opacity',1);
	});
});

/* Menu Récursif V4 */
var Menu = new Class({
	Implements : Options,
	options : {
		showMinLevel : 0,
		hideDelay : 300,
		morph : {
			showStyles : {opacity:0.95},
			hideStyles : {opacity:0},
			options : {link:'cancel',duration:200}
		}
	},
	initialize: function(menu,options){
		this.setOptions(options);
		this.items = $(menu).getChildren('li');
		this.items.each(function(item,i){
			item.i = i;
			item.link = item.getElement('a');
			item.link.addEvent('focus',this.showItem.bind(this,[item]));
			item.submenu = item.getElement('ul');
			if (this.options.showMinLevel>0){
				if (item.submenu) item.submenu.setStyle('display','block');
			}else{
				item.addEvents({mouseenter:this.showItem.bind(this,[item]),mouseleave:this.hideItem.bind(this,[item])});
			}
			if (item.submenu){
				item.submenu.morph = new Fx.Morph(item.submenu,this.options.morph.options);
				var options = $merge(this.options);
				options.showMinLevel = options.showMinLevel-1;
				new Menu(item.submenu,options);
			}
		}.bind(this));
	},
	showItem : function(item){
		this.timer = $clear(this.timer);
		item.link.addClass('hover');
		if (item.submenu){
			item.submenu.morph.start(this.options.morph.showStyles).chain(function(){item.submenu.setStyle('display','block');}.bind(this));
		}
		this.items.each(function(otherItem,j){
			if (item.i!=j) this.hideItemNow(otherItem);
		}.bind(this));
	},
	hideItem : function(item){
		this.timer = $clear(this.timer);
		this.timer = this.hideItemNow.bind(this,[item]).delay(this.options.hideDelay);
	},
	hideItemNow : function(item){
		item.link.removeClass('hover');
		if (item.submenu){
			item.submenu.morph.start(this.options.morph.hideStyles).chain(function(){item.submenu.setStyle('display','none');}.bind(this));
		}
	}
});

/* Gallery photo V1*/
var Gallery = new Class({
	initialize : function (element){
		this.element = $(element);
		var photos = this.element.getElements("ul li");
		var nav = this.element.getElements(".nav");
		if (photos && photos.length>1){
			var effets = new Array();
			photos.i = 0;
			photos.each(function(photo,i){
				photo.set('tween',{"duration":1000, "wait":true});
				photo.setStyle('opacity',i>0?0:1);
			});
			if (nav.length>1){
				nav[0].addEvent('click',function(){
					$clear(timer);
					defiler(-1);
					timer = defiler.periodical(3000);
				});
				nav[1].addEvent('click',function(){
					$clear(timer);
					defiler();
					timer = defiler.periodical(3000);
				});
			}
			var afficher = function(i){
				photos[i].tween('opacity',1);
			};
			var cacher = function(i){
				photos[i].tween('opacity',0);
			};
			var defiler = function (){
				sens = (arguments.length>0)?arguments[0]:1;
				cacher(photos.i);
				photos.i = (photos.i+sens+photos.length)%photos.length;
				afficher(photos.i);
			}
			var timer = defiler.periodical(3000);
		}
	}
});

/* Initialisation */
window.addEvent('domready',function(){
	new Menu('menu');
	$$('.diaporama').each(function(e){
		new Gallery(e);
	});
});
