// @date 11.11.2007
// @version 0.4.4
if (!CD3) var CD3 = {};
CD3.Gallery = Class.create({
	initialize: function(image, thumbsrule, options){
		this.element	= $(image);
		this.thumbs		= $$(thumbsrule);
		this.options	= Object.extend(
		{
			timer:			0,
			attribute:		'imgsrc',
			arrows:			null,
			scroll_by:		0,
			scroll_type:	'vertical',
			container:		null,
			moveprev:		'moveprev',
			afterShow:		function() {},
			setImage:		this.setImage,
			effects:		true
		}, options || {});
		
		// add on click events
		this.thumbs.each(function(i, k){
			i.observe('click', this.showImage.bind(this, i, k));
		}.bind(this));
		
		this.current 	= 0;
		
		// have timer ?
		if (this.options.timer) {
			this._gotoNext	= this.gotoNext.bind(this, 1);
			this.setTimer();
		}
		
		// have scroller ?
		if (this.options.arrows) {
			this.container	= $(this.options.container);
			this.scroll		=  (this.options.scroll_type == 'vertical') ? ['top', 'offsetHeight'] : ['left', 'offsetWidth'];
			
			$$(this.options.arrows).each(function(a){	
				this['btn' + (a.hasClassName( this.options.moveprev ) ? 'prev' : 'next')] = a;
				a.observe('click', function(button){
					var by	 = this.options.scroll_by;
					var dir  = button.hasClassName( this.options.moveprev ) ? 1 : -1;
						dir *= by;
								
					var side = this.container;
					var prop = parseInt(side.style[this.scroll[0]]) || 0;
					
					if (dir > 0 && (prop > -1 || !prop))						return;
					if (dir < 0 && (side[this.scroll[1]] + dir + prop < 10))	return;
					
					this.btnprev.style.visibility = !(prop+dir) ? 'hidden' : 'visible';
					this.btnnext.style.visibility = !(side[this.scroll[1]] + dir*2 + prop > 10) ? 'hidden' : 'visible';
					
					//this.btnprev.className = !(prop+dir) ? 'galarrow prevdis' : 'galarrow prev';
					//this.btnnext.className = !(side[this.scroll[1]] + dir*2 + prop > 1) ? 'galarrow next' : 'galarrow nextdis';
					
					if (this.options.scroll_type == 'vertical')
						Effect.MoveBy(side, dir, 0, {duration: 0.5, queue: {scope: 'gallery', limit:1}})
					else
						Effect.MoveBy(side, 0, dir, {duration: 0.5, queue: {scope: 'gallery', limit:1}})		
				}.bind(this, a));
			}.bind(this));
		}
	},
	setTimer: function(time) {
		if (time = time || this.options.timer) {
			window.clearTimeout(this.timer);
			this.timer = window.setTimeout(this._gotoNext, time);
		}
	},
	showImage: function(a, num, auto) {
		this.current = num;
		
		var src	= a.getAttribute(this.options.attribute);
		if (this.options.effects) 
			Effect.Fade(this.element, {
				duration: .1,
				afterFinish: function(ef) {
					this.options.setImage.call(this, this.element, src, a);
					Effect.Appear(ef.element);
				}.bind(this)
			});
		else
			this.options.setImage.call(this, this.element, src, a);
			
		if (this.options.timer)
			this.setTimer(auto ? this.options.timer : this.options.timer * 2);
		
		this.options.afterShow.call(this, a, num, auto);
	},
	gotoNext: function(to) {
		to = this.current + ( to ? to : 1 );
		
		if (!this.thumbs[to])
			to = 0;
	
		this.showImage(this.thumbs[to], to, 'auto');
	},
	setImage: function(element, src) {
		element.src = src;
	}
});
Event.observe(window, 'load', function(){
	if ($('thumbs') && $('thumblist'))
		new CD3.Gallery('mainimage', '#thumblist a', {
			container: 	$('thumblist').down('ul'),
			arrows:		'#thumbs .galarrow a',
			moveprev:	'prev',
			movenext:	'next',
			effects:	false,
			scroll_by:	260,
			setImage: function(element, src, thumb, alt) {
				//$('comment').innerHTML = thumb.getAttribute('title');
				element.src = src;
				var text = thumb.getAttribute('title');
				
				//if (text)
				//{
				  //   $('displaytext').show().innerHTML = text;
					// $('a1').show()
				//}
				//else
				//{
				  //   $('displaytext').hide();
					// $('a1').hide();
				//}
			}
		});
	EventSelectors.start({
          '#offers ul a:click': function(e){
               var number = (parseInt(this.innerHTML) - 1) || 0
               $$('#offers .offerbox').each(function(element, key){
                    element[number == key ? 'show' : 'hide']();
               });
               $(this.parentNode.parentNode).select('.selected').invoke('removeClassName', 'selected');
               this.addClassName('selected');
               $('show_offer').setAttribute('href', this.getAttribute('href'));
               e.stop();
          }
});	
});