
/**************************************************************

	Script		: SlideShow
	Version		: 0.1
	Authors		: Peter Eichhorn
	Desc		: 

**************************************************************/

var SlideShow = new Class({
			
	initialize : function(container, images, options){
	
		this.options = options
	
		this.container = $(container);
		this.slide = this.container.getElements('img')[0];
		this.slideshowimages = $(images).getElements('img');
		this.index = 0;
		this.interval = 0;
		
		this.maxwidth = this.options.maxwidth;
		this.maxheight = this.options.maxheight;
		this.imgpath = this.options.imgpath;
		
		this.autoplay = this.options.autoplay;
		this.enabled = false;
		
		
		if(this.slideshowimages.length > 0){
			this.setimage()
		}
		if(this.slideshowimages.length > 1){
			this.enabled = true;
			$(this.options.controlnext).setStyle('display','block');
			$(this.options.controlprevious).setStyle('display','block');
			
		}
		if(this.autoplay){
			this.play()
		}
		
	},
	play : function(){
		if(this.enabled){
			this.interval = setTimeout(function(){
				show.next();
			},this.options.wait);
			if(this.autoplay == false){
				this.autoplay = true;
			}
		}
	},
	next : function(){
		if(this.enabled){
			clearTimeout(this.interval)
			this.index++
			if(this.index == this.slideshowimages.length){
				this.index = 0;
			}
			this.setimage();
			if(this.autoplay){
				this.play();
			}
		}
	},
	previous : function(){
	
		if(this.enabled){
			clearTimeout(this.interval)
			this.index--
			if(this.index == -1){
				this.index = this.slideshowimages.length-1;
			}
			this.setimage();
			if(this.autoplay){
				this.play();
			}
		}
	},
	setslide : function(index){
		this.index = index;
		this.setimage();
	},
	setimage : function(){
		
		this.container.fade('hide');
		
		var imageName = this.slideshowimages[this.index].src.substr(this.slideshowimages[this.index].src.indexOf('_')+1);
		this.container.set('html', '<a href="'+this.imgpath+imageName+'" rel="boxed"><img src="'+this.slideshowimages[this.index].src+'" alt="'+this.slideshowimages[this.index].alt+'" /></a>');
		this.positionimage();
		
		SqueezeBox.assign($$('a[rel=boxed]'));
		
		this.container.fade('in');
		
	},
	positionimage : function(){
		
		var w = this.slideshowimages[this.index].width
		var h = this.slideshowimages[this.index].height
		
		if(w < this.maxwidth){
			
			//this.container.setStyle('margin-left',Math.ceil(this.maxwidth-w));
			this.container.setStyle('margin-left',0);
			
		}else{
			this.container.setStyle('margin-left',0);
		}
		if(h < this.maxheight){
			this.container.setStyle('margin-top',0);
		}else{
			this.container.setStyle('margin-top',0);
		}
		this.positioncontrols()
		
	},
	positioncontrols : function(){
		
		var w = this.slideshowimages[this.index].width
		var h = this.slideshowimages[this.index].height
		
		if($(this.options.controlnext)){
			var moveControl1 = new Fx.Morph($(this.options.controlnext), {duration: 'short', transition: Fx.Transitions.Cubic.easeOut});
			moveControl1.start({
				'margin-top': Math.ceil(h-60),
				'margin-left': 0
				//'margin-left': Math.ceil(this.maxwidth-w)
			});
			
			var moveControl2 = new Fx.Morph($(this.options.controlprevious), {duration: 'short', transition: Fx.Transitions.Cubic.easeOut});
			moveControl2.start({
				'margin-top': Math.ceil(h-60),
				'margin-left': Math.ceil(w-30)
			});
		}
		
		if($('tl')){
			var border1 = new Fx.Morph($('tl'), {duration: 'short', transition: Fx.Transitions.Cubic.easeOut});
			border1.start({
				'margin-top': 0,
				'margin-left': 0
				//'margin-left': Math.ceil(this.maxwidth-w)
			});
			var border2 = new Fx.Morph($('tr'), {duration: 'short', transition: Fx.Transitions.Cubic.easeOut});
			border2.start({
				'margin-top': 0,
				'margin-left': Math.ceil(w-11)
				//'margin-left': Math.ceil(this.maxwidth-11)
			});
			var border3 = new Fx.Morph($('br'), {duration: 'short', transition: Fx.Transitions.Cubic.easeOut});
			border3.start({
				'margin-top': Math.ceil(h-11),
				'margin-left': Math.ceil(w-11)
				//'margin-left': Math.ceil(this.maxwidth-11)
			});
			var border4 = new Fx.Morph($('bl'), {duration: 'short', transition: Fx.Transitions.Cubic.easeOut});
			border4.start({
				'margin-top': Math.ceil(h-11),
				'margin-left': 0
				//'margin-left': Math.ceil(this.maxwidth-w)
			});
		}
		if($('t')){
			var edge1 = new Fx.Morph($('t'), {duration: 'short', transition: Fx.Transitions.Cubic.easeOut});
			edge1.start({
				'margin-top': 0,
				//'margin-left': Math.ceil(this.maxwidth-w),
				'margin-left': 0,
				'height': 11,
				'width': w
			});
		}
		if($('r')){
			var edge2 = new Fx.Morph($('r'), {duration: 'short', transition: Fx.Transitions.Cubic.easeOut});
			edge2.start({
				'margin-top': 0,
				//'margin-left': Math.ceil(this.maxwidth-11),
				'margin-left': Math.ceil(w-11),
				'height': h,
				'width': 11
			});
		}
		if($('b')){
			var edge3 = new Fx.Morph($('b'), {duration: 'short', transition: Fx.Transitions.Cubic.easeOut});
			edge3.start({
				'margin-top': Math.ceil(h-11),
				'margin-left': 0,
				//'margin-left': Math.ceil(this.maxwidth-w),
				'height': 11,
				'width': w
			});
		}
		if($('l')){
			var edge4 = new Fx.Morph($('l'), {duration: 'short', transition: Fx.Transitions.Cubic.easeOut});
			edge4.start({
				'margin-top': 0,
				'margin-left': 0,
				//'margin-left': Math.ceil(this.maxwidth-w),
				'height': h,
				'width': 11
			});
		}
		
		
	}
})


/*************************************************************/

