//
// wmwSlideshow
// 
// @author: Nik Van Looy
// @version: 30 september 2008
//
//
 
var wmwSlideshow = {
 	initialize: function(elm, options) {
		this.setOptions({
			productSel: 'div.productEl',
			productIDEl: 'h1',
			productTitleEl: 'h2',
			productDescriptionEl: 'p.productdescription',
			imageSel: 'img.image',
			thumbSel: 'img.imagethumb',
			imagegroupSel: 'div.imagegroup',
			imageDescriptionSel: 'p.imagedescription',
			
			fadeDuration: 200,
			timer: true,
			timerDelay: 7000,
			timerInitDelay: 0
		}, options);
		
		this.timerInitDelay = this.options.timerInitDelay;
		
		this.currProductId = 0;
		this.currImageId = 0;
		this.imageElements = [];
		this.imageNavElements = [];
		
		this.initCatalog = true;
		this.loading = false;
		this.hasErrors = false;
		
		if($(elm) == null) {
			this.hasErrors = true;
			return;
		}
		this.galleryEl = $(elm);
    	this.sourceEl = $(elm).getElement('div');
        
        //this.slidesContainer = new Element('div').addClass('slidescontainer').injectInside(this.galleryEl);

		this.getHtmlData();
	},
	startSlideshow: function(productId) {
		this.currProductId = parseInt(productId);
		if(this.loading == true)
		{
			(function(){
				this.startImageShow();
			}).delay(this.options.fadeDuration+100, this);
		}	
		else
		{
			this.startImageShow();
		}
	},
	startImageShow: function() {
		if(this.hasErrors) {
			return;
		}
		this.currImageId = 0;
		this.constructImageElements();
		this.imageElements[parseInt(this.currImageId)].set(1);
		this.initCatalog = true;
	},
	constructImageElements: function(){
		el = this.sourceEl;
		
		el.destroy();
		
		var currentImg;
		this.maxImageId = this.images.length;
		this.imageElements.length = 0;
		
		for(i=0;i<this.maxImageId;i++)
		{				
			var currentImg = new Fx.Tween(
				new Element('div').addClass('imageSlide').setStyles({
					'position':'absolute',
					'left':'0px',
					'right':'0px',
					'margin':'0px',
					'padding':'0px',
					'backgroundPosition':"center center",
					'opacity':'0'
				}).injectInside(this.galleryEl),
				{duration: 1000, property: 'opacity'}
			);
			
			new Element('img').setProperties({
					'src': this.images[i].image
				}).injectInside(currentImg.element);

			/*currentImg.element.setStyle('backgroundImage',
				"url('" + this.products[this.currProductId].images[i].image + "')");*/
				
			this.imageElements[parseInt(i)] = currentImg;
		}
		
		this.showImage(0);
	},
	setCatalogDisable: function(delay)
	{
		this.loading = true;
		(function(){this.loading = false;}).delay(delay, this);
	},
	prepareTimer: function() {
		if(this.timerInitDelay > 0) {
			var temp = this.timerInitDelay;
			this.timer = this.nextImage.delay(this.options.timerDelay+temp, this);
			this.timerInitDelay = 0;
		}
		else {
			this.timer = this.nextImage.delay(this.options.timerDelay, this);
		}
	},
	clearTimer: function() {
		if (this.maxImageId > 1)
			$clear(this.timer);
	},
	nextImage: function() {
		if(this.currImageId == (this.maxImageId-1))
			this.showImage(0);
		else
			this.showImage(this.currImageId+1);
	},
	showImage: function(num) {
		if(!this.loading || this.initCatalog)
		{
			this.initCatalog = false;
			this.setCatalogDisable(this.options.fadeDuration);
			this.clearTimer();
			this.imagesInit = 0;
			if (this.currImageId != num)
			{
				for(i=0;i<this.maxImageId;i++) // alles onzichtbaar maken behalve current image
				{
					if (i != this.currImageId) 
						this.imageElements[i].set(0); 
				}
				wmwSlideshow.Transitions[this.images[num].transition].pass([
					this.imageElements[this.currImageId],
					this.imageElements[num],
					this.currImageId,
					num,
					700], this)();
				this.currImageId = num;
			}
			if(this.maxImageId > 1)
			{
				if(this.options.timer)
					this.prepareTimer();
				//this.changeImageNavElStatus();
			}
		}
	},
	getHtmlData: function(){
		// variabelen
		var images = [];
		
		options = this.options;
		element = this.sourceEl;
		
		element.getElements(options.imageSel).each(function(imageEl){
			// CATEGORIES
			imageTeller = 0;
			
			image = {
				id: imageTeller,
				image: imageEl.getProperty('src'),
				transition: 'fade'
			};
			images.extend([image]);
			imageTeller++;

			$(imageEl).destroy();
		}.bind(this));

		this.images = images;
	}	
};
 
wmwSlideshow = new Class(wmwSlideshow);
wmwSlideshow.implement(new Events);
wmwSlideshow.implement(new Options);
 
wmwSlideshow.Transitions = new Hash ({
	fade: function(oldFx, newFx, oldPos, newPos, fadeDuration){
		//oldFx.options.transition = newFx.options.transition = Fx.Transitions.linear;
		//oldFx.options.duration = newFx.options.duration = fadeDuration;
		if (newPos > oldPos) newFx.start(1);
		else
		{
			newFx.set(1);
			oldFx.start(0);
		}
	}
});

