window.addEvent('domready',function(){
    if($defined($('formScheda'))){
		$('formScheda').setStyles({'height':'0px','overflow':'hidden'}).set('class','unsel');
	}
	
    if($defined($('print'))){
		$('print').addEvent('click',function(){
			window.open('print.php?immobile='+$('printer').get('name'),'','width=740,height=500,menubar=0,scrollbars=1,resizable=0');
		})
	}
	
	if($defined($('foto'))){
		var list=new Array();
		$$('.thumbs img').each(function(el){
			var imgs=el.get('src').toString().replace('miniatura','zoom');
			var temp=[];
			temp.splice(1,0,imgs);
			list.splice(1,0,temp);
		});
		$('foto').getChildren('span').addEvent('click',function(e){
			var img=$('foto').getChildren('img').get('src').toString().replace('grande','zoom');
			var i=0,index;
			list.each(function(obj){
				//console.log(obj.indexOf(img));
				if(img.indexOf(obj)!=-1){
					index = i;
				} 
				i++
			});
			
			if(img.indexOf('default')==-1)
				Slimbox.open(list,index, 0);
				//Slimbox.open(img, "", 0);
		});
	}
	
    if($defined($('fotos'))){
		$('grande').addEvent('click',function(e){
			new Element('div',{'id':'thumbs','styles':{
					'position':'absolute',
					'margin':'5px',
					'width':'720px',
					'height':'490px',
					'background':'#666666',
					'z-index':'10'
				}
			}).inject($('printbox'),'before');
			
			$$('#miniature .th').each(function(src){
				new Element('img',{'src':src.get('name').replace('_miniatura','_primopiano'),
					'styles':{'margin':'10px','float':'left','cursor':'pointer'},
					'events':{
						'click':function(){
							$('grande').set('src',src.get('name').replace('_miniatura','_grande'));
							$('thumbs').dispose();
						}
					}
				}).inject($('thumbs'));
			});
			$('stampa').addEvent('click',function(){window.print();})
		});
	}
	

    if($defined($('code'))){
		$('code').addEvent('keydown', function(e){
			switch(e.key){
				case 'enter':
					if($('code').value=='')
						alert('Non hai digitato il codice!');
					else if (!$('code').value.match(/^[a-zA-Z]{2}[0-9]/))
						alert('Codice non corretto! Esempio: AA123, aa123');
					else{
						window.location="./scheda.php?immobile="+$('code').value;
					}
				break;
			}
		})
		$('codice').addEvent('click', function(){	
			if($('code').value=='')
				alert('Non hai digitato il codice!');
			else if (!$('code').value.match(/^[a-zA-Z]{2}[0-9]/))
				alert('Codice non corretto! Esempio: AA123, aa123');
			else{
				window.location="./scheda.php?immobile="+$('code').value;
			}
		})
	}
	
});

function showForm(node) {
    var sel=(node.hasClass('selected'))?'unsel':'selected';
    if(node.hasClass('unsel')){
        var start='0';
        var end='280px';
    } else {
        var start='280px';
        var end='0';
    }
     node.set('class',sel);
     var myFx = new Fx.Tween(node);
    myFx.start('height', start, end)
}

function enlarge(name) {
	document.getElementById('grande').src = document.getElementById('grande').src.replace(/immobili\/[a-z0-9_\/]+\.jpg$/, 'immobili/'+name+'_grande.jpg');
}

