ZNZSlideshow = new Class({
	initialize: function(thumbnails, slideshow_image, props) {
		this.props = Object.extend({
			images: [],
			poststampWidth: 86,
			poststampHeight: 88,
			poststampSize: 159,
			thumbnailsCountVisible: 3,
			leftArrowId: 'slideshow_left',
			rightArrowId: 'slideshow_right',
			poststampClass: 'poststamp_big'
		}, props || {});

		if (this.props.images.length < 1) { return; }

		var galleryImgs = $$('#'+slideshow_image+' img');
		if (galleryImgs.length > 0) galleryImgs.each(function(image) {	image.remove();	});

		this.thumbnails = $(thumbnails);
		this.slideshow_image = $(slideshow_image);

		if (this.thumbnails && this.slideshow_image)
		{
			this.currentPosition = 0;
			this.maxZIndex = 0;
			this.currentIndex = 0;
			this.leftArrow = $(this.props.leftArrowId);
			this.rightArrow = $(this.props.rightArrowId);

			if (this.props.images.length > this.props.thumbnailsCountVisible) {
				if (window.ie)
				{
				this.leftArrow.setStyle('cursor', 'hand');
				this.rightArrow.setStyle('cursor', 'hand');
				}
				else
				{
					this.leftArrow.setStyle('cursor', 'pointer');
					this.rightArrow.setStyle('cursor', 'pointer');
				}
				
				this.leftArrow.setStyle('opacity', 0.5);
		
				if (this.props.images.length<=this.props.thumbnailsCountVisible)
				{
					this.rightArrow.setStyle('opacity', 0.5);
				}
				else
				{
					this.rightArrow.setStyle('opacity', 1);
				}
			}
	
			this.fx = new Fx.Styles(this.thumbnails, {duration: 500, wait: false, transition: Fx.Transitions["Cubic"]["easeOut"]});
			this.fxLeftArrow = new Fx.Styles(this.leftArrow, {duration: 250, wait: false, transition: Fx.Transitions.linear});
			this.fxRightArrow = new Fx.Styles(this.rightArrow, {duration: 250, wait: false, transition: Fx.Transitions.linear});
			this.fxSlideshowImage = new Fx.Styles(this.slideshow_image, {duration: 1000, wait: false, transition: Fx.Transitions["Cubic"]["easeOut"]});
			this.pendingEffects = [];
			this.previousIndex = -1;
			this.busy = false;
			this.start();
		}
	},
	cleanQueue: function(){
			var znzSlideshow = this;
			var testAgain = false;
			if (znzSlideshow.pendingEffects.length>=2)
			{
				if (znzSlideshow.pendingEffects[0][0]==znzSlideshow.pendingEffects[1][0] && 
						znzSlideshow.pendingEffects[0][2]+znzSlideshow.pendingEffects[1][2]==1)
				{
					znzSlideshow.pendingEffects.shift();
					znzSlideshow.pendingEffects.shift();
					testAgain = true;
				}
			}
			if (znzSlideshow.pendingEffects.length>2)
			{
				if (znzSlideshow.pendingEffects[0][1]==1 && 
						znzSlideshow.pendingEffects[1][1]==0)
				{
					znzSlideshow.pendingEffects.shift();
					znzSlideshow.pendingEffects.shift();
					testAgain = true;
				}
			}
			if (znzSlideshow.pendingEffects.length>3)
			{
				if (znzSlideshow.pendingEffects[1][1]==1 && 
						znzSlideshow.pendingEffects[2][1]==0)
				{
					t = znzSlideshow.pendingEffects.shift();
					znzSlideshow.pendingEffects.shift();
					znzSlideshow.pendingEffects.shift();
					znzSlideshow.pendingEffects.unshift(t);
					testAgain = true;
				}
			}
			
			if (testAgain) {znzSlideshow.cleanQueue();}
	},
	runQueue: function(force){
		if (!this.busy || force)
		{
			var znzSlideshow = this;
			znzSlideshow.cleanQueue();
			
			if (znzSlideshow.pendingEffects.length>0)
			{
				znzSlideshow.busy = true;
				t = znzSlideshow.pendingEffects.shift();
				if (t[2]==1)
					t[0].setStyle('z-index', ++znzSlideshow.maxZIndex);
				t[0].effect('opacity', {duration: t[1]}).start(t[2]).chain(function() {znzSlideshow.runQueue(true)});
			}
			else
			{
				znzSlideshow.busy = false;
			}
		}
	},
	start: function(){
		
		var images = [];
		var znzSlideshow = this;
		
		for (j = 0; j < this.props.images.length; j++) {
			var span = new Element('span');
			var img = new Element('img');

			span.addClass(this.props.poststampClass);
			
			images[j] = this.props.images[j][1];
			
			img.setProperty('src', this.props.images[j][0]);
			img.setProperty('width', this.props.poststampWidth);
			img.setProperty('height', this.props.poststampHeight);
			img.addEvent('click',function(){

				for (k = 0; k < znzSlideshow.props.images.length; k++) {
					if (znzSlideshow.props.images[k][0]==this.getProperty('src'))
					{
						nextIndex = k;
						break;
					}
				}
				if (znzSlideshow.currentIndex != nextIndex)
				{
					znzSlideshow.previousIndex = znzSlideshow.currentIndex;
					znzSlideshow.currentIndex = nextIndex;
					var firstEffect = [];
					var secondEffect = [];
					znzSlideshow.loadedImages.each(function(image, i) {
						if (image.getProperty('src')==images[znzSlideshow.currentIndex])
						{
							secondEffect = [image, 500, 1];
						}
						else if (image.getProperty('src')==images[znzSlideshow.previousIndex])
						{
							firstEffect = [image, 500, 0];
						}
					});
					if (firstEffect.length>0)
						znzSlideshow.pendingEffects.push(firstEffect);
					if (secondEffect.length>0)
						znzSlideshow.pendingEffects.push(secondEffect);
					znzSlideshow.runQueue(false);
				}
			});
			img.injectInside(span);
			span.injectInside(this.thumbnails);
		}
		
		znzSlideshow.loadedImages = [];
		
		new Asset.images(images, {
			onProgress: function(i) {
				this.setStyles({
					'position': 'absolute',
					'z-index': i,
					'opacity': 0,
					'left': (znzSlideshow.slideshow_image.getCoordinates().width / 2) - (this.width / 2),
					'top': (znzSlideshow.slideshow_image.getCoordinates().height / 2) - (this.height / 2)
				});
				znzSlideshow.maxZIndex=(i>znzSlideshow.maxZIndex)?i:znzSlideshow.maxZIndex;
				znzSlideshow.loadedImages[i] = this;
				this.inject(znzSlideshow.slideshow_image);
				if (this.getProperty('src')==images[znzSlideshow.currentIndex] &&
						znzSlideshow.previousIndex != znzSlideshow.currentIndex)
				{
					znzSlideshow.previousIndex = znzSlideshow.currentIndex;
					znzSlideshow.pendingEffects.push([this, 1500, 1]);
					znzSlideshow.runQueue(false);
				}
			},
			onComplete: function() {
			}
		});

		if (this.props.images.length > this.props.thumbnailsCountVisible) {

			/*
			 * right arrow func
			 */
			this.rightArrow.addEvent('click',function(){
				if (this.currentPosition<this.props.images.length-this.props.thumbnailsCountVisible)
				{
					this.thumbnails.setStyle('width', this.thumbnails.getStyle('width').replace('px', '').toInt() + this.props.poststampSize);
					this.currentPosition++;
					this.fx.start({'left':-this.currentPosition*this.props.poststampSize});
				}
				
				if (this.currentPosition>=this.props.images.length-this.props.thumbnailsCountVisible)
				{
					this.fxRightArrow.start({'opacity':0.5});
				}
	
				if (this.currentPosition>0)
				{
					this.fxLeftArrow.start({'opacity':1});
				}
			}.bind(this));
	
			/*
			 * left arrow func
			 */
			this.leftArrow.addEvent('click',function(){
				if (this.currentPosition>0)
				{
					this.thumbnails.setStyle('width', this.thumbnails.getStyle('width').replace('px', '').toInt() - this.props.poststampSize);
					this.currentPosition--;
					this.fx.start({'left':-this.currentPosition*this.props.poststampSize});
				}
	
				if (this.currentPosition==0)
				{
					this.fxLeftArrow.start({'opacity':0.5});
				}
	
				if (this.props.images.length>this.props.thumbnailsCountVisible)
				{
					this.fxRightArrow.start({'opacity':1});
				}
			}.bind(this));
		}
	}
});


window.addEvent('domready', function(){

	var teasers = $$('#teasers a');
	if (teasers.length > 0) 
		teasers.each(function(teaser) {	
			teaser.addEvent('click',function(){
				var t = this;
				if (t.getProperty('href').substring(0,5)!='http:') {
					window.location.href = "/"+t.getProperty('href');
					return true;
				}
				else if (t.getProperty('target')=='_top') 
				{
					window.location.href = t.getProperty('href');
					return true;
				}
				else if (t.getProperty('target')=="_blank" || t.getProperty('target')=="_new") 
				{
					window.open(t.getProperty('href'));
					return false;
				}
				else 
				{
					return false;
				}
			});		
		});

});
