
var globalSlides = new Array();
var setSlide = function (  button , slidable, label, concurrents ) {
	if (($chk(slidable)) && ($chk(button))) {
		var fx = new Fx.Slide(slidable);
		if (!slidable.hasClass('notHide')) fx.hide();
		if ( $chk( label ) ) { globalSlides[label] = fx; }
		button.addEvent('click', function (e) {
			e = new Event(e).stop();
			if ( $chk( concurrents ) ) {
				concurrents.each( function ( clabels ) {
					if ( $chk(globalSlides[clabels]) ) { globalSlides[clabels].slideOut(); }
				} );
			}
			fx.toggle();
		});
	}
};

function PMRanimate () {
	$$('.displayer').each ( function(displayer, i) {
		var items = displayer.getElements(".content ul li");
		var pos = 5;
		var content = displayer.getElement('.content');

		var globalWidth = 0;
		var initialTime = (items.length * 2500);

                // emarsden2008-12-22 for mootools 1.2.1 transition
                // http://mootools.net/docs/Fx/Fx.Tween
		var fx = new Fx.Tween(content, {
			duration: initialTime,
			transition: Fx.Transitions.linear,
			onComplete: function () {
                            fx.set('duration', initialTime);
                            if (content.getStyle('left').toInt()>=0) {
                                fx.start('left',  - globalWidth + 'px');
                            } else
                                fx.start('left', '0px');
			}
                    });

                // emarsden: original version
// 		var fx = new Fx.Styles( content, {
// 			duration: initialTime,
// 			transition: Fx.Transitions.linear,
// 			onComplete: function () {
// 				fx.options.duration = initialTime;
// 				if (content.getStyle('left').toInt()>=0) {
// 					fx.start({'left':  - globalWidth + 'px'});
// 				} else fx.start({'left': '0px'});
// 			}
// 		} );

		var go = items.length;

		items.each (function (item, j) {

			var liens = item.getElements('a');
			if ($chk(liens)) {
				liens.each ( function(lien, k) {
					lien.addEvent('click', function (e) {
						e = new Event(e);
						window.open(this.href);
						e.stop();
					});
				});
			}
		
			item.addEvent('mouseenter', function (e) {
				fx.stop();

				var dest = fx.to;
				var res = new Array();
				
				res[0] = Math.abs(fx.from.left - fx.to.left).round();
				res[1] = Math.abs(fx.to.left - fx.now.left).round();
				var newTime = ((res[1] * fx.options.duration) / res[0]).round();
				
				item.addEvent('mouseleave', function (e) {
					if (!isNaN(newTime)) { fx.options.duration = newTime; }
					fx.start('left', fx.to.left);
				});
			});
			
			item.getElement('img').addEvent('load', function() {
				item.setStyles ({'left': pos+'px', 'top': '0px' });
				pos = pos + item.getElement('img').getCoordinates().width +10;
				if (pos>globalWidth)  globalWidth = pos - content.getCoordinates().width;
				
				go--;
				if (go<=0) { 
                                    fx.start('left', - globalWidth + 'px');
				}
				//$('monitor1').setText(Json.toString(fx));}
			});
		});
	});

	$$('.slideIt').each( function (button) {
		var slidable = button.getParent().getElement('.slidable');
		if ($chk(slidable)) {
			var fx = new Fx.Slide(slidable);
			if (!slidable.hasClass('notHide')) fx.hide();
			button.addEvent('click', function (e) { e = new Event(e).stop(); fx.toggle(); });
		}
	});
}

window.addEvent('domready', PMRanimate);

