var HSA_scrollAreas = new Array();



var HSA_default_imagesPath = "images";

var HSA_default_btnLeftImage = "scroll-left.gif";

var HSA_default_btnRightImage = "scroll-right.gif";

var HSA_default_scrollStep = 50;

var HSA_default_wheelSensitivity = 2;

var HSA_default_scrollbarPosition = 'top'; //'top';//'bottom';'inline';

var HSA_default_scrollButtonWidth = 12;

var HSA_default_scrollbarHeight = 14;



var HSA_resizeTimer = 200;



if (window.addEventListener)

	window.addEventListener("load", initScrollbars, false);

else if (window.attachEvent)

	window.attachEvent("onload", initScrollbars);



function initScrollbars()

{

	var scrollElements = getElements("text-scroll", "DIV", document, "class");

	for (var i=0; i<scrollElements.length; i++)

	{

		HSA_scrollAreas[i] = new hScrollArea(i, scrollElements[i]);

	}

}



function hScrollArea(index, elem) //constructor

{

	this.index = index;

	this.element = elem;

	this._scrollStep = 400;



	var attr = this.element.getAttribute("imagesPath");

	this.imagesPath = attr ? attr : HSA_default_imagesPath;



	attr = this.element.getAttribute("btnLeftImage");

	this.btnLeftImage = attr ? attr : HSA_default_btnLeftImage;



	attr = this.element.getAttribute("btnRightImage");

	this.btnRightImage = attr ? attr : HSA_default_btnRightImage;



	attr = Number(this.element.getAttribute("scrollStep"));

	this.scrollStep = attr ? attr : HSA_default_scrollStep;



	attr = Number(this.element.getAttribute("wheelSensitivity"));

	this.wheelSensitivity = attr ? attr : HSA_default_wheelSensitivity;



	attr = this.element.getAttribute("scrollbarPosition");

	this.scrollbarPosition = attr ? attr : HSA_default_scrollbarPosition;

	

	attr = this.element.getAttribute("scrollButtonWidth");

	this.scrollButtonWidth = attr ? attr : HSA_default_scrollButtonWidth;



	attr = this.element.getAttribute("scrollbarHeight");

	this.scrollbarHeight = attr ? attr : HSA_default_scrollbarHeight;



	this.scrolling = false;



	this.iOffsetX = 0;

	this.scrollWidth = 0;

	this.scrollContent = null;

	this.scrollbar = null;

	this.scrollleft = null;

	this.scrollright = null;

	this.scrollslider = null;

	this.scroll = null;

	this.enableScrollbar = false;

	this.scrollFactor = 1;

	this.scrollingLimit = 0;

	this.leftPosition = 0;



	//functions declaration

	this.init = HSA_init;



	this._scrollLeft = _HSA_scrollLeft;

	this._scrollRight = _HSA_scrollRight;





	this.createScrollBar = HSA_createScrollBar;

	this.scrollIt = HSA_scrollIt;

	this.scrollItFast = HSA_scrollItFast;



	this.init();

	

}





function HSA_init()

{



	this.scrollContent = document.createElement("DIV");

	this.scrollContent.style.position = "absolute";

	var uls = this.element.getElementsByTagName('ul');



	uls[0].parentNode.style.width = uls[0].offsetWidth + "px";

	this.scrollContent.style.width = uls[0].offsetWidth + "px";



	this.scrollContent.style.height = this.element.offsetHeight + "px";

	this.scrollContent.innerHTML = this.element.innerHTML;

	this.scrollContent.style.overflow = "hidden";	

	this.scrollContent.style.left = '0px';

	this.scrollContent.style.top = '0px';



	this.element.innerHTML = "";



	this.element.style.overflow = "hidden";

	this.element.style.display = "block";

	this.element.style.visibility = "visible";

	this.element.style.position = "relative";

	this.element.appendChild(this.scrollContent);



	this.scrollContent.className = 'scroll-content';



	this.element.index = this.index;



	if (document.all)

	{

		this.element.onscroll = HSA_handleOnScroll;

		this.element.onmousewheel = HSA_handleMouseWheel;

		this.element.onresize = HSA_handleResize;

	}

	else

	{

		this.element.addEventListener("DOMMouseScroll", HSA_handleMouseWheel, false);

		window.onresize = HSA_handleResize;

	}

	this.createScrollBar();



}



function HSA_createScrollBar()

{



	if (this.scrollbar != null)

	{

		this.element.removeChild(this.scrollbar);

		this.scrollbar = null;

	}



	

	if (this.scrollContent.offsetWidth <= this.element.offsetWidth)

		this.enableScrollbar = false;

	else if (this.element.offsetWidth > 2*this.scrollButtonWidth)

		this.enableScrollbar = true;

	else

		this.enableScrollbar = false;

	

	if (this.scrollContent.offsetWidth - Math.abs(this.scrollContent.offsetLeft) < this.element.offsetWidth)

		this.scrollContent.style.left = 0;



	if (true || this.enableScrollbar)

	{



		this.scrollContent.id = 'scroll-content-' + this.index;

		

		this.scrollbar = document.createElement("DIV");

		this.element.appendChild(this.scrollbar);		

		this.scrollbar.style.position = "absolute";

		this.scrollbar.style.left = "0px";

		this.scrollbar.style.width = this.element.offsetWidth+"px";

		this.scrollbar.style.height = this.scrollbarHeight + "px";



		this.scrollbar.className = 'hscroll-bar';



		if(this.scrollbarHeight != this.scrollbar.offsetHeight)

		{

			this.scrollbarHeight = this.scrollbar.offsetWidth;

		}

		

		this.scrollbarHeight = this.scrollbar.offsetHeight;



		if(this.scrollbarPosition == 'top')

		{

			this.scrollContent.style.top = this.scrollbarHeight + 5 + "px";			

			this.scrollContent.style.height = this.element.offsetHeight - this.scrollbarHeight - 5 + "px";

		}

		else if (this.scrollbarPosition == 'bottom')

		{

			this.scrollbar.style.top = this.element.offsetHeight - this.scrollbarHeight  + "px";

			this.scrollContent.style.height = this.element.offsetHeight - this.scrollbarHeight - 5 + "px";

		}



		//create scroll up button

		this.scrollleft = document.createElement("DIV");

		this.scrollleft.index = this.index;



/*

		this.scrollleft.onmousedown = HSA_handleBtnLeftMouseRight;

		this.scrollleft.onmouseup = HSA_handleBtnLeftMouseLeft;

		this.scrollleft.onmouseout = HSA_handleBtnLeftMouseOut;

*/

		this.scrollleft.onclick = HSA_handleBtnRightMouseClickLeft;



		this.scrollleft.style.position = "absolute";

		this.scrollleft.style.left = "0px";

		this.scrollleft.style.width = this.scrollButtonWidth + "px";		

		this.scrollleft.style.height = this.scrollbarHeight + "px";		

		this.scrollleft.innerHTML = '<img src="' + this.imagesPath + '/' + this.btnLeftImage + '" border="0"/>';

		this.scrollbar.appendChild(this.scrollleft);

		this.scrollleft.className = 'hscroll-left';

		if(this.scrollButtonWidth != this.scrollleft.offsetWidth)

		{

			this.scrollButtonWidth = this.scrollleft.offsetWidth;

		}

				

		//create scroll down button

		this.scrollright = document.createElement("DIV");

		this.scrollright.index = this.index;



/*

		this.scrollright.onmousedown = HSA_handleBtnRightMouseRight;

		this.scrollright.onmouseup = HSA_handleBtnRightMouseLeft;

		this.scrollright.onmouseout = HSA_handleBtnRightMouseOut;

*/

		

		this.scrollright.onclick = HSA_handleBtnRightMouseClickRight;



		this.scrollright.style.position = "absolute";

		this.scrollright.style.left =  this.scrollbar.offsetWidth - this.scrollButtonWidth + "px";

		this.scrollright.style.height = this.scrollbarHeight + "px";

		this.scrollright.innerHTML = '<img src="' + this.imagesPath + '/' + this.btnRightImage + '" border="0"/>';

		this.scrollbar.appendChild(this.scrollright);

		this.scrollright.className = 'hscroll-right';





		//create scroll

		this.scroll = document.createElement("DIV");

		this.scroll.index = this.index;

		this.scroll.style.position = "absolute";

		this.scroll.style.zIndex = 0;

		this.scroll.style.textAlign = "center";

		this.scroll.style.left = this.scrollButtonWidth + "px";

		this.scroll.style.height = this.scrollbarHeight + "px";		

		var h = this.scrollbar.offsetWidth - 2*this.scrollButtonWidth;

		this.scroll.style.width = ((h > 0) ? h : 0) + "px";		

		this.scroll.innerHTML = '';

		this.scroll.onclick = HSA_handleScrollbarClick;

		this.scrollbar.appendChild(this.scroll);

		this.scroll.style.overflow = "hidden";

		this.scroll.className = "hscroll-line";



		//create slider

		this.scrollslider = document.createElement("DIV");

		this.scrollslider.index = this.index;

		this.scrollslider.style.position = "absolute";

		this.scrollslider.style.zIndex = 1000;

		this.scrollslider.style.textAlign = "center";

		this.scrollslider.innerHTML = '<div id="scrollslider' + this.index + '" style="padding:0;margin:0;"></div>';

		this.scrollbar.appendChild(this.scrollslider);		

		this.subscrollslider = document.getElementById("scrollslider"+this.index);		

		//this.subscrollslider.style.width = "100px";

		this.subscrollslider.style.width = Math.round((this.element.offsetWidth/this.scrollContent.offsetWidth)*(this.scrollbar.offsetWidth - 2*this.scrollButtonWidth)) + "px";		

		this.scrollslider.className = "hscroll-slider";		

		this.scrollWidth = this.scrollbar.offsetWidth - 2*this.scrollButtonWidth - this.scrollslider.offsetWidth - 2;

		this.scrollFactor = (this.scrollContent.offsetWidth - this.element.offsetWidth)/this.scrollWidth;

		this.leftPosition = getRealLeft(this.scrollbar) + this.scrollButtonWidth;



		this.scrollslider.style.left =  this.scrollButtonWidth + "px";

		this.scrollslider.style.height = "12px";

		

		this.scrollslider.id = '_scrollslider' + this.index;



		this.scrollslider.onmousedown = HSA_handleSliderMouseDown;

		if (document.all)

			this.scrollslider.onmouseup = HSA_handleSliderMouseLeft;

			



		var myTransition = new Fx.Transition(Fx.Transitions.Quad, 3);

		this.mooeffect = new Fx.Styles(this.scrollContent.id,{transition: myTransition.easeOut,duration:700,

			onStart: function(){

			},

			onComplete: function(){

			}										

		});



		var _myTransition = new Fx.Transition(Fx.Transitions.Quad, 3);

		this.slidermooeffect = new Fx.Styles(this.scrollslider.id,{transition: _myTransition.easeOut,duration:700,

			onStart: function(){

			},

			onComplete: function(){

			}										

		});

			



	}

	else

	{

		this.scrollContent.style.height = this.element.offsetHeight + "px";

	}

}









function HSA_handleBtnRightMouseClickLeft()

{

	var sa = HSA_scrollAreas[this.index];

	sa.scrolling = true;

	sa._scrollLeft();

}



function HSA_handleBtnRightMouseClickRight()

{

	var sa = HSA_scrollAreas[this.index];

	sa.scrolling = true;

	sa._scrollRight();

}





function HSA_scrollIt(_left)

{		

	if(_left)

	{

		var _new_left = Math.round(-1*(this.scrollFactor * ((_left + this.scrollslider.offsetWidth/2) - this.scrollButtonWidth - this.scrollslider.offsetWidth/2)));

	}

	else

	{

		var _new_left = Math.round(-1*(this.scrollFactor * ((this.scrollslider.offsetLeft + this.scrollslider.offsetWidth/2) - this.scrollButtonWidth - this.scrollslider.offsetWidth/2)));		

	}

	this.mooeffect.custom({'left':[this.scrollContent.offsetLeft,_new_left]});

}



function HSA_scrollItFast()

{		

	this.scrollContent.style.left = Math.round(-1*(this.scrollFactor * ((this.scrollslider.offsetLeft + this.scrollslider.offsetWidth/2) - this.scrollButtonWidth - this.scrollslider.offsetWidth/2))) + "px";	

}



function _HSA_scrollLeft()

{

	if (!this.scrolling) return;

	if ( this.scrollContent.offsetLeft + this._scrollStep <= this._scrollStep)

	{

		var _new_left = this.scrollContent.offsetLeft + this._scrollStep;

		if(_new_left > 0) _new_left = 0;

		this.mooeffect.custom({'left':[this.scrollContent.offsetLeft,_new_left]});

		var __new_left = 1 / this.scrollFactor * Math.abs(_new_left) + this.scrollButtonWidth;

		this.slidermooeffect.custom({'left':[this.scrollslider.offsetLeft,__new_left]});

	}

	else

	{

		this.scrollContent.scrollLeft = "0";

		this.scrollslider.style.left = this.scrollButtonWidth + "px";

		return;

	}

}





function _HSA_scrollRight()

{

	if (!this.scrolling) return;



	if (!(-this.scrollContent.offsetLeft  >= (this.scrollContent.offsetWidth - this.element.offsetWidth)))

	{

		var _new_left = this.scrollContent.offsetLeft - this._scrollStep;

		if(_new_left + (this.scrollContent.offsetWidth - this.element.offsetWidth) < this._scrollStep) _new_left = -(this.scrollContent.offsetWidth - this.element.offsetWidth);

		this.mooeffect.custom({'left':[this.scrollContent.offsetLeft,_new_left]});



		var __new_left = 1 / this.scrollFactor * Math.abs(_new_left) + this.scrollButtonWidth;

		this.slidermooeffect.custom({'left':[this.scrollslider.offsetLeft,__new_left]});



	}

	else

	{

		this.scrollContent.style.left = -(this.scrollContent.offsetWidth - this.element.offsetWidth) + "px";

		this.scrollslider.style.left = this.scrollWidth + this.scrollButtonWidth + "px";

		return;

	}

}







function HSA_handleMouseMove(evt)

{

	var sa = HSA_scrollAreas[((document.all && !window.opera) ? this.index : document.documentElement.scrollAreaIndex)];

	var posy = 0;

	if (!evt) var evt = window.event;

	

	if (evt.pageX)

		posy = evt.pageX;

	else if (evt.clientX)

		posy = evt.clientX;

			

		if (document.all && !window.opera)

		{

			posy += document.documentElement.scrollLeft;

		}



	var iNewY = posy - sa.iOffsetX - sa.leftPosition;

		iNewY += sa.scrollButtonWidth;

		

	if (iNewY < sa.scrollButtonWidth)

		iNewY = sa.scrollButtonWidth;

	if (iNewY > (sa.scrollWidth + sa.scrollButtonWidth))

		iNewY = (sa.scrollWidth + sa.scrollButtonWidth);



	sa.scrollslider.style.left = iNewY + "px";



	sa.scrollItFast();

}



function HSA_handleSliderMouseDown(evt)

{

	var sa = HSA_scrollAreas[this.index];

	if (document.all && !window.opera)

	{

		sa.scrollslider.setCapture()

		sa.iOffsetX = event.offsetX;

		sa.scrollslider.onmousemove = HSA_handleMouseMove;

	}

	else

	{

		if(window.opera)

		{

			sa.iOffsetX = event.offsetX;

		}

		else

		{

			sa.iOffsetX = evt.layerX;

		}

		document.documentElement.scrollAreaIndex = sa.index;

		document.documentElement.addEventListener("mousemove", HSA_handleMouseMove, true);

		document.documentElement.addEventListener("mouseup", HSA_handleSliderMouseLeft, true);

	}

}



function HSA_handleSliderMouseLeft()

{

	if (document.all && !window.opera)

	{

		var sa = HSA_scrollAreas[this.index];

		sa.scrollslider.onmousemove = null;

		sa.scrollslider.releaseCapture();

		sa.scrollItFast();

	}

	else

	{

		var sa = HSA_scrollAreas[document.documentElement.scrollAreaIndex];

		document.documentElement.removeEventListener("mousemove", HSA_handleMouseMove, true);

		document.documentElement.removeEventListener("mouseup", HSA_handleSliderMouseLeft, true);

		sa.scrollItFast();

	}

}



function HSA_handleResize()

{

	if (HSA_resizeTimer)

	{

		clearTimeout(HSA_resizeTimer);

		HSA_resizeTimer = 0;

	}

	HSA_resizeTimer = setTimeout("HSA_performResizeEvent()", 100);

}



function HSA_performResizeEvent()

{

	for (var i=0; i<HSA_scrollAreas.length; i++)

		HSA_scrollAreas[i].createScrollBar();

}



function HSA_handleMouseWheel(evt)

{

	if(!evt) evt = event;

	var sa = HSA_scrollAreas[this.index];

	if (sa.scrollbar == null) return;

	sa.scrolling = true;

	sa.scrollingLimit = sa.wheelSensitivity;

	if ((!window.opera && (evt.wheelDelta > 0 || evt.detail < 0)) || (window.opera && (evt.wheelDelta > 0 || evt.detail > 0)))

		sa._scrollLeft();

	else

		sa._scrollRight();

}



function HSA_handleSelectStart()

{

	event.returnValue = false;

}



function HSA_handleScrollbarClick(evt)

{

	var sa = HSA_scrollAreas[this.index];

	var offsetX = (document.all ? event.offsetX : evt.layerX);

	

	var __new_left = 0;

	

	if (offsetX < (sa.scrollButtonWidth + sa.scrollslider.offsetWidth/2))

		__new_left = sa.scrollButtonWidth;

	else if (offsetX > (sa.scrollbar.offsetWidth - sa.scrollButtonWidth - sa.scrollslider.offsetWidth))

		__new_left = sa.scrollbar.offsetWidth - sa.scrollButtonWidth - sa.scrollslider.offsetWidth;

	else

	{

		__new_left = offsetX + sa.scrollButtonWidth - sa.scrollslider.offsetWidth/2;

	}

	

	//sa.scrollslider.style.left = _new_left + "px";

	sa.slidermooeffect.custom({'left':[sa.scrollslider.offsetLeft,__new_left]});	

	sa.scrollIt(__new_left);

}



function HSA_handleOnScroll()

{

	event.srcElement.doScroll("pageLeft");

}



//--- common functions ----



function getRealLeft(elem)

{

	var nLeft = 0;

	if(elem)

	{

		do

		{

			nLeft += elem.offsetLeft - elem.scrollLeft;

			elem = elem.offsetParent;

		}

		while(elem)

	}

	return nLeft;

}