/**
 * Hotel.js
 * hotel pages script
 */

function updateXhrLinks () {
    
    //Nettoyage des events precedents
    $("#hotelMenu a, .xhrLink").unbind( 'click', xhrLoader );
    //Click sur un element du menu ou sur un truc de class xhrLink : load ajax
    $("#hotelMenu a, .xhrLink").click( xhrLoader );

    //Factsheets et ouvertures en popup
    $(".popup").click(openPopup);

    //Gestion de why book with us
    $("#bookWithUsLink").click(function() {
        
        $("#bookWithUs").load($(this).attr("href"), function() {
        
            $("#bookWithUsClose").click(function() {
                $("#bookWithUs").empty();
                $('#bookWithUs').hide();
                if(jQuery.browser.msie == true && jQuery.browser.version == '6.0') {
                    $("select").show();
                }
                return false;
            });
        
            if(jQuery.browser.msie == true && jQuery.browser.version == '6.0') {
                $("select").toggle();
            }
            $('#bookWithUs').show();
        });
        return false;
    });

   
   //Gestion des special offers
   $(".soItem").click(function() {
        $(".soListbox").hide();
        var itemId = $(this).attr('id');
        var pos = itemId.lastIndexOf('_');
        var soId = itemId.substring(pos +1);
        $("#solist_"+soId).show();
        return false;
   }); 
   $(".soListboxClose").click(function() { 
        $(".soListbox").hide();
        return false;
   });
   
   $dpIn = $("#checkin_date").datePicker(dpOptions);
    $("#rooms_nb").change(function() { updateGuestRooms(); });
    updateGuestRooms();
   
}

function xhrLoader () {
       $("#hotelFlash").show();
       
       var hash = $(this).attr('rel');
        hash = hash.replace(/^.*#/, '');
        // alert('hash='+hash);
        // moves to a new page.
        // pageload is called at once.
        // hash don't contain "#", "?"
        $.historyLoad(hash);
        return false;
}

function openPopup() {
     window.open($(this).attr("href"), '', 'location=no, width=450, height=500, scrollbars=yes, menubar=no, status=no, menubar=no');
     return false;
}

function itemMenuClick(hash) {
    //console.log(hash);
    if(hash) {
        hash = hash.substring(7, hash.length - 1);
        var slashPos = hash.indexOf('/');
        hash = hash.substring(slashPos + 1, hash.length);
        //console.log('pageUrl='+hash);
        $.historyLoad(hash);
    }
}
   
   
function pageload(hash) {
    //alert("pageload: " + hash);
    // hash doesn't contain the first # character.
    if(hash) {
       // restore ajax loaded state
       if($.browser.msie) {
           // jquery's $.load() function does't work when hash include special characters like aao.
           hash = encodeURIComponent(hash);
       }
       if(hash == hotelUrl) {
           hash = '/';
       } else {
           hash = hash + '/';
       }
       $("#hotelPanel").load('/hotel/', {"url": "/hotel/"+hotelUrl+'/'+hash}, function() { updateXhrLinks(); });
    } else {
        //page de base
       $("#hotelPanel").load('/hotel/', {"url": "/hotel/"+hotelUrl+'/'+baseHash+'/'}, function() { updateXhrLinks(); });
    }
}

$(document).ready(function(){
    // Initialize history plugin.
    // The callback is called at once by present location.hash.
    $.historyInit(pageload, "jquery_history.html");
    updateXhrLinks();
});
