/* Script client commun à toutes les pages du site sunset-creation.com */

/* Déclaration */

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

// Pulse V1.0
var Pulse = new Class({
	Implements : Options,
	options : {
		from : 0.6,
		to : 1,
		duration : 500
	},
	initialize : function (element,options){
		this.element = $(element);
		this.setOptions(options);
		this.element.fade(this.options.to);
		setTimeout(function(){
			var tmp = this.options.from;
			this.options.from = this.options.to;
			this.options.to = tmp;
			this.initialize(this.element, this.options);
		}.bind(this),this.options.duration);
	}
});

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