function show_wallpaper()
{
    $('#wallpaper').stop().animate({'opacity': '1.0'}, t_switch);
}

function resize_main(is_first)
{
    if (is_first) {
        $('#main').height($(window).height() - $('#header').outerHeight() - $('#footer').outerHeight());
        $('#main').stop().animate({'opacity': '1.0'}, t_switch);
    } else {
        $('#main').css('opacity', '1.0');
        $('#main').stop().animate({'height': $(window).height() - $('#header').outerHeight() - $('#footer').outerHeight() + 'px'}, t_switch);
    }
}

function set_logo()
{
    $('.logo').mouseover(function(){
        $(this).css('cursor', 'pointer');
    });
    $('.logo').mousedown(function(){
        $(this).width('-=4px');
        $(this).height('-=4px');
    });
    $('.logo').mouseup(function(){
        $(this).width('+=4px');
        $(this).height('+=4px');
    });
    $('.logo').click(function(){
        if (is_expand) {
            $('#header').stop().animate({top: -$('#header').outerHeight() + 'px'}, t_exp);
            $('#footer').stop().animate({bottom: -$('#footer').outerHeight() + 'px'}, t_exp);

            // Hide buttons: 8 is double margin (enough since .col is overflow:hidden).
            $('#btn_l').stop().animate({marginLeft: $('#btn_l').outerWidth() + 8 + 'px'}, t_exp); 
            $('#btn_r').stop().animate({marginLeft: -$('#btn_r').outerWidth() - 8 + 'px'}, t_exp, function(){
                // Remove shadow.
                $('.shadow').css('-webkit-box-shadow', '0px 0px 0px 0px');
                $('.shadow').css('-moz-box-shadow', '0px 0px 0px 0px');
                $('.shadow').css('box-shadow', '0px 0px 0px 0px');
            });
        } else {
            $('#header').stop().animate({top: 0}, t_exp);
            $('#footer').stop().animate({bottom: 0}, t_exp);

            $('#btn_l').stop().animate({marginLeft: '0px'}, t_exp);
            $('#btn_r').stop().animate({marginLeft: '0px'}, t_exp);

            // Add shadow.
            $('.shadow').css('-webkit-box-shadow', '0px 0px 4px 0px #aaaaaa');
            $('.shadow').css('-moz-box-shadow', '0px 0px 4px 0px #aaaaaa');
            $('.shadow').css('box-shadow', '0px 0px 4px 0px #aaaaaa');
        }
        is_expand = !is_expand;

        snowStorm.toggleSnow();
    });
}

function set_slides()
{
	var slides_width = 0;
	var pos = new Array();

	$('#slides .slide').each(function(i){
		pos[i] = slides_width;
		slides_width += $(this).width();
	});

    var pos_c = 0;
    var n_pos = pos.length;

	$('#slides').width(slides_width);

    $('#btn_l').click(function(e){
		e.preventDefault();
        if (pos_c > 0) {
            pos_c -= 1;
            $('#slides').stop().animate({marginLeft: -pos[pos_c]+'px'}, t_switch);
        }
    });

    $('#btn_r').click(function(e){
		e.preventDefault();
        if (pos_c < n_pos - 1) {
            pos_c += 1;
            $('#slides').stop().animate({marginLeft: -pos[pos_c]+'px'}, t_switch);
        }
    });
}

$(document).ready(function(){
    // Whether in expanded UI.
    is_expand = false;
    // Animation time (in ms).
    t_exp = 900;
    // Slide switch time (in ms).
    t_switch = 450;

    // Show wallpaper when it's loaded.
    show_wallpaper();

    // Resize main's height to ($(window).height() - $('#header').outerHeight() - $('#footer').outerHeight()).
    resize_main(true);

    // Set resize_main as window resize event handler.
    $(window).resize(function(){
        resize_main(false);
    });

    // Set logo click event handler.
    set_logo();

    // Arrange slides.
    set_slides();

    // Set snow toggle shortcut.
    $(window).keyup(function(e){
        if (e.which == 190)
            snowStorm.toggleSnow();
    });
});

