var thumb_group_count=5; //number of thumbs displayed at once.  Can be externally set.
var cur_image=0;		 //index image being shown right now
var cur_thumb_group=0;	 //index of current group beign shownm
var disp_images=new Array();//image list.  contains large images, their thumsb, and popup detail html
var autoplay=false;		 //automaticall play the SS
var autoplay_timer_id=0; //timer id of autoplay
var autoplay_interval=2500;//duration between skiping slides
var thumbs_div_width=500;
var thumbs_div_height=300;
var slide_time=1000;
var slide_steps=25;
var fade_length=500;
var thumbs_vertical=true;
var preload=new Array();



var is_scrolling=true;

//add an image to the list of displayables
function AddImage(full,thumb,info,linkurl,html_large,html_thumb)
{
	var len=disp_images.length;
	disp_images[len]=new Array();
	disp_images[len]['full']=full;
	disp_images[len]['thumb']=thumb;  
	disp_images[len]['info']=info;  
	disp_images[len]['link']=linkurl;  	
	disp_images[len]['html_thumb']=html_thumb;  	
	disp_images[len]['html_large']=html_large;  	

	//preload
	PreloadImage(thumb);
	PreloadImage(full);
	
	//assign html for swapping here.
	if(!disp_images[len]['html_large'])
	{
		var html='';
		if(disp_images[len]['link'])
			html+='<a href="'+disp_images[len]['link']+'">';
		if(disp_images[len]['full'])
			html+='<img src="'+disp_images[len]['full']+'">'
		if(disp_images[len]['link'])
			html+='</a>';
		disp_images[len]['html_large']=html;
	}
}

function PreloadImage(imgsrc)
{
  	len=preload.length;
	preload[len]=new Image();
	preload[len].src=imgsrc;
  
}

//Change the display and highlighting
function ChangeImage(i_index,force)
{
  	//ignore if only safeguard call
  	if(cur_image==i_index && !force)
  		return;
  
	//remove highlighting of previous thumb
	var last_thumb=document.getElementById('image_thumb_'+cur_image);
	if(last_thumb)
		last_thumb.className='thumb';

	//who's being shown
	cur_image=i_index;
	if(cur_image>=disp_images.length)
		cur_image=0;
	if(cur_image<0)
		cur_image=disp_images.length-1;
		
	ChangeImageSet(cur_image/thumb_group_count);
		    	
	//update display
	var main_image=document.getElementById('image_main_container');
	var cur_thumb=document.getElementById('image_thumb_'+cur_image);
	var capt=document.getElementById('image_caption');

	if(cur_thumb)
		cur_thumb.className='selectedThumb';

	FadeOut();

		
}



function FadeOut()
{  
	FadeIn();
//	jQuery('#image_main_container').fadeOut(fade_length,function(){FadeIn();});
}

function FadeIn()
{
	var main_image=document.getElementById('image_main_container');
	var cur_thumb=document.getElementById('image_thumb_'+cur_image);
	var capt=document.getElementById('image_caption');
	
	jQuery('#image_main_container').html(disp_images[cur_image]['html_large']);	

	if(capt)
		capt.innerHTML=disp_images[cur_image]['info'];	


//	jQuery('#image_main_container').fadeIn(fade_length);  
}


function NextImage()
{
	ChangeImage(cur_image+1);
}

function PrevImage()
{
	ChangeImage(cur_image-1);
}

function ChangeImageSet(i_index,force)
{   	
  	var last_group=cur_thumb_group;
  	
 	//round, and ignore if only safe guard call 
  	i_index=Math.round(i_index-.5);
  	if(cur_thumb_group==i_index && !force)
  		return;

	  
	//who's being shown
	cur_thumb_group=i_index;
	if(cur_thumb_group>=disp_images.length/thumb_group_count)
		cur_thumb_group=0;
	if(cur_thumb_group<0)
		cur_thumb_group=(disp_images.length-1)/thumb_group_count;	



	MoveToImageSet(cur_thumb_group);

/*
	//safe guard call to change to current image - first in group if moving foreward,last if moving back
	if(cur_thumb_group>last_group || (cur_thumb_group==0 && Math.round((disp_images.length/thumb_group_count)-.5)==last_group))
		ChangeImage(cur_thumb_group*thumb_group_count);
	else
	{
	  	var last=(cur_thumb_group+1)*thumb_group_count-1;
	  	if(last>=disp_images.length)
	  		last=disp_images.length-1;
		ChangeImage(last);
	}
*/	
}

function NextImageSet()
{
  	var idx=((cur_thumb_group+1)*thumb_group_count);
  	if(idx>disp_images.length)	
  		idx=0;
  	
	ChangeImage(idx);  
}

function PrevImageSet()
{
  	var idx=(cur_thumb_group*thumb_group_count)-1;
  	if(idx<0)	
  		idx=disp_images.length-1;
  	
	ChangeImage(idx);  
}

function MoveToImageSet(i_index)
{
  	//record wher we are (going)).
	cur_thumb_group=i_index;	
  
	//animate
	var el=document.getElementById('image_thumb_container');
	if(el)
	{
		//set the scrolling flag
		is_scrolling=true;

		//initialize; we'll ignore the dimension we don't care about
		var new_top=null;
		var new_left=null;
		
		//calculate new position for current orintation
		if(thumbs_vertical)
		{
			new_top=0-(thumbs_div_height*cur_thumb_group);	  
	//		if(new_top<0-el.offsetHeight)	new_top=-1;		
			if (new_top==0)					new_top=-1;		  
		}
		else
		{
			new_left=0-(thumbs_div_width*cur_thumb_group);	  
	//		if(new_left<0-el.offsetWidth)	new_left=-1;		
			if (new_left==0)				new_left=-1;		  
		}

		//control visibility
		var vis=SetControlsVisible();vis=true;
		ShowSetControls(false);

		//begin the animation
//		jQuery('#image_thumb_container').animate({top: new_top,opacity:1}, slide_time,"",function() {ShowSetControls(vis);});
		new Rico.Effect.Position('image_thumb_container', new_left, new_top, slide_time, slide_steps,{complete:function() {ShowSetControls(vis);}});	
	}  
}



function MoveNextImageSet()
{
	MoveToImageSet(cur_thumb_group+1);  
}

function MovePrevImageSet()
{
	MoveToImageSet(cur_thumb_group-1);  
}

function SetImageAutoPlay(on,donotskip)
{
	//start/stop the timer
	autoplay=on;
	if(!autoplay)
		clearInterval(autoplay_timer_id);	  
	else
		autoplay_timer_id=setInterval("NextImage()",autoplay_interval);	  

	//update display
	var control=document.getElementById('autoplay_control');
	if(control)
	{
		var html='';
		if(!autoplay)
		{
			html+="<a href='#' onclick=\"SetImageAutoPlay(true);return false;\">";
			html+="<img alt='Play Photo Slideshow' title='Play Photo Slideshow' name='play' src='/images/play.gif' border='0'></a>&nbsp;&nbsp;";
		}
		else
		{
		  	if(!donotskip)
			  	NextImage();
			html+="<a href='#' onclick=\"SetImageAutoPlay(false);return false;\">";
			html+="<img alt='Stop Photo Slideshow' title='Stop Photo Slideshow' name='stop' src='/images/stop.gif' border='0'></a>&nbsp;&nbsp;";		  
		}
		control.innerHTML=html;
	}
}

function SetThumbGroupCount(cnt)
{
  	thumb_group_count=cnt;
}

function SetDelay(ms)
{
  	autoplay_interval=ms;
}

function SetThumbsDivWidth(px)
{
  	thumbs_div_width=px;
}

function SetThumbsVertical(vert)
{
  	thumbs_vertical=vert;
}


function SetSlide(steps,ms)
{
	slide_time=ms;
	slide_steps=steps;  
}

function ShowSetControls(visible)
{
	var prevcontrol=document.getElementById('prevset_control');
	var nextcontrol=document.getElementById('nextset_control');
	if(prevcontrol && nextcontrol)
	{
		var vis=visible?'visible':'hidden';
		prevcontrol.style.visibility=vis;	
		nextcontrol.style.visibility=vis;	
	}
	
	if(visible)
		is_scrolling=false;
}

function ShowImageControls(visible)
{
	var imagecontrols=document.getElementById('image_controls');
	if(imagecontrols)
	{
		var vis=visible?'visible':'hidden';
		imagecontrols.style.visibility=vis;	
	}
}

function SetControlsVisible()
{
	var prevcontrol=document.getElementById('prevset_control');
	var nextcontrol=document.getElementById('nextset_control');
	if(prevcontrol && nextcontrol)
	{
		if(prevcontrol.style.visibility=='visible')return true;	
		if(nextcontrol.style.visibility=='visible')return true;	
	}
	return false;
}

function FinishChangeImageSet(show)
{
	ShowSetControls(show);  
}

function IsScrolling()
{
	return is_scrolling;  
}