var contentSlider = new Class({
	currentEl: false
	,sliderId: '#slider'
	,infoEl: false
	,currentCount: 1
	,initialize: function()
	{
		// Load the infoEl
		this.infoEl = $$(this.sliderId+' .sliderInfo')[0];
		
		if(this.infoEl)
		{
		
			// Fade all images to 0
			var i = 0;
			$each($$(this.sliderId+' .sliderContent li'), function(el){
				//el.setStyle('opacity', 0);
				if(i==0) this.infoEl.set('text', el.getChildren('a')[0].title);
				// YouTube
				if(el.hasClass('youtube') && el.getChildren('a')[0].innerHTML == "")
				{
					var embedObj = this.makeEmbedObj(el.getChildren('a')[0].href);
					el.getChildren('a')[0].innerHTML = embedObj;
				}
				i++;
				el.fade(0);
				el.setStyle('display', 'block');
				
			}.bind(this));
			
			$("from").set('text', this.currentCount);
			$("to").set('text', i);
			
			// Fade active to 1
			$each($$(this.sliderId+' .sliderContent li.active'), function(el)
			{
				el.fade(1);
				this.currentEl = el;
				// YouTube
				if(el.hasClass('youtube') && el.getChildren('a')[0].innerHTML == "")
				{
					var embedObj = this.makeEmbedObj(el.getChildren('a')[0].href);
					el.getChildren('a')[0].innerHTML = embedObj;
				}
			}.bind(this));
			
			this.setIcon();
			
			$('sliderPrevious').addEvent('click', this.previous.bind(this));
			$('sliderNext').addEvent('click', this.next.bind(this));
		}
	}
	,next: function(e)
	{
		e.stop();
		if(this.currentEl.getNext() != null)
		{
			this.currentEl.fade(0);
			this.currentEl = this.currentEl.getNext();
			this.infoEl.set('text', this.currentEl.getChildren('a')[0].title);
			this.setIcon();
			this.currentEl.fade(1);
			this.currentCount++;
			$("from").set('text', this.currentCount);
		}
	}
	,previous: function(e)
	{
		e.stop();
		if(this.currentEl.getPrevious() != null)
		{
			this.currentEl.fade(0);
			this.currentEl = this.currentEl.getPrevious();
			this.infoEl.set('text', this.currentEl.getChildren('a')[0].title);
			this.setIcon();
			this.currentEl.fade(1);
			this.currentCount--;
			$("from").set('text', this.currentCount);
		}
	}
	,makeEmbedObj: function(url)
	{
		
		var str = '<object '
		str += 'width="386" ';
		str += 'height="262" ';
		str += 'title="MultiBoxMedia">';
		str += '<param name="movie" value="'+url+'"></param>'
		str += '<param name="quality" value="high"></param>';
		str += '<param name="allowscriptaccess" value="always"></param>';
		str += '<param name="allowFullScreen" value="true"></param>';
		str += '<embed src="'+url+'" ';
		str += 'quality="high" type="application/x-shockwave-flash" ';
		str += 'allowfullscreen="true" ';
		str += 'allowscriptaccess="always" ';
		str += 'width="386" ';
		str += 'height="262" wmode="transparent"></embed>';
		str += '</object>';
		return str;
	}
	,setIcon: function()
	{
		var icon = $$('.sliderIcon')[0];
		icon.href = this.currentEl.getChildren('a')[0].rel;
		if(this.currentEl.hasClass('flickr'))
		{
			icon.setStyle('background-image', 'url(/design/images/layout/slider/icon_flickr.png)');
		}
		else
		{
			icon.setStyle('background-image', 'url(/design/images/layout/slider/icon_youtube.png)');
		}
	}
});

$(document).addEvent('domready', function(){
	new contentSlider();
});
