var arr;
var objs;
var timer;
var timer_on=false;
var right_margin = 4;
var scroll_direction;		
var stop_moving=false;
var ScrollingSpeed;
ScrollingSpeed = 20;
		
var avail_width;

function AddEventHandler(el,eventMainName,observer)
{
	if (el.addEventListener) 
	{     
   			el.addEventListener(eventMainName, observer, false);
 			} 
	else if(el.attachEvent) 
	{
  			el.attachEvent('on' + eventMainName, observer);
 			}
}			
		
function ShowCaption(srcElem)
{
	var msg = srcElem.parentNode.getAttribute('title');			
	var el = document.getElementById("ImgCaption");	
	var msgCell = document.getElementById("captionCell");
	msgCell.innerHTML = msg;
	el.style.top = (parseInt(srcElem.parentNode.style.top,10) - parseInt(srcElem.parentNode.offsetHeight,10)) + "px";
	el.style.left = parseInt(srcElem.parentNode.style.left,10) + "px";		
	el.style.display = "";	
	
}

function HideCaption()
{
	var el = document.getElementById("ImgCaption");
	el.style.display="none";
}

function StopScrolling(e)
{
	e = e || event; 
	var target =  e.target || e.srcElement;
	//ShowCaption(target);	
	
	if(timer_on==true)
	{
		clearTimeout(timer);
		timer_on=false;
	}
}
			
function ReStartScrolling(direction)
{
	if(timer_on==true)
	{
		clearTimeout(timer);
		timer_on=false;
	}
	scroll_direction=direction;
	FixPositions();	
	StartScrolling(direction);
}

function FixPositions()
{
	stop_moving=true;
	var StartIndex=-1;
	var temp;
	var thisStyle;	
	var min_distance_from_edge=2000;
	var img_width;
	if(scroll_direction=='left')
	{
		for(i=0;i<arr.length;i++)
		{	
			thisStyle = document.getElementById(arr[i]).style;				
			temp = parseInt(thisStyle.left);
			if(temp>0)
			{
				if(temp<min_distance_from_edge)
				{
					StartIndex=i;
					min_distance_from_edge = temp;
				}
			}	
		}	
		
		thisStyle = document.getElementById(arr[StartIndex]).style;	
		temp=parseInt(thisStyle.left);
		for(i=StartIndex+1;i<arr.length+StartIndex;i++)
		{
			if(i<arr.length)
			{
				thisStyle = document.getElementById(arr[i]).style;	
			}
			else
			{
				thisStyle = document.getElementById(arr[i-arr.length]).style;	
			}	
			img_width = parseInt(thisStyle.width ,10);
			thisStyle.left = parseInt(temp+img_width+right_margin,10) + "px";			
			temp = parseInt(thisStyle.left,10);
		}
		
		thisStyle = document.getElementById(arr[StartIndex]).style;	
		temp=parseInt(thisStyle.left);
		if(temp>right_margin)
		{			
			if(StartIndex>0)
			{
				thisStyle = document.getElementById(arr[StartIndex-1]).style;	
			}
			else
			{
				thisStyle = document.getElementById(arr[arr.length-1]).style;	
			}
			img_width = parseInt(thisStyle.width ,10);
			thisStyle.left = temp - (right_margin+img_width) + "px";
		}
		
		
	}
	else if(scroll_direction=='right')
	{
		
		for(i=0;i<arr.length;i++)
		{	
			thisStyle = document.getElementById(arr[i]).style;				
			temp = parseInt(thisStyle.left);
			if(avail_width-temp>0)
			{
				if(avail_width-temp<min_distance_from_edge)
				{
					StartIndex=i;
					min_distance_from_edge = avail_width-temp;
				}
			}	
		}	
		
		thisStyle = document.getElementById(arr[StartIndex]).style;	
		temp=parseInt(thisStyle.left);
		for(i=StartIndex-1;i>StartIndex-arr.length;i--)
		{
			if(i>=0)
			{
				thisStyle = document.getElementById(arr[i]).style;	
			}
			else
			{
				thisStyle = document.getElementById(arr[i+arr.length]).style;	
			}	
			img_width = parseInt(thisStyle.width ,10);	
			thisStyle.left = parseInt(temp-(img_width+right_margin),10) + "px";			
			temp = parseInt(thisStyle.left,10);
		}
		
		thisStyle = document.getElementById(arr[StartIndex]).style;			
		temp=parseInt(thisStyle.left);
		img_width = parseInt(thisStyle.width ,10);
		if(temp+img_width+right_margin<avail_width)
		{			
			if(StartIndex<arr.legth-1)
			{
				thisStyle = document.getElementById(arr[StartIndex+1]).style;	
			}
			else
			{
				thisStyle = document.getElementById(arr[0]).style;	
			}			
			img_width = parseInt(thisStyle.width ,10);
			thisStyle.left = temp + (right_margin+img_width) + "px";
		}
		
		
	}
	
	
	
	
	
	
}

function PopulateScroller()
{
	var im;
	im = document.getElementById("scrolling_imgs")
	if(im)
	{	
		avail_width = parseInt(im.clientWidth,10);
		objs=im.getElementsByTagName("A");
		arr=new Array(objs.length);
		
		var width_counter;
		width_counter = 0;
		var i;
		var imgCol;
		var imgl;
		var img_width;
		for(i=0;i<arr.length;i++)
		{	
			imgCol = objs[i].getElementsByTagName("IMG")
			img = imgCol[0];
			AddEventHandler(img,'mouseout',StartScrolling);	
			//AddEventHandler(img,'mouseout',HideCaption);				
			AddEventHandler(img,'mouseover',StopScrolling);						
			arr[i]=objs[i].id;
			//Sto A
			document.getElementById(arr[i]).style.top = 0  + 'px';
			document.getElementById(arr[i]).style.left = width_counter + 'px';
			document.getElementById(arr[i]).style.position='absolute';			
			img_width = parseInt(document.getElementById(arr[i]).clientWidth,10);
			document.getElementById(arr[i]).style.width = img_width + "px";
			width_counter += img_width + right_margin;
		}
		
		StartScrolling('left');
		
	}
	
}

		
function StartScrolling()
{
	stop_moving=false;
	if(arguments.length>0)
	{				
		if(typeof(arguments[0])=='string')
		{
			scroll_direction = arguments[0];
		}
	}
	if(scroll_direction!='left' && scroll_direction!='right')
	{
		scroll_direction='left';
	}			
	
	if(timer_on==false)
	{
		timer=setTimeout("scroll();",ScrollingSpeed);
		timer_on=true;
	}
}
		
function scroll()
{
	if(stop_moving)
	{
		return
	}
	var i;
	var temp;
	var thisStyle;
	var move_factor;
	if(scroll_direction!='left' && scroll_direction!='right')
	{
		scroll_direction='left';
	}		
	
	if(scroll_direction=='left')
	{
		move_factor=-1;
	}
	else
	{
		move_factor=+1;
	}
	
	for(i=0;i<arr.length;i++)
	{	
		thisStyle = document.getElementById(arr[i]).style;				
		temp = parseInt(thisStyle.left);				
		thisStyle.left =  (temp + move_factor) + "px";
		temp = parseInt(thisStyle.left);
		
		if(scroll_direction=='left')
		{
			img_width = parseInt(thisStyle.width,10);
			if(parseInt(thisStyle.left) + (img_width + right_margin)==0)
			{
				thisStyle.left = (arr.length-1)*(img_width + right_margin) + "px";							
			}
		}
		else
		{
			img_width = parseInt(thisStyle.width,10);
			if(parseInt(thisStyle.left)==avail_width)
			{
				thisStyle.left = avail_width - ((arr.length-0)*(img_width + right_margin)) + "px";
			}
		}
	}
	timer=setTimeout("scroll();",ScrollingSpeed);
}		