$(function() {
    $('.photos a').lightBox();
});

/**
 * Project rotator on the homepage.
 */

var currBlock = 0;
var stopSeq = false;

$(document).ready(function()
{ 
	$('#header').cycle({
		fx      : 'fade',
		speed   : 500,
		timeout : 5000,
		sync    : 1,
	});    
	$('#header').cycle('pause'); // Pause the jQuery cycle to let our custom cycle take over.
	
	frameAdvance(); // Start the custom cycle.
    
    $('.headerProject').mouseover(function()
    {
        stopSeq = true;
        
        currBlock = getIndex(this.id);
        onBlock(currBlock);        
        $('#header').cycle(currBlock - 1);
    });
    
    $('#blockArrow').mouseover(function()   { stopSeq = true;  });    
    $('#header').mouseover(function()       { stopSeq = true;  });
    $('.fullContent').mouseover(function()       { stopSeq = true;  });
    
    $('.headerProject').mouseout(function() { stopSeq = false; });    
    $('#header').mouseout(function()        { stopSeq = false; });
    $('#blockArrow').mouseout(function()    { stopSeq = false; });    
    
    $('.headerProject').click(function()
    {
        stopSeq = true;
        currBlock = getIndex(this.id);
        $('#fc' + currBlock).slideDown('fast');
    });        
    $('.fullContent').mouseout(function()
    { 
        stopSeq = false;
        $('#fc' + currBlock).slideUp('fast');
    });        
});

function frameAdvance()
{
    // Advance only if the mouse is not somewhere on the header.
    if(!stopSeq)
    {
        currBlock++;
        if(currBlock > 3)
            currBlock = 1;
            
        $('#header').cycle(currBlock - 1);    
        onBlock(currBlock);     
    }     
    
    setTimeout('frameAdvance()', 5000);
}

function onBlock(index)
{
    $('#headerBlock' + index + ' p.title').css('color', '#000000');
    $('#headerBlock' + index + ' p.main').css('color', '#FFFFFF');
    $('#headerBlock' + index + ' a').css('color', '#FFFFFF');
    $('#headerBlock' + (index + 1)).addClass('withShade');  
    $('#blockArrow').css('top', 59 + ((index - 1) * 78) +'px'); 
    
    // Turn all project blocks off, except this one.
    for(var i = 1; i < 4; i++)
    {
        if(i != index)
            offBlock(i);
    } 
}

function offBlock(index)
{
    $('#headerBlock' + index + ' p.title').css('color', '#7B5121');
    $('#headerBlock' + index + ' p.main').css('color', '#E9C191');    
    $('#headerBlock' + index + ' a').css('color', '#E9C191');
    $('#headerBlock' + (index + 1)).removeClass('withShade');        
}

function getIndex(str)
{
    return parseInt(str.charAt(str.length - 1));
}
