// Menu
function menuMoltoFico() {
	// funzione da richiamare al setTimeout
	function chiudi(el){
		el.fadeOut("normal");
		el.removeClass("on");
	}
	
	// nascondo tutti gli <ul li ul>
	$("#top_menu ul li ul").hide();
	$("#top_menu ul li ul li a.on").parent().parent().fadeIn("normal");
	$("#top_menu ul li a[href='#']").click(function(){return false;});

	// al rollover su <ul li a> show del sottomenu
	$("#top_menu ul li a").not($("#top_menu ul li ul li a")).mouseover(function(){
		$(this).next().addClass("on").fadeIn("normal");
		return false;
	});
	
	// al rollout su <ul li a> timeout della chiusura del sottomenu
	$("#top_menu ul li a").not($("#top_menu ul li ul li a")).mouseout(function(){
		$(this).next().removeClass("on");
		var myElA = $(this).next();
//		var myElA = $(this).parent().childrens().not($(this));
		aTimer = setTimeout(
					function(){
						if (myElA.attr("class")!="on"){
							chiudi(myElA);
						}
					},200); 
	});
	
	// al rollover su <ul li ul> aggiungo classe on
	$("#top_menu ul li ul").mouseover(function(){
		$(this).addClass("on");
	});
	
	// al mousout tolgo la classe on e timeout della chiusura
	$("#top_menu ul li ul").mouseout(function(){
		$(this).removeClass("on");
		var myElUl = $(this);
		ulTimer = setTimeout(
					function(){
						if (myElUl.attr("class")!="on"){ // se non c'è classe on (che appare al rollover) lo chiudo
							chiudi(myElUl);
						}
					},1000); 
	});
	
	$("#top_menu ul li ul li a.on").parent().parent().hide();
}


// BOOK WITH US LINK
function bookLink() {
	//$(".book_with_us").css({"position":"relative"}).append('<a href="viewdoc.asp?co_id=2" class="bookus_link">&nbsp;</a>');
	
}


// SLIDESHOW
function slideshow(){
	var imgs = $("#slideshow img").size();
	var j = imgs - 1;
	function slideIt() {
		if (j == 0) {
			j = imgs;
			$("#slideshow img").fadeIn(800);
		}
		$("#slideshow img:eq(" + j + ")").fadeOut(800);
		j--;
	}
	
	slide = setInterval(slideIt,4000);
}


// widget tempo
// /wget.asp?url=http://weather.msn.com/RSS.aspx?wealocations=wc:RSXX0125&weadegreetype=F
function widgetTempo() {
	$.ajax({  
		type: "GET",
		url: "wget.asp?url=http://weather.msn.com/RSS.aspx?wealocations=wc:RSXX0125&weadegreetype=Fl",
		dataType: "xml",
		contentType: "text/xml",
		success: parseXml,
		error: parseXml
	});
	
	function parseXml(xml) {
		$(xml).find("rss").each(function() {

			var data0 = $(this).find("pubDate:eq(0)").text().split(", ")[1].split(" ")[0] + " " + $(this).find("pubDate:eq(0)").text().split(", ")[1].split(" ")[1] + " " + $(this).find("pubDate:eq(0)").text().split(", ")[1].split(" ")[2]; 

			var text = $(this).find("description:eq(2)").text();
						
			var day1 = text.split("<strong>")[1].split("</strong>")[0];
			var day2 = text.split("<strong>")[2].split("</strong>")[0];
			var day3 = text.split("<strong>")[3].split("</strong>")[0];
			var day4 = text.split("<strong>")[4].split("</strong>")[0];
			var day5 = text.split("<strong>")[5].split("</strong>")[0];
			
			var d1 = $(day1).text();
			var d2 = $(day2).text();
			var d3 = $(day3).text();
			var d4 = $(day4).text();
			var d5 = $(day5).text();
			
			var img1 = "<img src='" + text.split('<img src="')[1].split('.gif')[0] + ".gif' />";
			var img2 = "<img src='" + text.split('<img src="')[2].split('.gif')[0] + ".gif' />";
			var img3 = "<img src='" + text.split('<img src="')[3].split('.gif')[0] + ".gif' />";
			var img4 = "<img src='" + text.split('<img src="')[4].split('.gif')[0] + ".gif' />";
			var img5 = "<img src='" + text.split('<img src="')[5].split('.gif')[0] + ".gif' />";
			
			var hi1 = text.split('. Hi: ')[1].split('. ')[0];
			var hi2 = text.split('. Hi: ')[2].split('. ')[0];
			var hi3 = text.split('. Hi: ')[3].split('. ')[0];
			var hi4 = text.split('. Hi: ')[4].split('. ')[0];
			var hi5 = text.split('. Hi: ')[5].split('. ')[0];
			var lo1 = text.split('Lo: ')[1].split('. Hi: ')[0];
			var lo2 = text.split('Lo: ')[2].split('. Hi: ')[0];
			var lo3 = text.split('Lo: ')[3].split('. Hi: ')[0];
			var lo4 = text.split('Lo: ')[4].split('. Hi: ')[0];
			var lo5 = text.split('Lo: ')[5].split('. Hi: ')[0];
			
			function f2c(f){
				c = Math.ceil(5/9 * (parseInt(f) - 32)) + "&deg;C";
				return c;
			}
			
			$(".meteo .col:eq(0) em").text(d1).next().html(img1).next().html(f2c(hi1) + "|" + hi1).next().html(f2c(lo1) + "|" + lo1);
			$(".meteo .col:eq(1) em").text(d2).next().html(img2).next().html(f2c(hi2) + "|" + hi2).next().html(f2c(lo2) + "|" + lo2);
			$(".meteo .col:eq(2) em").text(d3).next().html(img3).next().html(f2c(hi3) + "|" + hi3).next().html(f2c(lo3) + "|" + lo3);
			$(".meteo .col:eq(3) em").text(d4).next().html(img4).next().html(f2c(hi4) + "|" + hi4).next().html(f2c(lo4) + "|" + lo4);
			$(".meteo .col:eq(4) em").text(d5).next().html(img5).next().html(f2c(hi5) + "|" + hi5).next().html(f2c(lo5) + "|" + lo5);
			
			$(".meteo .ora").text(data0);
		});
	}
	
}


// INIT
$(function(){
	menuMoltoFico();
	
	if ($("#slideshow img").size() > 1) {
		slideshow();
	} else if ($("#slideshow img").size() == 0) {
		$("#slideshow").append('<img src="img/slideshow5.jpg" alt="mira hotel" /><img src="img/slideshow3.jpg" alt="mira hotel" /><img src="img/slideshow4.jpg" alt="mira hotel" /><img src="img/slideshow2.jpg" alt="mira hotel" />');
		slideshow();
	}
	
	bookLink();
	$(".scrollable").scrollable().mousewheel({size:6});
	$(".items a").fancybox({
		overlayOpacity:0.8,
		overlayColor:"#000",
		padding:20,
		imageScale:true
	});
	widgetTempo();
});
