

/**************************************************************/

var fdSlideshow = new Class({
	
	//implements
	Implements: [Options],
	
	//options
	options:{
		contentHolder: 'stage', //Class of a tags that should trigger the image
		duration: '8000'
	},

	//initialization
	initialize: function(options){
		//set options
		this.setOptions(options);
		this.setOptions(options);
		this.thumbs = [];
		this.count = 0;
		this.index = 0;
		this.height = '250px';
		this.start();
	},
	
	start: function(){	
		$(this.options.contentHolder).getElements('img').each(function(el){
			if (this.count == 0) {
				$(this.options.contentHolder).setStyles({
					height: $(el.id).height + 'px'	
				});			
			
				el.setStyles({
					position:'absolute',
					top: 0,
					left: 0,
					display: 'block'
				});

			} else {
				el.setStyles({
					opacity: 0,
					position:'absolute',
					top: 0,
					left: 0,
					display: 'block'
				});			
			}
			
			this.thumbs.push(el);			
			this.count++;
		},this);
		if (this.count > 1) { this.play.periodical(this.options.duration,this); };
	},
	
	play:function() {
		this.oldThumbEffects = new Fx.Morph($(this.thumbs[this.index].id),{duration:400});
		this.oldThumbEffects.start({
				'opacity':[1,0]
		});	
		
		if (this.index == (this.count-1)) {
			this.index = 0;
		} else {		
			this.index = this.index+1;
		}

		this.thumbEffects = new Fx.Morph($(this.thumbs[this.index].id),{duration:400});
		this.thumbEffects.start({
				'opacity':[0,1]
		});		
	},
	
	
	addInfo:function(){
		this.info = $$('.svf-info');
		this.info.tween('opacity',0.6);
		//this.info[0].setStyle('bottom','-200px');
		
		var showinfo = $('showinfo');
		showinfo.addEvent('click', function(e){
				e.preventDefault();

				if (this.retrieve('hide') == 1) {
					infoEffects = new Fx.Morph($('svf-info'),{duration:500,transition: Fx.Transitions.Sine.easeInOut});
					infoEffects.start({
						'bottom':['-100px']
					});
					this.store('hide',0);
				} else {
					this.store('hide',1);
					infoEffects = new Fx.Morph($('svf-info'),{duration:200,transition: Fx.Transitions.Sine.easeIn});
					infoEffects.start({
						'bottom':['0px']
					});
			}
		},this);
	},

	createControls:function(){
		//next image button
		$$('.svf-next').each(function(el){
			el.removeEvents();
		});
		
		var last = this.thumbs.getLast();
		if (last.id != this.index) {
			$$('.svf-next').each(function(el){
				el.addEvent('click', function(e){
					e.preventDefault();
					this.next();
				}.bind(this));
			},this);
		}

		//image before button
		$$('.svf-back').each(function(el){
			el.removeEvents();
		});
		
		var first = this.thumbs[0].id;
		if (first != this.index) {
			$$('.svf-back').each(function(el){
				el.addEvent('click', function(e){
					e.preventDefault();
					this.back();
				}.bind(this));
			},this);
		}


	},
	
	back:function() {
		var checkArr = this.index.split('svf');
		checkArr[1] = (checkArr[1].toInt()-1);
		this.index = 'svf' + checkArr[1];
		this.loadImage($(this.index).get('href'));
	},


	next:function() {
		var checkArr = this.index.split('svf');
		checkArr[1] = (checkArr[1].toInt()+1);
		this.index = 'svf' + checkArr[1];
		this.loadImage($(this.index).get('href'));
	},
	
	addEventHandler:function(element){
		element.each(function(el,i){
			el.addEvent('click', function(e){
				var myTarget = ($(e.target).match('a')) ? $(e.target) : $(e.target).getParent('a');
				e.preventDefault();
				//alert(myTarget);
				this.index = el.id;
				if (this.index != this.oldIndex) {
					this.loadImage(myTarget);
				}
			}.bind(this));		
		},this)	
	},
	
	loadImage:function(target){
		this.contentObj = new Asset.image(target,{onload:this.controlImageSwitch.bind(this)});
		this.createControls();
	},
	
	controlImageSwitch:function(){
		//this.box = new Element('div').set('id', 'svf-holder').addClass('svfimageholder').inject($('svf-image'),'inside');
		
		this.oldImage = $('svf-image').getElement('img');
		this.contentObj.setStyles({
					display: 'block',
					opacity: 0,
					position:'absolute',
					top: 10,
					left: 10
				});

		this.contentObj.inject($('svf-image'),'inside');
		
		this.contentEffects = new Fx.Morph(this.oldImage,{duration:300});
		this.contentEffects.start({
				'opacity':[1,0]
			});

		this.destroyImage.delay(300,this.oldImage);
		this.showImage.delay(300,this);
		
		//this.size = [];
		this.size = new Object;
		this.size = {'width':this.oldImage.width, 'height':this.oldImage.height, 'newWidth':this.contentObj.width, 'newHeight':this.contentObj.height};
		this.resize.delay(200,this);

	},
	
	resize:function(){

		this.boxEffects = new Fx.Morph($('svf-image'),{duration:200});
		this.boxEffects.start({
				'width':[this.size.width,this.size.newWidth],
				'height':[this.size.height,this.size.newHeight]
			});	
		var info = $$('.svf-info');	
		this.infoEffects = new Fx.Morph(info[0],{duration:200});
		this.infoEffects.start({
				'width':[this.size.width,this.size.newWidth]
			});

	},
	
	showImage:function() {
		if (this.index != this.oldIndex) {	
			var parent = $(this.index).getParent('div');
			var oldParent = $(this.oldIndex).getParent('div');

			this.thumbEffects = new Fx.Morph(parent,{duration:300});
			this.thumbEffects.start({
					'background-color':'#990000'
				});		

			this.oldThumbEffects = new Fx.Morph(oldParent,{duration:300});
			this.oldThumbEffects.start({
					'background-color':'#f4f4ea'
				});	
		
			this.oldIndex = this.index;
		}

		this.contentEffects = new Fx.Morph(this.contentObj,{duration:300});
		this.contentEffects.start({
				'opacity':[0,1]
			});
	},
	
	destroyImage:function(){
		
		if(this){
			this.dispose();//dispose() instead of destroy() as IE 6&7 crashes
		};
	},
	
	addRollover:function(element){
		element.each(function(el,i){
			//if link contains an image ad overlay
			if(el.getElement('img')){
				//add event listeners
				el.addEvents({
					'mouseenter': function(){
						el.getElement('img').set('tween',{duration:150,transition:Fx.Transitions.linear}).tween('opacity',0.5);
					},
					'mouseleave': function(){
						el.getElement('img').set('tween',{duration:300,transition:Fx.Transitions.linear}).tween('opacity',1);
					}
				});
			};
		});
	}	
});






window.addEvent('domready', function() {
		//call fdSlideshow
		var initSlideshow = new fdSlideshow({
			contentHolder: 'stage', //id of the tag that holds the images
			duration: '8000'
		});
});

