var Site = {
	LANG: $$('body')[0].getAttribute('lang'),
	toggleBrandNames: function(value){ 
		$('brand_names')[value == -1 ? 'appear' : 'fade'](); 
	},
	musicSelect: null,
	changeMusic: function(value){
			
			if ( Site.LANG == 'bg' ){
				
				if ( value == 0 ){
						Site.musicSelect.setOptions([
							{text: 'струнни',	value: 'струнни'},
							{text: 'духови',	value: 'духови'},
							{text: 'клавирни',	value: 'клавирни'}
							
						]);	
				}else if ( value == 1 ){
					
						Site.musicSelect.setOptions([
							{text: 'струнни',	value: 'струнни'},
							{text: 'духови',	value: 'духови'},
							{text: 'ударни',	value: 'ударни'},
							{text: 'смесени',	value: 'смесени'},
							{text: 'старинни инструменти',	value: 'старинни инструменти'},
							{text: 'други инструменти',		value: 'други инструменти'}
							
						]);
							
				}
				
			}else{
				// english words!!!!!
				
				if ( value == 0 ){
						Site.musicSelect.setOptions([
							{text: 'string',		value: 'string'},
							{text: 'wind',			value: 'wind'},
							{text: 'percussion',	value: 'percussion'}
							
						]);	
				}else if ( value == 1 ){
					
						Site.musicSelect.setOptions([
							{text: 'string',	value: 'string'},
							{text: 'wind',		value: 'wind'},
							{text: 'drums',		value: 'drums'},
							{text: 'mixed',		value: 'mixed'},
							{text: 'historical instruments',	value: 'historical instruments'},
							{text: 'another instruments',		value: 'another instruments'}
							
						]);
							
				}
			}
					
			
	},
	musicSelect2: null,
	changeMusic2: function(value){
			
				if (Site.LANG == 'bg') {
						
						if (value == 0) {
						
							Site.musicSelect2.setOptions([
								{text: 'едно пиано',	value: 'едно пиано'},
								{text: 'две пиана',		value: 'две пиана'}
						
							]);	
						
						}else if( value == 1){
							
							Site.musicSelect2.setOptions([
								{text: 'едно пиано',	value: 'едно пиано'},
								{text: 'две пиана',		value: 'две пиана'}
						
							]);	
						}else if( value == 2){
							
							Site.musicSelect2.setOptions([
								{text: 'цигулков',				value: 'цигулков'},
								{text: 'вокален',				value: 'вокален'},
								{text: 'смесен',				value: 'смесен'},
								{text: 'виолончелисти',			value: 'виолончелисти'},
								{text: 'духови инструменти',	value: 'духови инструменти'},
								{text: 'ударни инструменти',	value: 'ударни инструменти'},
								{text: 'старинни инструменти',	value: 'старинни инструменти'},
								{text: 'други инструменти',		value: 'други инструменти'}
							
							]);	
						}
						
				}else{
					// english words!!!!!
					if (value == 0) {
						
							Site.musicSelect2.setOptions([
								{text: 'one piano',	value: 'one piano'},
								{text: 'two pianos', value: 'two pianos'}
						
							]);	
						
						}else if( value == 1){
							
							Site.musicSelect2.setOptions([
								{text: 'one piano',		value: 'one piano'},
								{text: 'two pianos',	value: 'two pianos'}
						
							]);	
						}else if( value == 2){
							
							Site.musicSelect2.setOptions([
								{text: 'tsigulkov',					value: 'tsigulkov'},
								{text: 'vocal',						value: 'vocal'},
								{text: 'mixed',						value: 'mixed'},
								{text: 'violonchellos,',			value: 'violonchellos,'},
								{text: 'wind instruments',			value: 'wind instruments'},
								{text: 'percussion instruments',	value: 'percussion instruments'},
								{text: 'historical instruments',	value: 'historical instruments'},
								{text: 'another instruments',		value: 'another instruments'}
							
							]);	
						}
							
				}
	},					
	getOptions: function(select, options){
		var name =  select.name;
		 
		return options.find(function(o){
			if ((!o.name || o.name == name) && (!o.className || select.hasClassName(o.className))){
				
				delete(o.name);
				
				delete(o.className);
				return true;
			}
			
			return false;
		}) || {};
	}
};
Site.beautifyUpload = (function(){
	var sendche = Site.LANG == 'bg' ? 'Прикачи' : 'Upload';
		
	var fakeFileUpload = new Element('div', {className: 'fakefile'})
		.insert(new Element('input'))
		.insert(new Element('span'));
		
	return function(element){
		element = $(element);
		element.className = 'file hidden';
			
		var clone = $(fakeFileUpload.cloneNode(true));
			
		element.insert({ after: clone });
		element.relatedElement = clone.down('input');
		element.observe('change', function(){
			this.relatedElement.value = this.value;
		});
		
		return element;
	}
})();

function clear(){
	Effect.Fade($('mail_error'));
	}
if ($('mail_error')){
	setTimeout('clear()', 5000);
};



function dinamic_year(){
	if ($('dinamic_date'))
		$('dinamic_date').innerHTML = new Date().getFullYear();
};dinamic_year();

CD3.Behaviors.assign({
	//error IE
	'select': function(select){
		var custom = new CD3.Select(select, Site.getOptions(select, [
				{name: 'brand_id',	className: 'droplink',	onChange: Site.toggleBrandNames},
				{					className: 'music-1',	onChange: Site.changeMusic},
				{					className: 'music-3',	onChange: Site.changeMusic2}
			]));
		
		if (select.hasClassName('music-2')) { Site.musicSelect = custom;	}
		if (select.hasClassName('music-4')) { Site.musicSelect2 = custom;   }
	},
	//error IE	
	'input[type=file]': Site.beautifyUpload,
	'input[type=text]': {
		focus: function(){
			if (this.getAttribute('value') == this.getAttribute('title'))
				this.setAttribute('value', '');
				
			this.up('span').style.backgroundPosition = 'left bottom';
		},
		blur: function(){
			if (!this.getAttribute('value'))
				this.setAttribute('value', this.getAttribute('title'));
			
			this.up('span').style.backgroundPosition = 'left top';
		}	
	},
	'input[type=submit]': {
		mouseover: function(){
			this.style.backgroundPosition = 'left bottom';
		},
		mouseout: function(){
			this.style.backgroundPosition = 'left top';
		}
	},
	'input[type=checkbox]': function(box){
		if (
			(!$('sum_timer')    || !box.descendantOf('sum_timer'))    && 
			(!$('sum_timer2')   || !box.descendantOf('sum_timer2'))	  &&
			(!$('sum_timer-2')  || !box.descendantOf('sum_timer-2'))  &&
			(!$('sum_timer-3')  || !box.descendantOf('sum_timer-3'))  &&
			(!$('sum_timer-4')  || !box.descendantOf('sum_timer-4'))  &&
			(!$('sum_timer-5')  || !box.descendantOf('sum_timer-5'))  &&
			(!$('sum_timer-6')  || !box.descendantOf('sum_timer-6'))  &&
			(!$('sum_timer-7')  || !box.descendantOf('sum_timer-7'))  &&
			(!$('sum_timer-8')  || !box.descendantOf('sum_timer-8'))  &&
			(!$('sum_timer-9')  || !box.descendantOf('sum_timer-9'))  &&
			(!$('sum_timer-10') || !box.descendantOf('sum_timer-10')) &&
			(!$('sum_timer-11') || !box.descendantOf('sum_timer-11')) &&
			(!$('sum_timer-12') || !box.descendantOf('sum_timer-12')) &&
			(!$('sum_timer-13') || !box.descendantOf('sum_timer-13')) &&
			(!$('sum_timer-14') || !box.descendantOf('sum_timer-14')) &&
			(!$('sum_timer-15') || !box.descendantOf('sum_timer-15')) &&
			(!$('sum_timer-16') || !box.descendantOf('sum_timer-16')) &&
			(!$('sum_timer-17') || !box.descendantOf('sum_timer-17')) &&
			(!$('sum_timer-18') || !box.descendantOf('sum_timer-18')) &&
			(!$('sum_timer-19') || !box.descendantOf('sum_timer-19')) &&
			(!$('sum_timer-20') || !box.descendantOf('sum_timer-20')) &&
			(!$('sum_timer-21') || !box.descendantOf('sum_timer-21')) &&
			(!$('sum_timer-22') || !box.descendantOf('sum_timer-22')) &&
			(!$('sum_timer-23') || !box.descendantOf('sum_timer-23')) &&
			(!$('sum_timer-24') || !box.descendantOf('sum_timer-24')) 
			
		)  
			new CD3.Checkbox(box);
	},
	//'select': CD3.Select,
	'textarea': {
		focus: function(){
			this.up('span').style.backgroundPosition = 'left bottom';
		},
		blur: function(){
			this.up('span').style.backgroundPosition = 'left top';
		}	
	},

	'#send_pic a.remove_file:click': function(){
		var input   = this.up('div.upload_file').down('input[type=file]');
		var replace = new Element('input', {type: 'file', name: input.getAttribute('name')});
			
		input.insert({ before: replace });
		input.relatedElement.up().remove();
		input.relatedElement = null;
		input.stopObserving();
		input.remove();
		
		Site.beautifyUpload(replace);
	},	
	
	'#drop_1,#drop_2': {
		click: function(){
			var ul = this.next('ul');
			
			Effect.toggle(ul, 'blind', { duration: 0.3 });
			
			if (ul.visible()) {
				this.addClassName('closed_a');
			} else {
				this.removeClassName('closed_a')
				this.addClassName('open_a');		
			};
		}
	},

	'.cen_img':{
		mouseover: function(){
			this.setStyle({ backgroundPosition: 'left bottom' });
		},
		mouseout: function(){
			this.setStyle({ backgroundPosition: 'left top' });
		}
	},
	'a.slinky':{
		mouseover: function(){
			this.up('div.qbox').setStyle({backgroundPosition: 'left bottom'})
		},
		mouseout: function(){
			this.up('div.qbox').setStyle({backgroundPosition: 'left top'})
		}
	},
	'#page_gallery': function(){
		new CD3.Lightbox(this.select('a.slinky'));
	},
	'#toggle_both-1 a:mouseup': function(){
		
		var show = this.up().down('input').checked;
		this.up('div').up('div').setStyle({backgroundColor: show ? '#F1ECE8' : '#867661', color: show ? '#64543D' : '#ffffff'});
		
		this.up('div.sh_row').select('.no-visible').invoke('setStyle', {visibility: show ?  'hidden' : 'visible'});
		
		$('sum_timer', 'sum_timer2', 'show_devider', 'show_devider2').invoke(this.up().down('input').checked ? 'hide' : 'show');
	},
	'#toggle_both-1 input': function(){
		$('sum_timer', 'sum_timer2', 'show_devider', 'show_devider2').invoke(!! this.checked ? 'show' : 'hide');
	},
	
	'#toggle-5 a:mouseup': function(){
		var show = this.up().down('input').checked;
		
			this.up('div').up('div').setStyle({backgroundColor: show ? '#F1ECE8' : '#867661', color: show ? '#64543D' : '#ffffff'});
	
		this.up('div.sh_row').select('.no-visible').invoke('setStyle', {visibility: show ?  'hidden' : 'visible'});
		
		$('sum_timer-5', 'sum_timer-6', 'devider-5', 'devider-6').invoke(this.up().down('input').checked ? 'hide' : 'show');
	},
	'#toggle-5 input': function(){
		$('sum_timer-5', 'sum_timer-5', 'devider-5', 'devider-6').invoke(!! this.checked ? 'show' : 'hide');
	},
	'#toggle-3 a:mouseup': function(){
		var show = this.up().down('input').checked;
		
			this.up('div').up('div').up('div').setStyle({backgroundColor: show ? '#F1ECE8' : '#867661', color: show ? '#64543D' : '#ffffff'});
		
		this.up('div.sh_row').select('.no-visible').invoke('setStyle', {visibility: show ?  'hidden' : 'visible'});
		
		$('sum_timer-3',  'devider-3').invoke(this.up().down('input').checked ? 'hide' : 'show');
	},
	'#toggle-3 input': function(){
		$('sum_timer-3', 'devider-3').invoke(!! this.checked ? 'show' : 'hide');
	}
	
});		

CD3.Behaviors.assign([2, 3, 4, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 ].inject({}, function(o, n){
	o['#toggle-' + n + ' a:mouseup'] = function(){
		var show = this.up().down('input').checked;//#E1D8D0
		this.up('div').up('div').setStyle({backgroundColor: show ? '#F1ECE8' : '#867661', color: show ? '#64543D' : '#ffffff' });
		
		this.up('div.sh_row').select('.no-visible').invoke('setStyle', {visibility: show ?  'hidden' : 'visible'});
		
		$('sum_timer-' + n, 'devider-' + n).invoke(this.up().down('input').checked ? 'hide' : 'show');
	};
	
	o['#toggle-' + n + ' input'] = function(){
		$('sum_timer-' + n, 'devider-' + n).invoke(!! this.checked ? 'show' : 'hide');
	};
	
	return o;
}));
			
// for gallery
CD3.Behaviors.assignIf('#gallery', function(){
	// for the load
	var loader    = null, 
		image     = $('mainimage').down('img'), 
		container = $('mainimagewrapper'),
		timer;
	
	// for the slide
	var thumbscount = $$('#thumbslist li').length, 
		thumbslist  = $('thumbslist').setStyle({
			width:		thumbscount * 19 + 'px',
			position:	'relative',
			left:		'0px'
		});
		
	function registerTimer(){
		if (timer){
			timer.stop();
			timer = null;
		}
		
		timer = new PeriodicalExecuter(function(){
			var thumbs	 = $('thumbslist'),
				selected = thumbs.down('a.selected');
					
			selectImage(((selected && selected.up('li').next('li')) || thumbs.select('li').first()).down('a'));
		}, 5);
	}
	
	function clearLoader(){
		if (loader != null){
			loader.onload	= null;
			loader			= null;
		}
	}
	
	function onLoad(){
		image.src = loader.src;
		container.morph('height: ' + loader.height + '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.up('div').addClassName('loading');
		
		image.fade({
			duration: .8,
			afterFinish: function(){		
				loader			= new Image();
				loader.onload	= onLoad;
				loader.src		= thumb.href;
				//console.log(loader);
			}
		});
		
		var position = parseInt(thumb.id.match(/\w+_(\d+)/)[1]);
			position = position < 21 ? 0 : position - 20;
			
		thumbslist.morph('left: ' + (-19 * position) + 'px');
	}
	
	
	registerTimer();
	
	return {
		'div.controls:click': {
			'#thumbslist a': function(e){
				e.stop();
				selectImage(this);
				registerTimer();
			},
			'#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'));
			}
		}
	};
});

Site.timerRow = function(element){
	element.select('input[type=text]').invoke('stopObserving');
	
	var clone = element.down('.sh_row').cloneNode(true);
	clone.select('input[type=text]').invoke('setValue', '');
	clone.down('input[type=checkbox]').checked = false;
	
	function preCalculateLineNumbers(){
		element.select('.sh_row').each(function(row, i){
			row.down('label', 0).innerHTML = i + 1 + '.';
		});
	}
	
	function preCalculateTime(){
		var get_time =  element.down('span').innerHTML;
		
		var left = get_time - element.select('span.best_small input').inject(0, function(sum, input){
			return sum + (parseInt(input.getValue()) || 0);
		});
		
		element.down('.total_time').update(left + ':00').setStyle({color: left < 0 ? 'red' : null});
	}
		
	function createLine(row){
		CD3.Behaviors.assign({
			'input[type=text]': {
				focus: function(){
					this.up('span').style.backgroundPosition = 'left bottom';
				},
				blur: function(){
					this.up('span').style.backgroundPosition = 'left top';
				}		
			},
			'input[type=checkbox]': CD3.Checkbox,
			'span.best_small input': {
				keyup: function(){
					var value = parseInt(this.getValue()) || ' ';
					
					preCalculateTime();
										
					this.setValue(value);
				}
			}
		}, row);
		
		return row;
	}
	
	function destroyLine(row){
		row.select('input[type=text]', 'a.checkbox').invoke('stopObserving');
		row.remove();
	}
		
	return {
		'.sh_row': createLine,
		'.add:click': function(){
			element.down('.container').insert({
				bottom: createLine($(clone.cloneNode(true)))
			});
			preCalculateLineNumbers();
		},
		'.remove:click': function(){
			element.select('input[type=checkbox]').each(function(input){
				if (input.checked){
					destroyLine(input.up('div.sh_row'));
				}
			});
			
			preCalculateTime();
			preCalculateLineNumbers();
		}
	};
};

CD3.Behaviors.assignIf('#sum_timer', Site.timerRow);
CD3.Behaviors.assignIf('#sum_timer2', Site.timerRow);

$R(2,24).each(function(n){
	CD3.Behaviors.assignIf('#sum_timer-' + n, Site.timerRow);
});



