var gdir = null, travelMode = null, gdirquery = "";
function initMappa(lat, lng, html) {
    if (GBrowserIsCompatible()) {
        var center = new GLatLng(lat, lng);
        gmap = new GMap2(document.getElementById("map"));
        gmap.addControl(new GLargeMapControl());
        gmap.addControl(new GMapTypeControl());
        gmap.addMapType(G_PHYSICAL_MAP);
        gmap.removeMapType(G_HYBRID_MAP);
        gdir = new GDirections(gmap, document.getElementById("percorso_map"));
        GEvent.addListener(gdir, "error", handleErrors);
        gmap.setCenter(center, 15);
        var Icon = new GIcon();
        Icon.image = "/styles/marker.png";
        Icon.iconSize = new GSize(33, 33);
        Icon.iconAnchor = new GPoint(6, 20);
        Icon.infoWindowAnchor = new GPoint(16, 16);
        var marker = new GMarker(center, {icon:Icon});
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(html);
        });
        gmap.addOverlay(marker);
        GEvent.trigger(marker, "click");
    }
}
function onClickTrovaIndirizzo() {
    var indirizzo = window.document.getElementById("indirizzo");
    if (indirizzo.value) {
        var from = ""
        var dest = "";
        var luogo = window.document.getElementById("luogo_indirizzo");
        if (luogo) {
            if (luogo.value == 'P') {
                from = "43.9161639,12.9145386";
                dest = indirizzo.value + ",Pesaro (PU),Italia";
            }
            else if (luogo.value == 'I') {
                dest = "43.9161639,12.9145386";
                from = indirizzo.value + ",Italia";
            }
        }
        else {
            from = "43.9161639,12.9145386";
            dest = indirizzo.value + ",Pesaro (PU),Italia";
        }
        gdir.clear();
        travelMode = G_TRAVEL_MODE_DRIVING;
        gdirquery = "from: " + from + " to: " + dest;
        gdir.load(gdirquery, {
            locale: myhlang
        });
    }
}
function handleErrors() {
    if (travelMode == G_TRAVEL_MODE_DRIVING) {
        gdir.clear();
        travelMode = G_TRAVEL_MODE_WALKING;
        gdir.load(gdirquery, {
            locale: "it",
            travelMode: G_TRAVEL_MODE_WALKING
        });
    } else {
        alert("Indirizzo non trovato");
    }
}
function onloadmap(lat, lng, html) {
    window.onload = function() {
        initMappa(lat, lng, html);
    }
    window.onunload = GUnload;
}
function loadPacchettiOfferhotel() {
    var ul = document.createElement("ul");
    for (var i=0; i<Math.min(3, ohpacchetti.length); i++) {
        var li = document.createElement("li");
        $(li)
        .mouseover(function() {
            $(this).addClass("offerup")
        })
        .mouseout(function() {
            $(this).removeClass("offerup")
        });
        var img = new Image();
        img.src = "http://www.offerhotel.com/data/pacchetti/" + ohpacchetti[i].id + "_1.jpg";
        img.onerror = function() {
            this.src = "/styles/pacchetti.jpg";
        }
        var a = document.createElement("a");
        a.href = "booking.html?ohpkg=" + ohpacchetti[i].id;
        a.appendChild(document.createTextNode(ohpacchetti[i].nome.substr(0, 60) + "..."));
        a.appendChild(img);
        li.appendChild(a);        
        ul.appendChild(li);
    }
    var d = document.getElementById("pacchetti");
    d.appendChild(ul);
}
function Modal(){
    var e = $("#modal");
    var w = $("#modal").width();
    var screenW = $(document).width();
    e.css({
        top: "200px",
        left: (screenW-w)/2+"px"
    });
    $("#fade").fadeTo('', '0.33').css({
        height: $("body").height()
    });
    e.fadeIn();
}
function hideModal() {
    $("#modal").fadeOut();
    $("#fade").fadeTo('', '0').css({
        display: 'none'
    });
}

$(document).ready(function(){
    $('.equipment').jScrollPane({showArrows: true});
    $('#gallery a, .lb a, .jpeg a').lightBox({overlayOpacity:0.8});
    $(".buttonbook, .clickhere").hover(function(){
        $(".buttonbook img, .clickhere img")
        .animate({
            top:"-10px"
        }, 200).animate({
            top:"-4px"
        }, 200) // first jump
        .animate({
            top:"-7px"
        }, 100).animate({
            top:"-4px"
        }, 100) // second jump
        .animate({
            top:"-6px"
        }, 100).animate({
            top:"-4px"
        }, 100); // the last jump
    });
    $('li.sublist').mouseover(function(){
        $(this).children('.first').addClass('up');
        $(this).children('.sublist ul').show();
    });
    $('li.sublist').mouseout(function(){
        $('.first').removeClass('up');
        $('.sublist ul').hide();
    });
    $('.BEscriptoh14').mouseover(function(){
        $(this).css({
            'background-position':'bottom',
            'color':'#003662'
        })
    });
    $('.BEscriptoh14').mouseout(function(){
        $(this).css({
            'background-position':'top',
            'color':'#fff'
        })
    });
    $('#BEclose').addClass('open_be');
    $('#BEclose').click(function(){
        $('#BEscript').toggle();
        if ($('#BEscript').is(':visible')){
            $(this).removeClass('close_be');
            $(this).addClass('open_be');
            $('.txt_be').hide();
            $('.txt365').fadeTo(300,0.2);
        } else {
            $(this).removeClass('open_be');
            $(this).addClass('close_be');
            $('.txt_be').show();
            $('.txt365').fadeTo(300,1);
        }
    });    
    $('.close').click(function(){
        $('.content').toggle(); 
    });
    $(".tab_content").hide();
    $("ul.tabs li:first").addClass("active").show();
    $(".tab_content:first").show();
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
        $(".tab_content").hide();
        var activeTab = $(this).find("a").attr("href");
        $(activeTab).show();
        return false;
    });
    var imagesIndex = 1;
    var imgtemp = new Image();
    imgtemp.src = 'styles/gallery/' + images[1];
    setInterval(function(){
        $('.sfondo').fadeOut(500, function() {
            $(this).attr('src','/styles/gallery/' + images[imagesIndex++ % images.length]);
            $(this).fadeIn(600);            
            imgtemp.src = '/styles/gallery/' + images[imagesIndex % images.length];
        });
    },5000);
    $(".pets a, .pets_txt").easyTooltip();
});
