var bookshelfs=new Array();
var bookshelf_current='';
var bookshelf_animating=new Array();
var bookshelf_animate_time=1000;
var bookshelf_autoscroll=0;
var bookshelf_autoscroll_time=3500;
var bookshelf_autoscroll_start_time=6000;
var bookshelf_autoscroll_restart_time=5000;
var bookshelf_autoscroll_can_happen=0;
var bookshelf_continuous=new Array();

function bookshelf_show(context,item_id,continuous)
{
  	if(bookshelf_animating[context])
  		return;

	var linkname=item_id+'_link';

	ShowOneOf(item_id,'bookshelves');
	bookshelf_current=item_id;
	bookshelf_continuous[item_id]=continuous;
		
	jQuery('A.bookshelf_toggle').removeClass('bookshelf_selected');
	jQuery('#'+linkname).addClass('bookshelf_selected');

	bookshelf_shownav(context,item_id,true);
//	bookshelf_scrollto(context,item_id,0);

}

jQuery(document).ready(function() 
{
  	jQuery('DIV.bookshelf_item').hover(
	  function() {
	    var item=jQuery('DIV.bookshelf_popup_item',this).get(0);
	    if(item)
	    {
		 	jQuery('DIV.bookshelf_item').css('z-index',1);
		 	jQuery(this).css('z-index',1000);
		    bookshelf_show_popup(item.id,true);
		}
	  },
	  function() {
	    var item=jQuery('DIV.bookshelf_popup_item',this).get(0);
	    if(item)
		    bookshelf_show_popup(item.id,false);
	  }
	);

});


function bookshelf_show_popup(context,popup_item_id,show)
{
  if(bookshelf_animating[context])
		return;

	if(show)
		ShowOneOf(popup_item_id,'bookshelvespopups','fade');
	else
		ShowOneOf('','bookshelvespopups','fade');
}

function bookshelf_scroll(context,item_id,prev)
{
  	if(bookshelf_animating[context])
  		return;
  
	var move_width=760;
	var min_size=100;
	if(context!='DIV.bookshelf')
	{
		move_width=468;
		min_size=50;	  
	}
	
	var the_item=jQuery('#'+item_id);
	var the_item_left=parseInt(the_item.css('left'));
	var the_item_width=the_item.width();
	if(prev && (the_item_left<0))
		bookshelf_scrollto(context,item_id,the_item_left+move_width);
	else if(prev && bookshelf_continuous[item_id])
	{
		bookshelf_moveto(context,item_id,Math.round((move_width-the_item_width)/move_width)*move_width);
		bookshelf_scroll(context,item_id,prev);
	}
	if(!prev && (((the_item_left+the_item_width)-move_width)>min_size))
		bookshelf_scrollto(context,item_id,the_item_left-move_width);
	else if(!prev && bookshelf_continuous[item_id])
	{
		bookshelf_moveto(context,item_id,0);
		bookshelf_scroll(context,item_id,prev);
	}
}

function bookshelf_scrollto(context,item_id,newleft)
{
	var the_item=jQuery('#'+item_id);

	bookshelf_shownav(context,item_id,false);
	the_item.animate({left:newleft},bookshelf_animate_time,'linear',function(){bookshelf_shownav(context,item_id,true)});   
}

function bookshelf_moveto(context,item_id,newleft)
{
	var the_item=jQuery('#'+item_id);
	the_item.css({left:newleft});   
}

function bookshelf_shownav(context,item_id,allow)
{
  
	var move_width=760;
	var min_size=100;
	if(context!='DIV.bookshelf')
	{
		move_width=468;
		min_size=50;	  
	}
	
	var the_item=jQuery('#'+item_id);
	var the_item_left=parseInt(the_item.css('left'));
	var the_item_width=the_item.width();

	var link_next=jQuery(context+' A.bookshelf_next');
	var link_prev=jQuery(context+' A.bookshelf_previous');

	if(the_item_left<0)
		link_prev.removeClass('bookshelf_disabled');	
	else
		link_prev.addClass('bookshelf_disabled');
	if(((the_item_left+the_item_width)-move_width)>min_size)
		link_next.removeClass('bookshelf_disabled');	
	else
		link_next.addClass('bookshelf_disabled');

	if(bookshelf_continuous[item_id])
	{
		link_next.removeClass('bookshelf_disabled');	
		link_prev.removeClass('bookshelf_disabled');		  
	}

		
	bookshelf_animating[context]=!allow;
}


function bookshelf_do_autoscroll(context,item_id)
{
  	if(bookshelf_animating[context])
  		return;
  
	var move_width=760;
	var min_size=100;
	
	var the_item=jQuery('#'+item_id);
	var the_item_left=parseInt(the_item.css('left'));
	var the_item_width=the_item.width();
	if((((the_item_left+the_item_width)-move_width)>min_size))
		bookshelf_scrollto(context,item_id,the_item_left-move_width);
	else if(bookshelf_continuous[item_id])
	{
		bookshelf_moveto(context,item_id,0);
		bookshelf_scroll(context,item_id,false);
	}
	else
		bookshelf_scrollto(context,item_id,0);
		
	//and continue....	
	if(bookshelf_autoscroll)//if timeout not cleared by something lelse..
		bookshelf_autoscroll_timer(context,bookshelf_autoscroll_time);		
}

function bookshelf_start_autoscroll(context)
{	
  	duration=bookshelf_autoscroll_start_time;
	bookshelf_autoscroll=window.setTimeout(function(){bookshelf_autoscroll_can_happen=true;bookshelf_do_autoscroll(context,bookshelf_current);},duration);	    
	
  	jQuery(context).hover(
	  function() {if(bookshelf_autoscroll_can_happen){bookshelf_end_autoscroll(context);}},
	  function() {bookshelf_autoscroll_timer(context,bookshelf_autoscroll_time);}
	);	
}

function bookshelf_end_autoscroll(context)
{
	if(!bookshelf_autoscroll_can_happen)
		return;

	window.clearTimeout(bookshelf_autoscroll);
	bookshelf_autoscroll=0;
}

function bookshelf_pause_autoscroll(context)
{
	bookshelf_end_autoscroll(context);
	bookshelf_autoscroll_timer(context,bookshelf_autoscroll_restart_time);
}

function bookshelf_autoscroll_timer(context,duration)
{
	if(!bookshelf_autoscroll_can_happen)
		return;

	window.clearTimeout(bookshelf_autoscroll);
	bookshelf_autoscroll=window.setTimeout(function(){bookshelf_do_autoscroll(context,bookshelf_current)},duration);	    
}
