//initially hide the body(for page fadein)
document.body.style.display = 'none';
jQuery(function() {
    $ = jQuery;
    //fade in the body
    $("body").fadeIn(2000);

    //glow: repeatedly wait 3s then fade in 1,5s then fade out 1,5s
    if ($.support.boxShadow) {
        $('.glow').css('boxShadow','0px 0px 0px #fff').css('boxShadowBlur', '30px');

        var bgcolor = '#f0bee5';
        var blurcolor = '#d95ebe';

        function wait_then_pulse_in() {
            setTimeout(pulse_in, 3000);
        }

        function pulse_out() {
            $('.glow').animate({boxShadowColor : '#fff', backgroundColor: '#fff'}, 1500, 'linear', wait_then_pulse_in);
        }

        function pulse_in() {
            $('.glow').animate({boxShadowColor : blurcolor, backgroundColor: bgcolor}, 1500, 'linear', pulse_out);
        }
        
        wait_then_pulse_in();
    }
    
    //initialize gallery
    new Adf_gallery();
    
    //ajax page loads using jquery.history and page transitions
    function render(data) {
        $('#body').replaceWith(data);
        $('#body').hide();
        if ($('#body').hasClass("fullwidth")) {
            new Adf_gallery();
        }
        $("#body").fadeIn(1000);
    }
    
    function fire_ajax(url) {
        $.get(url, function(data) {
            $("#body").fadeOut(500, function() {
                render(data);
            });
        });
    }
    
    var origContent = "";

    function load_content(hash) {
        if(hash != "") {
            if(origContent == "") {
                origContent = $('#bodywrapper').html();
            }
            fire_ajax(hash);
        } else if(origContent != "") {
            $("#body").fadeOut(500,function() {
                render(origContent);
            });
        }
    }
    $.history.init(load_content);
    
    $('#nav_menu-2 a').not('.external').click(function(e) {
        if ($(this).attr('target')) return;
        var url = $(this).attr('href');
        url = url.replace(/^.*#/, '');
        $.history.load(url);
        return false;
    });
});

