var SA_scrollAreas = new Array();
var SA_resizeTimer = null;

var SA_default_imagesPath = "imgs";
var SA_default_btnLeftImage = "arrow-up.gif";
var SA_default_btnRightImage = "arrow-down.gif";
var SA_default_scrollbarBackgroundColor = "";
var SA_default_scrollStep = 8;
var SA_default_scrollTimeOut = 10;
var SA_default_wheelSensitivity = 5;


if (window.addEventListener)
	window.addEventListener("load", initScrollbars, false);
else if (window.attachEvent)
	window.attachEvent("onload", initScrollbars);

function initScrollbars()
{

	var s = document.getElementById("scrollable");
	if (s)
		SA_scrollAreas[1] = new ScrollArea(1,s,794);
		
	var s = document.getElementById("scrollable-footer");
	if (s)
		SA_scrollAreas[2] = new ScrollArea(2,s,852);

}

function ScrollArea(index, elem, distance) //constructor
{
  this.distance = distance;
  this.scrollIndex = 0;
  this.segments = 0;

	this.element = elem;	
	this.index = index;

	var attr = this.element.getAttribute("imagesPath");
	this.imagesPath = attr ? attr : SA_default_imagesPath;
	attr = this.element.getAttribute("btnLeftImage");
	this.btnLeftImage = attr ? attr : SA_default_btnLeftImage;
	attr = this.element.getAttribute("btnRightImage");
	this.btnRightImage = attr ? attr : SA_default_btnRightImage;
	attr = this.element.getAttribute("scrollbarBackgroundColor");
	this.scrollbarBackgroundColor = attr ? attr : SA_default_scrollbarBackgroundColor;
	attr = Number(this.element.getAttribute("scrollStep"));
	this.scrollStep = attr ? attr : SA_default_scrollStep;
	attr = Number(this.element.getAttribute("wheelSensitivity"));
	this.wheelSensitivity = attr ? attr : SA_default_wheelSensitivity;

	this.scrolling = false;
	this.scrollbarWidth = 12;
	this.scrollButtonWidth = 12;
	this.iOffsetY = 0;
	this.scrollWidth = 0;
	this.scrollContent = null;
	this.scrollbar = null;
	this.scrollup = null;
	this.scrolldown = null;
	this.scrollslider = null;
	this.scroll = null;
	this.enableScrollbar = false;
	this.scrollFactor = 1;
	this.scrollingLimit = 0;

	this.autoScrolledLeft = false;
	this.autoScrolledRight = false;

	this.topPosition = 0;	
	this.widthCorrection = 0

	if(this.index == 1)
	{
		this.widthCorrection = 60;
	}

	this.scrollWidthCorrection = 0;

	if(this.index == 1)
	{
		this.scrollWidthCorrection = 20;
	}


	//functions declaration
	this.init = SA_init;
	this.scrollLeft = SA_scrollLeft;
	this.scrollRight = SA_scrollRight;
	this.createScrollBar = SA_createScrollBar;

	this.init();
}


function SA_init()
{

	if (document.all && this.element.getAttribute("scrollIEWrap") == "true") //fix IE event bug
	{
		var d = document.createElement("DIV");
		this.element.parentNode.replaceChild(d, this.element);
		d.appendChild(this.element);
	}


	this.scrollContent = document.createElement("DIV");
	this.scrollContent.style.position = "relative";
	this.scrollContent.style.width = this.element.offsetWidth + "px";
	this.scrollContent.innerHTML = this.element.innerHTML;

	this.element.innerHTML = "";
	this.element.style.width = (this.element.parentNode.parentNode.parentNode.offsetWidth - this.widthCorrection) + "px";	

	this.DefaultScrollingLimit = Math.round((this.element.offsetWidth - this.scrollWidthCorrection)/SA_default_scrollStep);
	this.scrollingLimit = this.DefaultScrollingLimit;

	//this.element.style.overflow = "hidden";
	//this.element.style.width = '850px';
	this.element.appendChild(this.scrollContent);
	this.element.style.display = "block";
	this.element.index = this.index;

  this.segments = Math.ceil(this.scrollContent.offsetWidth / this.distance);

	this.createScrollBar();

}

function SA_createScrollBar()
{
	
	//remove old if exists
	if (this.scrollbar != null)
	{
		this.element.removeChild(this.scrollbar);
		this.scrollbar = null;
	}
	//if (this.scrollContent.offsetWidth <= this.element.offsetWidth)
	if (this.scrollContent.offsetWidth <= this.distance)
		this.enableScrollbar = false;
	else if (this.element.offsetWidth > 2*this.scrollButtonWidth)
		this.enableScrollbar = true;
	else
		this.enableScrollbar = false;

	//	this.enableScrollbar = true;

	if (this.scrollContent.offsetWidth - Math.abs(this.scrollContent.offsetLeft) < this.element.offsetWidth)
		this.scrollContent.style.left = 0;

	if (this.enableScrollbar)
	{

		this.scrollup = document.getElementById('scroll-right-'+this.index);
		this.scrollup.index = this.index;
		this.scrollup.previousClass = this.scrollup.className;
		this.scrollup.defaultClass = this.scrollup.className;
		this.scrollup.className = this.scrollup.className + "-close";
		this.scrollup.style.display = "block";
		this.scrollup.onmousedown = SA_handleBtnLeftMouseDown;
		this.scrollup.onmouseup = SA_handleBtnLeftMouseUp;
		this.scrollup.onmouseout = SA_handleBtnLeftMouseOut;
		this.scrollup.onmouseover = SA_handleBtnLeftMouseOver;

		this.scrolldown = document.getElementById('scroll-left-'+this.index);
		this.scrolldown.index = this.index;
		this.scrolldown.previousClass = this.scrolldown.className + "-close";
		this.scrolldown.defaultClass = this.scrolldown.className;
		this.scrolldown.style.display = "block";
		this.scrolldown.onmousedown = SA_handleBtnRightMouseDown;
		this.scrolldown.onmouseup = SA_handleBtnRightMouseUp;
		this.scrolldown.onmouseout = SA_handleBtnRightMouseOut;
		this.scrolldown.onmouseover = SA_handleBtnRightMouseOver;
		
	}
	else
	{

		this.scrollup = document.getElementById('scroll-right-'+this.index);
		this.scrolldown = document.getElementById('scroll-left-'+this.index);
		this.scrollup.style.display = "none";
		this.scrolldown.style.display = "none";
		//this.scrollContent.style.width = this.element.offsetWidth + "px";
		
	}
}


function SA_handleBtnLeftMouseDown(evt)
{
    var sa = SA_scrollAreas[this.index];
  if(!isDisabled(sa.scrollup))
  {
    if(!sa.scrolling)
    {
      sa.scrolling = true;
      sa.scrollLeft();
      if (evt && evt.preventDefault) //disable default behavior in Mac FF
      {
        evt.preventDefault();
        evt.stopPropagation();
      }
    }
  }
}

function SA_handleBtnLeftMouseUp()
{
	//SA_scrollAreas[this.index].scrolling = false;
}

function SA_handleBtnLeftMouseOut()
{
	//SA_scrollAreas[this.index].scrolling = false;
}

function SA_handleBtnLeftMouseOver()
{
}

function SA_handleBtnRightMouseDown(evt)
{

	var sa = SA_scrollAreas[this.index];

  if(!isDisabled(sa.scrolldown))
  {
    if(!sa.scrolling)
    {
      sa.scrolling = true;
      sa.scrollRight();
      if (evt && evt.preventDefault) //disable default behavior in Mac FF
      {
        evt.preventDefault();
        evt.stopPropagation();
      }
    }
  }
}

function SA_handleBtnRightMouseUp()
{
	//SA_scrollAreas[this.index].scrolling = false;
}

function SA_handleBtnRightMouseOut()
{
	//SA_scrollAreas[this.index].scrolling = false;
}

function SA_handleBtnRightMouseOver()
{
}

function SA_scrollLeft()
{
  if (this.scrollIndex == 0)
    return;

  var moveBy = 0;

  if (this.scrollIndex == (this.segments - 1)) // we're at the end
    moveBy = this.scrollContent.offsetWidth - (this.distance * (this.segments - 1));
  else 
    moveBy = this.distance;

  var newPosition = this.scrollContent.offsetLeft + moveBy;

	OnNavigation(this.scrolldown);

  this.scrollIndex--;
  if(this.scrollIndex == 0)
  	OffNavigation(this.scrollup);

  var elScroll = new fx.ElementScroll(this.scrollContent, {duration: 600, transition: fx.sinoidal, onComplete: markDone, parent: this });
	elScroll.tween(newPosition);
}

function SA_Ext_scrollLeft(index)
{
	SA_scrollAreas[index].scrollLeft();
}

function SA_scrollRight()
{
  if (this.scrollIndex == (this.segments - 1)) // index is 0 based
    return;

  var spaceLeft = this.scrollContent.offsetWidth - Math.abs(this.scrollContent.offsetLeft) - this.distance;
  var moveBy = 0;
  var newPosition = 0;

  if (spaceLeft < this.distance)
    moveBy = spaceLeft;
  else 
    moveBy = this.distance;

  var newPosition = this.scrollContent.offsetLeft - moveBy;
  
  OnNavigation(this.scrollup);

  this.scrollIndex++;

  if (this.scrollIndex == (this.segments - 1))
  	OffNavigation(this.scrolldown);

  var elScroll = new fx.ElementScroll(this.scrollContent, {duration: 600, transition: fx.sinoidal, onComplete: markDone, parent: this });
	elScroll.tween(newPosition);
}

function markDone()
{
  this.options.parent.scrolling = false;
}

function SA_Ext_scrollRight(index)
{
	SA_scrollAreas[index].scrollRight();
}

function SA_handleMouseWheel()
{
	//Alert(this.index);
	var sa = SA_scrollAreas[this.index];
	sa.scrolling = true;
	sa.scrollingLimit = sa.wheelSensitivity;
	if (event.wheelDelta > 0)
		sa.scrollLeft();
	else
		sa.scrollRight();
}

function SA_handleOnScroll()
{
	event.srcElement.doScroll("pageLeft");
}

function getRealTop(elem)
{
	if (elem.offsetParent.tagName == "BODY")
		return elem.offsetTop;
	else
		return (elem.offsetTop + getRealTop(elem.offsetParent));
}

function OnOffNavigation(elem)
{
	var tempClass = elem.className;
	elem.className = elem.previousClass;	
	elem.previousClass = tempClass;
}

function OnNavigation(elem)
{
	if(elem.className != elem.defaultClass)
	{
		elem.className = elem.defaultClass;
	}
}

function OffNavigation(elem)
{
	if(elem.className == elem.defaultClass)
	{
		elem.className = elem.defaultClass + '-close';
	}
}

function isDisabled(elem)
{
	if(elem.className == elem.defaultClass)
		return false;
  else
    return true;
}
