// Version 1.0
var Scriptaculous = { Version: '1.9.0' };
if (Syberia == undefined) var Syberia = {};
var isLoading = false;
Syberia.ThumbsGallery = Class.create({
	
	menu_position: 0,
	offset: 0,
	noMoreItems: false,
	position: 0,
	container: null,
	itemWidth: 50,
	visibleItems: 0,
	maxWidth: 0,
	options: null,
	
	initialize: function(container, options) {
		this.container = container;
		this.options = options;

		if(!$(this.container)) return ;

		if(this.options.buttonRight) {
			Event.observe($(this.options.buttonRight),"click", this.moveRight.bind(this), false);
		}
		
		if(this.options.buttonLeft) {
			Event.observe($(this.options.buttonLeft), "click", this.moveLeft.bind(this), false);
		}		
		
		var items = $$('#'+this.container+' a');
		if(items.length) {
			/*if($$('#'+this.container+' a')[0].getWidth()) {
				this.itemWidth = $$('#'+this.container+' a')[0].getWidth() + 8;
			}*/
			
		}
		//this.maxWidth = this.itemWidth * items.length;
		
		document.observe('dom:loaded',this.measureWidth.bind(this));
		
		this.visibleItems = this.options.visibleItems;
	},
	
	measureWidth: function() {

		if($($$('#'+this.container+' a')[0]).getWidth()) {
			this.itemWidth = $$('#'+this.container+' a')[0].getWidth() + 8;
		}
		/*this.maxWidth = 0;
		for(var i=0; i< $$('#'+this.container+' a').length; i++) {
			this.maxWidth += $$('#'+this.container+' a')[i].getWidth()-10;
		}*/
		
	},
	
	moveRight: function() {
		this.moveMenu('right');
	},
	
	moveLeft: function() {
		
		this.moveMenu('left');
	},	
	
	moveMenu: function(dir)	{
		
		if(this.itemWidth == 50) this.measureWidth();
		
		var items = $$('#'+this.container+' a');
		this.maxWidth = this.itemWidth * items.length;

		if(dir=='right'){
			
			if(this.menu_position < (this.maxWidth - ($($(this.container).parentNode).getWidth()) ) ) {
				

				new Effect.Move(this.container, { x: -this.itemWidth, y: 0,transition: Effect.Transitions.sinoidal, duration: 0.5 });
				this.menu_position += this.itemWidth;
				this.position ++;

				if(this.options.dynamic) {
					this.maxWidth = this.itemWidth * items.length;
					
					if( (items.length - this.position) <= 15 && !isLoading && !this.noMoreItems) {
						this.offset ++;
						var request = new Prado.CallbackRequest('ctl0$content$ctlMenu$btnMenuNext', {'CausesValidation':'','ValidationGroup':'','CallbackParameter':this.offset,'ID':'ctl0_content_ctlMenu_btnMenuNext','EventTarget':'ctl0$content$ctlMenu$btnMenuNext','StopEvent':true});
						request.dispatch();	
						isLoading = true; 
					}
				}
			}
			

		} else {
			if(this.menu_position > 0) {
				new Effect.Move(this.container, { x: this.itemWidth, y: 0,transition: Effect.Transitions.sinoidal, duration: 0.5 });
				this.menu_position -= this.itemWidth;
				this.position --;
			}
			
		}

	}


});

function addMenuItems(container, html, cant, limit) {
	isLoading = false;
	$(container).innerHTML += html;

	if(cant < limit) noMoreItems = true;
}

Syberia.ImageRotator = Class.create({
	
	currentIndex: 0,
	count: 0,
	container: null,
	options: null,
	currentImage: 0,
	intval: 0,	
	isFading: false,
	autoLoad: true,
	
	initialize: function(container, options) {
		this.container = container;
		this.count = $$('#'+this.container+' .thumb').length;
		this.options = options;
		
		for(var i=0; i< this.count; i++) {
			Event.observe($$('#'+this.container+' .thumb')[i],'click', this.onThumbClick.bindAsEventListener(this));	
		}
		Element.hide($('galleryImage1'));
		Element.hide($('galleryImage2'));
		document.observe('dom:loaded',this.start.bind(this));
		
		if(this.options.buttonRight) {
			Event.observe($(this.options.buttonRight), "click", this.moveRight.bind(this), false);
		}
		
		if(this.options.buttonLeft) {
			Event.observe($(this.options.buttonLeft), "click", this.moveLeft.bind(this), false);
		}	
	},
	
	moveLeft: function(event) {
		
		this.autoLoad = false;

		if(this.currentIndex > 0) {
			
			this.currentIndex --;
			
			this.changeImage($$('#'+this.container+' .thumb')[this.currentIndex]);
		}
		Event.stop(event);
		
	},
	
	moveRight: function(event) {
		
		this.autoLoad = false;
		if(this.currentIndex < (this.count-1)) {
			this.currentIndex ++;
			
			this.changeImage($$('#'+this.container+' .thumb')[this.currentIndex]);
		}
		if(event) {
			Event.stop(event);
		}
		
	},	
	
	autoLoadImage: function(event) {
		
		if(this.currentIndex < (this.count-1)) {
			this.currentIndex ++;
			
			this.changeImage($$('#'+this.container+' .thumb')[this.currentIndex]);
		}
		if(event) {
			Event.stop(event);
		}
		
	},		
	
	
	start: function() {
		var t = this;
		$$('#galleryImage1 img')[0].onload = function() {
			new Effect.Appear('galleryImage1');
			t.intval = setTimeout(t.autoLoadImage.bind(t), 4000);
		};
		
		$$('#galleryImage1 img')[0].src = $$('#'+this.container+' .thumb')[0].href;
		
	},
	
	onThumbClick: function(event) {
		
		this.autoLoad = false;
		this.changeImage(event.target.parentNode);
		this.currentIndex = event.target.parentNode.tabIndex;
		
		
		
		Event.stop(event);
		
	},
	
	changeImage: function(image) {
		
		if(this.isFading) return ;
		
		clearTimeout(this.intval);
		$$('#galleryImage1 img')[0].onload = null;
		
		/*var elem = $$('#'+this.container+' .thumb')[ind];*/
		var t = this;
		if(!this.currentImage || !$$('#galleryImage2 img')[0].complete) {
			var fadeImage = 'galleryImage1';
			var appearImage = 'galleryImage2';
			$$('#galleryImage2 img')[0].src = image.href;
			this.currentImage = 1;
		} else {
			$$('#galleryImage1 img')[0].src = image.href;
			var fadeImage = 'galleryImage2';
			var appearImage = 'galleryImage1';
			this.currentImage = 0;
		}
		
		
		$$('#'+appearImage+' img')[0].onload = function() {

			if(t.autoLoad) {
				t.intval = setTimeout(t.autoLoadImage.bind(t), 5000);
			}
			
			new Effect.Appear(appearImage, {afterFinish: function() {t.isFading = false;delay:1}});
			
			new Effect.Fade(fadeImage, {afterFinish: function() {t.isFading = false;}});
		};
		
		this.isFading = true;
		
		
		
		
		
		
	}
	
	
});

Syberia.ProductRotator = Class.create({
	
	currentIndex: 0,
	count: 0,
	container: null,
	options: null,
	items: null,
	
	initialize: function(container, options) {
		this.container = container;
		this.options = options;
		this.items =  $$('#'+this.container+' .data');
		this.count = this.items.length;

		document.observe('dom:loaded',this.start.bind(this));
		
		if(this.options.buttonRight) {
			Event.observe($(this.options.buttonRight), "click", this.moveRight.bind(this), false);
		}
		
		if(this.options.buttonLeft) {
			Event.observe($(this.options.buttonLeft), "click", this.moveLeft.bind(this), false);
		}	
	},
	
	start: function() {
		this.changeProduct(0);
	},
	
	moveLeft: function(event) {
		
		if(this.currentIndex > 0) {
			this.currentIndex --;
		} else {
			this.currentIndex = this.count-1;
		}
		this.changeProduct(this.currentIndex);
		
		Event.stop(event);
		
	},
	
	moveRight: function(event) {
		
		if(this.currentIndex < (this.count-1)) {
			this.currentIndex ++;
		} else {
			this.currentIndex = 0;
		}
		this.changeProduct(this.currentIndex);
		Event.stop(event);
		
	},	
	
	changeProduct: function(ind) {
		var prod = this.items[ind];
		
		$(this.options.labelName).innerHTML = prod.innerHTML;
		$(this.options.image).src = prod.href;
		if(prod.title != '0') {
			$(this.options.labelPrice).innerHTML = prod.title + ' €';
			$(this.options.labelPrice).show();
		} else {
			$(this.options.labelPrice).hide();
		}
		
	}
	
});
