var addthis_pub				= 'Dobrudjanski hliab AD'; 
var addthis_logo			= 'http://www.dobrudjanskihliab.com/img/title-bg.png';
var addthis_logo_background	= '910000';
var addthis_logo_color		= 'f4f4f4';
var addthis_brand			= 'Dobrudjanski hliab AD';
var addthis_options			= 'favorites, email, delicious, google, digg, reddit, facebook, live, more';

CD3.FontSwitcher = Class.create({
	initialize: function(element, panel, options){
		options = Object.extend({
			classname:	'text-size-',
			max: 		4,
			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]();
		});
		
		this.element.select('font[size]').each(function(font){
			font._size = parseInt(font.getAttribute('size'));
		});
	},
	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);
		
		size = this.size;
		this.element.select('font[size]').each(function(font){
			font.setAttribute('size', font._size + size);
		});
	}
});
CD3.Behaviors.assign({
	// for search box
	'input[type=text],input[type=password]': {
		focus: function(){
			if (this.getAttribute('value') == this.getAttribute('title'))
				this.setAttribute('value', '');
			this.up().addClassName('active');
		},
		blur: function(){
			if (!this.getAttribute('value'))
				this.setAttribute('value', this.getAttribute('title'));
			this.up().removeClassName('active');
		}	
	},
	'input[type=submit]': {
		mouseover: function(){
			this.addClassName('hovered');
		},
		mouseout: function(){
			this.removeClassName('hovered');
		}
	},
	'textarea': {
		focus: function(){
			if (this.getAttribute('value') == this.getAttribute('title'))
				this.setAttribute('value', '');
		},
		blur: function(){
			if (!this.getAttribute('value'))
				this.setAttribute('value', this.getAttribute('title'));
		}	
	},
	'#carousel': function(carousel){
		Event.observe(window, 'load', function(){
			$('carousel-loading').hide();
			new CD3.Carousel(carousel.show());
		});
	},
	'#text-resize': function(){
		new CD3.FontSwitcher('content', this, {
			plus:	'.bigger',
			minus:	'.smaller',
			reset:	'.reset'
		});
	},
	'#nav-tools .share': {
		mouseover: function(){
			addthis_open(this, '', location.href, 'Dobrudjanski hliab')
		},
		mouseout: function(){
			addthis_close()
		},
		click: function(){
			addthis_sendto()
		}
	},
	'#nav-tools-list .print:click': function(){
		print();
	},
	'#order-form select.dropper': CD3.Select,
	'#nav-profile:click': function(e){
		var a = e.findElement('a');
		if (a && (a = $(a.href.match(/#(.*)/)[1]))){
			Effect.ScrollTo(a);
			e.stop();
		}
	}
});

CD3.Behaviors.assignIf('#login-box', {
	'#pass-show:click': function(){
		var errormsg = $('login-error');
		if (errormsg){
			new Effect.Opacity('login-error', {duration: .4, from:1.0, to:0});
			new Effect.Morph('login-error', {duration: .5, style: {marginTop: '-35px' }});
		};
		new Effect.Morph('login-slider', {duration: .5, style: {left: '-315px' }});
	},
	'#login-show:click': function(){
		var errormsg = $('pass-error');
		if (errormsg){
			new Effect.Opacity('pass-error', {duration: .4, from:1.0, to:0});
			new Effect.Morph('pass-error', {duration: .5, style: {marginTop: '-35px' }});
		};
		new Effect.Morph('login-slider', {duration: .5, style: {left: '0px' }});
	}
});

CD3.Behaviors.assignIf('#pics-select', function(){
	var position = 0;
	
	function gotoItem(direction){
		var images = $$('#pics-select img');
		
		images[position].hide();
		position += direction;
		position  = position < 0 ? images.length - 1 : (position > images.length - 1 ? 0 : position);
		images[position].show();
	}
	
	return {
		'.prev:click': gotoItem.curry(-1),
		'.next:click': gotoItem.curry(1)
	}
});

CD3.Behaviors.assignIf('#order-form', {
	'table': {
		mouseover: {
			'td.prod': function(){
				if (this.down('div')) this.down('div').show();
			}
		},
		mouseout: {
			'td.prod': function(){
				if (this.down('div')) this.down('div').hide();
			}
		},
		keyup: {
			'input[type=text]': function(){
				var value = parseInt(this.getValue()) || 0;
				this.setValue(value < 0 ? 0 : value);
			}
		}
	}
});
