$(document).ready(function() {

    // homepage slider
    try {
        // preload images before slideshow
        $('.slideshow ul li img').each(function() {
            jQuery('<img>').attr('href', $(this).href);
        });
        $('.slideshow').show();
        $('.slideshow').innerfade({
            'speed': 'normal',
            'timeout': 3000,
            'type': 'sequence',
            'animationtype': 'fade',
            'containerheight': '521px'
        });
    } catch (e) {}

    // google maps
    try {
        if ($('#map').length && GBrowserIsCompatible()) {
             var map = new GMap2(document.getElementById("map"));
            map.removeMapType(G_HYBRID_MAP);
            //map.setCenter(new GLatLng(43.591, 1.403), 14);
            map.setCenter(new GLatLng(43.591, 1.409), 14);
            map.addControl(new GLargeMapControl());
            var mapControl = new GMapTypeControl();
            map.addControl(mapControl);
            var marker = new GMarker(new GLatLng(43.5915, 1.4031));
            var html = "<b>Hippodrome de Toulouse</b> <br/>1 chemin des Courses<br/>31100 Toulouse";
            GEvent.addListener(marker, 'click', function() {
                marker.openInfoWindowHtml(html);
            });
            map.addOverlay(marker);
        }
    } catch (e) {}

    // menu button
    try {
        var timeout = 1000;
        var closeTimer = 0;
        var cancelTimer = function() {
            if (closeTimer) {
                window.clearTimeout(closeTimer);
            }
        };
        var showMenu = function() {
            cancelTimer();
            $('#content-menu').slideDown('normal');
            $('#content-video a, .ss-controls, .nav-controls, .caption').fadeOut('normal');
        };
        var hideMenu = function() {
            closeTimer = window.setTimeout(
                function() {
                    $('#content-menu').slideUp('normal');
                    $('#content-video a, .ss-controls, .nav-controls, .caption').fadeIn('normal');
                },
                timeout
            );
        };
        $('#content-nav-right-menu, #content-menu').hover(showMenu, hideMenu);
    } catch (e) {}

    // pages scrollable area
    try {
        $('.scrollpane').jScrollPane({
            'scrollbarMargin': 20,
            'showArrows':      true
        });
    } catch (e) {}

    // galleries
    if ($('#content-main-gallery').length) {

        // Initially set opacity on thumbs and add
        // additional styling for hover effect on thumbs
        var onMouseOutOpacity = 0.67;
        $('#thumbs ul.thumbs li').css('opacity', onMouseOutOpacity).hover(
            function () {
                $(this).not('.selected').fadeTo('fast', 1.0);
            }, 
            function () {
                $(this).not('.selected').fadeTo('fast', onMouseOutOpacity);
            }
        );

        // Initialize Advanced Galleriffic Gallery
        var galleryAdv = $('#gallery').galleriffic('#thumbs', {
            delay:                  2000,
            numThumbs:              12,
            preloadAhead:           10,
            enableTopPager:         false,
            enableBottomPager:      true,
            imageContainerSel:      '#slideshow',
            controlsContainerSel:   '#controls',
            captionContainerSel:    '#caption',
            loadingContainerSel:    '#loading',
            renderSSControls:       true,
            renderNavControls:      true,
            playLinkText:           'Diaporama',
            pauseLinkText:          'Pause',
            prevLinkText:           '&lt; Photo précédente',
            nextLinkText:           'Photo suivante &gt;',
            nextPageLinkText:       'Suivant &gt;',
            prevPageLinkText:       '&lt; Précédent',
            //enableHistory:          true,
            autoStart:              false,
            onChange:               function(prevIndex, nextIndex) {
                $('#thumbs ul.thumbs').children()
                    .eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
                    .eq(nextIndex).fadeTo('fast', 1.0);
            },
            onTransitionOut:        function(callback) {
                $('#caption').fadeOut('fast');
                $('#slideshow').fadeOut('fast', callback);
            },
            onTransitionIn:         function() {
                $('#slideshow, #caption').fadeIn('fast');
            },
            onPageTransitionOut:    function(callback) {
                $('#thumbs ul.thumbs').fadeOut('fast', callback);
            },
            onPageTransitionIn:     function() {
                $('#thumbs ul.thumbs').fadeIn('fast');
            }
        });
    }

    // lightboxes
    $(".colorbox").colorbox();
    $(".colorbox-inline").each(function() {
        $(this).colorbox({'inline': true, 'href': $(this).attr('href') }, function() {});
    });

    var login_form_initial = 'identifiant';
    $('#login-form #id_username, #login-form #id_password').focus(function() {
        if ($(this).val() == login_form_initial) {
            $(this).val(''); 
        }
    });
    $('#login-form #id_username, #login-form #id_password').blur(function() {
        if (!$(this).val()) {
            $(this).val(login_form_initial);
        }
    });
});
