Element.implement({
	hiddenVal: function(value){
		this.value = value;
		span = this.getNext('span');
		if ( value == 2){
			this.removeClass('invalid');
			this.removeClass('require');
			this.addClass('valid');
			
			span.removeClass('invalid');
			span.removeClass('require');
			span.addClass('valid');
		} else {
			this.removeClass('valid');
			this.removeClass('require');
			this.addClass('invalid');
			
			span.removeClass('valid');
			span.removeClass('require');
			span.addClass('invalid');
		}
	}
});
Address = new Class({
		name: 'Address Class',
		Implements: [Events,Options],
		options: {
			parentTag: 'fieldset',
			id: null,
			model: '',
			suffix: '',
			valid: false,
			edit: false
		},
		initialize: function(options){
			this.setOptions(options);
			this.currentNode = $(options.id);
			this.parentNode = this.currentNode.getParent(this.options.parentTag);
			
			// nastaveni velikosti jednotlivych prvku
			this.mainWidth = this.parentNode.getSize().x;
			this.currentNode.setStyle('width', this.mainWidth);
			this.currentNode.getElement('.address_obal_in').setStyles({
					'width': 3 * this.mainWidth,
					'left': (this.options.edit == true)?(-2) * this.mainWidth:0
			});
			this.currentNode.getElements('.address_element').setStyle('width', this.mainWidth);
			
			// nastaveni motion effect 
			this.motion = new Fx.Morph(this.currentNode.getElement('.address_obal_in'), {duration: 'long', transition: Fx.Transitions.Sine.easeOut });
			
			// nastaveni Events
			this.currentNode.getElement('.address_nadefinovat_adresu').addEvent('click', (function(e){
				new Event(e).stop();
				this.motion.start({'left':[0,(-1) * this.mainWidth]});
			}).bind(this));
			
			this.currentNode.getElement('.address_hledat_adresu').addEvent('click', this.search.bindWithEvent(this));
			this.currentNode.getElement('.address_zpet_na_zadanou').addEvent('click', (function(e){
				this.motion.start({'left':[(-1) * this.mainWidth ,(-2) * this.mainWidth]});
				if($(this.options.id + 'Valid')) $(this.options.id + 'Valid').hiddenVal(2);
			}).bind(this));
			this.currentNode.getElement('.address_enter_again').addEvent('click', (function(e){
				this.motion.start({'left':[(-2) * this.mainWidth ,(-1) * this.mainWidth]});
				if($(this.options.id + 'Valid')) $(this.options.id + 'Valid').hiddenVal(1);
			}).bind(this));
		},
		
		search: function(e, data){
			var preloader = this.currentNode.getElement('.address_preloader'), row_list = this.currentNode.getElement('.address_row_list'), p;
			new Event(e).stop();
			
			if (data != null){ p = data;} 
			else {p = {'data[addr][name]': this.currentNode.getElement('.address_hledat_text').value};}

			preloader.setStyle('display','block');
			new Request.JSON({
				url: '/system/find_addr/',
				onComplete: (function(json){
					preloader.setStyle('display','none');
					if (json.status == 200){
						row_list.empty().setStyle('display','block');
						$each(json.result, function(row){
							new Element('div',{style:'cursor:pointer; cursor:hand'})
								.setHTML(row.location)
								.inject(row_list)
								.addEvents({
									'mouseover': function(e){ new Event(e).target.setStyle('background','#f1f1f1')},
									'mouseout': function(e){ new Event(e).target.setStyle('background','#fff')},
									'click': (function(e){
										new Event(e).stop();
										switch(row.entityType){
											case 'region': case 'district': case 'municipality': case 'quarter': case 'ward': case 'street':
												this.search(e,{'data[addr][name]': row.entityId, 'data[addr][entity]': row.entityType});
												break;
											case 'address':
												this.get_detail(row.entityId);
												break;
											default:
												alert('Jeste je tu: ' + row.entityType);						
												break;
										}
									}).bind(this)
								});
						}, this);
					} else if (json.status == 505){
						alert('Nedostacujici zadani adresy');
					} else {
						alert('Nenalezeno');
					}
				}).bind(this)
			}).post(p);
		},
		get_detail: function(UIN){
			var preloader = this.currentNode.getElement('.address_preloader'), row_list = this.currentNode.getElement('.address_row_list');
			row_list.setStyle('display','none');
			preloader.setStyle('display','block');
			new Request.JSON({
				url:'/system/load_podrobnosti/' + UIN,
				onComplete: (function(json2){
					preloader.setStyle('display','none');
					$(this.options.model + 'Stat' + this.options.suffix).value = json2.structure.country;
					$(this.options.model + 'Kraj' + this.options.suffix).value = json2.structure.region;
					$(this.options.model + 'Okres' + this.options.suffix).value = json2.structure.district;
					$(this.options.model + 'Mesto' + this.options.suffix).value = json2.structure.municipality;
					$(this.options.model + 'Ulice' + this.options.suffix).value = json2.structure.street;
					$(this.options.model + 'Cp' + this.options.suffix).value = json2.structure.streetNumber;
					$(this.options.model + 'Co' + this.options.suffix).value = json2.structure.houseNumber;
					$(this.options.model + 'Psc' + this.options.suffix).value = json2.structure.zip;
					this.motion.start({'left':[(-1) * this.mainWidth ,(-2) * this.mainWidth]});
					
					this.currentNode.getElement('.address_zpet_na_zadanou').setStyle('display','block');
					if($(this.options.id + 'Valid')) $(this.options.id + 'Valid').hiddenVal(2);
				}).bind(this)
			}).send();	
		}
});
