var box_height = 388;
var el;
var old_el;
var play;
var elements = new Array();
elements[0] = '#bigpic-1';
elements[1] = '#bigpic-2';
elements[2] = '#bigpic-3';
elements[3] = '#bigpic-4';
elements[4] = '#bigpic-5';

$(document).ready( function() 
{
	$('li.bigpic').find('img').hide();
	$('a.control-item').each(function(i, selected)
	{
		if ($(this).hasClass('activ')) el = i;
	});
	
	$('a.control-item').hover(
	function()
	{
		$('div.controls').removeClass('closed');
		$('div.controls').addClass('open');
	},
	function()
	{
		$('div.controls').removeClass('open');
		$('div.controls').addClass('closed');
	});
	$('a.control-item').click(function()
	{		
		$('a.control-item').removeClass('activ');
		$(this).addClass('activ');
	});
	
	
	play_it();
	
	
	$('a.item-1').click(function()
	{
		clearTimeout(play);
		old_el = el;
		el = 0;
		play_it();
	});
	$('a.item-2').click(function()
	{
		clearTimeout(play);
		old_el = el;
		el = 1;
		play_it();
	});
	$('a.item-3').click(function()
	{
		clearTimeout(play);
		old_el = el;
		el = 2;
		play_it();
	});
	$('a.item-4').click(function()
	{
		clearTimeout(play);
		old_el = el;
		el = 3;
		play_it();
	});
	$('a.item-5').click(function()
	{
		clearTimeout(play);
		old_el = el;
		el = 4;
		play_it();
	});
});

function play_it()
{
	$(elements[old_el]).stop();
	if ( old_el != undefined ) $('li.bigpic').fadeOut( 500 );
	setTimeout('waiting()', 600);
}
function waiting()
{
	if ( old_el != 'undefined' ) $('a.item-' + (old_el + 1)).removeClass('activ');
	$('a.item-' + (el + 1) ).addClass('activ');
	if ( old_el != undefined ) 
	{
		$(elements[old_el]).css({backgroundPosition: '0 0'});
		$(elements[el]).fadeIn( 500 );
	}
	setTimeout('slide_it()', 600);
}
function slide_it()
{
	pic_height = $(elements[el] + ' > img').height();
	set = pic_height - box_height;
	$(elements[el]).css({backgroundPosition: '0 0'}); // inainte sa faca play, ne asiguram ca e pozitionata bine
	speed = set * 20; //20 in mod normal :D - ms per pixel
	$(elements[el]).animate({backgroundPosition: '(0 -' + set + 'px)'}, speed, "linear");
	clearTimeout(play);
	play = setTimeout('slide_next()', speed); // timeout ID in caz ca vrem sa-l oprim
}
function slide_next()
{
	clearTimeout(play);
	old_el = el;
	if ( el == 4 ) el = 0;
	else el++;
	play_it();
}
