$(function()
{
    var selected = 0;
    var cur_cont = $('.image-0');

    var index = 0;

    function advance()
    {
	showImage(index);
	index ++;
	if (index == $('.hp-image').size()){
	    index = 0;	
	}
    }

    window.activateHP = function(){
	if($('.hp-image').size() == 1) return;
	setInterval(advance, 6000);
    };

    window.activateProjects = function(data){
        $('#projects img').click(function() {
	    var index = $('#projects img').index(this);
	    showImage(index);
	    var projectData = data[index];
	    var title = projectData[1].toUpperCase();
	    if (title)
	    {
		sIFR.callbacks[0].replaceText(title);
		sIFR.callbacks[1].replaceText(projectData[2].toUpperCase());
	    }
	});
    };



    function showImage(index) {
	var width = $('#tiles').parent().width();
	if (selected == index) return;


	var cont = $('.image-' + index);
	var old_cont = cur_cont;

	$('#tiles').css('margin-left', 0).css('width', 0);
	$('#tiles div').css('margin-left', 0);


	$('#tiles').animate({width : width + 'px'}, 500, 'linear', function(){
	    
	    
	    old_cont.css('z-index', 0);
	    cont.css('z-index', 1);
	    if (width == 448)
	    {
		cont.width(6);
	    }
	    
	    var marginLeft = 0;
	    var t = setInterval(function(){
		
		cont.width(26 + cont.width());
		marginLeft += 26;

		$('#tiles').width(width - marginLeft);
		$('#tiles').css('margin-left', marginLeft + 'px');
		$('#tiles div').css('margin-left', '-' + marginLeft + 'px');

		if(cont.width() == width)
		{
		    
		    
		    $('#tiles').width(0);
		    old_cont.width(0)
		    clearInterval(t);
		}
	    }, 80);
	});

	cur_cont = cont;
	selected = index;
    };

});


$(function(){
    $('#projects .project-item a').mouseover(function(){
	var rel = $(this).attr('rel');
	var img = $('#slideshow .' + rel);
	if (img.size())
	{
		$('#slideshow .hp-image').hide();
		img.show();
		img.width('100%');
	}
    });

});
