/* Note: In the html page:
div#popup:
	div#popup-content
*/

$('#popup').jqm({});


function bind_popup(sender){
    sender.click(function(){
        var btn = $(this);
        url = btn.attr('href');
        $.get(url, {}, function(response, textStatus){
            show_popup(btn, response);
        });
        return false;
    });
}


function show_popup(btn, html){
    var offset = btn.offset();
    var popup = $('#popup');
    $('#popup-content').html(html);
    
    var left = ($(window).width() - popup.width()) / 2;
    var top = ($(window).height() - popup.height()) / 2;
    popup.css({
        'left': '' + left + 'px',
        'top': top
    });
    popup.jqmShow();
}


function update_handlers(){
    $(".popup_button").click(function(){
        $.ajax({
            url: $('#popup-form').attr('action'),
            data: $('#popup-form').formSerialize(),
            type: "POST",
            success: function(response){
                $('#popup-content').html(response);
            }
        });
        return false;
    });
}

/*
 * Show static popup, without ajax request.
 * Usage:
 *   Include rental.popup.js
 *   Example code:
 *     <a href="#" rev="ID-INFO-STATIC-DIV" class="info-static">Info</a>
 *     <div id="ID-INFO-STATIC-DIV" class="info-static-div">More Info</div>
*/
$(document).ready(function(){
     $('.info-static').live('click', function(){
         var btn = $(this);
         info_div = btn.attr('rev');
         info_div = $('#' + info_div);
         html = info_div.html();
         show_popup(btn, html);
         return false;
     })
});
