var currentTab = 0;

$(document).ready(function ()
{
    $('#img').Slides({
        images : [
            BASE_URL + '/images/1.jpg',
            BASE_URL + '/images/2.jpg',
            BASE_URL + '/images/3.jpg',
            BASE_URL + '/images/4.jpg',
            BASE_URL + '/images/5.jpg',
            BASE_URL + '/images/6.jpg',
            BASE_URL + '/images/7.jpg'],
        pause : 5000,
        fade : 1000,
        wait : 0
    });

    $('#background').at_intervals(function() {
        var topRand = Math.floor(Math.random() * 500);
        var leftRand = Math.floor(Math.random() * 450);
        $('#slideshow').animate({top: -topRand + 'px', left: -leftRand + 'px'}, 10000);
    });

    $('#menu a, .sitemap').each(function (i, e) {
        $(e).attr('href', 'javascript:load(' + i + ', "' + $(e).attr('href') + '")');
        $(e).attr('id', 'menu' + i);
    });

});

function sortByContinent()
{
    $.ajax({
        type: 'POST',
        url: BASE_URL + '/index/filter/',
        data: 'continent_id=' + escape($('#continent_id').val()),
        success: function(msg) {
            $('#destination_id').html(msg);
            updateProduits();
            updateDescription();
        }
    });
}

function load(index, url)
{
    $('#menu a').addClass('off');
    $('#menu' + index).removeClass('off');
    $('#menu' + index).addClass('on');
    $('#col_left_txt').hide();
    $.ajax({
        type: 'POST',
        url: url,
        data: 'isXmlHttpRequest=true',
        success: function(msg){
            if (url.indexOf('liens.html') < 0) {
                $('#col_left_txt').html(msg);
                $('#col_left_txt').fadeIn("slow");
                if ($('#col_right_txt').html().indexOf('bons plans') == -1) {
                    loadBonsPlans(0);
                }
            } else {
                $('#col_left_txt').html('<iframe name="_target" style="width:100%;height:100%;border:0"></iframe>');
                $('#col_left_txt').fadeIn("slow");
                $('#col_right_txt').html(msg);
                $('#col_right_txt').fadeIn("slow");
            }
        }
    });
    currentTab = index;
}

function updateProduits()
{
    $.ajax({
        type: 'POST',
        url: BASE_URL + '/produits.html',
        data: 'destination_id=' + escape($('#destination_id').val()),
        success: function(msg){
            $('#produit_id').html(msg);
        }
    });
}

function loadBonsPlans(id)
{
    $('#col_right_txt').hide();
    $.ajax({
        type: 'POST',
        url: BASE_URL + '/bonsplans.html',
        data: 'bp_id=' + id,
        success: function(msg) {
            $('#col_right_txt').html(msg);
            $('#col_right_txt').fadeIn("slow");
        }
    });
}

function updateDescription()
{
    $.ajax({
        type: 'POST',
        url: BASE_URL + '/destination.html',
        data: 'destination_id=' + escape($('#destination_id').val()),
        success: function(msg) {
            $('#description').html(msg);
        }
    });
}

function downloadPDF()
{
    window.open(BASE_URL + $('#produit_id').val());
}

function toggleBg(e)
{
    alert(e);
}
