debug_msg = false

function toDebug(string)
{
	if (debug_msg)
	{
		if((typeof window.console=="undefined"))
		{			
			alert(string);
		}
		else
		{		
			 window.console.log(string);
		}
	}
}



function popUpAppear()
{
    //  creazione overlay
    var pop_overlay = document.createElement("div");
	pop_overlay.setAttribute("id","pop_overlay");
	$$('body')[0].appendChild(pop_overlay);
	
    //  creazione box che contiene img
    var my_box = document.createElement("div");
	my_box.setAttribute("id","my_box");
	pop_overlay.appendChild(my_box);
    
    //  creazione link nel box
    var my_link = document.createElement("a");
	my_link.setAttribute("id","my_link");
	my_link.setAttribute("href","http://www.minicitymap.it/");
	my_box.appendChild(my_link);
    
    //  creazione bottone chiusura nel box
    var my_close = document.createElement("a");
	my_close.setAttribute("id","my_close");
	//my_close.setAttribute("href","javascript:return false;");
	my_box.appendChild(my_close);
    var my_close_text = document.createTextNode("Chiudi");
	my_close.appendChild(my_close_text);
    
    
    //  creazione img nel link
    var my_image = document.createElement("img");
	my_image.setAttribute("id","my_image");
	my_image.setAttribute("src","public/pop-up_minicity.gif");
	my_link.appendChild(my_image);
    
        var box_left = (screen.width/2)-(350/2);
    //  stili dell'overlay
    Element.setStyle('pop_overlay',
    {
        'position':'fixed',
        'top':'0px',
        'left':'0px',
        'background': '#000000',
        'width':(screen.width)+'px',
        'height':(screen.height) +'px',
        'opacity':'0.8',
        'zIndex':'10000'
    });
    //  stili del box
    Element.setStyle('my_box',
    {
        'position':'absolute',
        'top':'150px',
        'left':box_left+'px',
        'background':'#ffffff',
        'width':'350px'
    });
    //  stili dell'img
    Element.setStyle('my_image',
    {
        'border': '10px solid #dacbcb'
    });
    //  stili del close
    Element.setStyle('my_close',
    {
        'position':'absolute',
        'top':'-18px',
        'cursor': 'pointer',
        'right':' -20px',
        'color': '#000000',
        'fontWeight:': 'bold',
        'textDecoration:': 'none',
        'padding': '2px 4px 2px 4px',
        'background': '#dacbcb'
    });
        Element.observe('my_close', 'click', function()
    {
        Effect.Fade(my_box);
	    Effect.Fade(pop_overlay);
	    return false;
    });
}





/*BOF FORM CONTATTI*/

//restituisco true in caso di errore
function validateFormElement(elem)
{
	if(elem.value=="")
	{
		errorOnFormElement(elem);
		return true;
	}
	else 
	{
		return false;
	}
}


function validateHiddenElement(element)
{
	if(validateFormElement(element))
	{
		errorOnFormElement('fieldset_dettagli_richiesta');
		errorOnFormElement('fieldset_dettagli_preventivo');
		return true;
	}
	else return false;



}

function validateCheckBoxElement(elem)
{
	if(elem.checked)
	{
		return false;
	}
	else
	{
		errorOnFormElement(elem.parentNode);	
		return true;
	}
}

function validateFormEmail(elem)
{
	var emailRegexp = RegExp('^[A-Za-z0-9._-]+[@]([A-Za-z0-9-]+[.])+([A-za-z]{2,4})$', 'i');
	
	if (emailRegexp.test(elem.value))
	{
		return false;
	}
	else
	{
		errorOnFormElement(elem);
		return true;
	}
}


function errorOnFormElement(elem)
{
	Effect.Shake(elem);
	new Effect.Highlight(elem);
}

function submitGuestBook()
{
	var d = new Date()
	param = d.getTime();
		
	var form = $('guestbooksign');
	document.forms['guestbooksign'].secure.value = param;


	//copio testo della text area
	if (document.forms['guestbooksign'].frm_tipo_richiesta.value=="richiesta preventivo")
	{
		document.forms['guestbooksign'].frm_messaggio.value=document.forms['guestbooksign'].frm_messaggio_a.value;
	}
	else
	{
		document.forms['guestbooksign'].frm_messaggio.value=document.forms['guestbooksign'].frm_messaggio_b.value;	
	}

//inizio validazione
	var error_found = 0;
			
	if(validateFormElement(form.frm_nome)) error_found++;
	if(validateFormEmail(form.frm_email)) error_found++;
	if(validateFormElement(form.frm_telefono)) error_found++;
	if(validateCheckBoxElement(form.frm_privacy_a) && validateCheckBoxElement(form.frm_privacy_b)) error_found++;
	//if(validateHiddenElement(form.frm_tipo_richiesta)) error_found++;
//fine validazione	


	if(error_found==0)
	{
		Effect.BlindUp('form_email_all_fields');

		form.request({
		
		  onComplete: function()
		  { 	
			Effect.BlindDown('form_email_thanks');
			Form.reset('guestbooksign');

			  return false; 
		   }
		
		});

/*
	document.forms['guestbooksign'].submit();		
*/	
	}	
	return false;
}


function playVerticalMenu(menu)
{

	try
	{
		var menu = $(menu);
		var li1 = menu.immediateDescendants();
		li1.each(function(lv1)
		{
			var link_box = lv1.down('div');
			if(lv1.down('ul'))
			{
				var lv2 = lv1.down('ul');
				var lv3 = lv2.immediateDescendants();
				var lv1_active = false;
				var lv3_active = false;
				
				
				lv3.each(function(el)
				{
					if(el.className.match('active'))
					{
						lv3_active = true;
					}
				});
				
				if(lv1.className.match('active'))
				lv1_active = true;
				
				if(!lv1_active) lv2.hide();
				
				/*
				link_box.onclick = function()
				{
					Effect.toggle(lv2,'blind');
					closeAll(lv2);
					//if(lv2.down('ul')) playVerticalMenu(lv2);
					return false;
				}
				*/
			}
			else link_box.onclick = function(){window.location.href  = link_box.down('a').href;}
		});
		
	}
	catch(e)
	{
		toDebug(e.toString());
	}
}



function showOnClick(element)
{
	
	if($(element).style.display == '') return false;
	else
	{
		if(element == 'frm_dettagli_richiesta')
		{
			Effect.BlindUp($('frm_dettagli_preventivo'), {afterFinish: down=function()
			{
				Effect.BlindDown(element);				
				document.forms['guestbooksign'].frm_tipo_richiesta.value = 'richiesta informazioni';				
				
			}});
		}
		else
		{
			Effect.BlindUp($('frm_dettagli_richiesta'), {afterFinish: down=function()
			{
				Effect.BlindDown($('frm_dettagli_preventivo'));
				document.forms['guestbooksign'].frm_tipo_richiesta.value = 'richiesta preventivo';
			}});
		}
	}
	
}

/*EOF FORM CONTATTI*/
function setBodyBg(altezza_main) //Imposta proprieta dei div sotto al main per dare 2 colori di sfondo al body
{
	try
	{
		var lato_sinistro = $('body_sx');
		var lato_destro = $('body_dx');
		var body = document.getElementsByTagName('body')[0];
		var height;
		
		if(altezza_main < screen.height) height = screen.height;
		else height = altezza_main;
		lato_sinistro.setStyle
		({
			'position':'absolute',
			'top':'0px',
			'left':'0px',
			'width':(screen.width)/2+'px',
			'height':height +'px',
			'display':'block'
		});
		lato_destro.setStyle
		({
			'position':'absolute',
			'top':'0px',
			'right':'0px',
			'width':(screen.width)/2+'px',
			'height':height + 'px',
			'display':'block'
		});
	}
	catch(e)
	{
		toDebug(e.toString());
	}
}


function mainHeight()
{
	try
	{
		var main = $('main');
		var head = $('head');
		var body = $$('body')[0];
		var colonnaSx = $('colonnaSx');
		var container = $('container');

		var vetrine = $('vetrine');
		var contenuto = $('contenuto');
		
		var footer = $('footer');
		var subfooter = $('sub-footer');
		var header_txt = $('header_txt');
		var img_leaderboard = $('img_leaderboard');		
		var container_offsetHeight = container.offsetHeight;
		
		
		
		if (vetrine)
		{
			contenuto.setStyle({'top':vetrine.offsetHeight+  50+ 'px'});
			container_offsetHeight = contenuto.offsetHeight + 50 + vetrine.offsetHeight;			
		}
		else container_offsetHeight = contenuto.offsetHeight + 50;			
				
		
		
		
		if($('frm_dettagli_richiesta')) container_offsetHeight += 350;
		var media_altezza = Math.max(container_offsetHeight,colonnaSx.offsetHeight);
		var altezza_main;

		colonnaSx.setStyle({'top':head.offsetHeight+'px','height':media_altezza+'px'});
		container.setStyle({'top':head.offsetHeight+'px','height':media_altezza+'px'});
		footer.setStyle({'top':head.offsetHeight + media_altezza +'px'});
		subfooter.setStyle({'top':head.offsetHeight + media_altezza + footer.offsetHeight +'px'});
		if (header_txt) header_txt.setStyle({'top':head.offsetHeight + media_altezza + footer.offsetHeight + subfooter.offsetHeight +'px'});
		
		$('wrap').setStyle({'height':media_altezza+'px'});
		$('wrap').down().setStyle({'top':$('wrap').offsetHeight-144+'px'});
		
		
		if (header_txt) header_txt_offsetHeight = header_txt.offsetHeight;
		else header_txt_offsetHeight=0;
		
		altezza_main = head.offsetHeight+media_altezza+footer.offsetHeight +subfooter.offsetHeight + header_txt_offsetHeight;
		main.setStyle({'height':altezza_main+'px'});
		

		
		setBodyBg(altezza_main);
		// posiziono il footer wrap
		var footer_wrap = $('footer-wrap');
		footer_wrap.setStyle({'top':footer.getStyle('top'),'height':footer.offsetHeight + subfooter.offsetHeight + header_txt_offsetHeight + 'px'});
		
		// se ci sono le vetrine le rendo completamente cliccabili e anche il box parchi
		
		var add_vetrine = false;
		
		if(vetrine && add_vetrine==true)
		{
			

			
			//var vetrine_array = ($('vetrine')).getElementsByClassName('vetrina');
			
			var vetrine_array = $$('.vetrina');
			

			
			vetrine_array.each(function(el)
			{
				
				
				
				el.onclick = function()
				{
					

				
					/*disattivami per le statistiche*/
					
					
					
					window.location.href = el.down('a').href;					
					//window.open = el.down('a').href;
					
					/*inizio riattivami per le statistiche*/					
					//var href = el.down('a').href;
					//$('gotovetrina').setAttribute('action',href);
					//$('gotovetrina').submit();
					/*fine riattivami per le statistiche*/					
					
				}
			});
		}	
		
		
		
			
		// se 'right-col' non ha dei contenuti la nascondo e allargo il 'contenuto'
		if($('right-col'))
		{
			if($('right-col').empty() || $('right-col').innerHTML == "\n&nbsp;\n" || $('right-col').innerHTML == "&nbsp; ")
			{
				$('right-col').hide();
				$('contenuto').setStyle(
				{
					'width': '710px',
					'float': 'none'
				});
			}
		}
		if($('box_parchi'))
		{
			var box_parchi = $('box_parchi');
			box_parchi.onclick = function(){window.location.href = box_parchi.down('a').href;}
		}
		if($('box_eventi'))
		{
			var box_eventi = $('box_eventi');
			box_eventi.onclick = function(){window.location.href = box_eventi.down('a').href;}
		}
		
		if($('box_entroterra'))
		{
			var box_entroterra = $('box_entroterra');
			box_entroterra.onclick = function(){window.location.href = box_entroterra.down('a').href;}
		}
		
		// margino le immagini che sono il float
		var immagini = $$('#container img');
		var style_float;
		immagini.each(function(img)
		{
			if(img.getStyle('float') == 'left') img.setStyle({'margin':'0px 10px 0px 0px'});
			if(img.getStyle('float') == 'right') img.setStyle({'margin':'0px 0px 0px 10px'});
		});
		
	}
	catch(e)
	{
		toDebug(e.toString());
	}
}



function toggleMenu(menu, toggler, disappear, toggler2)
{
	try
	{
		var menu = $(menu);
		var toggler = $(toggler);
		if(disappear) var disappear = $(disappear);
		if(toggler2) var toggler2 = $(toggler2);
		
		
		if(menu)
		{
			var figli_array = menu.descendants();
			var active = false;
			figli_array.each(function(el)
			{
				if(el.className.match('active')) 
				{
					active = true;
				}
			});
		}
		
		//blocco menu di dani (preferivo il mio)
		active = false;
		
		toggler.setStyle({'cursor':'pointer'});
		
		
		//menu sempre aperto
		//if(!active) menu.hide();
		
		
		
		if(active && disappear) disappear.hide();
		toggler.onclick = function()
		{
			if(disappear && (disappear.getStyle('display') == 'block' || disappear.getStyle('display') == ''))
			{
				Effect.SwitchOff(disappear,{afterFinish:function()
				{
					Effect.toggle(menu, 'blind');
				}});
			}
			else if(disappear &&  disappear.getStyle('display') == 'none')
			{
				Effect.toggle(menu, 'blind',{afterFinish:function()
				{
					Effect.BlindDown(disappear);
				}});
			}
			else Effect.toggle(menu, 'blind');
		//	closeAll(menu);
		}
		
		if(toggler2)
		{
			toggler2.setStyle({'cursor':'pointer'});
			toggler2.onclick = function()
			{
				if(disappear && (disappear.getStyle('display') == 'block' || disappear.getStyle('display') == ''))
				{
					Effect.SwitchOff(disappear,{afterFinish:function()
					{
						Effect.toggle(menu, 'blind');
					}});
				}
				else if(disappear &&  disappear.getStyle('display') == 'none')
				{
					toggler2.onclick = function(){alert('ciao');}
					Effect.toggle(menu, 'blind',{afterFinish:function()
					{
						Effect.BlindDown(disappear);
					}});
				}
				else Effect.toggle(menu, 'blind');
				
			//	closeAll(menu);
			}
		}
	}
	catch(e)
	{
		toDebug(e.toString());
	}
}



function closeAll(opened)
{
try
{
	var boxes = $$('#menu_localita ul.level-2');
	boxes.each(function(el)
	{
		if(el != opened && (el.getStyle('overflow') == 'visible' || el.getStyle('display') == ''))
		Effect.BlindUp(el);
	
	});
}
catch(e){alert(e.toString());}
}




function setTableBg()
{
	try
	{
		var tables = $$('#hotel-contenuti .table');
		tables.each(function(el)
		{
			var righe = new Array();
			var childs = el.descendants();
			childs.each(function(child)
			{
				if(child.tagName == 'TR')
				{
					righe.push(child);
				}
			});
			
			for (var i=0;i<righe.length;i++)
			{
				
				if(i%2 == 0) righe[i].addClassName('a');
				else righe[i].addClassName('b');
			}
			
		});
		
	}
	catch(e){/*alert(e.toString());*/}
}


function assignOnClick()
{
	var from_index_localita = $('fiv');
	
	var vetrine = $$('.vetrina');
	
		vetrine.each(function(n,index) {
			var vetrina_name_raw =	n.down('span').innerHTML;
			var vetrina_name = vetrina_name_raw.unescapeHTML();
			vetrina_name = vetrina_name.replace(" ","_");
			vetrina_name = vetrina_name.replace("<br>","");
			vetrina_name = vetrina_name.replace("<BR>","");
			vetrina_name = vetrina_name.replace("<BR>","");
			
			if (from_index_localita) vetrina_name = "index_localita_" + vetrina_name;
						
			n.down('p a').onclick= function()
										{											
											//alert(vetrina_name);
											pageTracker._trackPageview ('/click-away/' + vetrina_name);	
											
										};
		
			});
	
}

function randomizeVetrine()
{

	try
	{
	var vetrine = $$('.vetrina');	
	
	var my_array = new Array(vetrine.size());
	
	vetrine.each(function(n,index) {		
		my_array[index] = new Array(4);
				
		my_array[index][0] = n.down('.link img').src;		
		my_array[index][1] = n.down('span').innerHTML;
		my_array[index][2] = n.down('.img-container img').src;
		my_array[index][3] = n.down('p a').href;

//		toDebug(n.down('.link img').src);
//		toDebug(n.down('span').innerHTML);
//		toDebug(n.down('.img-container img').src);
//		toDebug(n.down('p a').href);	

//		toDebug(my_array[index]);

	});

	my_array = my_array.randomize();

/*
	my_array.each(function(n,index) {	
	
	toDebug(n[0]);
	toDebug(n[1]);	
	toDebug(n[2]);	
	toDebug(n[3]);	
	toDebug('--');
	});
*/

	vetrine.each(function(n,index) {	
	n.down('.link img').src = my_array[index][0];
	n.down('span').innerHTML =my_array[index][1];
	n.down('.img-container img').src = my_array[index][2];
	n.down('p a').href = my_array[index][3];
	});
	
	}
	catch(e)
	{
		toDebug(e.toString());
	}

}


Array.prototype.randomize = function() {
 var i = this.length;
 if (i == 0) return;
 while (--i) {
  var j = Math.floor(Math.random()*(i+1));
  var tmp1 = this[i];
  var tmp2 = this[j];
  this[i] = tmp2;
  this[j] = tmp1;
 }
 return this;
}

function allFunctions()
{
	
	if($('menu_localita')) playVerticalMenu('menu_localita');
	mainHeight();
    if($('main').hasClassName('open_popup')) popUpAppear();
	toggleMenu('menu_localita', 'menu_bottone');
	if($('preventivo')) toggleMenu('preventivo', 'preventivo-button');
	if($('lastminute_box')) toggleMenu('lastminute_box', 'lastminute_bottone', 'lastminute-space', 'lastminute-space');
	if($$('#hotel-contenuti table')) setTableBg();
	$('loading_page').hide();
	
	
	if($('vetrine')) 
	{
		randomizeVetrine();
		assignOnClick();
	}
	
	
}

if (window.attachEvent) window.attachEvent("onload", allFunctions);
else window.onload=allFunctions;


