JaSlideshowClass = new Class({
	initialize: function(options) {
		this.options = options;
		if (this.options.images.length <= 1) { return; }

		if (this.options.pan != 'rand') {
			if (isNaN(this.options.pan.toInt()) || this.options.pan.toInt() < 0 || this.options.pan.toInt() > 100) { this.options.pan = 0; }
		}
		if (this.options.zoom != 'rand') {
			if (isNaN(this.options.zoom.toInt()) || this.options.zoom.toInt() < 0 || this.options.zoom.toInt() > 100) { this.options.zoom = 0; }
		}
		this.elementdiv = $('ja-slideshow-case');
		this.bardiv = $('ja-slidebar');
		this.image = img = $E('img', this.elementdiv);
		this.fx = [];
		this.start();
	},

	start: function() {
		this.elementdiv.setHTML('');

		this.image.setStyles({display: 'block', position: 'absolute', left: '0px', top: '0px', zIndex: 1});
		this.image.injectInside(this.elementdiv);
		this.setSize();
		this.elementdiv.setStyles({display: 'block', position: 'relative', width: this.width + 'px'});
		this.div = new Element('div');
		this.div.setStyles({display: 'block', height: (this.height + 'px'), overflow: 'hidden', position: 'relative', width: (this.width + 'px')});
		this.div.injectInside(this.elementdiv);
		this.image.injectInside(this.div);

		this.imageAF = this.image.clone();
		this.imageAF.setStyle('opacity', 0);
		this.imageAF.injectAfter(this.image);
		
		if (this.options.navigation) { this.navigation(); }

		this.direction = 'left';
		this.current = [1, 1];
		this.timer = (this.timer) ? [0] : [(new Date).getTime() + this.options.duration[1], 0];

		this.loader = new Image();
		this.loader.src = this.options.url + this.options.images[this.current[0]].trim();

		this.preload();
	},

	setSize: function () {
		obj = this.image.getCoordinates();
		this.height = ((this.options.height) ? this.options.height : obj['height']);
		this.width = ((this.options.width) ? this.options.width : obj['width']);		
	},
	
	resize: function (){
		dh = this.height / this.loader.height;
		dw = this.width / this.loader.width;
		n = (dw > dh) ? dw : dh;
		if (this.options.resize) { img.setStyles({height: Math.ceil(this.loader.height * n) + 'px', width: Math.ceil(this.loader.width * n) + 'px'}); }		
	},
	
	preload: function(fast) {
		if (this.loader.complete && ((new Date).getTime() > this.timer[0])) {
			img = (this.current[1] % 2) ? this.imageAF : this.image;
			img.setStyles({height: 'auto', opacity: 0, width: 'auto', zIndex: this.current[1]});
			img.setProperty('src', this.loader.src);
			
			this.resize();
			
			if (fast) {
				img.setStyles({left: '0px', opacity: 1, top: '0px'});
				return this.loaded();
			}

			this.fx = [];

			if (this.options.type.test(/push|wipe/)) {
				img.setStyles({left: 'auto', right: 'auto'});
				img.setStyle(this.direction, this.width + 'px');
				img.setStyle('opacity', 1);

				if (this.options.type == 'wipe') {
					fx = new Fx.Style(img, this.direction, {duration: this.options.duration[0], transition: this.options.transition});
					fx.start(this.width, 0);
					this.fx.push(fx);
				} 
				else {
					arr = [img, ((this.current[1] % 2) ? this.image : this.imageAF)];

					p0 = {};					
					p0[this.direction] = [this.width, 0];
					p1 = {};
					p1[this.direction] = [0, (this.width * -1)];

					if (arr[1].getStyle(this.direction) == 'auto') {
						x = this.width - arr[1].getStyle('width').toInt();
					
						arr[1].setStyle(this.direction, x + 'px');
						arr[1].setStyle(((this.direction == 'left') ? 'right' : 'left'), 'auto');
						 
						p1[this.direction] = [x, (this.width * -1)];
					}
					
					fx = new Fx.Elements(arr, {duration: this.options.duration[0], transition: this.options.transition});
					fx.start({'0': p0, '1': p1});
					this.fx.push(fx);						
				}
		
			} 
			else {	
				img.setStyles({bottom: 'auto', left: 'auto', right: 'auto', top: 'auto'});

				arr = ['left top', 'right top', 'left bottom', 'right bottom'][this.current[1] % 4].split(' ');
				arr.each(function(p) { img.setStyle(p, 0); });

				zoom = ((this.options.type).test(/zoom|combo/)) ? this.zoom() : {};
					
				pan = ((this.options.type).test(/pan|combo/)) ? this.pan() : {};
				
				fx = new Fx.Style(img, 'opacity', {duration: this.options.duration[0]});
				fx.start(0, 1);
				this.fx.push(fx);

				fx = new Fx.Styles(img, {duration: (this.options.duration[0] + this.options.duration[1]), transition: Fx.Transitions.linear});
				fx.start(Object.extend(zoom, pan));
				this.fx.push(fx);
			}

			this.loaded();
		}
		else { this.timeout = this.preload.delay(100, this); }
	},

	loaded: function() {
		if (this.ul) {
			anchors = $ES('a[name]', this.ul);
			anchors.each(function(a, i) {
				if (i == this.current[0]) { a.addClass(this.options.classes[2]); }
				else { a.removeClass(this.options.classes[2]); }
			}, this);
		}

		this.direction = 'left';
		this.current[0] = (this.current[0] == this.options.images.length - 1) ? 0 : this.current[0] + 1;
		this.current[1]++;
		this.timer[0] = (new Date).getTime() + this.options.duration[1] + ((this.options.type.test(/fade|push|wipe/)) ? this.options.duration[0] : 0);
		this.timer[1] = (new Date).getTime() + this.options.duration[0];

		this.loader = new Image();
		this.loader.src = this.options.url + this.options.images[this.current[0]].trim();
		
		this.preload();
	},

	zoom: function() {
		z = (this.options.zoom == 'rand') ? Math.random() + 1 : (this.options.zoom.toInt() / 100.0) + 1;

		eh = Math.ceil(this.loader.height * n);
		ew = Math.ceil(this.loader.width * n);

		sh = parseInt(eh * z);
		sw = parseInt(ew * z);

		return {height: [sh, eh], width: [sw, ew]};
	},

	pan: function() {
		p = (this.options.pan == 'rand') ? Math.random() : Math.abs((this.options.pan.toInt() / 100.0) - 1);

		ex = (this.width - img.width);
		ey = (this.height - img.height);

		sx = parseInt(ex * p);
		sy = parseInt(ey * p);

		obj = {};

		if (dw > dh) { obj[arr[1]] = [sy, ey] }
		else { obj[arr[0]] = [sx, ex]; }
		
		return obj;
	},

	navigation: function() {
		this.ul = new Element('ul');
		if (this.options.navigation.test(/arrows|number/)) {
			li = new Element('li');
			a = new Element('a');
			a.addClass(this.options.classes[0]);
			//src = this.options.siteurl + "/modules/ja_slideshow/pre.gif";
			//a.innerHTML = '<img src="' +  src + '" alt="Prevous" title="Prevous" border=0>';
			a.innerHTML = '&laquo;';
			a.onclick = function() {
				if (this.options.navigation.test(/fast/) || (new Date).getTime() > this.timer[1]) {	
					$clear(this.timeout);
					if (this.options.navigation.test(/fast/)) {
						this.fx.each(function(fx) { 
							fx.time = fx.options.duration = 0;
							fx.stop(true); 
						});
					}

					this.direction = 'right';
					this.current[0] = (this.current[0] < 2) ? this.options.images.length - (2 - this.current[0]) : this.current[0] - 2;
					this.timer = [0];
					
					this.loader = new Image();
					this.loader.src = this.options.url + this.options.images[this.current[0]].trim();

					this.preload(this.options.navigation.test(/fast/));
				} else {
					this.direction = 'right';
					this.current[0] = (this.current[0] < 2) ? this.options.images.length - (2 - this.current[0]) : this.current[0] - 2;
					this.timer = [0];
					
					this.loader = new Image();
					this.loader.src = this.options.url + this.options.images[this.current[0]].trim();

					this.preload(this.options.navigation.test(/fast/));
					
					this.timer = 0;
				}
			}.bind(this);
			a.injectInside(li);
			li.injectInside(this.ul);
		}
		
		
		if (this.options.navigation.test(/arrows\+|thumbnails|number/)) {
			for (i = 0; i < this.options.images.length; i++) {
				li = new Element('li');
				a = new Element('a');
				a.setProperty('name', i);
				if (this.options.navigation.test(/thumbnails/)) {
					src = this.options.url + this.options.images[i].trim().replace(this.options.thumbnailre[0], this.options.thumbnailre[1]);
					a.setStyle('background-image', 'url(' + src + ')');
				}
				if(this.options.navigation.test(/number/)) {
					a.innerHTML = i+1;
				}
				if (i == 0) { a.className = this.options.classes[2]; }
				a.onclick = function(i) {
					if (this.options.navigation.test(/fast/) || (new Date).getTime() > this.timer[1]) {	
						$clear(this.timeout);
				
						if (this.options.navigation.test(/fast/)) {
							this.fx.each(function(fx) { 
								fx.time = fx.options.duration = 0;
								fx.stop(true); 
							});
						}
						
						this.direction = (i < this.current[0] || this.current[0] == 0) ? 'right' : 'left';
						this.current[0] = i;
						this.timer = [0];			
		
						this.loader = new Image();
					  	this.loader.src = this.options.url + this.options.images[this.current[0]].trim();
		
						this.preload(this.options.navigation.test(/fast/));
					} else {
						this.direction = (i < this.current[0] || this.current[0] == 0) ? 'right' : 'left';
						this.current[0] = i;
						this.timer = [0];			
		
						this.loader = new Image();
					  	this.loader.src = this.options.url + this.options.images[this.current[0]].trim();
		
						this.preload(this.options.navigation.test(/fast/));
						
						this.timer = 0;
					}
				}.pass(i, this);
				a.injectInside(li);
				
				li.injectInside(this.ul);
			}
		}

		if (this.options.navigation.test(/arrows|number/)) {
			li = new Element('li');
			a = new Element('a');
			a.addClass(this.options.classes[1]);
			//src = this.options.siteurl + "/modules/ja_slideshow/next.gif";
			//a.innerHTML = '<img src="' +  src + '" alt="Next" title="Next" border=0>';
			a.innerHTML = '&raquo;';
			a.onclick = function() {	
				if (this.options.navigation.test(/fast/) || (new Date).getTime() > this.timer[1]) {	
					$clear(this.timeout);
					if (this.options.navigation.test(/fast/)) {
						this.fx.each(function(fx) { 
							fx.time = fx.options.duration = 0;
							fx.stop(true); 
						});
					}

					this.timer = [0];					

					this.preload(this.options.navigation.test(/fast/));
				} else {
					this.preload(this.options.navigation.test(/fast/));
					
					this.timer = 0;
				}
			}.bind(this);
			a.injectInside(li);
			
			li.injectInside(this.ul);
		}
		this.buttonsp();
		this.ul.injectInside(this.bardiv);
	},
	
	buttonsp: function () {
			li = new Element('li');
			a = new Element('a');
			a.addClass("ps");
			a.innerHTML = "Pause";
			a.onclick = function() {	
				if(a.text == "Pause"){
					if (this.options.navigation.test(/fast/) || (new Date).getTime() > this.timer[1]) {	
						$clear(this.timeout);
						if (this.options.navigation.test(/fast/)) {
							this.fx.each(function(fx) { 
								fx.time = fx.options.duration = 0;
								fx.stop(true); 
							});
							this.timer = [0];					
							this.preload(this.options.navigation.test(/fast/));
						}
						else{ this.timer = [0];	}
						a.innerHTML = "Play";
					}										
					
				}
				else{						
					this.preload(this.options.navigation.test(/fast/));
					a.innerHTML = "Pause";
				}
			}.bind(this);
			a.injectInside(li);
			li.injectInside(this.ul);		
	}
});
