CD3.TabPanel = Class.create({
	initialize: function(panel, options){
		panel			= $(panel);
		options			= Object.extend({ selected: 'selected', content: 'div.tab-content', buttons: 'li'}, options || {});
		
		this.elements	= panel.select(options.content);
		this.buttons	= panel.down('ul').select(options.buttons);
		this.selected	= options.selected;
		
		panel.down('ul').observe('click', function(e){
			var key = this.buttons.indexOf(e.findElement(options.buttons));
			if (key != -1) {
				e.stop();
				this.activate(key);
			}
		}.bind(this));
	},
	activate: function(key){
		this.buttons.invoke('removeClassName', this.selected);
		this.buttons[key].addClassName(this.selected);
		this.elements.invoke('hide');
		this.elements[key].show();
	}
});

CD3.FontSwitcher = Class.create({
	initialize: function(element, panel, options){
		options = Object.extend({
			classname:	'text-size-',
			max: 		5,
			plus:		'span.big_text',
			reset:		'span.normal_text',
			minus:		'span.small_text'
		}, options || {});
		
		var buttons = {};
		
		if (options.plus)	buttons[options.plus]	= this.change.bind(this, 1);
		if (options.reset)	buttons[options.reset]	= this.change.bind(this, 0);
		if (options.minus)	buttons[options.minus]	= this.change.bind(this, -1);
		
		this.size		= 0;
		this.maxsize	= options.max;
		this.classname	= options.classname;
		this.element	= $(element);
		this.panel		= $(panel).observe('click', function(e){
			for(var b in buttons)
				if (e.findElement(b))
					return buttons[b]();
		});
	},
	change: function(value){
		this.element.removeClassName(this.classname + this.size);
		
		var size = value == 0 ? 0 : this.size + value;
		
		this.size = size < 0 ? 0 : ( size > this.maxsize ? this.maxsize : size );
		
		if (this.size != 0) this.element.addClassName(this.classname + this.size);
	}
});

CD3.Behaviors.assign({
	'a#scroll_to_top:click': function(e){
		e.stop();	
		Effect.ScrollTo(this.getAttribute('href').substr(1), {duration: .5});
	},
	'form input[type=text], form textarea': {
		focus: function(){
			if (this.getValue() == this.getAttribute('title')) this.setValue('');
		},
		blur: function(){
			if (this.getValue() == '') this.setValue(this.getAttribute('title'));
		}
	},
	'form:submit': function(){
		this.getElements().each(function(element){
			var title = element.getAttribute('title');
			if (title && element.getValue() == title){
				element.setValue('');
			}
		});
	},
	'#print_button:click': function(){ print(); },
	'#text_size': function(sizer){
		new CD3.FontSwitcher($$('div.down')[0], sizer, {
			plus:	'span.big_text',
			reset:	'span.normal_text',
			minus:	'span.small_text'
		});	
	},
	'#tabpanel': function(panel){
		new CD3.TabPanel(panel, {selected: 'tab-selected', content: 'div.imot_text2', buttons: 'a'});
	},
	'#mainimage': function(){
		window.loadMainImage = (function(main){
			var loader	= null, image = main.down('img'), loading = main;
			
			function clearLoader(){
				if (loader != null){
					loader.onload	= null;
					loader			= null;
				}
			}
			
			function showPre(){
				$('preloader').toggle();
			}
			function hidePre(){
				$('preloader').toggle();
			}
			
			function onLoad(){
				image.src = loader.src;
				
				loading.morph('width: ' + loader.width + 'px;height: ' + loader.height + 'px;', {
					duration: .6,
					afterFinish: function(){
						hidePre();
						image.appear({duration: .5});
						
					}
				});
				
				clearLoader();
			}
			
			return function(src){
				clearLoader();
				
				loading.setStyle({
					width:	image.width + 'px',
					height:	image.height + 'px'
				});
				image.fade({
					duration: .8,
					afterFinish: function(){
						showPre();
						loader			= new Image();
						loader.onload	= onLoad;
						loader.src		= src;
					}
				});
			};
		})(this);
	},
	'#thumbslist:click': {
		'a': function(e){
			e.stop();
			
			this.up('ul').select('a.thumb-selected').invoke('removeClassName', 'thumb-selected');
			this.addClassName('thumb-selected');
			
			//$('pic_info').innerHTML = this.getAttribute('title');
			
			loadMainImage(this.getAttribute('href'));
		}
	},
	'#search_input': function(){
		if (tags) new Autocompleter.Local('search_input', 'choices', tags, { tokens: [' '] });
	}
});
// Galio
CD3.Behaviors.assign({	
	'#askthedealer:click': Effect.toggle.curry('ask-form', 'blind'),
	'#tabs li a.some': {
		mouseover: function(){
			this.down('img').show().style.width = this.clientWidth + 'px';
		},
		mouseout: function(){
			this.down('img').hide();
		}
	},
	'#thumbslist a': {
		mouseover: function(){
			this.down('.thumb').show();
			this.down('.arrow22').show();
		},
		mouseout: function(){
			this.down('.thumb').hide();
			this.down('.arrow22').hide();
		}
	},
	'.left .menu ul li a':{
		mouseover: function(){
			this.down('span.mid').setStyle({
				background: '#59a2d1',
				color: 		'#fff'
			});
			this.down('span.topp').show();
			this.down('span.bott').show();
		},
		mouseout: function(){
			if (!this.hasClassName('selected')){
				this.down('span.mid').setStyle({
					background:	'none',
					color:		'#085889'
				});
				this.down('span.topp').hide();
				this.down('span.bott').hide();
			}
		}
	},
	'#topmenu li a':{
		mouseover: function(){
			var span = this.down('span');
			if (span){
				span.show().style.left = ((this.clientWidth-56)/2) + 'px';
			} else {
				this.down('img').show().style.width = this.clientWidth + 'px';
			}
		},
		mouseout: function(){
			if (!this.hasClassName('selected')){
				this.down(this.down('span') ? 'span' : 'img').hide();
			}
		}
	},
	'#thepic': {
		mouseover: function(){
			var picinfo = this.down('.picinfo');
			if (picinfo.innerHTML != ""){
				picinfo.show();
			}
		},
		mouseout: function(){
			this.down('.picinfo').hide();
		}
	},
	'#topmenu a.selected': function(a){
		if (a.down('span')) {
			a.down('span').toggle().style.left = ((a.parentNode.clientWidth - 56) / 2) + 'px';
		} else {
			a.down('img').toggle().style.width = a.parentNode.clientWidth + 'px';
		}
	},
	'.left .menu ul li a.selected': function(a){
		a.down('span.mid').setStyle({
			background: '#59a2d1',
			color:      '#fff'
		});
		a.down('span.topp').toggle();
		a.down('span.bott').toggle();
	},
	'#tabs li a.tab-selected img': function(img){
		img.show().style.width = img.up('a').clientWidth + 'px';
	},
	'#underbg': function(underbg){
		var down = $$('.down').first();
		underbg.style.height = (down.clientHeight + down.offsetTop - underbg.offsetTop) + 'px';
	}
});