var ready = false;
$(window).load(function() {
    readytimer = setInterval(function() {
        if (ready) {
            clearInterval(readytimer);
            $("#orange img").animate({
                opacity: 0
            },
            1500,
            function() {
                $("#orange").animate({
                    opacity: 0
                },
                500,
                function() {
                    $("#orange").remove();
                    $("#navigation").animate({
                        left: 12
                    },
                    {
                        queue: false,
                        duration: 1000,
                        easing: "",
                        complete: function() {}
                    });
                    $("#booking").animate({
                        right: 12
                    },
                    {
                        queue: false,
                        duration: 1000,
                        easing: "",
                        complete: function() {}
                    });
                    if ($("#page-gallery-container").length > 0) {
                        setUpGallery();
                        $("#viewpagegallery").animate({
                            opacity: 1
                        },
                        1000)
                    }
                    resizeScroller()
                })
            })
        }
    },
    50)
});
function readcookie(name) {
    var re = new RegExp("(?:^| )" + name + "=([^;]*)", "i");
    var matches = document.cookie.match(re);
    return matches && matches.length == 2 ? matches[1] : null
}
function savecookie(name, value, days) {
    var date = new Date;
    date.setTime(date.getTime() + (typeof days != "undefined" ? days: 3) * 24 * 60 * 60 * 1000);
    var expires = "; expires=" + date.toGMTString();
    document.cookie = name + ("=" + value + expires + "; path=/; domain=" + document.domain)
}
function setCentered() {
    win_height = $(window).height();
    content_height = $("#main").height();
    $("#main").css("margin-top", (win_height / 2) - (content_height / 2));
    $(window).resize(function() {
        win_height = $(window).height();
        box_height = $("#navigation").height();
        $("#main").css("margin-top", (win_height / 2) - (box_height / 2))
    });
    $(window).trigger('resize');
}
function setGalleryHandlers() {
    $("a.gallery, a#gal-close").unbind("click");
    $("a.gallery").click(function() {
        $("#hideall").unbind("click").bind("click",
        function() {
            if ($("#gallery-container").css("bottom") == "20px") {
                $("#gallery-container").animate({
                    bottom: -100
                },
                1000);
                $(this).find("img").remove();
                $('<img src="/assets/i/fullscreen-off.png" alt="Toggle">').appendTo($(this));
                $('<div id="fs-gallery-console"><img src="/assets/i/gal-play-fs.png" alt="Pause" /></div>').appendTo($("body"));
                $("#fs-gallery-console img").css("cursor", "pointer").unbind("click").bind("click",
                function(evt) {
                    startFsSlideshow();
                    $("#fs-gallery-console img").attr("src", "/assets/i/gal-pause-fs.png").attr("alt", "Pause").css("cursor", "pointer")
                })
            } else {
                $("#gallery-container").animate({
                    bottom: 20
                },
                1000);
                $(this).find("img").remove();
                $('<img src="/assets/i/fullscreen.png" alt="Toggle">').appendTo($(this));
                $("#fs-gallery-console").remove()
            }
            return false
        });
        $("#gallery-container").animate({
            bottom: 20
        },
        1000);
        $("#navigation").animate({
            left: -300
        },
        1000);
        $("#booking").animate({
            right: -300
        },
        1000);
        $("#panel *").animate({
            opacity: 0
        },
        1000);
        $("#panel").animate({
            opacity: 0
        },
        1000);
        return false
    });
    $("a#gal-close").click(function() {
        if (typeof slidetimer != "undefined") {
            clearInterval(slidetimer)
        }
        if ($("#page-gallery-container").length > 0) {
            $("#viewpagegallery").animate({
                opacity: 1
            },
            1000)
        }
        $("#hideall").unbind("click").bind("click",
        function() {
            if ($("#navigation").css("left") == "12px") {
                $("#navigation").animate({
                    left: -300
                },
                1000);
                $("#booking").animate({
                    right: -300
                },
                1000);
                $("#panel *").animate({
                    opacity: 0
                },
                1000);
                $("#panel, #viewpagegallery").animate({
                    opacity: 0
                },
                1000,
                function() {
                    $("#viewpagegallery").hide()
                });
                $(this).find("img").remove();
                $('<img src="/assets/i/fullscreen-off.png" alt="Toggle">').appendTo($(this))
            } else {
                $("#navigation").animate({
                    left: 12
                },
                1000);
                $("#booking").animate({
                    right: 12
                },
                1000);
                $("#panel *").animate({
                    opacity: 1
                },
                1000);
                $("#panel").animate({
                    opacity: 1
                },
                1000,
                function() {
                    if (jQuery.browser.msie) {
                        this.style.removeAttribute("filter")
                    }
                });
                if ($("#page-gallery-container").length > 0) {
                    $("#viewpagegallery").show();
                    $("#viewpagegallery").animate({
                        opacity: 1
                    },
                    1000)
                }
                $(this).find("img").remove();
                $('<img src="/assets/i/fullscreen.png" alt="Toggle">').appendTo($(this))
            }
            return false
        });
        $("#gallery-container").animate({
            bottom: -100
        },
        1000);
        $("#navigation").animate({
            left: 12
        },
        1000);
        $("#booking").animate({
            right: 12
        },
        1000);
        $("#panel *").animate({
            opacity: 1
        },
        1000);
        $("#panel").animate({
            opacity: 1
        },
        1000,
        function() {
            if (jQuery.browser.msie) {
                this.style.removeAttribute("filter")
            }
        });
        return false
    })
}
function resizeScroller() {
    $("#scroller").jScrollPane({
        verticalGutter: 20,
        animateScroll: true
    });
    var api = $("#scroller").data("jsp");
    var throttleTimeout;
    $(window).bind("resize",
    function() {
        if ($.browser.msie) {
            if (!throttleTimeout) {
                throttleTimeout = setTimeout(function() {
                    api.reinitialise();
                    throttleTimeout = null
                },
                50)
            }
        } else {
            api.reinitialise()
        }
    })
}
function setScrollerHeight() {
    if ($("#inpage-nav").length > 0) {
        $("#scroller").css("height", 320)
    }
}
function getAlias(obj) {
    separator = obj.attr("rel").indexOf("_") + 1;
    alias = obj.attr("rel").substring(separator);
    return alias
}
function loading() {
    if (position == 12) {
        $("#loading").css("background-position", "0 0");
        position = 1
    } else {
        $("#loading").css("background-position", "0 " + -position * 40 + "px");
        position++
    }
}
function swapImage(path) {
    if (typeof timer != "undefined") {
        clearInterval(timer)
    }
    $("#loading").remove();
    $to_remove = $("#backstretch img");
    $('<div id="loading" />').css({
        position: "absolute",
        top: (($(window).height() - 120) / 2) - 20,
        left: ($(window).width() / 2) - 20,
        width: 40,
        height: 40,
        backgroundImage: "url('/assets/i/loading.png')",
        zIndex: 2
    }).appendTo("#backstretch");
    position = 1;
    timer = setInterval("loading()", 100);
    img = $('<img class="gal-current" />').css({
        position: "absolute",
        top: 0,
        left: 0,
        opacity: 0
    }).bind("load",
    function(e) {
        $("#loading").css("z-index", "0").remove();
        clearTimeout(timer);
        var self = $(this);
        imgRatio = $(e.target).width() / $(e.target).height();
        $('#page-gal-thumbs li a[href="' + path + '"]').parent().siblings().find("a").removeClass("active");
        $('#page-gal-thumbs li a[href="' + path + '"]').addClass("active");
        _adjustBG(function() {
            self.animate({
                opacity: 1
            },
            500,
            function() {
                self.css("position", "relative");
                self.removeClass("gal-current");
                $to_remove.remove();
                if (typeof callback == "function") {
                    callback()
                }
            })
        })
    }).appendTo("#backstretch");
    img.attr("src", path);
    $(window).resize(_adjustBG)
}
function _adjustBG(fn) {
    try {
        bgCSS = {
            left: 0,
            top: 0
        };
        bgWidth = rootElement.width();
        bgHeight = bgWidth / imgRatio;
        if (bgHeight >= rootElement.height()) {
            bgOffset = (bgHeight - rootElement.height()) / 2
        } else {
            bgHeight = rootElement.height();
            bgWidth = bgHeight * imgRatio;
            bgOffset = (bgWidth - rootElement.width()) / 2;
            bgCSS = {
                left: "-" + bgOffset + "px"
            }
        }
        if ($("#backstretch img.gal-current").length) {
            $("#backstretch img.gal-current").width(bgWidth).height(bgHeight).css(bgCSS)
        } else {
            $("#backstretch img").width(bgWidth).height(bgHeight).css(bgCSS)
        }
    } catch(err) {}
    if (typeof fn == "function") {
        fn()
    }
}
rootElement = ("onorientationchange" in window) ? $(document) : $(window);
position = 1;
function initPageGallery() {
    $("#page-gal-prev, #page-gal-next").remove();
    if ($("#page-gal-thumbs li").size() > 7) {
        $left = $("#page-gal-thumbs li").width() + 10;
        $("#page-gal-thumbs").css("width", $("#page-gal-thumbs li").size() * ($("#page-gal-thumbs li").width() + 10));
        $totalWidth = $("#page-gal-thumbs").width();
        $btn_prev = $('<a id="page-gal-prev" title="Precedenti"></a>').appendTo("#page-gal-thumbs-container").css({
            visibility: "hidden"
        }).click(showPrevPage);
        $btn_next = $('<a id="page-gal-next" title="Successive"></a>').appendTo("#page-gal-thumbs-container").css({
            visibility: "visible"
        }).click(showNextPage)
    }
}
function showPrevPage() {
    $galleryLeft = $("#page-gal-thumbs").position().left;
    $shift = $galleryLeft + ($left * 7);
    if ($galleryLeft < 0) {
        $("#page-gal-thumbs").animate({
            left: $shift
        },
        300,
        function() {
            $btn_next.css("visibility", "visible");
            if ($shift == 0) {
                $btn_prev.css("visibility", "hidden")
            }
        })
    }
}
function showNextPage() {
    $shift = $("#page-gal-thumbs").position().left - ($left * 7);
    if (Math.abs($shift) < $totalWidth) {
        $("#page-gal-thumbs").animate({
            left: $shift
        },
        300,
        function() {
            $btn_prev.css("visibility", "visible");
            if (($totalWidth - Math.abs($shift)) <= ($left * 7)) {
                $btn_next.css("visibility", "hidden")
            }
        })
    }
}
function setUpGallery(html) {
    if ($("#page-gallery-container").length == 0) {
        $gallery = $(html);
        $gallery.css("bottom", -100).insertAfter($("#main"))
    }
    $("#viewpagegallery").css({
        top: $("#panel").offset().top - $("#viewpagegallery").outerHeight() - 12,
        left: $("#panel").offset().left,
        display: "block"
    }).animate({
        opacity: 1
    },
    1000).click(function() {
        $("#hideall").unbind("click").bind("click",
        function() {
            if ($("#page-gallery-container").css("bottom") == "20px") {
                $("#page-gallery-container").animate({
                    bottom: -100
                },
                1000);
                $(this).find("img").remove();
                $('<img src="/assets/i/fullscreen-off.png" alt="Toggle">').appendTo($(this));
                $('<div id="fs-gallery-console"><img src="/assets/i/gal-play-fs.png" alt="Pause" /></div>').appendTo($("body"));
                $("#fs-gallery-console img").css("cursor", "pointer").unbind("click").bind("click",
                function(evt) {
                    startPageFsSlideshow();
                    $("#fs-gallery-console img").attr("src", "/assets/i/gal-pause-fs.png").attr("alt", "Pause").css("cursor", "pointer")
                })
            } else {
                $("#page-gallery-container").animate({
                    bottom: 20
                },
                1000);
                $(this).find("img").remove();
                $('<img src="/assets/i/fullscreen.png" alt="Toggle">').appendTo($(this));
                $("#fs-gallery-console").remove()
            }
            return false
        });
        $("#page-gallery-container").animate({
            bottom: 20
        },
        1000);
        $("#navigation").animate({
            left: -300
        },
        1000);
        $("#booking").animate({
            right: -300
        },
        1000);
        $("#panel *").animate({
            opacity: 0
        },
        1000);
        $("#panel, #viewpagegallery").animate({
            opacity: 0
        },
        1000,
        function() {
            $("#viewpagegallery").hide()
        });
        return false
    });
    $("a#page-gal-close").click(function() {
        if (typeof pageslidetimer != "undefined") {
            clearInterval(pageslidetimer)
        }
        $("#hideall").unbind("click").bind("click",
        function() {
            if ($("#navigation").css("left") == "12px") {
                $("#navigation").animate({
                    left: -300
                },
                1000);
                $("#booking").animate({
                    right: -300
                },
                1000);
                $("#panel *").animate({
                    opacity: 0
                },
                1000);
                $("#panel, #viewpagegallery").animate({
                    opacity: 0
                },
                1000,
                function() {
                    $("#viewpagegallery").hide()
                });
                $(this).find("img").remove();
                $('<img src="/assets/i/fullscreen-off.png" alt="Toggle">').appendTo($(this))
            } else {
                $("#navigation").animate({
                    left: 12
                },
                1000);
                $("#booking").animate({
                    right: 12
                },
                1000);
                $("#panel *").animate({
                    opacity: 1
                },
                1000);
                $("#panel").animate({
                    opacity: 1
                },
                1000,
                function() {
                    if (jQuery.browser.msie) {
                        this.style.removeAttribute("filter")
                    }
                });
                if ($("#page-gallery-container").length > 0) {
                    $("#viewpagegallery").show();
                    $("#viewpagegallery").animate({
                        opacity: 1
                    },
                    1000)
                }
                $(this).find("img").remove();
                $('<img src="/assets/i/fullscreen.png" alt="Toggle">').appendTo($(this))
            }
            return false
        });
        $("#page-gallery-container").animate({
            bottom: -100
        },
        1000);
        $("#visibility-manager").animate({
            right: 12
        },
        1000);
        $("#navigation").animate({
            left: 12
        },
        1000);
        $("#booking").animate({
            right: 12
        },
        1000);
        $("#viewpagegallery").show();
        $("#panel *").animate({
            opacity: 1
        },
        1000);
        $("#panel, #viewpagegallery").animate({
            opacity: 1
        },
        1000,
        function() {
            if (jQuery.browser.msie) {
                this.style.removeAttribute("filter")
            }
        });
        return false
    });
    $("#page-gal-thumbs a").unbind("click").click(function(evt) {
        if (typeof pageslidetimer != "undefined") {
            clearInterval(pageslidetimer)
        }
        swapImage($(this).attr("href"));
        evt.stopPropagation();
        return false
    });
    $("#page-gal-slideshow").unbind("click").click(function(evt) {
        startPageSlideshow();
        evt.stopPropagation();
        return false
    });
    initPageGallery()
}
function startPageSlideshow() {
    $("#page-gal-slideshow").unbind("click").css("cursor", "default");
    $("#page-gallery-container").animate({
        bottom: -100
    },
    1000);
    $('<div id="fs-gallery-console"><img src="/assets/i/gal-pause-fs.png" alt="Pause" /></div>').appendTo($("body"));
    $("#fs-gallery-console img").css("cursor", "pointer").unbind("click").bind("click",
    function(evt) {
        stopPageFsSlideshow();
        $("#fs-gallery-console img").attr("src", "/assets/i/gal-play-fs.png").attr("alt", "Play").css("cursor", "pointer")
    });
    $("#hideall").find("img").remove();
    $('<img src="/assets/i/fullscreen-off.png" alt="Toggle">').appendTo($("#hideall"));
    $("#hideall").unbind("click").click(function() {
        stopPageSlideshow();
        $("#fs-gallery-console").remove()
    });
    startPageImgLoop();
    pageslidetimer = setInterval("startPageImgLoop()", 5000)
}
function startPageFsSlideshow() {
    startPageImgLoop();
    pageslidetimer = setInterval("startPageImgLoop()", 5000);
    $("#hideall").find("img").remove();
    $('<img src="/assets/i/fullscreen-off.png" alt="Toggle">').appendTo($("#hideall"));
    $("#hideall").unbind("click").click(function() {
        stopPageSlideshow();
        $("#fs-gallery-console").remove()
    });
    $("#fs-gallery-console img").css("cursor", "pointer").unbind("click").bind("click",
    function(evt) {
        stopPageFsSlideshow();
        $("#fs-gallery-console img").attr("src", "/assets/i/gal-play-fs.png").attr("alt", "Play").css("cursor", "pointer")
    })
}
function stopPageSlideshow() {
    if (typeof pageslidetimer != "undefined") {
        clearInterval(pageslidetimer)
    }
    $("#page-gallery-container").animate({
        bottom: 20
    },
    1000);
    $("#hideall").find("img").remove();
    $('<img src="/assets/i/fullscreen.png" alt="Toggle">').appendTo($("#hideall"));
    $("#hideall").unbind("click").click(function() {
        if ($("#page-gallery-container").css("bottom") == "20px") {
            $("#page-gallery-container").animate({
                bottom: -100
            },
            1000);
            $(this).find("img").remove();
            $('<img src="/assets/i/fullscreen-off.png" alt="Toggle">').appendTo($(this));
            $('<div id="fs-gallery-console"><img src="/assets/i/gal-play-fs.png" alt="Pause" /></div>').appendTo($("body"));
            $("#fs-gallery-console img").css("cursor", "pointer").unbind("click").bind("click",
            function(evt) {
                startPageFsSlideshow();
                $("#fs-gallery-console img").attr("src", "/assets/i/gal-pause-fs.png").attr("alt", "Pause").css("cursor", "pointer")
            })
        } else {
            $("#page-gallery-container").animate({
                bottom: 20
            },
            1000);
            $(this).find("img").remove();
            $('<img src="/assets/i/fullscreen.png" alt="Toggle">').appendTo($(this));
            $("#fs-gallery-console").remove()
        }
        return false
    });
    $("#page-gal-slideshow").css("cursor", "pointer").unbind("click").click(function() {
        startPageSlideshow()
    })
}
function stopPageFsSlideshow() {
    if (typeof pageslidetimer != "undefined") {
        clearInterval(pageslidetimer)
    }
    $("#fs-gallery-console img").css("cursor", "pointer").unbind("click").bind("click",
    function(evt) {
        startPageFsSlideshow();
        $("#fs-gallery-console img").attr("src", "/assets/i/gal-pause-fs.png").attr("alt", "Pause").css("cursor", "pointer")
    })
}
function startPageImgLoop() {
    var $pageactive = $("#page-gal-thumbs li a.active").parent();
    if ($pageactive.length == 0) {
        $pageactive = $("#page-gal-thumbs li:last a").parent()
    }
    var $pagenext = ($pageactive.next().length) ? $pageactive.next().find("a") : $("#page-gal-thumbs li:first a");
    swapImage($pagenext.attr("href"))
}
function updateContents(obj, url) {
    $.ajax({
        url: "/ajax-backend/get-pagetitle.html?alias=" + obj,
        dataType: "html",
        success: function(data, textStatus, xhr) {
            $.address.title(data)
        }
    });
    $("#page-gallery-container").remove();
    $("#navigation-content, #viewpagegallery").stop(true, true).animate({
        opacity: 0
    },
    400);
    $("#content #panel *").stop(true, true).animate({
        opacity: 0
    },
    400);
    $("#content #panel").stop(true, true).animate({
        opacity: 0
    },
    400,
    function() {
        $.ajax({
            url: "/ajax-backend/get-page.html?alias=" + obj + "&langid=" + langid,
            dataType: "json",
            success: function(data, textStatus, xhr) {
                trackVisit(url);
                if (data.background != "") {
                    swapImage(data.background)
                }
                $("#navigation-content").html(data.navigation);
                $("#content").html(data.content);
                $("#content #panel").css("opacity", 0);
                setScrollerHeight();
                resizeScroller();
                if (data.gallery != "") {
                    setUpGallery(data.gallery)
                }
                $("#content #panel, #navigation-content").stop(true, true).animate({
                    opacity: 1
                },
                400,
                function() {
                    if (jQuery.browser.msie) {
                        this.style.removeAttribute("filter")
                    }
                    setGalleryHandlers()
                })
            }
        })
    })
}
function updateDashboard(langid, alias) {
    $("#logo a").attr("href", (langid == 642) ? "/": "it/");
    $("#logo a").attr("rel", (langid == 642) ? "doc_642": "doc_1");
    if (!$("#booking").is(".set")) {
        $.ajax({
            url: "/ajax-backend/get-dashboard.html?langid=" + langid + "&alias=" + alias,
            dataType: "html",
            success: function(data, textStatus, xhr) {
                $("#booking").replaceWith(data);
                $("#booking").addClass("set").css("right", -300);
                ready = true;
            }
        })
    }
    if (!$("#gallery-container").is(".set")) {
        $.ajax({
            url: "/ajax-backend/get-gallery.html?langid=" + langid,
            dataType: "html",
            success: function(data, textStatus, xhr) {
                $("#gallery-container").replaceWith(data);
                $("#gallery-container").addClass("set").css("bottom", -100);
                if ($("#gal-navigation ul li").size() > 0 && $("#gal-navigation ul li.active").length == 0) {
                    $("#gal-navigation ul li:first").addClass("active")
                }
                $("a.gallery").click(function() {
                    $("#hideall").unbind("click").bind("click",
                    function() {
                        if ($("#gallery-container").css("bottom") == "20px") {
                            $("#gallery-container").animate({
                                bottom: -100
                            },
                            1000);
                            $(this).find("img").remove();
                            $('<img src="/assets/i/fullscreen-off.png" alt="Toggle">').appendTo($(this));
                            $('<div id="fs-gallery-console"><img src="/assets/i/gal-play-fs.png" alt="Play" /></div>').appendTo($("body"));
                            $("#fs-gallery-console img").css("cursor", "pointer").live(function() {
                                if (typeof slidetimer == "undefined") {
                                    startSlideshow();
                                    $("#fs-gallery-console img").remove();
                                    $('<img src="/assets/i/gal-pause-fs.png" alt="Pause" />').appendTo($("#fs-gallery-console"))
                                } else {
                                    stopSlideshow();
                                    $("#fs-gallery-console img").remove();
                                    $('<img src="/assets/i/gal-play-fs.png" alt="Play" />').appendTo($("#fs-gallery-console"))
                                }
                                evt.stopPropagation();
                                return false
                            })
                        } else {
                            $("#gallery-container").animate({
                                bottom: 20
                            },
                            1000);
                            $(this).find("img").remove();
                            $('<img src="/assets/i/fullscreen.png" alt="Toggle">').appendTo($(this));
                            $("#fs-gallery-console").remove()
                        }
                        return false
                    });
                    $("#gallery-container").animate({
                        bottom: 20
                    },
                    1000);
                    $("#navigation").animate({
                        left: -300
                    },
                    1000);
                    $("#booking").animate({
                        right: -300
                    },
                    1000);
                    $("#panel *").animate({
                        opacity: 0
                    },
                    1000);
                    $("#panel").animate({
                        opacity: 0
                    },
                    1000);
                    return false
                });
                $("a#gal-close").unbind("click").click(function() {
                    if (typeof slidetimer != "undefined") {
                        clearInterval(slidetimer)
                    }
                    if ($("#page-gallery-container").length > 0) {
                        $("#viewpagegallery").animate({
                            opacity: 1
                        },
                        1000)
                    }
                    $("#hideall").unbind("click").bind("click",
                    function() {
                        if ($("#navigation").css("left") == "12px") {
                            $("#navigation").animate({
                                left: -300
                            },
                            1000);
                            $("#booking").animate({
                                right: -300
                            },
                            1000);
                            $("#panel, #viewpagegallery").animate({
                                opacity: 0
                            },
                            1000,
                            function() {
                                $("#viewpagegallery").hide()
                            });
                            $(this).find("img").remove();
                            $('<img src="/assets/i/fullscreen-off.png" alt="Toggle">').appendTo($(this))
                        } else {
                            $("#navigation").animate({
                                left: 12
                            },
                            1000);
                            $("#booking").animate({
                                right: 12
                            },
                            1000);
                            $("#panel").animate({
                                opacity: 1
                            },
                            1000);
                            if ($("#page-gallery-container").length > 0) {
                                $("#viewpagegallery").show();
                                $("#viewpagegallery").animate({
                                    opacity: 1
                                },
                                1000)
                            }
                            $(this).find("img").remove();
                            $('<img src="/assets/i/fullscreen.png" alt="Toggle">').appendTo($(this))
                        }
                        return false
                    });
                    $("#gallery-container").animate({
                        bottom: -100
                    },
                    1000);
                    $("#navigation").animate({
                        left: 12
                    },
                    1000);
                    $("#booking").animate({
                        right: 12
                    },
                    1000);
                    $("#panel").animate({
                        opacity: 1
                    },
                    1000);
                    return false
                });
                $("#gal-navigation a").unbind("click").click(function() {
                    if (typeof slidetimer != "undefined") {
                        clearInterval(slidetimer)
                    }
                    separator = $(this).attr("id").indexOf("-") + 1;
                    gal_id = $(this).attr("id").substring(separator);
                    clicked = $(this);
                    $.ajax({
                        url: "/ajax-backend/get-main-gallery.html?id=" + gal_id,
                        dataType: "html",
                        success: function(data, textStatus, xhr) {
                            $("#gal-thumbs-container").animate({
                                opacity: 0
                            },
                            500,
                            function() {
                                $("#gal-thumbs").html(data);
                                $("#gal-thumbs a").click(function(evt) {
                                    if (typeof slidetimer != "undefined") {
                                        clearInterval(slidetimer)
                                    }
                                    loadImage($(this));
                                    evt.stopPropagation();
                                    return false
                                });
                                init();
                                $("#gal-thumbs img").load(function() {
                                    $("#gal-thumbs-container").animate({
                                        opacity: 1
                                    },
                                    500,
                                    function() {
                                        clicked.parent().siblings().removeClass("active");
                                        clicked.parent().addClass("active")
                                    })
                                })
                            })
                        }
                    });
                    return false
                });
                $("#gal-thumbs a").unbind("click").click(function(evt) {
                    if (typeof slidetimer != "undefined") {
                        clearInterval(slidetimer)
                    }
                    loadImage($(this));
                    evt.stopPropagation();
                    return false
                });
                $("#gal-slideshow").unbind("click").click(function(evt) {
                    startSlideshow();
                    evt.stopPropagation();
                    return false
                })
            }
        })
    }
}
function updateContentsFromHash(obj, url) {
    $("#page-gallery-container").remove();
    $("#navigation-content, #viewpagegallery").animate({
        opacity: 0
    },
    400);
    $("#content #panel *").animate({
        opacity: 0
    },
    400);
    $("#content #panel").animate({
        opacity: 0
    },
    400,
    function() {
        $.ajax({
            url: "/ajax-backend/get-page.html?alias=" + obj + "&langid=" + langid,
            dataType: "json",
            success: function(data, textStatus, xhr) {
                if (data.background != "") {
                    swapImage(data.background)
                }
                $("#navigation-content").html(data.navigation);
                $("#content").html(data.content);
                $("#content #panel").css("opacity", 0);
                setScrollerHeight();
                resizeScroller();
                if (data.gallery != "") {
                    setUpGallery(data.gallery)
                }
                $("#content #panel, #navigation-content").animate({
                    opacity: 1
                },
                400,
                function() {
                    setGalleryHandlers()
                })
            }
        })
    })
}
function updatePanel(obj, url) {
    $.ajax({
        url: "/ajax-backend/get-pagetitle.html?alias=" + obj,
        dataType: "html",
        success: function(data, textStatus, xhr) {
            $.address.title(data)
        }
    });
    $("#viewpagegallery").stop(true, true).animate({
        opacity: 0
    },
    400);
    $("#page-gallery-container").remove();
    $("#content #panel *").stop(true, true).animate({
        opacity: 0
    },
    400);
    $("#content #panel").stop(true, true).animate({
        opacity: 0
    },
    400,
    function() {
        $.ajax({
            url: "/ajax-backend/get-panel-content.html?alias=" + obj + "&langid=" + langid,
            dataType: "json",
            error: function(jqXHR, textStatus, errorThrown) {},
            success: function(data, textStatus, xhr) {
                trackVisit(url);
                if (data.background != "") {
                    swapImage(data.background)
                }
                if ($("#back-to-menu").length > 0 && data.navigation != "") {
                    $("#navigation-content").stop(true, true).animate({
                        opacity: 0
                    },
                    400,
                    function() {
                        $("#navigation-content").html(data.navigation)
                    })
                }
                $("#content").html(data.content);
                $("#content #panel").css("opacity", 0);
                setScrollerHeight();
                resizeScroller();
                if (data.gallery != "") {
                    setUpGallery(data.gallery)
                }
                if ($("#back-to-menu").length > 0 && data.navigation != "") {
                    $("#navigation-content").stop(true, true).animate({
                        opacity: 1
                    },
                    400)
                }
                $("#content #panel").stop(true, true).animate({
                    opacity: 1
                },
                400,
                function() {
                    if (jQuery.browser.msie) {
                        this.style.removeAttribute("filter")
                    }
                    setGalleryHandlers()
                })
            }
        })
    })
}
function goHome(alias, url, track) {
    track = typeof(track) != "undefined" ? track: true;
    $.ajax({
        url: "/ajax-backend/get-pagetitle.html?alias=" + alias,
        dataType: "html",
        success: function(data, textStatus, xhr) {
            $.address.title(data)
        }
    });
    $("#page-gallery-container").remove();
    $("#navigation-content, #viewpagegallery").stop(true, true).animate({
        opacity: 0
    },
    400);
    $("#content #panel *").stop(true, true).animate({
        opacity: 0
    },
    400);
    $("#content #panel").stop(true, true).animate({
        opacity: 0
    },
    400,
    function() {
        $.ajax({
            url: "/ajax-backend/get-page.html?alias=" + alias + "&langid=" + langid,
            dataType: "json",
            success: function(data, textStatus, xhr) {
                if (track) {
                    trackVisit(url)
                }
                if (data.background != "") {
                    swapImage(data.background)
                }
                $("#navigation-content").html(data.navigation);
                $("#content").html(data.content);
                $("#content #panel").css("opacity", 0);
                setScrollerHeight();
                resizeScroller();
                if (data.gallery != "") {
                    setUpGallery(data.gallery)
                }
                $("#content #panel, #navigation-content").stop(true, true).animate({
                    opacity: 1
                },
                400,
                function() {
                    setGalleryHandlers()
                })
            }
        })
    })
}
function trackVisit(url) {
    _gaq.push(function() {
        if (url.indexOf("gclid") != 0) {
            var code = url.split("=")[1];
            var utmz = readcookie("__utmz");
            if (utmz.match(/utmcsr\=\(direct\)\|utmccn\=\(direct\)\|utmcmd\=\(none\)/)) {
                utmz = utmz.replace(/utmcsr\=\(direct\)\|utmccn\=\(direct\)\|utmcmd\=\(none\)/, "utmgclid=" + code + "|utmccn=(not%20set)|utmcmd=(not%20set)")
            }
            savecookie("__utmz", utmz, 182)
        }
    });
    _gaq.push(["_trackPageview", url])
} (bg == "") ? $.backstretch("/assets/i/bg.jpg", {
    centeredY: false
}) : $.backstretch("/" + bg, {
    centeredY: false
});
var clicked_home = false;
$(document).ready(function() {
    $.address.init(function(event) {}).externalChange(function(event) {
        if (event.path == "/") {
            goHome(642, "/");
            updateDashboard(642, 642);
            return false
        }
        function updateAlias(alias) {
            addr_alias = alias;
            url = event.path;
            updateContents(addr_alias, url);
            updateDashboard(langid, addr_alias)
        }
        var addr_alias = "";
        if (event.pathNames.length > 0) {
            if (event.pathNames.length == 1) {
                langid = (event.pathNames[0] == "it") ? 1: 642;
                addr_alias = (event.pathNames[0] == "it") ? 1: 642;
                updateContents(addr_alias, event.path);
                updateDashboard(langid, addr_alias)
            } else {
                langid = (event.pathNames[0] == "it") ? 1: 642;
                alias_tmp = event.pathNames[event.pathNames.length - 1];
                alias_tmp = alias_tmp.replace(/\/|\.html/i, "");
                $.ajax({
                    url: "/ajax-backend/get-id-from-alias.html?alias=" + alias_tmp + "&langid=" + langid,
                    dataType: "html",
                    success: function(data, textStatus, xhr) {
                        updateAlias(data)
                    }
                })
            }
        } else {}
    });
    $("#hideall").click(function() {
        if ($("#navigation").css("left") == "12px") {
            $("#navigation").animate({
                left: -300
            },
            1000);
            $("#booking").animate({
                right: -300
            },
            1000);
            $("#panel *").animate({
                opacity: 0
            },
            1000);
            $("#panel, #viewpagegallery").animate({
                opacity: 0
            },
            1000,
            function() {
                $("#viewpagegallery").hide()
            });
            $(this).find("img").remove();
            $('<img src="/assets/i/fullscreen-off.png" alt="Toggle">').appendTo($(this))
        } else {
            $("#navigation").animate({
                left: 12
            },
            1000);
            $("#booking").animate({
                right: 12
            },
            1000);
            $("#panel *").animate({
                opacity: 1
            },
            1000);
            $("#panel").animate({
                opacity: 1
            },
            1000,
            function() {
                if (jQuery.browser.msie) {
                    this.style.removeAttribute("filter")
                }
            });
            if ($("#page-gallery-container").length > 0) {
                $("#viewpagegallery").show();
                $("#viewpagegallery").animate({
                    opacity: 1
                },
                1000)
            }
            $(this).find("img").remove();
            $('<img src="/assets/i/fullscreen.png" alt="Toggle">').appendTo($(this))
        }
        return false
    });
    $("#back-to-menu a, #logo a").live("click",
    function() {
        alias = ($(this).attr("href") != "" && $(this).attr("href") != "/") ? 1: 642;
        url = ($(this).attr("href") == "" || $(this).attr("href") == "/") ? "/": "/it/";
        if ( !! (window.history && window.history.pushState)) {
            goHome(alias, url)
        } else {
            if (url != "/") {
                goHome(alias, url)
            }
        }
        $("#service-navigation li, #subnavigation li, #info li").removeClass("active");
        $.address.value($(this).attr("href").replace("http://" + document.location.hostname + "/", ""));
        return false
    });
    $("#submenu-title a").live("click",
    function() {
        alias = getAlias($(this));
        url = "/" + $(this).attr("href");
        updatePanel(alias, url);
        $("#service-navigation li, #subnavigation li, #info li").removeClass("active");
        $.address.value($(this).attr("href").replace("http://" + document.location.hostname + "/", ""));
        return false
    });
    $("#navigation-content > ul:not(#subnavigation) > li > a:not(a.gallery)").live("click",
    function() {
        alias = getAlias($(this));
        url = "/" + $(this).attr("href");
        updateContents(alias, url);
        $("#service-navigation li, #subnavigation li, #info li").removeClass("active");
        $.address.value($(this).attr("href").replace("http://" + document.location.hostname + "/", ""));
        return false
    });
    $('#navigation-content ul#subnavigation > li a, #service-navigation li a, #info a, #contacts-link, #booking-link, #panel a[rel^="doc_"]').live("click",
    function() {
        alias = getAlias($(this));
        url = "/" + $(this).attr("href");
        updatePanel(alias, url);
        if (!$(this).is("#panel a")) {
            $("#service-navigation li, #subnavigation li, #info li").removeClass("active")
        }
        $(this).parent().addClass("active");
        $.address.value($(this).attr("href").replace("http://" + document.location.hostname + "/", ""));
        return false
    }); ($("#inpage-nav").length > 0) ? $("#scroller").css("height", 320) : $("#scroller").css("height", 345);
    setCentered()
});
