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 NewSlider=new Class({
	Extends : BoxSlider,
	boxId: '',
	boxImage: '',
	boxTitle: '',
	boxLink: '',
	boxDescription: '',
	boxSpan: '',
	slideElement: '',
	onState: {'opacity':1},
	offState: {'opacity':0},
	initialize: function(id,sourceUrl,frequency)
		{
		this.boxId=id;
		this.slideElement=$$('#'+this.boxId+' div.bcontent')[0];
		this.boxTitleLink=$$('#'+this.boxId+' h3 a')[0];
		if(this.boxTitleLink)
		this.boxTitleSpan=this.boxTitleLink.firstChild;
		this.boxDescription=$$('#'+this.boxId+' span.description')[0];
		this.boxMoreLink=$$('#'+this.boxId+' a.more')[0];
		if($(this.boxMoreLink))
			this.boxMoreSpan=this.boxMoreLink.firstChild;
		this.boxImage=$$('#'+this.boxId+' img')[0];
/*		var boxParagraph=$$('#'+this.boxId+' div.bcontent p')[0];
		if(boxParagraph&&!this.boxImage)
			{
			this.boxImage=document.createElement('img');
			boxParagraph.parentNode.insertBefore(this.boxImage,boxParagraph.firstChild);
			$(this.boxImage).setStyle('display','none');
			}*/
		this.parent(sourceUrl,frequency);
		},
	displayContent: function()
		{
		if($(this.boxImage)&&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 if($(this.boxImage))
			{
			$(this.boxImage).setStyle('display','none');
			}
		if(this.boxTitleLink)
			{
			$(this.boxTitleLink).set('href', this.entries[this.curEntry].permalink);
			$(this.boxTitleLink).set('title', this.entries[this.curEntry].shortdesc);
			if(this.entries[this.curEntry].shorttitle||this.entries[this.curEntry].title)
				{
				$(this.boxTitleSpan).innerHTML=(this.entries[this.curEntry].shorttitle?this.entries[this.curEntry].shorttitle:this.entries[this.curEntry].title);
				$(this.boxTitleSpan).setStyle('display','block');
				}
			else
				{
				$(this.boxTitleSpan).setStyle('display','none');
				}
			}
		if(this.boxDescription)
			$(this.boxDescription).innerHTML=this.entries[this.curEntry].description;
		if($(this.boxMoreLink))
			{
			$(this.boxMoreLink).set('href', this.entries[this.curEntry].permalink);
			$(this.boxMoreLink).set('title', this.entries[this.curEntry].shortdesc);
			}
		}
	});