$(document).ready(function(){

	//floating left box
	var offset = $("#floating-box").offset();
	var height = $("#floating-box").height();
	var topPadding = 15;
	
	var scrollBox=function() {
		var limit=$("#contfooter").offset().top - height - topPadding*2;
		
		var top=offset.top;
		if ($.browser.msie && $.browser.version!="8.0"){
			top=234;//ie7 fix
		}
		if ($(window).scrollTop() > top) {
			if ($(window).scrollTop() < limit){	
				$("#floating-box").stop().animate({
					marginTop: $(window).scrollTop() - top + topPadding
				}, 500);
			} else {
				$("#floating-box").stop().animate({
					marginTop: limit - top + topPadding
				}, 500);
			}
		} else {
			$("#floating-box").stop().animate({
				marginTop: 0
			}, 500);
		};
	};
	
	$(window).scroll(scrollBox);


	$("p.error").each(function (i, e){
		var wrapped=$(e);
		wrapped.prev('label').before(e);
	});

	//home slider if present
    var slider=$('#slider1').bxSlider({
		auto: true,
		pause: 5000,
		onAfterSlide: function(currentSlideNumber, totalSlideQty, currentSlideHtmlObject){
			var description=currentSlideHtmlObject.children('.description_'+currentSlideNumber);
			if (description.length==1){
				$('#descrizione').html(description.html());
			}
			
		},
		prevText: "<div onClick=\"_gaq.push(['_trackEvent', 'Home Page', 'Freccia', 'Sinistra']);return false;\"></div>",
		nextText: "<div onClick=\"_gaq.push(['_trackEvent', 'Home Page', 'Freccia', 'Destra']);return false;\"></div>"
	});
	
	//lightbox galleries if present
    var prettyPhoto = function(selector, configOverrides) {

        var config={
            animation_speed: 'normal', /* fast/slow/normal */
            slideshow: 5000, /* false OR interval time in ms */
            autoplay_slideshow: false, /* true/false */
            opacity: 0.80, /* Value between 0 and 1 */
            show_title: true, /* true/false */
            allow_resize: true, /* Resize the photos bigger than viewport. true/false */
            default_width: 800,
            default_height: 600,
            counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
            theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
            hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
            wmode: 'opaque', /* Set the flash wmode attribute */
            autoplay: true, /* Automatically start videos: True/False */
            modal: false, /* If set to true, only the close button will close the window */
            overlay_gallery: false, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
            keyboard_shortcuts: true, /* Set to false if you open forms inside prettyPhoto */
			callback: function(){$(window).scroll(scrollBox);}//restore scroll box func
        };

        if (configOverrides){
            jQuery.each(configOverrides, function(key,value){
               config[key]=value;
            });
        }

        $(selector).prettyPhoto(config);
    };

	prettyPhoto("a[rel^='lightbox']");
	
	//fix piantina height in accommodations
	var accommodationMap = $("div#colonnasx div.corposx:first");
	var iterationsLimit = 5;
	if (accommodationMap.length==1){
		var leftCol = $("div#colonnasx");
		iterations=0;
		while(accommodationMap.height() != leftCol.height() && iterationsLimit > iterations++){
			accommodationMap.height(leftCol.height());
		}
	}
	
	//accordion where needed
	var TOGGLING_SPEED=300;
	$( ".toggle" ).css('cursor', 'pointer');
	$( ".toggle" ).next().hide();
	$( ".toggle" ).each(function(){
		var wrapped=$(this);
		wrapped.attr('depth', wrapped.parents().length);
	});
	$( ".toggle" ).click(function(){
		var wrapped=$(this);
		
		if (wrapped.hasClass('toggled')){
			wrapped.removeClass("toggled");
		} else {
			wrapped.next().slideToggle(TOGGLING_SPEED, function() {
				var depth=wrapped.attr('depth');
				
				var toggled=$('.toggled[depth='+depth+']');
				
				if (toggled.length>0){
					if (toggled.is(':visible')){
						toggled.next().slideToggle(TOGGLING_SPEED);
					} else {
						toggled.next().hide();
					} 
					toggled.removeClass('toggled');
				}
				
				wrapped.addClass("toggled");
			});
		}
	});

	$('ul.toggled > li:not(.alone) > h2').click();
	
	//make launches boxes clickable
	$(".launch").each(function(i,e){
		var wrapped=$(e);
		var link=wrapped.find("a");
		wrapped.click(function(){wrapped.unbind();link.click();location.href=link.attr("href")});
		wrapped.css("cursor", "pointer");
	});

    $("a.blank").each(function(){
        this.target = "_blank";
    });

});

