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

/* Gallery V15 */
/* TODO Recode to handle variable image width with SLIDE effect */
var SLIDE = 0, FADE = 1;
var Gallery = new Class({
	Implements : Options,
	options : {
		container : 'div',
		list : 'ul',
		items : 'li',
		prev : '.prev',
		next : '.next',
		auto : true,
		step : 1,
		delay : 3000,
		loop : true,
		links : 'ul.nav li a',
		linksEvent : 'click',
		linksRel : false,
		play : '.play',
		autoHide : true,
		tween : {'link':'cancel'},
		effect : SLIDE, // SLIDE | FADE
		pause : true
	},
	initialize : function(element,options){
		this.element = document.id(element);
		this.setOptions(options);
		this.prev = this.element.getElement(this.options.prev);
		this.next = this.element.getElement(this.options.next);
		this.play = this.element.getElement(this.options.play);
		this.links = this.element.getElements(this.options.links);
		this.container = this.element.getElement(this.options.container);
		this.list = this.container.getElement(this.options.list);
		this.items = this.list.getElements(this.options.items);
		this.itemWidth = this.items[0].getStyle('width').toInt()+this.items[0].getStyle('padding-right').toInt()+this.items[0].getStyle('padding-left').toInt();
		this.i = 0;
		this.maxStep = Math.ceil(this.items.length/this.options.step);
		if (this.maxStep>0){
			if (this.options.auto){
				this.start();
				if (this.options.pause) this.element.addEvents({'mouseenter':this.stop.bind(this),'mouseleave':this.start.bind(this)});
			}
			if (this.prev) this.prev.addEvent('click',this.scrollBy.bind(this,0));
			if (this.next) this.next.addEvent('click',this.scrollBy.bind(this,1));
			if (this.play) this.play.addEvent('click',function(){if (this.timer) this.stop(); else	this.start();}.bind(this));
			if (this.options.autoHide){
				this.element.getElements('.button').setStyle('opacity',0);
				this.element.addEvents({
					'mouseenter' : function(){
						$$(this.prev,this.next,this.play).fade('in');
					}.bind(this),
					'mouseleave' : function(){
						$$(this.prev,this.next,this.play).fade('out');
					}.bind(this)
				});
			}
		};
		if (this.options.effect==SLIDE){
			if (this.options.loop){
				this.items.clone().inject(this.list);
				this.items.clone().inject(this.list);
				this.list.setStyle('margin-left',-this.items.length * this.itemWidth * this.options.step);
			}
			this.list.tween = new Fx.Tween(this.list, this.options.tween);
			this.list.setStyle('width',this.itemWidth*3*this.items.length);
		}else{
			this.zIndex = this.items.length+1;
			for(var i=0;i<this.items.length;i++){
				this.items[i].set('tween',this.options.tween);
				this.items[i].setStyles({'position':'absolute','z-index':this.items.length-i});
			}
		}
		if (typeof(Slimbox)!='undefined') Slimbox.scanPage();
		if (this.links.length>0){
			this.links.each(function(link,j){
				link.addEvent(this.options.linksEvent,function(event){
					event.stop();
					this.stop();
					if (this.options.linksRel) j = this.getIndexByLink(link);
					this.scrollTo(j);
				}.bind(this));
			}.bind(this));
			this.link = this.links[0];
			this.link.addClass('selected');
		}
	},
	getIndexByLink : function(link){
		for(var i=0;i<this.items.length;i++){
			if (this.items[i].id==link.rel) return i;
		}
		return 0;
	},
	getLinkByIndex : function(j){
		if (this.options.linksRel){
			for (var i=0;i<this.links.length;i++){
				if (this.links[i].rel==this.items[j].id) return this.links[i];
			}
		}else{
			this.link = this.links[j];
		}
		return this.link;
	},
	scrollBy : function (direction){
		this.scrollTo(this.i+direction*2-1);
		return false;
	},
	scrollTo : function (index,callback){
		if (this.link) this.link.removeClass('selected');
		this.i = index;
		if (this.options.effect==SLIDE){
			this.list.tween.start('margin-left',-this.itemWidth * (this.i+this.items.length) * this.options.step).chain(function(){
				if (this.options.loop && Math.abs(this.i)>=this.maxStep){
					this.list.setStyle('margin-left',-this.items.length * this.itemWidth * this.options.step);
					this.i = (this.i+this.maxStep) % this.maxStep;
					if (callback) callback();
				}
			}.bind(this));
		}else if (this.options.effect==FADE){
			this.i = (this.i+this.maxStep) % this.maxStep;
			this.items[this.i].setStyles({'z-index':this.zIndex,'opacity':0});
			this.items[this.i].tween('opacity',1);
			this.zIndex++;
		}
		this.link = this.getLinkByIndex((this.i+this.maxStep) % this.maxStep);
		if (this.link) this.link.addClass('selected');
	},
	start : function(){
		if (this.play) this.play.addClass('stop');
		if (this.next) this.next.fireEvent('mouseenter');
		this.timer = this.scrollBy.bind(this,1).periodical(this.options.delay);
	},
	stop : function(){
		if (this.play) this.play.removeClass('stop');
		if (this.next) this.next.fireEvent('mouseleave');
		this.timer = $clear(this.timer);
	}
});


// 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(){
	// Créer un objet "pulse" pour chaque élément ayant la classe "pulse"
	$$('.pulse').each(function(e){
		new Pulse(e),{morph:true,duration:1500};
	});
	/* Initialise la galerie */	
	$$('.gallery.slide').each(function(g){
		new Gallery(g,{delay:4000,tween:{duration:400},effect:FADE,pause:false,autoHide:false});
	});
	// Transition entre les titres des dernières news
	/*var lis = $$('#last_news li');
	var morph = Array();
	lis.each(function(e,i){
	   morph[i] = new Fx.Morph(e,{'duration':4000}).set({'opacity':0});
    })
    function aff(li,i){
        if($chk(li)){
            morph[i].start({'opacity':1})
                .chain(function(){ morph[i].start({'opacity':0}) })
                    .chain(function(){ aff(li.getNext('li'),i+1) });
        }else{
            aff(lis[0],0)
        }
    }
    aff(lis[0],0);*/
});

