var addthis_pub = 'Diminutiv';
var addthis_logo = '';
var addthis_logo_background = 'ffffff';
var addthis_logo_color = 'ffffff';
var addthis_brand = 'Diminutiv OOD';
var addthis_options = 'favorites, email, delicious, google, digg, reddit, facebook, live, more';


CD3.Behaviors.Hover = function(hoverClass, selector){
	return {
		mouseover: function(){ (selector ? this.down(selector) : this).addClassName(hoverClass || 'hover'); },
		mouseout:  function(){ (selector ? this.down(selector) : this).removeClassName(hoverClass || 'hover'); }
	};
};
CD3.Behaviors.TitleAsDefaultValue = {
	focus: function(){ if (this.getValue() == this.getAttribute('title')) this.setValue(''); },
	blur:  function(){ if (this.getValue().length == 0) this.setValue(this.getAttribute('title')); }
};

CD3.Slider = Class.create({
	initialize: function(container, options){
		var options = Object.extend({
			prev: 			null,
			next:			null,
			scrollBy:		0,
			scrollType:		'horizontal',
			event:			'click',
			beforeSlide:	false,
			afterSlide:		false,
			effectDuration: 0.8
		}, options || {});
		
		this.container	= $(container);
		this.scroll		= options.scrollType == 'vertical' ? ['top', 'offsetHeight', 'y'] : ['left', 'offsetWidth', 'x'];
		this.prev		= $(options.prev).observe(options.event, this.slide.bind(this, options.scrollBy));
		this.next		= $(options.next).observe(options.event, this.slide.bind(this, -options.scrollBy));
		this.sliding	= false;
		
		this.effectOptions = {
			duration: options.effectDuration || 0.8 ,
			queue: {scope: 'cd3:slider', limit:1},
			afterFinish: this.afterSlide.bind(this, options.afterSlide)
		};
		
		if (options.beforeSlide){
			this.effectOptions = options.beforeSlide.bind(this);
		}
		
		var pos = parseInt(this.container.style[this.scroll[0]]) || 0;
		
		this.setVisibility('prev', pos != 0);
		this.setVisibility('next', this.container[this.scroll[1]] - (options.scrollBy - pos) >= 1);
	},
	setVisibility: function(button, visible){
		this[button].style.visibility = visible ? 'visible' : 'hidden';
	},
	slide: function(moveBy){
		if (this.sliding) return;
		
		var property	= parseInt(this.container.style[this.scroll[0]]) || 0,	// top or left
			offset		= this.container[this.scroll[1]]; // offsetHeight or offsetWidth
		
//		if ((moveBy > 0 && (property > -1 || property != 0)) || (moveBy < 0 && offset + moveBy + property < 0))
		if ((moveBy > 0 && property > 0) || (moveBy < 0 && property + offset + moveBy < 0)) return;
		
		this.setVisibility('prev', property + moveBy < 0);
		this.setVisibility('next', offset + property + moveBy * 2 > 1);
		this.sliding = true;
		
		moveBy = moveBy < 0 ? Math.max(moveBy, - (offset + property + moveBy)) : Math.min(moveBy, -property);
				
		var options = this.effectOptions;
		options[this.scroll[2]] = moveBy; // x or y
		
		new Effect.Move(this.container, options);
	},
	afterSlide: function(callback){
		this.sliding = false;
		if (callback) callback.call(this);
	}
});


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({
	'#video_play_button': CD3.Behaviors.Hover('hovered'),
	'#search input[type=text]': CD3.Behaviors.TitleAsDefaultValue,
	// CONTACT MAP
	'#text_size': function(sizer){
		new CD3.FontSwitcher($$('div.outer')[0], sizer, {
			plus:	'span.big_text',
			reset:	'span.normal_text',
			minus:	'span.small_text'
		});	
	},
	'#print-btn:click': function(){ print(); },
	'#share': {
		mouseover: function(){
			addthis_open(this, '', location.href, 'Diminutiv')
		},
		mouseout: function(){
			addthis_close()
		},
		click: function(){
			addthis_sendto()
		}
	},
	'form#search .srch-btn':{
		mouseover: function(){
			this.addClassName('srch-btn2');
		},
		mouseout: function(){
			this.removeClassName('srch-btn2');
		}
	},
	// GALLERY-VIDEO-SWITCH
	'#gallery_switcher:click': {
		'.snimki-btn': function(){
			this.up().down('a.video-btn').removeClassName('selected');
			this.addClassName('selected');
			$('gallerybox').show();
			$('videobox').hide();
		},
		'.video-btn': function(){
			this.up().down('a.snimki-btn').removeClassName('selected');
			this.addClassName('selected');
			$('gallerybox').hide();
			$('videobox').show();
		}
	},
	// SLIDER
	'#slider': function(slider){
		var table = slider.down('table');
		//table.style.width = table.select('img.grayscale').inject(0, function(s, e){ return s += e.getWidth() + 54; }) + 'px';
		
		new CD3.Slider(table, {
			prev: slider.down('a.prev'),
			next: slider.down('a.next'),
			scrollBy: 290
		});
	},
		'.contact-btn': {
			mouseover: function(){
				this.setStyle({
					backgroundPosition: "top right"
				})
			},
			mouseout: function(){
				this.setStyle({
					backgroundPosition: "top left"
				})
			}
		},
	'div.inner-td':{
		mouseover: function(){
			this.up('td').addClassName('gray');
		},
		mouseout: function(){
			this.up('td').removeClassName('gray');
		}
	}
});

// GALLERY
CD3.Behaviors.assignIf('#gallerybox', function(){
	var thumbs = $$('#thumbs img');
	var current = 0;
	
	function showImage(number){
		current 				= number;
		$('mainimage').src		= thumbs[number].src;
		$('mainlink').href		= thumbs[number].getAttribute('fullsize');
		$('current').innerHTML	= number + 1;
	}
	
	return {
		'a.prev:click': function(){
			showImage(current == 0 ? thumbs.length - 1 : current - 1);
		},
		'a.next:click': function(){
			showImage(current == thumbs.length -1 ? 0 : current + 1);
		}
	};
});
/*
CD3.Behaviors.assign({
	'.inner-td .img-link':function(){
		alert(this.down('img').getHeight());
	}
})
*/

// Catalog

CD3.Behaviors({
	'#issuue': function(){
		Effect.ScrollTo('scroll');
	}
});


