﻿var _moduleBar = null;

function ModuleBar(parentDiv) {
  // stop any other instance  
  if ( (_moduleBar) && (_moduleBar.modules) && (_moduleBar.haltScroll))
    _moduleBar.haltScroll();

  _moduleBar = this;
  this.wrapperX = 0;
  this.modules = new Array();
  this.Ymovement = 10;
  this.moduleWidth = 68;
  this.moduleCount = 4.2;
  
  this.speed = 110;
  this.interval = 0;
  this.dirMultiplier = 0;
  this.currX = 0;
  this.currIconIdx = 0;

  this.clipTag = document.getElementById(parentDiv);
  this.clipTag.style.width = this.moduleCount * this.moduleWidth;

  this.contentTag = document.createElement("DIV");
  this.contentTag.className = "contentLayer";
  this.clipTag.appendChild(this.contentTag);

this.scroll = function(direction) {
  this.maxX = this.moduleWidth * (this.modules.length  - this.moduleCount);
  switch (direction) {
	case "left" : this.dirMultiplier = 2; break;
	case "right" : this.dirMultiplier = -2; break;
  }
  this.lastTime = ( new Date() ).getTime();
  if (!this.interval) {
	this.stopping = false;
	this.interval = setInterval("_moduleBar.startScroll()",1); 
  }
}

this.setState = function() {
    
  this.currX <= -this.maxX ? document.getElementById("right").style.visibility = "hidden" : document.getElementById("right").style.visibility = "visible";
  this.currX == 0 ? document.getElementById("left").style.visibility = "hidden" : document.getElementById("left").style.visibility = "visible";
}

this.getModuleById = function(moduleId) {
  var index = 0;
  while (this.modules[index].moduleId != moduleId) index++;
  return index+1;
}

this.scrollToId = function(moduleId) {
  this.scrollTo(this.getModuleById(moduleId));
}

this.jumpTo = function(offset) {
  this.currX = parseInt(offset);
  this.contentTag.style.left = parseInt(offset) + "px";
  this.setState();
}

this.scrollTo = function(moduleNumber) {
  this.maxX = this.moduleWidth * (this.modules.length  - this.moduleCount);
  var inView = false;
//alert(this.maxX);
  if (this.modules[moduleNumber-1].getPosition() < 0) {
	  var stopAt = -((moduleNumber-1) * this.moduleWidth)      
	  this.dirMultiplier = 4
	inView = true;
  } else if (this.modules[moduleNumber-1].getPosition() > (this.moduleCount-1) ) {
	  var stopAt = ((  this.moduleCount * this.moduleWidth)  - (moduleNumber * this.moduleWidth));      
	  this.dirMultiplier = -4
	inView = true;
  } 
  if (inView && !this.interval) {
	this.lastTime = ( new Date() ).getTime();
	this.stopping = false;
	this.interval = setInterval("_moduleBar.startScroll(" + stopAt + ")",1); 
  }
  this.modules[moduleNumber-1].setClick();
  this.setState();
}

this.startScroll = function(stopAt) {
  if (! (document.getElementById("right")))
  {
  	this.haltScroll()
  	return;
  }
  if (this.delay()) this.contentTag.style.left = (this.currX += this.dirMultiplier) + "px";
  this.setState();
  if ((this.currX == -this.maxX) || (this.currX == 0) || ((this.currX == stopAt) || (this.stopping && -this.currX % this.moduleWidth == 0))) {
	this.haltScroll()
  } 
}

this.delay = function() {
  this.now = ( new Date() ).getTime();
  var d = (this.now - this.lastTime)/100 * this.speed;
  if (d > 1) {
	this.lastTime = this.now;
	return true;
  } else return false;
}

this.stopScroll = function() {
  this.stopping = true;
}

this.haltScroll = function() {
//alert("currX:" + this.currX + " | maxX: " + this.maxX);
  clearInterval(this.interval);
  this.interval = 0;
  this.stopping = false;
}

this.goToModule = function(moduleId,noHistory) {

    this.modules[this.getModuleById(moduleId)-1].click(noHistory);

    this.scrollTo(this.getModuleById(moduleId));

}

this.goToLastModule = function()
{
    this.scrollTo(this.modules.length);
    this.modules[this.modules.length-1].click();

}

this.goToFirstModule = function(noHistory)
{
    this.scrollTo(1);
    this.modules[0].click(noHistory);
}

this.moveNext = function()
{
	for (var zm=0; zm < (this.modules.length - 1) ; zm++) {    
	  if (this.modules[zm].currentSelected == true) 
	  {
		this.scrollTo( zm + 2 );
		this.modules[zm+1].click();
		return;
	  }
	}
	this.haltScroll();
	tabNext();
	return;
}

this.movePrev = function()
{
	for (var zm=0; zm < (this.modules.length) ; zm++) {    
	  if (this.modules[zm].currentSelected == true) 
	  {
	    if (zm==0)
	    {
	        tabPrev();
	        return;
	    }
		this.scrollTo( zm );
		this.modules[zm-1].click();
		return;
	  }
	}
	return;
}


} // End ModuleBar


function IconObject(iconTitle, iconName, moduleId, previousSelected, currentSelected) {

  this.getPosition = function() {
	return(_moduleBar.currX / _moduleBar.moduleWidth + this.moduleNumber);
  }

  this.setState = function() {
	this.currentSelected ? this.txtTag.className = "scrollIconText scrollIconTextCurrent" : this.beenSelected ? this.txtTag.className = "scrollIconText scrollIconTextPrevious" : this.txtTag.className = "scrollIconText";
	this.currentSelected ? this.triangleTag.style.visibility = "visible" : this.triangleTag.style.visibility = "hidden";
	this.beenSelected ? this.divTag.setAttribute("src",this.iconOnName) : this.divTag.setAttribute("src",this.iconName); 
	//this.divTag.setAttribute("alt",this.iconTitle);
  }

  this.wrapperTag = document.createElement("DIV");
  this.divTag = document.createElement("IMG");
  this.txtTag = document.createElement("DIV"); 

  this.divObject = this.divTag;
  this.beenSelected = previousSelected ? 1 : 0;
  this.currentSelected = currentSelected ? 1 : 0;

  this.divTag.className = "imageElement";
  this.wrapperTag.className = "wrapperElement";
//  this.divTag.setAttribute("src",iconName);
  this.iconTitle = iconTitle;
  this.iconName = iconName.substr(0,iconName.lastIndexOf('.')) + '_off' + iconName.slice(iconName.lastIndexOf('.'));
  this.iconOnName = iconName.substr(0,iconName.lastIndexOf('.')) + '_on' + iconName.slice(iconName.lastIndexOf('.'));




this.triangleTag = document.createElement("IMG");
this.triangleTag.setAttribute("src","images/global/config/pointer.gif");
this.triangleTag.className = "triangle";
//this.triangleTag.appendChild(document.createTextNode("<!-- -->"));

  
  this.wrapperTag.style.left = _moduleBar.wrapperX;
  this.wrapperTag.style.width = _moduleBar.moduleWidth;
  _moduleBar.wrapperX += _moduleBar.moduleWidth;

  this.txtTag.appendChild(document.createTextNode(iconTitle));
  this.setState();
   
  this.wrapperTag["onmouseover"] = new Function("_moduleBar.modules[" + _moduleBar.modules.length + "].raise()");
  this.wrapperTag["onmouseout"] = new Function("_moduleBar.modules[" + _moduleBar.modules.length + "].lower()");
  //this.wrapperTag["onclick"] = new Function("_moduleBar.modules[" + _moduleBar.modules.length + "].click()");
  
  this.wrapperTag["onclick"] = new Function("if (_moduleBar.modules[" + _moduleBar.modules.length + "].moduleId==1){location.href='special?id=3733&ref=1'};if (_moduleBar.modules[" + _moduleBar.modules.length + "].moduleId==2){location.href='special?id=3733&ref=2'};if (_moduleBar.modules[" + _moduleBar.modules.length + "].moduleId==3){location.href='special?id=3733&ref=3'};if (_moduleBar.modules[" + _moduleBar.modules.length + "].moduleId==4){location.href='special?id=3733&ref=4'};");
  //document.getElementById('divM').style.display='none';document.getElementById('divGift').style.display='none';
   
  this.wrapperTag.appendChild(this.divTag);
  this.wrapperTag.appendChild(this.triangleTag);
  this.wrapperTag.appendChild(this.txtTag);
  _moduleBar.contentTag.appendChild(this.wrapperTag);
   
  this.divTag.style.top = _moduleBar.Ymovement+"px";
  this.currY = this.minY = this.range = _moduleBar.Ymovement;

  this.moduleId = moduleId
  this.interval = 0;
  this.maxY = this.minY - this.range;
  this.moduleNumber = _moduleBar.modules.length;
  _moduleBar.modules[this.moduleNumber] = this;
  if (this.currentSelected)
	_moduleBar.currIconIdx = this.moduleNumber;
  

  this.raise = function() {
	this.currentSelected ? this.txtTag.className = "scrollIconText scrollIconTextCurrent" : this.txtTag.className = "scrollIconTextHover scrollIconText" ;
	if (this.interval != 0) {
	  this.stopMotion();
	}
	this.direction=-1;
	this.interval = setInterval("_moduleBar.modules[" + this.moduleNumber + "].startMotion()",1); 
  }

  this.lower = function() {
  	this.currentSelected ? this.txtTag.className = "scrollIconText scrollIconTextCurrent" : this.beenSelected ? this.txtTag.className = "scrollIconText scrollIconTextPrevious" : this.txtTag.className = "scrollIconText";
    if (this.interval != 0) {
	  this.stopMotion();
	}
	this.direction=1
	this.interval = setInterval("_moduleBar.modules[" + this.moduleNumber + "].startMotion()",1); 
  }

  this.setClick = function() {
	_moduleBar.currIconIdx = this.moduleNumber;
	this.beenSelected = 1;
    for (i=0; i<_moduleBar.modules.length ; i++) {    
	  (i == this.moduleNumber) ? _moduleBar.modules[i].currentSelected = true : _moduleBar.modules[i].currentSelected = false; 
	  _moduleBar.modules[i].setState();
	}
  }

  this.click = function(noHistory) {

    onModuleChange(this.moduleId, noHistory); // this function in config_icon.js
    showHidePreviousBtn(this.moduleNumber);
    this.setClick();
  }

  this.startMotion = function() {
	if ((this.currY + this.direction >= this.maxY ) && (this.currY + this.direction <= this.minY)) {
	  this.currY += this.direction;
	  this.divObject.style.top = this.currY + "px"; 
	} else {
	  this.stopMotion();
	}
  }

  this.stopMotion = function() {
	clearInterval(this.interval);
	this.interval = 0;
  }

}  //End IconObject

