// psScroller.js
  
function prepScrollers()
{
  var divP = document.getElementById('productScroll');
  var menuS = document.getElementById('productNoScroll');
  var menuF = document.createElement('ul');
  var menuF2 = document.createElement('ul');
  
  menuF.setAttribute("id", "productsMenuF1");
  menuF2.setAttribute("id", "productsMenuF2");
  divP.appendChild(menuF);
  divP.appendChild(menuF2);
  
  var divC = document.getElementById('catalogScroll');
  var menuC = document.getElementById('catalogNoScroll');
  var menuF3 = document.createElement('ul');
  var menuF4 = document.createElement('ul');
  
  menuF3.setAttribute("id", "catalogsMenuF1");
  menuF4.setAttribute("id", "catalogsMenuF2");
  divC.appendChild(menuF3);
  divC.appendChild(menuF4);
}

function buildProductList()
{
  var docFrag = document.createDocumentFragment();
  var docFrag2 = document.createDocumentFragment();
  var menuS = document.getElementById('productsMenuSimple');
  var listA = menuS.getElementsByTagName('a');
  var listP = menuS.getElementsByTagName('img');
  var menuF = document.getElementById('productsMenuF1');
  var menuF2 = document.getElementById('productsMenuF2');
  var listI;
  var imgI;
  var imgP;
  var imgT;
  var imgA;
  var imgH;
  var totalWidthList1 = 176;
  var totalWidthList2 = 176;
  
  
  for(var i = 0; i < 4; i++)
  {
    listI = document.createElement("li");
    imgA = document.createElement("a");
    imgI = document.createElement("img");
    imgH = listA[i].getAttribute("href");
    imgP = listP[i].getAttribute("src");
    totalWidthList1 += parseInt(listP[i].getAttribute("width"))
    imgT = listA[i].firstChild.nodeValue;
    imgI.setAttribute("src", imgP);
    imgI.setAttribute("alt", imgT);
    imgI.setAttribute("title", imgT);
    imgA.setAttribute("href", imgH);
    imgI.onmouseover = function() {
      stopScrollP();
    }
    imgI.onmouseout = function() {
      startScrollP();
    }
    imgA.appendChild(imgI);
    listI.appendChild(imgA);
    docFrag.appendChild(listI);
  }
  
  for(var j = 4; j < 8; j++)
  {
    listI = document.createElement("li");
    imgA = document.createElement("a");
    imgI = document.createElement("img");
    imgH = listA[j].getAttribute("href");
    imgP = listP[j].getAttribute("src");
    totalWidthList2 += parseInt(listP[j].getAttribute("width"));
    imgT = listA[j].firstChild.nodeValue;
    imgI.setAttribute("src", imgP);
    imgI.setAttribute("alt", imgT);
    imgI.setAttribute("title", imgT);
    imgA.setAttribute("href", imgH);
    imgI.onmouseover = function() {
      stopScrollP();
    }
    imgI.onmouseout = function() {
      startScrollP();
    }
    imgA.appendChild(imgI);
    listI.appendChild(imgA);
    docFrag2.appendChild(listI);
  }

  menuF.appendChild(docFrag);
  menuF2.appendChild(docFrag2);
  menuF.setAttribute("width", totalWidthList1);
  menuF2.setAttribute("width", totalWidthList2);
  menuS.style.display = "none";
  menuF.style.display = "block";
  menuF2.style.display = "block";
  
  //alert(totalWidthList1 + ", " + totalWidthList2);
}

function buildCatalogList()
{
  var menuS = document.getElementById('catalogsMenuSimple');
  var listA = menuS.getElementsByTagName('a');
  var listP = menuS.getElementsByTagName('img');
  var listD = menuS.getElementsByTagName('p');
  var menuF = document.getElementById('catalogsMenuF1');
  var menuF2 = document.getElementById('catalogsMenuF2');
  var titleB = document.getElementById('catalogInfoBox');
  var titleH = document.getElementById('catHeader');
  var titleI = document.getElementById('catInfo');
  var listI;
  var imgI;
  var imgP;
  var imgT;
  var imgA;
  var imgH;
  var imgD;
  
  for(var i = 0; i < 3; i++)
  {
    listI = document.createElement("li");
    imgA = document.createElement("a");
    imgI = document.createElement("img");
    imgD = listD[i].firstChild.nodeValue;
    imgH = listA[i].getAttribute("href");
    imgP = listP[i].getAttribute("src");
    imgT = listA[i].firstChild.nodeValue;
    imgI.setAttribute("src", imgP);
    imgI.setAttribute("alt", imgT);
    imgI.setAttribute("title", imgT);
    imgA.setAttribute("title", imgD);
    imgA.setAttribute("id", imgT);
    imgA.setAttribute("href", imgH);
    imgA.onmouseover = function() {
      stopScrollC();
      titleB.style.display = "block";
      titleH.firstChild.nodeValue = this.id;
      titleI.firstChild.nodeValue = this.title;
    }
    imgA.onmouseout = function() {
      startScrollC();
      titleB.style.display = "none";
      titleH.firstChild.nodeValue = "";
      titleI.firstChild.nodeValue = "";
    }
    imgA.appendChild(imgI);
    listI.appendChild(imgA);
    menuF.appendChild(listI);
  }
  
  for(var j = 3; j < 6; j++)
  {
    listI = document.createElement("li");
    imgA = document.createElement("a");
    imgI = document.createElement("img");
    imgD = listD[j].firstChild.nodeValue;
    imgH = listA[j].getAttribute("href")
    imgP = listP[j].getAttribute("src");
    imgT = listA[j].firstChild.nodeValue;
    imgI.setAttribute("src", imgP);
    imgI.setAttribute("alt", imgT);
    imgI.setAttribute("title", imgT);
    imgA.setAttribute("title", imgD);
    imgA.setAttribute("id", imgT);
    imgA.setAttribute("href", imgH);
    imgA.onmouseover = function() {
      stopScrollC();
      titleB.style.display = "block";
      titleH.firstChild.nodeValue = this.id;
      titleI.firstChild.nodeValue = this.title;
    }
    imgA.onmouseout = function() {
      startScrollC();
      titleB.style.display = "none";
      titleH.firstChild.nodeValue = "";
      titleI.firstChild.nodeValue = "";
    }
    imgA.appendChild(imgI);
    listI.appendChild(imgA);
    menuF2.appendChild(listI);
  }
  
  menuS.style.display = "none";
  menuF.style.display = "block";
  menuF2.style.display = "block";
}

function getWidthListOne()
{
  var menu1 = document.getElementById('productsMenuF1');
  return parseInt(menu1.getAttribute("width"));
}

function getWidthListTwo()
{
  var menu2 = document.getElementById('productsMenuF2');
  return parseInt(menu2.getAttribute("width"));
}

var scrollSpeed = 25;
var timer;
var timer2;
var roundOne = 0;
var menu1scroll = 1;
var menu2scroll = 0;
var menu3scroll = 1;
var menu4scroll = 0;
var leftPos1 = 0;
var leftPos2 = 0;
var leftPos3 = 0;
var leftPos4 = -489;
var totalWidth1 = 0;
var totalWidth2 = 0;
var menuF;
var menuF2;

function scrollProducts()
{   
  var menuF = document.getElementById('productsMenuF1');   
  var menuF2 = document.getElementById('productsMenuF2');
  
  if(roundOne == 0)
  { 
    menuF = document.getElementById('productsMenuF1');   
    menuF2 = document.getElementById('productsMenuF2');
    leftPos2 = getWidthListOne();
    totalWidth1 = leftPos2;
    totalWidth2 = getWidthListTwo();
    menuF2.style.left = leftPos2 + "px"
    roundOne = 1;
  }
  
  if(menu1scroll == 1)
  {
    leftPos1--;
    menuF.style.left = leftPos1 + "px";
    
    if(leftPos1 == -1 && menu2scroll == 0)
    {
      menu2scroll = 1;
    }
    
    if(leftPos1 == -totalWidth1)
    {
      menu1scroll = 0;
      menuF.style.left = totalWidth2 + "px";
      leftPos1 = totalWidth2;   
    } 
  }
  
  if(menu2scroll == 1)
  {
    leftPos2--;
    menuF2.style.left = leftPos2 + "px";
    
    if(leftPos2 == -1 && menu1scroll == 0)
    {
      menu1scroll = 1;
    }
    
    if(leftPos2 == -totalWidth2)
    {
      menu2scroll = 0;
      menuF2.style.left = totalWidth1 + "px";
      leftPos2 = totalWidth1;    
    }  
  }  
}

function scrollCatalogs()
{     
  var menuF = document.getElementById('catalogsMenuF1');   
  var menuF2 = document.getElementById('catalogsMenuF2');
    
  if(menu3scroll == 1)
  {
    leftPos3++;
    menuF.style.left = leftPos3 + "px";
    
    if(leftPos3 == 1 && menu4scroll == 0)
    {
      menu4scroll = 1;
    }
    
    if(leftPos3 == 489)
    {
      menu3scroll = 0;
      menuF.style.left = -489 + "px";
      leftPos3 = -489;   
    } 
  }
  
  if(menu4scroll == 1)
  {
    leftPos4++;
    menuF2.style.left = leftPos4 + "px";
    
    if(leftPos4 == 1 && menu3scroll == 0)
    {
      menu3scroll = 1;
    }
    
    if(leftPos4 == 489)
    {
      menu4scroll = 0;
      menuF2.style.left = -489 + "px";
      leftPos4 = -489;    
    }  
  }  
}

function startScrollP()
{
  timer = setInterval("scrollProducts()", scrollSpeed);
}

function startScrollC()
{
  timer2 = setInterval("scrollCatalogs()", scrollSpeed);
}

function stopScrollP()
{
  clearInterval(timer);
}

function stopScrollC()
{
  clearInterval(timer2);
}

function addLoadEvent(func)
   {
     var oldonload = window.onload;
     if(typeof window.onload != 'function')
     {
       window.onload = func;
     }
     else
     {
       window.onload = function() {
        oldonload();
        func();
       }
     }
   }
   
addLoadEvent(prepScrollers);
addLoadEvent(buildProductList);
addLoadEvent(buildCatalogList);
addLoadEvent(startScrollP);
addLoadEvent(startScrollC);
