var entries;
var current_id;
var curent_animation;
var max_entries;
var offset_x = 450;
var cycle_id;
var cycle_speed = 5000;

function init_slideshow()
{
	var parent = document.getElementById("slide_content");
    var li_num = parent.getElementsByTagName('li').length;
	
	var entry;
	var target;
	
	dojo.require("dojo.fx");
	
	current_id	= 0;	
	entries 	= new Array();	
	
	for(var i=1; i<=li_num; i++)
	{
		entry 			= new Object();
		entry.moveLeft	= moveLeft;
		entry.moveRight	= moveRight;
		
		entry.id 		= i-1;		
		target 			= document.getElementById('entry_' + i);	
		
		entry.target 	= target;			
		entries.push( entry );
	}
	
	max_entries = entries.length;
	displayById( Math.floor( max_entries * Math.random() ) );
	
	cycle_id = setInterval("doCycle()", cycle_speed);
}

function displayById( id )
{
	var pos_id;
	var pos_x;
	var on_end;
	
	this.current_id = id;
	
	
	for(var i=0; i<entries.length; i++)
	{
		target = entries[i].target;
		//
		pos_id 	= (entries[i].id + current_id) % max_entries;	
		pos_x	= (offset_x * pos_id);
		//
		if(entries[i].id + current_id == max_entries -1 ) pos_x = -offset_x;		
		
		target.style.visibility = "hidden";
		
		slide = dojo.fx.slideTo({node: target, delay: 0, duration: 1, left: pos_x, top: 51, onEnd:  function(){ showAll() }   });
		slide.play();
	}
}

function showAll()
{
	var target;
	
	for(var i=0; i<entries.length; i++)
	{
		target = entries[i].target;
		target.style.visibility = "visible";
	}
}


function clearCycle()
{
	clearInterval( cycle_id );
	cycle_id = setInterval("doCycle()", cycle_speed);
}


function doCycle()
{
	doAnimation(1);
}


function moveLeft(index)
{
	var pos_id;
	var pos_x;
	var slide
	
	
	pos_id 	= (this.id + current_id) % max_entries;	
	pos_x	= (offset_x * pos_id);
		
	if( this.target.style.left == "-450px")
	{
		this.target.style.left = (max_entries * offset_x) + "px";
	}
	
	
	if(this.id + current_id == max_entries -1 ) pos_x = -offset_x;

	curent_animation = true;
	slide = dojo.fx.slideTo({node: this.target, delay: 100, duration: 500, left: pos_x, top: 51, onEnd: function(){ curent_animation = null } });
	slide.play();
}


function moveRight( index )
{
	var pos_id;
	var pos_x;
	var slide
	
	pos_id 	= (this.id + current_id) % max_entries;
	pos_x	= (450 * pos_id);
			
	
	if(pos_id == 0) 
	{
		this.target.style.left	= -offset_x+"px";		
	}	
	
	curent_animation = true;	
	slide = dojo.fx.slideTo({node: this.target, delay: 100, duration: 500, left: pos_x, top: 51, onEnd: function(){ curent_animation = null } });
	slide.play();
}





function doAnimation(index) 
{
	if(curent_animation)
		return;
	
	
	current_id = (current_id + index) % entries.length;
	
	if(current_id < 0) 
		current_id = entries.length-1;	
		
	clearCycle();
	
	
	if(index==-1)
	{
		for(var i=0; i<entries.length; i++)
		{
			entries[i].moveLeft( index );
		}
	}
	else if(index==1)
	{
		for(var i=0; i<entries.length; i++)
		{
			entries[i].moveRight( index );
		}
	}
	
	/*

	
	
	
	
	if(index == -1)
	{
		currentAnimation = slide_left;
	}
	else if(index == 1)
	{
		currentAnimation = slide_right;
	}
	
	 if (currentAnimation) 
	 {
     	//dojo.connect(currentAnimation, "onEnd", function() { currentAnimation = null; });
		currentAnimation.play();
	}
	*/
}

