/*
    article.js
    Thomas Segal
    22 November 2010
*/

var tiles = ['#tile_button_one', '#tile_button_two', '#tile_button_three', '#tile_button_four'];
var positions = ['0px 0px', '0px -300px', '0px -600px', '0px -900px'];
var index = 0;
var poster_timer;

var clear_button = function()
    {
        $('#tile_button_one, #tile_button_two, #tile_button_three, #tile_button_four').removeClass('tile_button_on');
    }

var clear_stop_play = function()
    {
        $('#stop_button, #play_button').removeClass('tile_button_on');
    }

var display_tile = function(tile_id, position)
    {
        clear_button();
        $(tile_id).addClass('tile_button_on');
        $('#poster_space').hide().css('background-position', position).fadeIn(400);
    }

var display_next_tile = function()
    {
        if (index == 4)
        {
            index = 0;
        }
        display_tile(tiles[index], positions[index]);
        index += 1;
    }
    
var choose_poster = function(tile_info, position_info, index_reset)
    {
        clearInterval(poster_timer);
        display_tile(tile_info, position_info);
        index = index_reset;
        clear_stop_play();
        $('#stop_button').addClass('tile_button_on');
    }


jQuery(document).ready(function() {
    $('#poster_space').hide();
    display_next_tile();
    poster_timer = setInterval(display_next_tile, 6000);
    $('#play_button').addClass('tile_button_on');
    
    $('#play_button').click(function() {
        clearInterval(poster_timer);
        clear_stop_play();
        $(this).addClass('tile_button_on');
        $('#poster_space').hide();
        display_next_tile();
        poster_timer = setInterval(display_next_tile, 6000);
    });
    
    $('#stop_button').click(function() {
        clear_stop_play();
        $(this).addClass('tile_button_on');
        clearInterval(poster_timer);
    });
    
    $('#tile_button_one').click(function() {
        choose_poster('#tile_button_one', positions[0], 1);
        /*choose_poster('#tile_button_one', div_blocks[0], 0);*/
    });
    
    $('#tile_button_two').click(function() {
        choose_poster('#tile_button_two', positions[1], 2);
        /*choose_poster('#tile_button_two', div_blocks[1], 1);*/
    });
    
    $('#tile_button_three').click(function() {
        choose_poster('#tile_button_three', positions[2], 3);
        /*choose_poster('#tile_button_three', div_blocks[2], 2);*/
    });
    
    $('#tile_button_four').click(function() {
        choose_poster('#tile_button_four', positions[3], 0);
        /*choose_poster('#tile_button_four', div_blocks[3], 3);*/
    });

});

