var addthis_pub				= 'E-moto cafe'; 
var addthis_logo			= 'http://www.e-motocafe.net/img/emoto-logo-litle.png';
var addthis_logo_background	= '454544';
var addthis_logo_color		= 'f4f4f4';
var addthis_brand			= 'E-moto cafe';
var addthis_options			= 'favorites, email, delicious, google, digg, reddit, facebook, live, more';

Emoto.Form = {
	initialize: function(){
		document.observe('click', CD3.Behaviors.delegate({
			'.action-preview': 	Emoto.Form.submit,
			'.action-edit':		Emoto.Form.clear
		}));
	},
	submit: function(){
		$('ad_form').submit();
	},
	request: function(e){
		e.stop();
		this.up('form').request({
			parameters: { preview: true },
			onLoading: 	Emoto.Form.load,
			onLoaded:	Emoto.Form.loaded, 
			onFailure:	Emoto.Form.submit,
  			onComplete: Emoto.Form.handle
  		});
	},
	load: function(){
		$('form_buttons').hide();
		$('form_loading').show();
	},
	loaded: function(){
		$('form_buttons').show();
		$('form_loading').hide();
	},
	handle: function(t){
		var data = t.responseText.evalJSON();
  		Emoto.Form.loaded();
		Emoto.Form.clear();
  		Emoto.Form['errors' in data ? 'errors' : 'preview'](data);
  	},
	clear: function(){
		if ($('offer_errors'))  $('offer_errors').remove();
		if ($('offer_preview')) $('offer_preview').remove();
		
		$('form_container').show();
 		$('ad_form').select('li').invoke('removeClassName', 'error');
	},
  	errors: function(data){
  		$('ad_form').insert({ before: data.html });
  	
		$A(data.errors).each(function(name){
			var element = $('offer_item_' + name);
			if (element) element.addClassName('error');
		});
		
		var errors = $('offer_errors');
		errors.down('p').style.backgroundColor = '#FF4E00';
		new Effect.ScrollTo(errors, {
			afterFinish: function(e){
				errors.down('p').highlight({
					startcolor: '#FF4E00',
					endcolor: 	'#666666',
					afterFinish: function(e){
						e.element.style.backgroundColor = null;
					}
				});
			}
		});
	},
	preview: function(data){
		var span = new Element('span', {id: 'offer_preview'});
		span.innerHTML = data.html;
		$('form_container').hide().insert({ after: span });
		new Effect.ScrollTo('offer_preview');
	}
};

if($('form_container')) Emoto.Form.initialize();

CD3.Behaviors.assign({
	'#tyxo_counter': function(){
		this.innerHTML = '<a href="http://www.tyxo.bg/?61072" title="Tyxo.bg counter" target=" blank"><img width="88" height="31" border="0" alt="Tyxo.bg counter" src="http://cnt.tyxo.bg/61072?rnd='+Math.round(Math.random()*2147483647) + '&sp='+screen.width+'x'+screen.height+'&r='+escape(document.referrer)+'" /><\/a>';
	},
	'#navi-issues div.issues': Emoto.Slider,
	'#addon-bookmark': {
		mouseover: function(){
			addthis_open(this, '', location.href, 'E-moto cafe')
		},
		mouseout: function(){
			addthis_close()
		},
		click: function(){
			addthis_sendto()
		}
	},
	'input[type=text]': {
		focus: function(){
			if (this.getAttribute('value') == this.getAttribute('title'))
				this.setAttribute('value', '');
		},
		blur: function(){
			if (this.value.length == 0 && this.getAttribute('title'))
				this.setAttribute('value', this.getAttribute('title'));
		}	
	},
	'input.hover': {
		mouseover: function(){
			this.addClassName('hovered');
		},
		mouseout: function(){
			this.removeClassName('hovered');
		}
	},
	'#msize': function(btn){
		new CD3.FontSwitcher('content2', 'text-switch', {/* btn.up('dl') */
			plus:	'#psize',
			minus:	'#msize',
			reset:	false
		});
	},
	'#content2': function(){
		new CD3.Lightbox(this.select('a.pred_image'));
	},
	'#offer_pictures': function(){
		new CD3.Lightbox(this.select('a'));	
	},
	'#addon-toggle:click': function(){
		$('addon-toggle-txt1', 'addon-toggle-txt2', 'addon-news', 'addon-comments').invoke('toggle');
	},
	'#ad_form input[type=file], #request_form input[type=file]': Emoto.beautifyUpload,
	'#theme-buttons:click': {
		'.green':  Emoto.setTheme.curry('orange'),
		'.orange': Emoto.setTheme.curry('green')
	},
	'#submit_preview:click':Emoto.Form.request,
	'#submit_form': 		Element.show,
	'#submit_form:click':	Emoto.Form.submit,
	'#offer_category_id':	function(){
		var select  = $('offer_manufacturer_id'),
			size	= $('offer_item_size');
			options = select.select('option').inject({}, function(memo, option){
			var categoryId = option.getAttribute('data-category');
			
			if (!(categoryId in memo)){
				memo[categoryId] = document.createDocumentFragment();
			}
			memo[categoryId].appendChild(option.cloneNode(true));
			option.remove();
			return memo;
		});
		
		size._name = size.down('select').getAttribute('name');
		
		(function(value){
			var visible = [1,8,9,10].indexOf(1 * value) != -1
		
			size[visible ? 'hide' : 'show']();
			size.down('select').setAttribute('name', visible ? '_size_' : size._name);
			
			if (value in options){
				select.appendChild(options[value].cloneNode(true));
			}
			
			$('offer_item_year', 'offer_item_mileage').invoke(value == 1 ? 'show' : 'hide');
			
			visible = [9,10].indexOf(value) == -1;
			$('offert_title_note')[visible ? 'show' : 'hide']();
			$('offert_title_note2')[visible ? 'hide' : 'show']();
		})(this.getValue());
		
		this.observe('change', function(){
			var value	= 1 * this.getValue(),
				visible = [1,8,9,10].indexOf(value) == -1;
				
			select.select('option').invoke('remove');
			if (value in options)
				select.appendChild(options[value].cloneNode(true));
			
			// size
			if (size.visible() != visible){
				size[visible ? 'blindDown' : 'blindUp']();
				size.down('select').setAttribute('name', visible ? size._name : '_size_');
			}
			
			// year / mileage
			$('offer_item_year', 'offer_item_mileage').invoke(value == 1 ? 'show' : 'hide').invoke('down', 'input').invoke('setValue', '');
			
			// title
			visible = [9,10].indexOf(value) == -1;
			$('offert_title_note')[visible ? 'show' : 'hide']();
			$('offert_title_note2')[visible ? 'hide' : 'show']();
		});
	}
});