// <![CDATA[


var BgCarrouselWidget = Class.create(
{
	initialize: function(element)
	{
		this.element = $(element);

		if(!this.element)
		{
			return;
		}

		this.options = Object.extend({colors: ['#ffffff', '#000000'], startcolor: '#ffffff', duration: 10}, arguments[1] || {});
		this.current = 0;
		this.element.setStyle({backgroundColor: this.options.colors[this.current]});

		this.run();
	},

	run: function()
	{
		var effect = Class.create(Effect.Base,
		{
			initialize: function(element, options)
			{
				this.element = element;

				this.start(options);
			},

			setup: function()
			{
				this._base  = $R(0,2).map(function(i){ return parseInt(this.options.startcolor.slice(i*2+1,i*2+3),16) }.bind(this));
				this._delta = $R(0,2).map(function(i){ return parseInt(this.options.endcolor.slice(i*2+1,i*2+3),16)-this._base[i] }.bind(this));
			},

			update: function(position)
			{
				this.element.setStyle({backgroundColor: $R(0,2).inject('#',function(m,v,i){
					return m+((this._base[i]+(this._delta[i]*position)).round().toColorPart()); }.bind(this))
				});
			}
		});

		var colors = this.options.colors;
		var start  = colors[this.current];
		var next   = ((this.current+1) % colors.length);
		var end    = colors[next];

		new effect(
			this.element, {
				startcolor: start,
				endcolor: end,
				duration: this.options.duration,
				afterFinish: function(){
					this.current = next;
					this.run();
				}.bind(this)
		});
	}
});


var TickerWidget = Class.create(
{
	initialize: function(element)
	{
		this.element = $(element);

        if(!this.element)
        {
            return false;
        }

		this.options = Object.extend({dataURL: '', duration: 5, start: 0}, arguments[1] || {});

		new Ajax.Request(
			this.options.dataURL,
			{
				method: 'get',
				onSuccess: function(transport)
				{
					this.options.texts = transport.responseJSON;
				}.bind(this),
				onFailure: function()
				{
					this.options.texts = [{message: '<a href="http://e-spiration.fr" title="Développement pour le Web">E-Spiration</a>'}];
				}.bind(this),
				onComplete: function()
				{
					this.current = this.options.start % this.options.texts.length;
					this.run();
				}.bind(this)
			}
		);
	},

	run: function()
	{
		var texts = this.options.texts;

		this.element.update('<table id="ticktext"><tbody><tr><td>' + texts[this.current].message + '</td></tr></tbody></table>');

		var text = $('ticktext').hide();

		new Effect.toggle(text, 'appear', {
			duration: 2,
			afterFinish: function(){
				setTimeout(function(){
					new Effect.toggle(text, 'appear', {
						duration:2,
						afterFinish: function(){
							this.current =((this.current+1) % texts.length);
							this.run();
						}.bind(this)});
					}.bind(this),
					this.options.duration*1000);
			}.bind(this)
		});
	}
});

var NewsWidget = Class.create(
{
	initialize: function()
	{
		this.news     = $$('div.news-content');

		if(this.news.length==0) return;

		this.commands = $$('div.news-date a');
		this.current  = 0;
		this.updating = false;
		var height    = 0;
		this.news.each(function(n){ n.hide(); });
//		$('main-content').setStyle({'height': height + 'px'});

//		this.news[0].show();

		this.commands.each(function(c, i){
			c.observe('click', function(event){
				event.stop();

				if(!this.updating)
				{
					this.updating = true;
//
//					new Effect.Parallel(
//					[
//						new Effect.BlindUp(this.news[this.current], {sync: true}),
//						new Effect.BlindDown(this.news[i], {sync: true}),
//					],
//					{
//						duration:.5,
//						afterFinish: function()
//						{
//							this.current  = i;
//							this.updating = false;
//						}.bind(this)
//					});

					if(c.hasClassName('selected'))
					{
						new Effect.BlindUp(this.news[i], {duration:.2, afterFinish: function(){
							c.removeClassName('selected');
							this.updating = false;
						}.bind(this)});
					}
					else
					{
						new Effect.BlindUp(this.news[this.current], {duration:.2, afterFinish: function(){
							this.commands[this.current].removeClassName('selected');
							this.current = i;
							c.addClassName('selected');
							new Effect.BlindDown(this.news[this.current], {duration:.2, afterFinish: function(){
								this.updating = false;
							}.bind(this)});
						}.bind(this)});
					}
				}
			}.bind(this))
		}.bind(this));
	}
});

var PrintWidget = Class.create(
{
	initialize: function()
	{
		this.element = $('print');

		if(!this.element)
		{
			return;
		}

		this.element.observe('click', function(event){
			event.stop();
			window.print();
		});
	}
});

var BackWidget = Class.create(
{
	initialize: function()
	{
		this.element = $('go-back');

		if(!this.element)
		{
			return;
		}

		this.element.observe('click', function(event){
			event.stop();
			history.go(-1);
		});
	}
});

document.observe('dom:loaded', function(){
	new NewsWidget();
	new BgCarrouselWidget('top-container', {colors: ['#7b003b', '#263188', '#6fab2b']});
	new TickerWidget('ticker', {dataURL: '/API/Ajax/getTickerData'});
	new PrintWidget();
	new BackWidget();
	/*
	var logo = $('logo-BCCC');
	if(typeof swfobject != 'undefined' && logo)
	{
		swfobject.embedSWF("/medias/anims/BCCC_small_logo.swf", "logo-BCCC", "150", "80", "9.0.0", "/scripts/lib/swfobject/expressInstall.swf", null, {wmode: 'transparent'});
		logo.styleFloat = 'left';
	}
	*/
});


// ]]>
