var overlay_mask = {
    color: '#000',
    loadSpeed: 100,
    opacity: 0.6
};

function init_login_facebox()
{
    var is_login = $(document).getUrlParam('login');
    if (is_login === 'true'){
        if ($('#user_name').val() === undefined){
            $("#overlay").overlay({
                onBeforeLoad: function() {
                    var wrap = this.getOverlay().find(".overlay_content");
                    wrap.load('/accounts/login/');
                },
                mask: overlay_mask,
                load: true
            });
        }
    }
}

function init_overlays()
{
    $("a[rel=#overlay]").overlay({
        mask: overlay_mask,
        closeOnClick: false, // makes this modal
        onBeforeLoad: function() {
            // grab wrapper element inside content
            var wrap = this.getOverlay().find(".overlay_content");
            // load the page specified in the trigger
            wrap.load(this.getTrigger().attr("href"));
        }
    });
}

$(function() {
        
    // All links with a a rel=facebox attribute will open in a facebox
    $('a[rel*=facebox]').facebox();

    // Google Analytics event tracking plugin detects 'activity' attribute
    // and sends the activity to analytics for in-page tracking
    $('a').trackActivities();
    $('select').trackActivities({
        'events':['change'], 
        'label_callback': function() {
            return $(this).find(':selected').text();
        }
    }); 

    // There is a country selection dropdown in the footer which
    // allows for testing different localizations (e.g. currency)
    // Changing the value automatically submits the form.
    $("#id_localization_country").change(function(){
        $("#country_localization_form").submit();
    });

    $("#id_select_language").change(function(){
        $("#set_language_form").submit();
    });
 
    /* Tabs */
    // nojs can be added as a class on the navigation to turn off the tab javascript
    // for situations where we want the tab style, but regular hyperlink behaviour.
    $('ul.tab-nav:not(.nojs)').tabs('div.tab-panes > div', { history: false });
    $('#dest-tab-nav').tabs('div.dest-tab-panes > div');

    var initialization_functions = [
        ff2_inline_stack_fix,
        init_login_facebox,
        init_overlays,
        init_marketing_ads,
        init_featured_trips,
        init_main_banner,
        init_full_carousel,
        init_wir_carousel,
        init_input_default_text
    ];
    _.each(initialization_functions, function(f) {
        if(typeof(f) !== 'undefined') { 
            f(); 
        }
    });

    function show_social_buttons() {
        $("#h-social .share-loading").hide();
        $("#h-social .share").fadeIn();
    }

    // We only load the social media buttons if a user interacts with the
    // placeholders; then we load all of them.
    var social_loaded = false;
    function load_social_buttons() {
        if(social_loaded) {
            return;
        }
        social_loaded = true;
        $("#h-social .share-button").hide();
        ShareButtons.load('facebook');
        ShareButtons.load('google');
        ShareButtons.load('twitter');
        $("#h-social .share-loading").show();
        setTimeout(show_social_buttons, 1000);
    }
    // Triggered on hover OR click
    //$("#h-social .share-button").hover(load_social_buttons).click(load_social_buttons);
    load_social_buttons();

    $(document).placeholder();

    $('.flowgrid3.round-images img:first-child').round_corners({'radius':'5px'});
    $('.flowgrid4.round-images img:first-child').round_corners({'radius':'5px'});
    $('.home-page .flowgrid2 img').round_corners({'radius':'5px'});
//    $('.home-page .trips-carousel .scrollable .items div.trip img').round_corners({'radius':'5px'});
});


