(function($){$.fn.forCarousel=function(b,c,d){fields={arr:[],where:'#header',total:0,imgs:[],time:5000};fields.arr=this||d;fields.where=b||fields.where;fields.total=fields.arr.length;fields.time=c||fields.time;counter=0;rad=$('<div id="tempHeader"></div>').css({display:'none'});rad.appendTo('body');put=$(fields.where).find('img:first');clo=0;$(fields.arr).each(function(a){if(this==put.attr('src')){fields.imgs[a]=put;counter++}else{fields.imgs[a]=$('<img src="'+this+'" alt="Header" />').load(function(){counter++;callback=function(){if(counter==fields.total){clearInterval(clo);forCarouselRec()}};if(a==fields.total-1){clo=setInterval(callback,50)}}).appendTo(rad)}});forCarouselRec=function(){forCarouselRecIn=function(){current=(current+1)%fields.total;$(fields.where).fadeOut(400,function(){put=$(fields.where).find('img:first');put.appendTo(rad);rad.find('img:first').appendTo(this);$(this).fadeIn(400)})};current=0;jobim=setInterval(forCarouselRecIn,fields.time)}}})(jQuery);

headers_ita = [
	'/images/header/header_1_ita.jpg',
	'/images/header/header_6_ita.jpg',
	'/images/header/header_8_ita.jpg',
	'/images/header/header_5_ita.jpg',
	'/images/header/header_9_ita.jpg'
];

headers_tv_ita = [
	'/images/header/header_7_ita.jpg',
	'/images/header/header_8_ita.jpg'
];

headers_je_ita = [
	'/images/header/header_1_ita.jpg',
	'/images/header/header_2_ita.jpg',
	'/images/header/header_3_ita.jpg',
	'/images/header/header_4_ita.jpg',
];

headers_eng = [
	'/images/header/header_1_eng.jpg',
	'/images/header/header_6_eng.jpg',
	'/images/header/header_8_eng.jpg',
	'/images/header/header_5_eng.jpg',
	'/images/header/header_9_eng.jpg'
];

headers_tv_eng = [
	'/images/header/header_7_eng.jpg',
	'/images/header/header_8_eng.jpg'
];

headers_je_eng = [
	'/images/header/header_1_eng.jpg',
	'/images/header/header_2_eng.jpg',
	'/images/header/header_3_eng.jpg',
	'/images/header/header_4_eng.jpg',
];

headers_deu = [
	'/images/header/header_1_deu.jpg',
	'/images/header/header_6_deu.jpg',
	'/images/header/header_8_deu.jpg',
	'/images/header/header_5_deu.jpg',
	'/images/header/header_9_deu.jpg'
];

headers_tv_deu = [
	'/images/header/header_7_deu.jpg',
	'/images/header/header_8_deu.jpg'
];

headers_je_deu = [
	'/images/header/header_1_deu.jpg',
	'/images/header/header_2_deu.jpg',
	'/images/header/header_3_deu.jpg',
	'/images/header/header_4_deu.jpg',
];
ForcolaMap = {
	version:1.0,
	init:function(){
		//ForcolaMap.animationBindings();
		//ForcolaMap.changeOrder();
		//ForcolaMap.compareAnimationBindings();
		ForcolaMap.refreshForm();
	},
	changeOrder:function(){
		$('input[name=structure_order]').click(function(){
				$('#searchForm').submit();
		});
	},
	refreshForm:function(){
		$("input[id^='filter'][type=checkbox],input[id^='filter'][type=radio],input[id^='advancedFilter'][type=checkbox]").click(function(){
			$("#searchForm").submit();
		});
	},
	animationBindings:function(){
		$('#map_after_eyes').css({display:"none"});

		$('#map_butt').click(function(evt){
			evt.preventDefault();
			$('#map_butt_close').css({display:'block'});
			$('#map_after_eyes').slideDown(1000);
			$(this).fadeOut(200,function(){
				$(this).css({display:'none'});
			});
		});
		$('#map_butt_close').click(function(evt){
			evt.preventDefault();
			$('#map_after_eyes').each(function(){
				if($.browser.msie == true && $.browser.version == 7){
					$(this).css({height:$(this).height()+"px"});
				}
			}).slideUp(1000,function(){
				$('#map_butt').css({display:'block'});
			});
		});
	},
	compareAnimationScript:function(evt){
		evt.preventDefault();
		structures = $("div[id^='hotel_']");
		notChecked = $("input[id^='forCompare_']:not(:checked)");
		if(notChecked.size() < (structures.size() - 1)){ // DIMINUENDO DI 1 CONTROLLO CHE CI SIANO ALMENO 2 STRUTTURE SELEZIONATE
			notChecked.each(function(){
				myPanel = structures.filter('#hotel_'+$(this).attr('value'));
				if(myPanel.css('display') != 'none'){
					myPanel.animate({height:0},400);
					myPanel.queue(function(){
						$(this).css({display:'none'});
						$(this).dequeue();
					});
				}
			});
		}else{
			alert("Please select at least two structures");
		}
	},
	compareAnimationBindings:function(){
		$('#compareTop').click(ForcolaMap.compareAnimationScript);
		$('#compareBot').click(ForcolaMap.compareAnimationScript);
	}
};

ForcolaHome = {
	version:1.0,
	timer:5,
	init:function(){
		if(arguments.length >0 && (typeof(arguments[0]) == 'object')){
			
			recalc = arguments[0].height || 0;
			return function(){
				ForcolaHome.elements = {
					rotomatic:$('#journeys_container'),
					rotomatic_sub:$('#journeys_sub_container')
			
				};
				ForcolaHome.rotomatic(recalc);
			};
		}else{
			ForcolaHome.elements = {
				rotomatic:$('#journeys_container'),
				rotomatic_sub:$('#journeys_sub_container')
				
			};
			ForcolaHome.rotomatic();
		}
	},
	rotomatic:function(recalc){
		remains = recalc || 0;
		theHeight = 0;
		ForcolaHome.elements.rotomatic.addClass('rotomatic');
		ForcolaHome.elements.rotomatic_sub.addClass('rotomatic_sub');
		ForcolaHome.elements.rotomatic_sub.find('div.box_journey').each(function(){
			if(remains-- > 0){
				theHeight += $(this).height();
			}
			ForcolaHome.elements.rotomatic_sub.append($(this).clone());
		});
		
		if(recalc > 0){
			ForcolaHome.elements.rotomatic.css({height:theHeight});
		}
		
		ForcolaHome.elements.rotomatic_sub.find('div.box_journey:first').addClass('current_box_journey');
		id = setTimeout(ForcolaHome.recursiveRotomatic,ForcolaHome.timer*1000);
	},
	recursiveRotomatic:function(){
		currentElement = ForcolaHome.elements.rotomatic_sub.find('div.current_box_journey');
		ForcolaHome.elements.rotomatic_sub.animate({top:'-='+currentElement.height()},800,'easeOutQuad',function(){
			currentElement = $(this).find('div.current_box_journey');
			if(currentElement.hasClass('last_box_journey')){
				currentElement.removeClass('current_box_journey');
				$(this).css({top:0}).find('div.box_journey:first').addClass('current_box_journey');
			}else{
				currentElement.removeClass('current_box_journey').next().addClass('current_box_journey');
			}
			id = setTimeout(ForcolaHome.recursiveRotomatic,ForcolaHome.timer*1000);
		});
	}
};

function searchBigMap(city_id,zone_id){
	$("input[id^='filter[zone_id]']").each(function(){
		$(this).attr('checked',false);
	});
	$("[id^='filter[city_id]']").attr({id:'filter[city_id]['+city_id+']',value:city_id});
	$("[id='filter[zone_id]["+zone_id+"]']").attr('checked','checked');
	$('#searchForm').submit();
}
function open_close_map(){
	if (document.getElementById('map_after_eyes').style.display=="none" && document.getElementById('map_after_eyes').style.height=='0px'){
		document.getElementById('map_after_eyes').style.display="block";
		document.getElementById('map_after_eyes').style.height="0px";
		document.getElementById('map_after_eyes').style.overflow="hidden";
		expand('map_after_eyes',0);
	} else if (document.getElementById('map_after_eyes').style.display=="block" && document.getElementById('map_after_eyes').style.height=='500px'){
		unexpand('map_after_eyes',500);
	}
}
function expand(id, halt_now){
	halt = 500;
	if (halt_now <=halt){
		halt_now=(halt_now+(halt-halt_now)/8+1);
		document.getElementById(id).style.height = halt_now + "px";
		if (halt_now<250){
			document.getElementById('map_butt').style.visibility='hidden';
		}
		setTimeout(function(){expand(id, halt_now)}, 50)
	} else if (halt_now>halt) {
		document.getElementById(id).style.height =halt + "px";
		document.getElementById('map_butt').style.visibility='hidden';
		document.getElementById('mapstatus').value = 1;
	}

}

function unexpand(id, halt_now){
	if (halt_now >0){
		halt_now=(halt_now-(halt_now/8)-1);
		document.getElementById(id).style.height = halt_now + "px";
		if (halt_now<150){
			document.getElementById('map_butt').style.visibility='visible';
		}
		setTimeout(function(){unexpand(id,halt_now)}, 50)
	} else if (halt_now<=0){
		document.getElementById(id).style.height ="0px";
		document.getElementById(id).style.display="none";
		document.getElementById('mapstatus').value = 0;
	}
}

function showmap() {
	if (document.getElementById('map_after_eyes').style.display=="none" && document.getElementById('map_after_eyes').style.height=='0px') {	
		document.getElementById('map_after_eyes').style.display="block";
		document.getElementById('map_after_eyes').style.height = "500px";
		document.getElementById('map_after_eyes').style.overflow="hidden";
		document.getElementById('map_butt').style.visibility='hidden';	
	}
}