var BoxSlider=new Class({
	frequency:5,
	entries:{},
	curEntry:1,
	curTimeOut:'',
	imageBind:'',
	imageLoaded:[],
	transition:Fx.Transitions.Sine.easeOut,
	initialize: function(sourceUrl,frequency)
		{
		if(frequency)
			this.frequency=frequency;
		new Request.JSON({url: sourceUrl, onSuccess: this.handleDatas.bind(this)}).send();
		this.imageBind=this.letAppear.bind(this);
		},
	handleDatas: function(datas)
		{
		if(datas.entries.entry.length>1)
			{
			this.entries=datas.entries.entry;
			//this.curTimeOut=this.makeDisappear.delay(this.frequency*1000,this);
			this.prepareNext();
			}
		},
	prepareNext: function()
		{
		$clear(this.curTimeOut);
	//	confirm(this.curEntry);
		if(this.curEntry>=this.entries.length-2)
			this.curEntry=0;
		else
			this.curEntry++;
	//	confirm(this.curEntry);
		this.curTimeOut=this.makeDisappear.delay(this.frequency*1000,this);
		},
	makeDisappear: function()
		{
		$clear(this.curTimeOut);
		var fx=new Fx.Morph($(this.slideElement),{duration: 'short', transition: this.transition, onComplete:this.loadContent.bind(this)});
		fx.start(this.offState);
		},
	loadContent: function()
		{
		if(this.entries[this.curEntry].thumb&&!this.imageLoaded[this.curEntry])
			{
			this.imageLoaded[this.curEntry]=new Image();
			this.imageLoaded[this.curEntry].src=this.entries[this.curEntry].thumb;
			$(this.imageLoaded[this.curEntry]).addEvent('load', this.imageBind);
			}
		this.displayContent();
		if((!this.entries[this.curEntry].thumb)||this.imageLoaded[this.curEntry])
			this.letAppear();
		},
	letAppear: function()
		{
		if(this.entries[this.curEntry].thumb&&this.imageLoaded[this.curEntry])
			this.imageLoaded[this.curEntry].removeEvent('load', this.imageBind);
		if(this.beforeAppear)
			this.beforeAppear();
		var fx=new Fx.Morph($(this.slideElement),{duration: 'long', transition: this.transition, onComplete:null});
		fx.start(this.onState);
		this.prepareNext();
		}
	});

var RefSlider=new Class({
	Extends : BoxSlider,
	boxImage: '',
	boxTitle: '',
	boxLink: '',
	boxSpan: '',
	slideElement: '',
	onState: {'width':300, 'height':225, 'opacity':[0,1]},
	offState: {'width':0, 'height':225, 'opacity':[1,0]},
	initialize: function(sourceUrl,frequency)
		{
		this.slideElement=this.boxImage=$$('#refbox img')[0];
		this.boxTitle=$$('#refbox h3 a')[0];
		this.boxLink=$$('#refbox p a')[0];
		this.boxSpan=$$('#refbox p a span')[0];
		this.parent(sourceUrl,frequency);
		},
	displayContent: function()
		{/*
		$(this.boxImage).set('src', this.entries[this.curEntry].thumb);
		$(this.boxImage).set('alt', this.entries[this.curEntry].shorttitle);*/
		$(this.imageLoaded[this.curEntry]).set({'opacity': 0, 'height':''});
		$(this.imageLoaded[this.curEntry]).set('alt', this.entries[this.curEntry].shorttitle);
		this.boxImage.parentNode.insertBefore(this.imageLoaded[this.curEntry],this.boxImage);
		this.boxImage.parentNode.removeChild(this.boxImage);
		this.slideElement=this.boxImage=this.imageLoaded[this.curEntry];
		$(this.boxTitle).set('href', this.entries[this.curEntry].permalink);
		$(this.boxTitle).set('title', this.entries[this.curEntry].shortdesc);
		$(this.boxLink).set('href', this.entries[this.curEntry].permalink);
		$(this.boxLink).set('title', this.entries[this.curEntry].shortdesc);
		$(this.boxSpan).innerHTML=this.entries[this.curEntry].shorttitle;
		},
	beforeAppear: function()
		{
		var height=this.imageLoaded[this.curEntry].height;
		if(height>225);
			height=225;
		var padding=(225-height)/2;
		if(padding<=0)
			padding=0;
		this.onState={'width':300, 'height':height, 'opacity':[0,1]};
		this.offState={'width':0, 'height':height, 'opacity':[1,0]};
		$(this.boxImage).style.paddingTop=padding+'px';
		$(this.boxImage).style.paddingBottom=padding+'px';
		}
	});

var NewSlider=new Class({
	Extends : BoxSlider,
	boxImage: '',
	boxTitle: '',
	boxLink: '',
	boxDescription: '',
	boxSpan: '',
	slideElement: '',
	onState: {'opacity':1},
	offState: {'opacity':0},
	initialize: function(sourceUrl,frequency)
		{
		this.slideElement=$$('#newsbox div.bcontent')[0];
		this.boxTitleLink=$$('#newsbox h3 a')[0];
		this.boxTitleSpan=this.boxTitleLink.firstChild;
		this.boxDescription=$$('#newsbox span.description')[0];
		this.boxMoreLink=$$('#newsbox a.more')[0];
		this.boxMoreSpan=this.boxMoreLink.firstChild;
		this.boxImage=$$('#newsbox img')[0];
		if(!this.boxImage)
			{
			this.boxImage=document.createElement('img');
			this.boxDescription.parentNode.insertBefore(this.boxImage,this.boxDescription);
			$(this.boxImage).setStyle('display','none');
			}
		this.parent(sourceUrl,frequency);
		},
	displayContent: function()
		{
		if(this.entries[this.curEntry].thumb)
			{
			$(this.boxImage).set('src', this.entries[this.curEntry].thumb);
			$(this.boxImage).set('alt', this.entries[this.curEntry].shorttitle);
			$(this.boxImage).setStyle('display','block');
			}
		else
			$(this.boxImage).setStyle('display','none');
		$(this.boxTitleLink).set('href', this.entries[this.curEntry].permalink);
		$(this.boxTitleLink).set('title', this.entries[this.curEntry].shortdesc);
		$(this.boxTitleSpan).innerHTML=this.entries[this.curEntry].shorttitle;
		$(this.boxDescription).innerHTML=this.entries[this.curEntry].description;
		$(this.boxMoreLink).set('href', this.entries[this.curEntry].permalink);
		$(this.boxMoreLink).set('title', this.entries[this.curEntry].shortdesc);
		$(this.boxMoreSpan).innerHTML=this.entries[this.curEntry].shorttitle;
		}
	});
