/* 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Title : Just Hotels
Author : Michele De Angelis + Daniele Bianco
Description : #######
Created : 03.09.2007
Modified : #######
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
*/

/* funzione per inviare il solo guestbook*/
function submitTheGuestBook()
{
	var d = new Date()
	param = d.getTime();

	document.forms['guestbooksign'].secure.value = param;
	
	try { document.forms['guestbooksign'].onsubmit(); }
	catch(e){}

	document.forms['guestbooksign'].submit();
}




//preload immagini
function preloadIMG(){	
	var images = document.getElementsByTagName('img');
	var immagini=new Array();	
	var cont = 0;
	
	//percorro tutte le immagini		
	for (var i=0; i<images.length; i++){
		var image = images[i];
		var relAttribute = String(image.getAttribute('name'));
	
		//se hanno match preload... associo funzioni di preload
		if (relAttribute.toLowerCase().match('preload')){
			immagini[cont]=new Image();
			immagini[cont].src=image.src;
			cont++;
		}
	}
}

// Over delle immagini <img src=""..>
function attachSwitch(){	
	var images = document.getElementsByTagName('img');
	
	//percorro tutte le immagini		
	for (var i=0; i<images.length; i++){
		var image = images[i];
		var relAttribute = String(image.getAttribute('name'));
		
		//se hanno match over... associo funzioni di swap img
		if (relAttribute.toLowerCase().match('over')){
			image.onmouseover = function(){
				if(this.className!="active"){
					this.src= this.src.replace('_off', '_on');	
				}
			};
			image.onmouseout = function(){
				if(this.className!="active"){
					this.src= this.src.replace('_on', '_off');	
				}
			};			
		}
		if(image.className == "active") image.src= image.src.replace('_off', '_on');
	}
}

/************************************************************************************************************************/
//	CALCOLO DELL'ALTEZZA DEL MAIN

function mainHeight()
{	
	var main = $('main');
	var head = $('head');
	var colonnaSx = $('colonnaSx');
	var container = $('container');
	var colonnaDx = $('colonnaDx');
	var footer = $('footer');
	var firma_edita = $('firma_edita');
	
	var altezza_colonne;
	var altezza_main;
	

	altezza_colonne = Math.max(colonnaSx.offsetHeight, Math.max(container.offsetHeight,colonnaDx.offsetHeight));
	altezza_main = head.offsetHeight + altezza_colonne + footer.offsetHeight;

	if(navigator.userAgent.match('MSIE'))
	{
		colonnaSx.setStyle({'top': head.offsetHeight - 3 +'px','height':altezza_colonne +'px'});
		container.setStyle({'top': head.offsetHeight - 3 +'px','height':altezza_colonne +'px'});
		colonnaDx.setStyle({'top': head.offsetHeight - 3 +'px','height':altezza_colonne +'px'});
	}
	else
	{
		colonnaSx.setStyle({'top': head.offsetHeight+'px','height':altezza_colonne+'px'});
		container.setStyle({'top': head.offsetHeight+'px','height':altezza_colonne+'px'});
		colonnaDx.setStyle({'top': head.offsetHeight+'px','height':altezza_colonne+'px'});
	}
	footer.setStyle({'top': head.offsetHeight + altezza_colonne + 'px'});
	
	// SE SONO NELLA HOME ABBASSO IL TOP DEL FOOTER
	if(main.className == 'home') footer.setStyle({'top': head.offsetHeight + altezza_colonne + 'px'});
	
	main.setStyle({'height': altezza_main + 'px'});

	if(firma_edita) firma_edita.setStyle({'top': footer.offsetTop + 85 + 'px'});
	
//	SISTEMO POSIZIONE DELLE IMMAGINI DI SFONDO DE CONTAINER IN HOTEL PER IE
	if(navigator.userAgent.match('MSIE') && $('cont_bot'))
	{		
		
		var cont_bot_elem = $('cont_bot');
		var array_padri = cont_bot_elem.ancestors();
		if(cont_bot_elem) cont_bot_elem.setStyle({'top': array_padri[0].offsetHeight - 22 + 'px'});

	}

	
//	SE C'E' L'IMMAGINE BOTTOM DELLA COLONNA SINISTRA LA POSIZIONO	
	if($('bottom_sx')) $('bottom_sx').style.top = altezza_colonne - 24 + 'px';

	
	// margino le immagini che sono il float
	var immagini = $$('#container img');
	var style_float;
	for (var m=0;m<immagini.length;m++)
	{
		style_float = immagini[m].getStyle('float');
		if(style_float == 'left') immagini[m].setStyle({'margin-right':'10px'});
		if(style_float == 'right') immagini[m].setStyle({'margin-left':'10px'});
	}
	
}

//	FINE CALCOLO DELL'ALTEZZA DEL MAIN
/***************************************************************************************************************************/


function calculateFilettus()
{
	if($('filetto_offerte'))
	{
		var array_offerte = document.getElementsByClassName('box_offerta');
		var filetto = $('filetto_offerte');
		var altezza_filetto = (array_offerte.size() * 115) / 2;
		if($('testo_offerta')) var testo_offerta_height = $('testo_offerta').offsetHeight;
		else var testo_offerta_height = 0;
		altezza_filetto = Math.ceil(altezza_filetto);
		filetto.setStyle(
		{
			'position': 'absolute',
			'top': testo_offerta_height + 70 + 'px',
			'left': '365px',
			'background': '#9C9C9E',
			'width': '1px',
			'height': altezza_filetto+'px'
		});
	}
}





//	Corregge la visualizzazione dei PNG in explorer
function correctPNG() {
	for(var i=0; i<document.images.length; i++){
		var img = document.images[i];
		var imgName = img.src.toUpperCase();
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
			var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText ;
			if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;     
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + img.src + "\', sizingMethod='crops');\"></span>";
			img.outerHTML = strNewHTML;
			i = i-1;
		}
	}
}


var timer;
var moving = false;
var element_to_move;
var max_move;


function scrollingContent(el, clipHeight)
{
	var element = $(el);
	var element_dimension = element.getDimensions();
	var clip_element = element.up();
	var clip_dimension = clip_element.getDimensions();
	var scroll_container = clip_element.up();

	clip_element.setStyle({'clip': 'rect(0px,'+clip_dimension.width+'px,'+clipHeight+'px,0px)'});
	
	if(element_dimension.height > clipHeight)
	{
		var goUp = $$('#'+scroll_container.id+' .goUp')[0];
		var goDown = $$('#'+scroll_container.id+' .goDown')[0];
		var down_top = scroll_container.offsetHeight - goDown.offsetHeight;
		// settaggi freccia in alto
		goUp.setStyle({
			'top': '10px',
			'cursor': 'pointer'
		});
		goUp.onmouseover =function()
		{
			element_to_move = element;
			max_move = clipHeight;
			scrollUp();
		};
		goUp.onmouseout = function()
		{
			
			moving = false;
			clearTimeout(timer);
		}
		goUp.show();
		
		
		// settaggi per freccia in basso
		goDown.setStyle({
			'top': '30px',
			'cursor': 'pointer'
		});
		goDown.onmouseover =function()
		{
			element_to_move = element;
			max_move = clipHeight;
			scrollDown();

		};
		goDown.onmouseout = function()
		{
			moving = false;
			clearTimeout(timer);
		}
		
		goDown.show();
	}
	
}

function scrollUp()
{	
	if(element_to_move)
	{
		if(element_to_move.offsetTop>=0) return false;
		moving = true;
		new Effect.Move(element_to_move,{x:0,y:1, duration: 0.01});
		if(moving) timer = setTimeout(scrollUp,10);
		else return false;
	}
	return false;
}
function scrollDown()
{
	if(element_to_move)
	{
		if(element_to_move.offsetTop < -(element_to_move.offsetHeight - max_move)) return false;
		moving = true;
		new Effect.Move(element_to_move,{x:0,y:-1, duration: 0.01});
		if(moving) timer = setTimeout(scrollDown,10);
		else return false;
	}
	return false;
}




function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}






function showNewsDetail(el, type)
{



	try
	{
		var body = $$('body')[0];
		var scroll_top = getPageScroll();
		var left_position = ((body.offsetWidth/2) - 300);
		var top_position = (body.offsetWidth/ 15) + scroll_top[1];
	
	//	CUSTOM OVERLAY SETTINGS	
		var custom_overlay = document.createElement('div');
		custom_overlay.setAttribute('id', 'custom_overlay');

		body.appendChild(custom_overlay);

		custom_overlay.onclick = function(){closeDetail()};		
		Element.setStyle('custom_overlay',{
			'position': 'absolute',
			'top': '0px',
			'left':'0px',
			'height': body.offsetHeight + 'px',
			'width': body.offsetWidth + 'px',
			'background': '#000000',
			'display': 'none',
			'z-index': '30'
		});
		var left_position = ((body.offsetWidth/2) - 300);
		
	//	NEWS_DETAIL SETTINGS	
		var news_detail = document.createElement('div');
		news_detail.setAttribute('id', 'news_detail');
		body.appendChild(news_detail);
		Element.setStyle('news_detail',{
			'position': 'absolute',
			'top': top_position + 'px',
			'left': left_position+'px',
			'height': '450px',
			'width': '620px',
			'background': '#FFFFFF',
			'display': 'none',
			'text-align':'justify',
			'border': '1px solid #000000',
			'z-index': '40'
		});		

	//	NEWS_DETAIL_CONTAINER SETTINGS	
		var news_detail_container = document.createElement('div');
		news_detail_container.setAttribute('id', 'news_detail_container');
		news_detail.appendChild(news_detail_container);
		Element.setStyle('news_detail_container',{'position': 'absolute','left':'14px','top':'9px','max-height': '428px','width': '600px','padding': '0px 0px 0px 0px','overflow': 'auto'});
		if(type=='news') news_detail_container.setStyle({'background': 'url(\'images/background/box_news.jpg\') repeat-y left', 'color':'#FFFFFF'});
		if(type=='last-minute') news_detail_container.setStyle({'background': 'url(\'images/background/box_last-minute.jpg\') repeat-y left', 'color':'#010101'});
	
	
	

	
	
	
	//	NEWS_DETAIL_CLOSER SETTINGS		
		var news_detail_closer = document.createElement('img');
		news_detail_closer.setAttribute('src', 'images/cancel.gif');
		news_detail_closer.setAttribute('id', 'news_detail_closer');
		news_detail.appendChild(news_detail_closer);
		news_detail_closer.onclick = function(){closeDetail()};
		Element.setStyle('news_detail_closer',{
			'position':'absolute',
			'right':'52px',
			'top':'30px',
			'height':'14px',
			'display': 'none',
			'cursor':'pointer'
		});
		
		Effect.Appear(custom_overlay,{from:0.0, to:0.5,afterFinish:openNews = function()
		{

			catchHtmlViaAjax(el.href,news_detail_container,type);
			
			Effect.Appear(news_detail,{afterFinish: ciao=function()
			{
				Effect.Appear(news_detail_closer);
	
			}});
	
			
		}});

	}
	catch(e){alert(e.toString());}
}



function catchHtmlViaAjax(url,elem,type)
{
	
	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess: function(transport) 
	  {		
			//scrivo dentro elem il transport text
			//alert(transport.responseText);
		    $(elem).innerHTML = transport.responseText;
		   if(type=='news') $(elem).innerHTML+='<img src="images/background/bottom_box_news.jpg">';
		   if(type=='last-minute') $(elem).innerHTML+='<img src="images/background/bottom_box_last-minute.jpg">';
	  },
	  
	onFailure: function()
	{
		$(elem).innerHTML = 'Notizia non piu\' presente in archivio.';		
	}
	});
}


function closeDetail()
{
	Effect.Fade('news_detail',{duration: 0.5,afterFinish: ciao=function()
		{
			Effect.Fade('custom_overlay',{duration: 0.4,afterFinish: remove=function(){
				Element.remove('news_detail');
	Element.remove('custom_overlay');
			}});
		}});
}


// Richiamo delle funzioni precedenti in un unica funzione
function allFunctions() 
{
	preloadIMG();
	attachSwitch();
	mainHeight();
	calculateFilettus();
	if(navigator.userAgent.match('MSIE')) correctPNG();
	if($('news_content')) scrollingContent('news_content', 90);
	if($('lastminute_content')) scrollingContent('lastminute_content', 200);
	
	$('loading_page').hide();

}


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