if(window.image_roller_images == undefined) 
	 window.image_roller_images = new Array();
	
$(document).ready(function(){
	$("div.image_roller").each(function(){
		var e = $(this);
		
		e.data("sfondi", image_roller_images[e.data("collection")]);
		e.data("current", 0);
		
		if(e.data("len") == undefined)
			e.data("len", e.data("sfondi").length);
		
		if(e.data("path") == undefined)
			e.data("path", "content/sfondi/" + pagina)
		
		
		switchImage($(e));
	});
});

function switchImage(e)
{
  if(e == undefined || e == null)
    return;
	var collection = e.data("collection");
	var current = e.data("current");
	var sfondi = e.data("sfondi");
	var len = e.data("len");
	
	//controllo se c'è già  una foto visualizzata, se no parto dalla prima
	var shown = e.find("div.image_roller_shown");
	var hidden = e.find("div.image_roller_hidden");
	
	var path = e.data("path");
	
	if(shown.length == 0)
	{
		e.append($("<div class='image_roller_div_element image_roller_shown'><img src='" + abs_url +path+"/"+getSfondo(sfondi, current)+"'></div>"));
		current++;
		if(current == len)
		  current=0;
		  
		//Preparo la foto dopo intanto
		e.append($("<div class='image_roller_div_element image_roller_hidden'><img src='" + abs_url +path+"/"+getSfondo(sfondi, current)+"'></div>"));
		e.data("current", current);
	}
	else
	{
		//altrimenti faccio sparire quella attualmente visualizzata
		var s = $(shown[0]);
		var h = $(hidden[0]);
		s.fadeOut(1000, function(){
			s.remove();
			h.removeClass("image_roller_hidden").addClass("image_roller_shown");
			
			current++;
			if(current == len)
			  current=0;
			  
			e.find("div.image_roller_hidden").remove();
			//Preparo la foto dopo intanto
			e.append($("<div class='image_roller_div_element image_roller_hidden'><img src='" + abs_url +path+"/"+getSfondo(sfondi, current)+"'></div>"));
			e.data("current", current);
		});
//		return;
		/*$(shown[0]).fadeOut("slow", function(){
			.removeClass("image_roller_hidden").addClass("image_roller_shown");
			$(shown[0]).remove();
		});*/
	}
	
	if(len > 1)
		setTimeout(switchImage, 5000, e);
}

function getSfondo(sfondi, index)
{
	return sfondi[index] == undefined ? undefined : (sfondi[index].constructor == String ? sfondi[index] : sfondi[index].path)
}
$(document).ready( function() {
	$("div.item_preview div.img img").each(function(){
		var parent_h = $(this).parent().height();
		var this_h = $(this).attr("height");
		var top = this_h/2 - parent_h/2;
		$(this).css("margin-top", "-" +  top + "px");
	});
});

var abs_url = "http://www.termedimontepulciano.it/";


var sfondi = [{"0":"im-salute4.jpg","path":"im-salute4.jpg"},{"0":"benessere.jpg","path":"benessere.jpg"},{"0":"im-salute2.jpg","path":"im-salute2.jpg"}];

var pagina = "index";
var id_lingua = "it";
var class_skype = "offline";
var icbm = [43.07230, 11.80361];
var current_url = abs_url + ".html";

image_roller_images[''] = sfondi;
var current_sfondo = 0;
var last_sfondo = 0;

var sfondo_timeout;

$(document).ready(function(){
	/*$("ul.main_menu>li>ul").hide();*/
	$("ul.main_menu > li").hover(
		function(evt){ //Hover :: visualizzo menu
			$(this).find("ul").fadeToggle("fast");
		}/*,
		function(evt){ //Out :: nascondo menu
			$(this).find("ul").fadeOut();
		}*/
	);
	
	//Riposiziono i tasti per la paginazione
	var w = $("div.content_center").width();
	$("div.pager").each(function(){
		var marginLeft = ($(this).hasClass("prev_page") ? -12 : w - $(this).width() -22);
		
		$(this).css({
			top: $("div.content_center").position().top + 15,
			marginLeft: marginLeft
		});

	});
	
	var max_h = 0;
	$("div.item_slider div.item_preview, div.items div").each(function(){
		max_h = Math.max(max_h, $(this).height());
		$(this).data("height", $(this).height());
	});
	$("div.item_slider div.item_preview, div.items div").height(max_h);
	$("div.item_slider div.item_preview, div.items div").each(function(){
		var h =$(this).data("height");
		$(this).find("div.prezzi").css({
			marginTop: max_h - h
		});
	});
	
	$("div.item_links").each(function(){
	  if($(this).html().replace(/^\s+|\s+$/g, '') == "||")
	    $(this).hide();
	});

  // il div content_center deve essere alto li massimo tra se stesso ed il primo div al suo interno
  $("div.content_center").height(
    Math.max($("div.content_center").height(), $("div.content_left").height())
  );

  // il div main_content deve essere alto quanto li massimo tra il div content_left ed il content_center
  $("div.main_content").height(
    Math.max($("div.content_center").height(), $("div.content_left").height()) + 30
  );
  
  if($("div.content_center").children().length == 1)
  {
    // il div contenuto dentro content_center (quando cen'è solo uno) deve essere alto li massimo tra se stesso ed div content_center
    $("div.content_center>div").height(
      Math.max($("div.content_center").height(), $("div.content_center>div").height()) - 20
    );
  }
  
  //controllo lo stato di skype sulla home
  $("ul.menu_home li.chatta a").append("<div class='"+class_skype+"' />");
  
	doColorBox();
	
	//Recupero il link corrente
	$("div.spacer").find("a[href='"+current_url+"']").addClass("opaque50");
	
});

$(window).load(function(){
  try{
    if(!$("body").hasClass("index"))
       $("html:not(:animated),body:not(:animated)").animate({ scrollTop: $('a[name="testo"]').offset().top-50}, 500 );
	   }catch(e){}
});

function nextSfondo()
{
	var sfondo = abs_url + "content/sfondi/" + pagina + "/" + sfondi[current_sfondo].path;
	$.backstretch(sfondo, {
		speed:250,
		id:"full_bg_" + current_sfondo,
		fixed: false,
		height: 640
	});
	
	if(sfondi.length == 1)
	  return;
	
	var last = (current_sfondo == 0 ? last_sfondo - 1 : current_sfondo - 1);
	
	$("#full_bg_" + (last)).fadeOut(2000, function(){
		$("#full_bg_" + (last)).remove()
	});
	
	current_sfondo++;
	if(current_sfondo == sfondi.length)
		current_sfondo = 0;
	
	sfondo_timeout = setTimeout("nextSfondo()",10000);
}

function loadModule(elem, url, new_id, post_function)
{
	
	dest = $("#" + elem).clone();//$("<div />");
	if(isEmpty(new_id))
	  dest.removeAttr("id");
	else
	  dest.attr("id", new_id);
	dest.hide();
	
	//console.log(url);
	
	dest.load(url, function(){
		$("#" + elem).parent().append(dest);
		$("#" + elem).fadeOut("fast", function(){
			dest.fadeIn("fast", function(){
				$(window).resize();
				$("#" + elem).remove();
				if(!isEmpty(post_function))
				{
					if(post_function.indexOf("(")==-1)
					  post_function+="();";
					try{ eval(post_function); } catch(e) { try{console.log(e);}catch(ee){} }
				}
			});
		});
	});
}

function loadTimelapse()
{
  var swf = abs_url + "graph/swf/timelap.swf";
  $("body").prepend('<div class="timelapse_container"><div class="timelapse">'
+'<object width="1920" height="640" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> '
+' <param value="'+swf+'" name="movie"> '
+' <param value="high" name="quality"> '
+' <param name="wmode" value="transparent" />'
+' <embed width="1920" wmode="transparent" height="640" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" src="'+swf+'" /> '
+'</object></div></div>');
  
}

function checkForm(form)
{
  if(!check_captcha())
    return myAlert("Controllo antispam non valido!", false);
    
  
  if($("#privacy").length > 0 && $("#privacy").attr("checked") != "checked")
    return myAlert("Accettare la privacy per continuare.", false);
    
  for(var f=0; f<form.length; f++)
  {
    if(form[f].hasAttribute("obbl") && $(form[f]).attr("obbl") == "y" && $(form[f]).val() == "")
      return myAlert("Campi obbligatori non compilati.", false);
  }

  return true;
}
