$(document).ready(function(){

    /* init shadowbox */
    Shadowbox.init({
        overlayColor: '#fff',
        overlayOpacity: 0.9,
        resizeDuration: 0.5,
        players: ["img", "flv", "swf"]
    });

    /* add zoom picto to shadowbox links */
    $('a.sbx').prepend('<span class="zoom png_bg"></span>');

    $('a.contact, a.mail').addClass('png_bg');
    
    $('#bookmark').jBrowserBookmark();

});

/* shadowbox */
$('a.sbx').live('click',function() {
    Shadowbox.open({
        //players: ['img', 'flv', 'swf'],
        player: 'img',
        title: $(this).attr('title'),
        content: $(this).attr('href'),
        autoplayMovies: true
    });
    return false;
});

$('a.arrow_blue').live('mouseover', function(){
    $(this).stop().animate({ backgroundPosition : '3px 50%' },100);
}).live('mouseout', function(){
   $(this).stop().animate({ backgroundPosition : '0px 50%' },100);
});

$('.arrow_blue a').live('mouseover', function(){
    $(this).parent().stop().animate({ backgroundPosition : '3px 50%' },100);
}).live('mouseout', function(){
   $(this).parent().stop().animate({ backgroundPosition : '0px 50%' },100);
});


/* document ready */
jQuery(function($) {

    /* rajout du [ + ] pour les blocks collpase */
    $('.collapse h2').append(' <span class="show_more">[+]</span>').css({ cursor: 'pointer' }).siblings().hide();

    /* rajout de la classe sbx auto */
    $('img').parent('a[href$="jpg"]').addClass('sbx').prepend('<span class="zoom png_bg"></span>');
    $('img').parent('a[href$="png"]').addClass('sbx').prepend('<span class="zoom png_bg"></span>');
    $('img').parent('a[href$="gif"]').addClass('sbx').prepend('<span class="zoom png_bg"></span>');

    /* search input */
    $('.wipeOnFocus').focus(function() {
        var $input = $(this);
        if($input.val()==this.defaultValue) $input.val('');
    }).blur(function() {
        var $input = $(this);
        if($input.val()=='') $input.val(this.defaultValue);
    });
    
    /* Actions */
    $('.actions').find('.print').click(function() {
        var $iframe = $('<iframe />').css({
            'opacity': '1%',//visibility: hidden doesn't work in IE
            'width': '1px',
            'height': '1px',
            'position': 'absolute',
            'top': '0',
            'left': '0',
            'border': '0'
        });

        $('body').append($iframe);

        $iframe.load(function()
        {
            var iWindow = $(this)[0].contentWindow;

            iWindow.focus();//without focus it is the parent that gets printed (in IE)
            iWindow.print();

        }).attr('src', $(this).attr('href'));

        return false;
    });

//    $('.actions').find('.share').click(function() {
//        return false;
//    });

});


/* Share icons peek-a-boo */
jQuery(function($) {
    $('.buttons .share .icon a').mouseover(function() {
        $(this).siblings('img').stop().animate({ bottom: '0px' },500,'easeOutBounce');
    }).mouseout(function() {
        var button = $(this);
        setTimeout(function() {
            button.siblings('img').stop().animate({ bottom: '-20px' },750,'easeOutBounce');
        },100);
    });
});


jQuery(function($) {

    /* site map */
    footerBlock(
        '<h3><strong>ATR site</strong> map</h3>', /* titre */
        'siteMap', /* id */
        $('.container'), /* body / parent */
        $('footer .site-map'), /* bouton déclencheur */
        $('nav > ul.menu') /* contenu */
    );

    /* quick links */
    footerBlock(
        '<h3><strong>Quick</strong> links</h3>',
        'quickLinks',
        $('.container'),
        $('footer .quick-links'),
        $('footer #quick-links-content').remove()//Pour que le clone ne duplique pas l'id
    );

});

function footerBlock(title, idMap, $body, $button, $clone) {

    var $block = $('#' + idMap),
        bottom = 80,
        timer,
        hideTimeout = 500,
        hideMap = function() {

            $block.stop().fadeTo(0,1).animate({ opacity: 0, bottom: bottom },function() {
               $block.hide();
            });

        };

    $button.live('mouseover',function() {
    
        if($block.length === 0) {
            $block = $('<div id="' + idMap + '" class="footerBlock grad_shadow_gris">' + title + '</div>');

            $block.hide().append($clone.clone().css({'display' : 'block'})).css({ 'bottom': bottom });

            var isIE = $.browser.msie && /^[678]/.test($.browser.version);
            if(isIE){
                $block.css({
                    border: '1px solid #dfdfdf',
                    filter: 'progid:DXImageTransform.Microsoft.gradient( startColorstr="#FFFFFF", endColorstr="#E5E5E5",GradientType=0 )'
                });
            }

            $body.append($block);
        }

        clearTimeout(timer);
        $button.addClass('active');
        $block.stop().fadeTo(0,0).animate({ 'opacity': 1, 'bottom': (bottom + 15) });

    }).live('mouseout',function() {

        timer = setTimeout( hideMap ,hideTimeout);
        $button.removeClass('active');

        $block.mouseenter(function() {
            clearTimeout(timer);
        }).mouseleave(function() {
            clearTimeout(timer);
            timer = setTimeout( hideMap ,hideTimeout);
        });

    });

}

$('a.quick-links').live('click',function(){
    return false;
});

$('a.site-map').live('click',function(){
    return false;
});

(function($) {

    var $box = $('<div class="regardsBox grad_shadow_gris" />');
    
    $.fn.regardsBox = function(topPos) {
        var boxWidth,
        boxHeight,
        $body = $('body'),
        $html = $body.parent(),
        $window = $(window),
        scrollTopOffset = $body[0].scrollTop || $html[0].scrollTop;

        $box.stop().remove().children().remove();
        
        $box.append($('<a class="close png_bg" href="#close" title="Close"><span class="hidden">close</span></a>').click(function() {
            $box.remove();
            return false;
        }));
    
        $box.css({
            position: 'absolute',
            visibility: 'hidden',
            left: '50%',
            marginLeft: 0
        });

        var isIE = $.browser.msie && /^[678]/.test($.browser.version);
        if(isIE){
            $box.css({
                border: '1px solid #dfdfdf',
                filter: 'progid:DXImageTransform.Microsoft.gradient( startColorstr="#FFFFFF", endColorstr="#E5E5E5",GradientType=0 )'
            });
        }

        $box.append(this);
    
        $body.append($box);
    
        boxWidth = $box.outerWidth();
        boxHeight = $box.outerHeight();
        
        if (typeof(topPos) == 'undefined')
            topPos = parseInt(($window.height() / 2) - (boxHeight / 2) + scrollTopOffset);

        $box.css({
            visibility: 'visible',
            marginLeft: (parseInt(boxWidth / 2) * -1) + 'px',
            top: topPos + 'px'
        });
    
        return $box;
    };

})(jQuery);

/*******************************  COLLAPSING BLOCKS  ************************************/

$('.collapse h2.heading').live('click',function(){
   $('.collapse h2.heading').removeClass('active').siblings().hide(10);
   $(this).addClass('active').siblings().show(100);
   $('.collapse h2.heading').children('span.show_more').html('[+]');
   $(this).children('span.show_more').html('[ - ]');   
});

$('.collapse h2.heading.active').live('click', function(){
   $(this).children('span.show_more').html('[+]');
   $(this).removeClass('active').siblings().hide(10);
  
});

