// <![CDATA[

Object.extend(Event, {
	wheel:function (event){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120; 
			if (window.opera) delta = -delta;
		} else if (event.detail) { delta = -event.detail/3;	}
		return Math.round(delta); //Safari Round
	}
});

var News = Class.create({
	initialize: function(el, news)
	{
		var id = '#{newsID}'.interpolate(news);
		el.insert('<div id="news' + id + '" class="actu"></div>');
		
		this.container = $('news' + id);
		
		this.opened = '<img alt="" src="/images/news/news_#{newsID}.jpg"/><div id="nb#{newsID}" class="newsblock"><div class="actu-title">#{title}</div><div id="newscontent#{newsID}" class="actu-content">#{content}</div></div><div id="commands#{newsID}" class="actu-commands"><a href="#" onclick="return news.close(#{newsID});" class="close" title="fermer">fermer</a></div>'.interpolate(news);
		this.closed = '<img alt="" src="/images/news/news_#{newsID}.jpg"/><div class="actu-title">#{title}</div><div id="commands#{newsID}" class="actu-commands"><a href="#" onclick="javascript:return news.open(#{newsID});" class="open" title="en savoir +">en savoir +</a></div>'.interpolate(news);

		this.container.insert(this.closed);
	}
});

var NewsWidget = Class.create({
	initialize: function(){
		this.news = $A();
		var container = $('news').insert('<div id="actualites">ACTUALIT&Eacute;S</div>');

		new Ajax.Request(
			'/Ajax/getnews',
			{
				method: 'get',
				onSuccess: function(transport)
				{
					var news = transport.responseJSON;
					
					this.news.push(new News(container, news[0]));
					this.news.push(new News(container, news[1]));
				}.bind(this)
			}
		);
	},
	
	open: function(newsID)
	{
		var n = this.news[newsID-1];
		n.container.update(n.opened).addClassName('opened');;

		var c   = $('nb' + newsID).hide();
		var cmd = $('commands' + newsID).hide();

		new Effect.BlindDown(c,
		{
			duration:.5,
			afterFinish: function(){
					new Effect.Appear(cmd, {duration:.2});
				}
		});
		
		return false;
	},
	
	close: function(newsID)
	{
		var n = this.news[newsID-1];
		var cmd = $('commands' + newsID);		
		new Effect.toggle(cmd, 'appear', {duration:.2});
		new Effect.BlindUp($('nb' + newsID),
		{
			duration:.5,
			afterFinish: function(){
					n.container.update(n.closed).removeClassName('opened');
					var cmd = $('commands' + newsID).hide();
					new Effect.Appear(cmd, {duration:.2});
				}
		});

		return false;
	}
});

var ScrollPane = Class.create({
	initialize: function(el, maxHeight){
		this.el = $(el);
		if(!this.el) return;

		this.content = $('content');
		this.h       = this.el.getHeight();
		this.timer   = null;
		this.loop    = false;

		this.setMaxHeight(maxHeight);
	},
	
	setMaxHeight: function(maxHeight){
		this.max_h  = parseInt(maxHeight);
		
		if(this.h>this.max_h)
		{
			this.addScrollBar();
		}
	},
	
	addScrollBar: function(){
		if($('scrollbar')){
			this.scroll = null;
			$('scrollbar').remove();
		}

		var s = '<div id="scrollbar">' +
					 '<a href="#" id="scrollUp" title="monter">Monter</a>' +
					 	'<div id="track" style="height:' + (this.max_h-35) + 'px">' +
					 		'<div id="handle"><img src="/images/scroll_handle.gif" alt="" /></div>' +
					 	'</div>' +
					 '<a href="#" id="scrollDown" title="descendre">Descendre</a>' +
				 '</div>';

		this.el.setStyle({
			width: this.content.getWidth() - 65 + "px",
			height: this.max_h + "px",
			overflow: 'hidden'
		});
		this.el.addClassName('scrolling');

		this.content.style.height = this.max_h + "px";
		this.content.insert({'top': s});

		this.scroll = new Control.Slider('handle', 'track', {
			axis:     'vertical',
			range:    $R(0, this.h),
			onSlide:  function(v){ this.scrollVertical(v); }.bind(this),
			onChange: function(v){ this.scrollVertical(v); }.bind(this)
		});

		var btn_scrollUp = $('scrollUp');
		btn_scrollUp.observe("mousedown", this.startScrollUp.bindAsEventListener(this));
		btn_scrollUp.observe("mouseup",   this.stopScroll.bindAsEventListener(this));
		btn_scrollUp.observe("click",     this.stopScroll.bindAsEventListener(this));
		btn_scrollUp.observe("mouseout",  this.stopScroll.bindAsEventListener(this));

		var btn_scrollDown = $('scrollDown')
		btn_scrollDown.observe("mousedown", this.startScrollDown.bindAsEventListener(this));
		btn_scrollDown.observe("mouseup",   this.stopScroll.bindAsEventListener(this));
		btn_scrollDown.observe("click",     this.stopScroll.bindAsEventListener(this));
		btn_scrollDown.observe("mouseout",  this.stopScroll.bindAsEventListener(this));
		
		this.content.observe("mousewheel", this.wheelScroll.bindAsEventListener(this), false);
		this.content.observe("DOMMouseScroll", this.wheelScroll.bindAsEventListener(this), false);
	},
	
	wheelScroll: function(event){
		this.scroll.setValueBy(-Event.wheel(event)*36);
	},
	
	scrollVertical: function(value){
		var el = this.el;
		el.scrollTop = Math.round(value/this.scroll.maximum*(el.scrollHeight-el.offsetHeight));
	},
	
	startScrollUp: function(event){
		event.stop();

		this.scroll.setValueBy(-24);
		this.timer = setTimeout(this.scrollUp.bind(this), 500);
		this.loop  = true;
	},

	scrollUp: function(){
		var v = this.scroll.value - 14;

		if(v<0){
			v = 0;
			this.loop = false;
		}

		this.scroll.setValue(v);

		if(this.loop){
			this.timer = setTimeout(this.scrollUp.bind(this), 18);
		}
	},
	
	startScrollDown: function(event){
		event.stop();

		this.scroll.setValueBy(24);
		this.timer = setTimeout(this.scrollDown.bind(this), 500);
		this.loop  = true;
	},

	scrollDown: function(){
		var v   = this.scroll.value + 14;
		var max = this.scroll.maximum;

		if(v>max){
			v = max;
			this.loop = false;
		}

		this.scroll.setValue(v);

		if(this.loop){
			this.timer = setTimeout(this.scrollDown.bind(this), 18);
		}
	},

	stopScroll: function(event){
		event.stop();
		this.loop = false;
		clearTimeout(this.timer);
	}
});

var PopupPages = Class.create(
{
	initialize: function()
	{
		$$('a.popup').each(function(link){
			link.observe('click', function(event){
				event.stop();
				
				var dims = document.viewport.getDimensions();
				var left = (dims.width-860)/2;
				var top  = (dims.height-455)/2;
	
				window.open(link.href, 'popup', 'menubar=no,status=no,scrollbars=yes,menubar=no,width=860,height=455,resize=no,left=' + left + ',top=' + top);
			});
		});
	}
});

var scroll = null;
var news   = null;
var popup  = null;

document.observe("dom:loaded", function(){
	scroll = new ScrollPane('data', 350);
	news   = new NewsWidget();
	popup  = new PopupPages();
});

// ]]>
