var addthis_pub				= 'Bebok.eu'; 
var addthis_logo			= 'http://www.bebok.eu/img/winter/logo.gif';
var addthis_logo_background	= '10d6f9';
var addthis_logo_color		= 'fff';
var addthis_brand			= 'Bebok.eu';
var addthis_options			= 'favorites, email, delicious, google, digg, reddit, facebook, live, more';


CD3.Behaviors.assign({
	// for search box
	'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'));
		}	
	},
	'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'));
		}	
	},
	'input[type=submit]': {
		mouseover: function(){
			this.style.backgroundPosition = 'left bottom';
		},
		mouseout: function(){
			this.style.backgroundPosition = 'left top';
		}
	},
	'#gadgets-bookmark': {
		mouseover:	function(){ addthis_open(this, '', location.href, 'Bebok.eu');},
		mouseout:	function(){ addthis_close(); },
		click:		function(){ addthis_sendto(); }
	},
	// for bookmarck
	'#bookmark:click': function(){
		var title = 'Bebok.eu', url = 'http://www.bebok.eu';		
		if (window.sidebar){ 						// Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url, '');
		} else if(window.external){					// IE Favorite
			window.external.AddFavorite(url, title);
		} else if(window.opera && window.print){	// Opera Hotlist
			return true;
		}
	}
});

CD3.Behaviors.assignIf('#gallery', function(){
	if (!$('thumbslist')) return {};
	
	$('thumbslist').setStyle({
		width:		$$('#thumbslist li').length * 29 + 'px',
		position:	'relative',
		left:		'0px'
	});
	
	// for the load
	var loader = null, image = $('mainimage').down('img'), container = $('mainimagewrapper');
	
	// for the slide
	var thumbscount = $$('#thumbslist li').length, thumbslist = $('thumbslist').setStyle({
		width:		thumbscount * 29 + 'px',
		position:	'relative',
		left:		'0px'
	});
	
	function clearLoader(){
		if (loader != null){
			loader.onload	= null;
			loader			= null;
		}
	}
	
	function onLoad(){
		image.src = loader.src;
		
		container.morph('width: ' + loader.width + 'px;', {
			duration: .6,
			afterFinish: function(){
				image.appear({
				    duration: .5,
				    afterFinish: function(){
				        container.removeClassName('loading');
			        }
		        });
			}
		});
		
		clearLoader();
	}
			
	function selectImage(thumb){
		$('thumbslist').select('.selected').invoke('removeClassName', 'selected');
		thumb.addClassName('selected');
		
		clearLoader();
		
		container.addClassName('loading');
		
		image.fade({
			duration: .8,
			afterFinish: function(){		
				loader			= new Image();
				loader.onload	= onLoad;
				loader.src		= thumb.href;
			}
		});
		
		var position = parseInt(thumb.id.match(/\w+_(\d+)/)[1]);
			position = position < 10 ? 0 : (position < thumbscount - 1 ? position - 9 : thumbscount - 11);
			
		thumbslist.morph('left: ' + (-29 * position) + 'px');
	}
	
	return {
		'div.controls:click': {
			'#thumbslist a': function(e){
				e.stop();
				selectImage(this);
			},
			'#preva': function(){
				var thumbs	 = $('thumbslist'),
					selected = thumbs.down('a.selected');
					
				selectImage(((selected && selected.up('li').previous('li')) || thumbs.select('li').last()).down('a'));
			},
			'#nexta': function(){
				var thumbs	 = $('thumbslist'),
					selected = thumbs.down('a.selected');
					
				selectImage(((selected && selected.up('li').next('li')) || thumbs.select('li').first()).down('a'));
			}
		}
	};
});