var ScrollGallery =
{
	// enums
	Left: "left",
	Right: "right",
	
	// globals
	ScrollDivID: "GalleryScrollDiv", // name of scrollable div
	Interval: 10, // time interval between each movement step. determines the speed of the scroll
	StepSize: 5, // pixels to move by in each movement step. determines the speed of the scroll
	iImageWidth: 150, // image from gallery width
	iPosCounter: 0, //counts how many pixels untill the next image in the gallery, helps with the stop
	sStopDir: 'right',
	flag: false // to show if the scroll is stilll moving
}

// public methods

ScrollGallery.init = function()
{
	if (ScrollGallery.m_bInitizlized)
		return;
	ScrollGallery.m_oScrollLyr = new Layer(ScrollGallery.ScrollDivID);
	if (!ScrollGallery.m_oScrollLyr.elm)
	{
		ScrollGallery.m_oScrollLyr = null;
		return -1;
	}
	ScrollGallery.m_oScrollDiv = ScrollGallery.m_oScrollLyr.elm;
	ScrollGallery.m_oContainer = ScrollGallery.m_oScrollLyr.elm.offsetParent;
	ScrollGallery.m_iOffsetWidth = ScrollGallery.m_oContainer.offsetWidth;
	ScrollGallery.m_iScrollWidth = ScrollGallery.m_oScrollDiv.firstChild.offsetWidth;
	ScrollGallery.m_bInitizlized = true;
	return 0;
}

ScrollGallery.start = function(i_enumDirection)
{
	if (i_enumDirection != ScrollGallery.Left && i_enumDirection != ScrollGallery.Right)
		return -1;
	if (!ScrollGallery.m_bInitizlized)
		return -1;
	
	// stop scrolling left condition
	if (i_enumDirection == ScrollGallery.Left && ScrollGallery.m_oScrollLyr.x <= ScrollGallery.m_iOffsetWidth - ScrollGallery.m_iScrollWidth)
		ScrollGallery.stop();
	// stop scrolling right condition
	else if (i_enumDirection == ScrollGallery.Right && ScrollGallery.m_oScrollLyr.x >= 0)
		ScrollGallery.stop();
	else
		ScrollGallery.scroll(i_enumDirection);
}

ScrollGallery.stop = function()
{
	if (ScrollGallery.flag) {
		document.getElementById("arrow_"+ScrollGallery.sStopDir).src = eval("arrow_"+ScrollGallery.sStopDir+"_e").src;
		ScrollGallery.flag = false;
	}
	if (Math.abs(Math.round(ScrollGallery.m_oScrollLyr.x/ScrollGallery.iImageWidth)) == Math.abs(ScrollGallery.m_oScrollLyr.x/ScrollGallery.iImageWidth)) {
		if (ScrollGallery.m_iTimeoutID != -1)
			clearTimeout(ScrollGallery.m_iTimeoutID);
	}
	else {
		//alert(ScrollGallery.m_oScrollLyr.x);
		if (ScrollGallery.sStopDir == ScrollGallery.Left)
			ScrollGallery.m_oScrollLyr.moveBy(-ScrollGallery.StepSize, 0);
		else
			ScrollGallery.m_oScrollLyr.moveBy(ScrollGallery.StepSize, 0);
		if (ScrollGallery.m_iTimeoutID != -1)
			clearTimeout(ScrollGallery.m_iTimeoutID);
		ScrollGallery.m_iTimeoutID = setTimeout("ScrollGallery.stop(\"\")", ScrollGallery.Interval);
	}
}

ScrollGallery.countPos = function(iScrollByTemp,i_enumDirectionTemp) {
//if (ScrollGallery.sStopDir != i_enumDirection)
//	ScrollGallery.iPosCounter = 0;
if (i_enumDirectionTemp == ScrollGallery.Left)
	if (ScrollGallery.iPosCounter >= ScrollGallery.iImageWidth)
		ScrollGallery.iPosCounter = iScrollByTemp;
	else
		ScrollGallery.iPosCounter += iScrollByTemp;
else
	if (ScrollGallery.iPosCounter <= -ScrollGallery.iImageWidth)
		ScrollGallery.iPosCounter = iScrollByTemp;
	else
		ScrollGallery.iPosCounter -= iScrollByTemp;
}

ScrollGallery.scroll = function(i_enumDirection)
{		
	if (!ScrollGallery.m_bInitizlized)
		return -1;
	if (i_enumDirection != ScrollGallery.Left && i_enumDirection != ScrollGallery.Right)
		return -1;
	if (ScrollGallery.m_iTimeoutID != -1)
		clearInterval(ScrollGallery.m_iTimeoutID);
		
	var iScrollBy = 0;
	
	if (i_enumDirection == ScrollGallery.Left)
		iScrollBy = -Math.min(ScrollGallery.StepSize, ScrollGallery.m_oScrollLyr.x - ScrollGallery.m_iOffsetWidth + ScrollGallery.m_iScrollWidth);
	else 
		iScrollBy = Math.min(ScrollGallery.StepSize, -ScrollGallery.m_oScrollLyr.x);
	
	//if (ScrollGallery.sStopDir != i_enumDirection)
	//	ScrollGallery.iPosCounter = 0;
	/*
	if (i_enumDirection == ScrollGallery.Left)
		if (ScrollGallery.iPosCounter >= ScrollGallery.iImageWidth)
			ScrollGallery.iPosCounter = iScrollBy;
		else
			ScrollGallery.iPosCounter += iScrollBy;
	else
		if (ScrollGallery.iPosCounter <= -ScrollGallery.iImageWidth)
			ScrollGallery.iPosCounter = iScrollBy;
		else
			ScrollGallery.iPosCounter -= iScrollBy;
	*/
	
	ScrollGallery.countPos(iScrollBy,i_enumDirection);
	if (!ScrollGallery.flag) {
		document.getElementById("arrow_"+i_enumDirection).src = eval("arrow_"+i_enumDirection+"_r").src;
		ScrollGallery.flag = true;
	}
	
	ScrollGallery.sStopDir = i_enumDirection
	ScrollGallery.m_oScrollLyr.moveBy(iScrollBy, 0);
	if (Math.abs(iScrollBy) == ScrollGallery.StepSize)
		ScrollGallery.m_iTimeoutID = setTimeout("ScrollGallery.scroll(\"" + i_enumDirection + "\")", ScrollGallery.Interval);
	return 0;
}

ScrollGallery.gotoPoint = function(iGotoX) {
	//option 1: makes gallery jump to point
	ScrollGallery.m_oScrollLyr.moveTo(iGotoX, null);
}


//function recieves an X point that can be changed later on to Y if needed and scrolls to that point
	
ScrollGallery.slideToPoint = function(iGotoX) {
	//(ScrollGallery.m_oScrollLyr.x <= ScrollGallery.m_iOffsetWidth - ScrollGallery.m_iScrollWidth && ScrollGallery.m_oScrollLyr.x >= 0)
	//if (ScrollGallery.m_oScrollLyr.x > 0 ) {
	//	ScrollGallery.stop();
	//	clearInterval(ScrollGallery.m_iTimeoutID);
	//	return -1;
	//}
	
	var iScrollBy = 0;
	var i_enumDirection;
	/*
	if (iGotoX < ScrollGallery.m_oScrollLyr.x)
		i_enumDirection = ScrollGallery.Left;
	if (iGotoX > ScrollGallery.m_oScrollLyr.x)
		i_enumDirection = ScrollGallery.Right;
	if (!i_enumDirection || iGotoX == ScrollGallery.m_oScrollLyr.x)
		return -1;
	*/	
	if (i_enumDirection == ScrollGallery.Left)
		iScrollBy = -Math.min(ScrollGallery.StepSize, ScrollGallery.m_oScrollLyr.x - ScrollGallery.m_iOffsetWidth + ScrollGallery.m_iScrollWidth);
	else
		iScrollBy = Math.min(ScrollGallery.StepSize, -ScrollGallery.m_oScrollLyr.x);
	ScrollGallery.m_oScrollLyr.slideTo(iGotoX,null,iScrollBy, ScrollGallery.Interval, null);
	//ScrollGallery.m_oScrollLyr.moveBy(iScrollBy, 0);
	
	if (Math.abs(iScrollBy) == ScrollGallery.StepSize)
		ScrollGallery.m_oScrollLyr.slideTo(iGotoX,null,iScrollBy, ScrollGallery.Interval, null);
}

ScrollGallery.showTooltip = function(i_sTooltipDivID)
{
	if (!ScrollGallery.m_bInitizlized)
		return;
	if (i_sTooltipDivID == ScrollGallery.m_sVisibleTooltipDivID)
		return;
		
	ScrollGallery.hideTooltip();
	var oTooltipDiv = document.getElementById(i_sTooltipDivID);
	//oTooltipDiv = new Layer(i_sTooltipDivID);
	if (oTooltipDiv)
	//if (oTooltipDiv.elm)
	{
		//oTooltipDiv.fadeTo(100,20,30);
		oTooltipDiv.style.display = "block";
		ScrollGallery.m_sVisibleTooltipDivID = i_sTooltipDivID;
		var iParentRelLeftPos = ScrollGallery.m_oScrollLyr.x + oTooltipDiv.offsetParent.offsetLeft;
		if (iParentRelLeftPos < 0)
			oTooltipDiv.style.pixelLeft -= iParentRelLeftPos;
		else if (iParentRelLeftPos + 20 + oTooltipDiv.offsetWidth > ScrollGallery.m_iOffsetWidth)
			oTooltipDiv.style.pixelLeft -= iParentRelLeftPos + 20 + oTooltipDiv.offsetWidth - ScrollGallery.m_iOffsetWidth;
		else
			oTooltipDiv.style.pixelLeft = 20;
	}
}
ScrollGallery.hideTooltip = function()
{
	if (!ScrollGallery.m_bInitizlized)
		return;
	if (!ScrollGallery.m_sVisibleTooltipDivID)
		return;
	var oTooltipDiv = document.getElementById(ScrollGallery.m_sVisibleTooltipDivID);
	//oTooltipDiv = new Layer(ScrollGallery.m_sVisibleTooltipDivID);
	if (oTooltipDiv)
	//if (oTooltipDiv.elm)
	{
		oTooltipDiv.style.display = "none";
		ScrollGallery.m_sVisibleTooltipDivID = null;
		//oTooltipDiv.fadeTo(0,20,30);
	}
}
	
// private members
ScrollGallery.m_oScrollLyr = null;
ScrollGallery.m_oScrollDiv = null;
ScrollGallery.m_oContainer = null;
ScrollGallery.m_iOffsetWidth = 0;
ScrollGallery.m_iScrollWidth = 0;
ScrollGallery.m_iTimeoutID = -1;
ScrollGallery.m_sVisibleTooltipDivID = null;
ScrollGallery.m_bInitizlized = false;

// initialize scroll gallery object on window load
if (window.attachEvent)
	window.attachEvent("onload", ScrollGallery.init);
else if (window.addEventListener)
	window.addEventListener("load", ScrollGallery.init, false);
else
	window.onload = ScrollGallery.init;

function show(id)
{
	if (id == DivVisible)
		return;
	else
	{
		hide();
		var oTooltipDiv = document.getElementById(id);
		
		//oTooltipDiv = new Layer(id);
		if (oTooltipDiv)
		//if (oTooltipDiv.elm)
		{
			//oTooltipDiv.fadeTo(100,20,30);
			
			oTooltipDiv.style.display = "block";
			DivVisible = id;
			var oMainDiv = document.getElementById("GalleryScrollDiv");
			var iParentRelLeftPos = oMainDiv.offsetLeft + oTooltipDiv.offsetParent.offsetLeft;
			//alert(iParentRelLeftPos + ',' + oTooltipDiv.offsetLeft + ',' + oTooltipDiv.offsetWidth);
			if (iParentRelLeftPos < 0)
				oTooltipDiv.style.pixelLeft -= iParentRelLeftPos;
			else if (iParentRelLeftPos + 20 + oTooltipDiv.offsetWidth > 600)
				oTooltipDiv.style.pixelLeft -= iParentRelLeftPos + 20 + oTooltipDiv.offsetWidth - 600;
			else
				oTooltipDiv.style.pixelLeft = 20;
		}
	}
}
function hide()
{
	var oTooltipDiv = document.getElementById(DivVisible);
	//oTooltipDiv = new Layer(DivVisible);
	if (oTooltipDiv)
	//if (oTooltipDiv.elm)
	{
		oTooltipDiv.style.display = "none";
		DivVisible = -1;
		//oTooltipDiv.fadeTo(0,20,30);
	}
}
