CD3.Scroller = Class.create({
	initialize: function(container, scroller, options){
		scroller = $(scroller);
		options  = Object.extend({
			styleArrow:		'arrow',
			styleMoveUp:	'moveup',
			styleMoveDown:	'movedown',
			styleSlider:	'slider',
			speed:			1,
			wheelSpeed:		5,
			timerSpeed:		3
		}, options || {});					
		
		this.container	= $(container);
		this.speed		= options.speed;
		
		scroller
			.delegate('.' + options.styleArrow,		'mouseup',		this.stopScroll.bind(this))
			.delegate('.' + options.styleArrow,		'mouseout',		this.stopScroll.bind(this))
			.delegate('.' + options.styleMoveUp,	'mousedown',	this.startScroll.bind(this, -1, options.timerSpeed))
			.delegate('.' + options.styleMoveDown,	'mousedown',	this.startScroll.bind(this,  1, options.timerSpeed));
					
		// wheel
		Event.wheel(this.container, this.scrollByMouseWheel.bind(this, options.wheelSpeed));
		
		// check is needed 
		scroller[this.container.scrollHeight > this.container.offsetHeight ? 'show' : 'hide']();
	},
	startScroll: function(value, timer){
		this.interval = setInterval(this.scrollBy.bind(this, value), timer);
	},
	stopScroll: function(){
		clearInterval(this.interval);
		this.interval = null;
	},
	scrollByMouseWheel: function(value, delta){
		if (delta != 0) this.scrollBy((delta > 0 ? -1 : 1) * value);			
	},
	scrollBy: function(value){
		this.container.scrollTop = this.validateTopPosition((this.container.scrollTop || 0) + value * this.speed );
	},
	validateTopPosition: function(y){
		if (y <= 0)								return 0;
		if (y >= this.container.scrollHeight)	return this.container.scrollHeight;
		
		return y;
	}
});

// behaviors
CD3.Behaviors({
	'#scrolling': function(){
		CD3.Scroller.instance = new CD3.Scroller('content', this.show(), {
			styleArrow:		'arrow',
			styleMoveUp:	'uparrow',
			styleMoveDown:	'downarrow',
			styleSlider:	'scroll',
			speed:			1
		});
	},
	'#triple_a': function(){
		new CD3.FontSwitcher('triple_a', 'content', {
			reset:		'a.normal_a',
			plus:		'a.big_a',
			minus:		'a.small_a',
			callback:	function(){
				if (CD3.Scroller.instance){
					CD3.Scroller.instance.checkIfneeded();
				}
			}
		});
	},
	'input[type=text]': {
		focus: function(){
			if (this.getAttribute('value') == this.getAttribute('title'))
				this.setAttribute('value', '');
		},
		blur: function(){
			if (!this.getAttribute('value'))
				this.setAttribute('value', this.getAttribute('title'));
		}	
	},
	'#info_sub_menu': {
		mouseenter: function(){
			this.down('div').show();
			this.down('a').addClassName('selected');
		},
		mouseleave: function(){
			this.down('div').hide();
			this.down('a').removeClassName('selected');
		}
	},
	'#con_send, #mein_but': {
		mouseover: function(){
			this.style.backgroundPosition = 'left bottom';
		},
		mouseout: function(){
			this.style.backgroundPosition = 'left top';
		}
	},
	'#buto_zusa': {
		mouseenter:	function(e){
			e.stop();
			$('smolther').appear({duration:0.3});
		},
		mouseleave: function(e){
			e.stop();
			$('smolther').fade({duration:0.3});
		}
	},
	'#hide_notice:click': function(e){
		e.stop();
		
		new Ajax.Request(this.href);
		this.hide();
	},
	'#take_me:mousedown': {
		'input[type=text]': function(){
			var value = 1 * this.getValue() || 0;
			if (value == 0) this.setValue('');
		}
	},
	'#take_me input[type=text]:blur': function(){
		var value = 1 * this.getValue() || 0;
		
		if (value < 0) value = 0;
		
		this.setValue(value);
	
		this.up('form').request();
	},
	'#gallery': function(gallery){
		var next		= gallery.down('.next'),
			prev		= gallery.down('.prev'),
			container	= $('mainimage'),
			images		= $$('#thumbslist a'),
			last		= images.length - 1;

		var current	= (function(){			
			var src = container.down('img').src;
			for(var i=0; i <= last; i++)
				if (images[i].down('img').src == src)
					return i;

			return 0;
		})();

		var show = (function(){
			var changing = false;

			function validateCurrent(number){
				if (number < 0)		number = last;
				if (number > last)	number = 0;

				return number;
			}

			return function(number){
				if (changing || number == current) return;

				changing = true; 
				current  = validateCurrent(number);

				container.down('div').mutateTo('<div style="background-image: url(' + images[current].down('img').src + ')"></div>', {
					replace:	 true,
					resize:		 false,
					afterFinish: function(){ changing = false; }
				});

				location.hash = images[current].getAttribute('data-id');

				prev.setAttribute('href',  (images[current - 1] || images[last]).getAttribute('href'));			
				next.setAttribute('href',  (images[current + 1] || images[0]   ).getAttribute('href'));		
			}
		})();

		function select(index, e){
			show(current + index);
		}
		gallery.observe('click', function(e){ if (!e.element().match('#order_view')) e.stop(); });
		container.observe('click', select.curry(1));

		if ($('thumbscontainer')){
			(function(){
				var perPage	= 10,
					
					thumbs	= $('thumbslist'),
					width	= (thumbs.down('li').getWidth() + 14) * perPage,

					item	= $('order_item'),
					href	= item.href.substr(0, item.href.lastIndexOf('/') + 1);
					
				function pageNumber(){
					return Math.floor(current / perPage);
				}

				show = show.wrap(function(show, number){
					show(number);

					// mark image thumb as selected
					gallery.select('a.selected').invoke('removeClassName', 'selected');
					images[current].addClassName('selected');

					// scroll to thumbnail
					thumbs.morph('left: -' + (pageNumber() * width) + 'px', {duration: 0.5});

					// change image href (for the order)
					item.href = href + images[current].getAttribute('data-id');
				});

				function paging(page){
					show((pageNumber() + page) * perPage);
				}

				item.observe(			  'click', function(){ new Ajax.Updater('order_items', this.href); });
				gallery.delegate('li', 	  'click', function(){ show(this.previousSiblings().length); });
				gallery.delegate('.prev_page', 'click', paging.curry(-1));
				gallery.delegate('.next_page', 'click', paging.curry( 1));
				gallery.delegate('.prev_up',   'click', select.curry(-1));
				gallery.delegate('.next_up',   'click', select.curry( 1));
			})();
		} else {
			gallery.delegate('.prev', 'click', select.curry(-1));
			gallery.delegate('.next', 'click', select.curry( 1));
		}
		
		(function(){
			var hash = location.hash.substr(1);
			if (hash && hash != 'mainimage'){
				for(var i=0; i <= last; i++){
					if (images[i].getAttribute('data-id') == hash){
						return show(i);
					}
				}
			}
		})();
	}
});
