
	/**
	* Rednut Horizonal Scroller in a window stuff
	* @author	stuart nixon <stuart@rednut.net>
	*
	**/


	/**
	* function to write a message into a status bar div
	*
	*
	**/
	var setMessage = function (text) {
		var statusbar = document.getElementById("statusbar");
		statusbar.innerHTML = text;
	}



	/**
	* function that sets up the Rednut HScroller
	*
	* The rednutHScroller takes a reference to the element that contains all the things to be scrolled plus refs to the two left and right buttons
	* the above element should sit in the window that is to be used as the viewport for the rednutHScroller
	*
	* elements:
	* document > viewport -> scroll items container -> scroll items [*n]
	*
	* @param	element		the element that contains all the items to be scrolled
	* @param	element		the element that is to be used as the left scroll area / button
	* @param	element		the element that is to be used as the right scroll area / button
	* @param	number		amount to offset the window scroll on each shuffle (in px) cn be positive or neatve to move it backwards
	* @param	int				number of milli seconds between offset shuffles
	**/
	var setupRednutHScroller = function (container, lscroll, rscroll, offset, timeout) {
			/**
			* get the width of the contents of the scroll container
			**/
			var getScrollContentWidth = function (obj) {
				// avoid js error whenthere are no thumbnails
				
				//obj.items = obj.getElementsByTagName("div");
				//var last_item = obj.items[obj.items.length-1];
				//var width = last_item.offsetLeft + last_item.offsetWidth;
				//obj.width = width;
				//return width;
				
				var width = 0;
				obj.items = obj.getElementsByTagName("div");
				
				if (obj.items.length > 0)
				{
					var last_item = obj.items[obj.items.length-1];
					width = last_item.offsetLeft + last_item.offsetWidth;
					obj.width = width;
				}
				return width;

			}


			/**
			* this is the main rednutHScroller worker function, it saves the left offset in the scroll items container and uses this
			* left value when adding the offset amount (can be negative ) to achive a shuffling effect by calling itself after the
			* milli second timeout has expired.
			**/
			var doRednutHScroll = function (obj, offset, timeout) {

				/**
				* set iitial position on first run
				**/
				if (!obj.scrollPosLeft) obj.scrollPosLeft = obj.scrollLeft;
				if (!obj.scrollContentWidth) obj.scrollContentWidth = getScrollContentWidth(obj);

				/**
				* handy vars
				**/
				var scrollPosLeft = obj.scrollPosLeft;
				var scrollMax = obj.scrollContentWidth - obj.parentNode.clientWidth
				var scrollMin = -scrollMax


				/**
				* just some debug
				**/
				//setMessage("lpos = " + scrollPosLeft + "max="+scrollMax+", min="+scrollMin);


				/**
				* make sure we keep the view port full if we can othersise stop scrolling
				**/
				if ((offset < 0 && scrollPosLeft < scrollMin) || (offset >= 0 && scrollPosLeft > -offset)) {
					obj.isScrolling = false;
					return true;
				}


				//if (scrollPosLeft - obj.

				/**
				* do the suffle of the container to the left by offset amount (can be negative)
				**/
				obj.scrollPosLeft += offset;

				/**
				* set the css left position in pixels
				**/
				obj.style.left = obj.scrollPosLeft + "px";


				/**
				* if we are still scrolling then set callback timeout
				**/
				if (obj.isScrolling) setTimeout(function () {
					doRednutHScroll(obj, offset, timeout);
				}, timeout);


			}


			/**
			* bind events to buttons
			**/
			addEventSimple(lscroll, "mouseover", function () {
				if (container.isScrolling) return false;
				container.isScrolling = true;	doRednutHScroll(container, offset, timeout);
			});
			addEventSimple(rscroll, "mouseover", function () {
				if (container.isScrolling) return false;
				container.isScrolling = true; doRednutHScroll(container, -offset, timeout);
			});


			/**
			* on mouse out stop scrolling
			**/
			var onMouseOut = function () {
				container.isScrolling = false;
			}
			addEventSimple(lscroll, "mouseout",onMouseOut);
			addEventSimple(rscroll, "mouseout", onMouseOut);
		}


	function addEventSimple(obj, evt,fn)
	{	
		if (obj != null)
		{
			if (obj.addEventListener)
			{
				obj.addEventListener(evt, fn, false)
			} else if (obj.attachEvent) {
				obj.attachEvent('on' + evt,fn);
			}
		}
	}
