var $lugar;

function mostrar_info(x) {
    $('#lugares_info').children().hide();
    $('#lugar_' + x).show();
}

function mostrar_lugar(title, id) {
    $('#lugares_titulo').text(title);
    mostrar_info(id);
    $('#fondo_lugares').show();
    $('#lugar').show();
}

function mostrar_lugares() {
    $('#lugares_titulo').text($lugar.attr('title'));
    $('#lugares_values').children().removeClass('selected')
    .each(function () {
        if ($(this).attr('value') == $lugar.children('input').val())
            $(this).addClass('selected');
    });
    mostrar_info($lugar.children('input').val());
    $('#fondo_lugares').show();
    $('#lugares').show();
}

function ocultar_lugares() {
    $('#lugar').hide();
    $('#lugares').hide();
    $('#fondo_lugares').hide();
}

function seleccionar_lugar(x) {
    $lugar.children('input').val(x.attr('value'));
    $lugar.children('span').text(x.text());
}

function entrega_callback() {
    $('#devolucion').children('input').val(
        $('#entrega').children('input').val());
    $('#devolucion').children('span').text(
        $('#entrega').children('span').text());
    $('#texto_devolucion').text($('#devolucion').children('span').text());
}

function devolucion_callback() {
    $('#texto_devolucion').text($('#devolucion').children('span').text());
}

function cambiar_devolucion() {
    $lugar = $('#devolucion');
    mostrar_lugares();
}

$('#fondo_lugares').click(ocultar_lugares);
$('#lugares_values').children().click(function () {
    seleccionar_lugar($(this));
    if ($lugar.attr('callback'))
        eval($lugar.attr('callback'));
    ocultar_lugares();
}).mouseover(function () {
    $(this).addClass('over');
    mostrar_info($(this).attr('value'));
}).mouseout(function () {
    $(this).removeClass('over');
    mostrar_info($lugar.children('input').val());
}).each(function () {
    var $li = $(this);

    $('.lugar').each(function () {
        if ($(this).children('input').val() == $li.attr('value'))
            $(this).children('span').text($li.text());
    });
});
$('#lugares_cerrar').click(function () {
    ocultar_lugares();
    return false;
});

$(".lugar").click(function () {
    $lugar = $(this);
    mostrar_lugares();
});

$("#lugares_titulo").click(ocultar_lugares);
$("#lugares_info li").click(ocultar_lugares);


$('#fondo_lugares').hide();
$('#lugares').hide();
$('#devolucion').hide();
$('#texto_devolucion').text($('#devolucion').children('span').text());
