jQuery(function($){
    var content = $('#content');

    $(document).bind('keyup', function(e){
        if (e.keyCode == 83){
            var color = '#ebebeb';
            var r = 10;
            $('.name', content).css({
                'text-shadow' : '1px 1px '+r+'px '+color+', -1px -1px '+r+'px '+color+', 1px -1px '+r+'px '+color+', -1px 1px '+r+'px '+color
            });
        }
    });

    var speed = Mano.settings.speed*1000;

    $(window).bind('load', function(){
        $('.name, .contact, .description, .about_image, .bg_control', content).css('opacity', 0).show();
        fd($('.name', content), speed);
        setTimeout(function(){
            fd($('.contact', content), speed);
        },speed);
        setTimeout(function(){
            fd($('.description, .about_image, .bg_control', content), speed*2);
            bgRotate();
        }, speed*2);
    });

    function fd(element, duration, callback){
        callback = callback || function(){};
        element.animate({'opacity':1}, duration, false, callback);
    }

    var bg = $('#bg');
    Mano.bg = []
    for(i in Mano.bgImages){
        Mano.bg[i] = new Image();
        Mano.bg[i].src = 'images/'+Mano.bgImages[i];
        Mano.bg[i].alt = "Mano Architects";
    }

	function bgRotate() {
        window.bgChange = setTimeout(arguments.callee, Mano.settings.bgChange*1000);

        if ($('img', bg).length == 0){
            bg.html(Mano.bg[0]).find('img').fadeIn(speed*2);
            bg.data('current', 0);
            return;
        }
        
        var cur_id = bg.data('current');
        var next = Mano.bgImages[cur_id+1]?cur_id+1:0;

        bg.data('current', next);

        var cur_image = $('img:first', bg);

        cur_image
            .css('z-index', 2)
            .after(Mano.bg[next])
            .fadeOut(speed*2, function(){$(this).remove();})
            .next().fadeIn(speed*2);
	}

    $('.bg_control').children().bind('click', function(){
        switch($(this).attr('class')) {
            case 'stop':
                clearTimeout(window.bgChange);
                window.bgChange = false;
                break;
            case 'play':
                if (window.bgChange === false)
                    bgRotate();
                break;
        }
       
    });
	
});

