/*
#################################################
#################################################
#####                                       #####
#####  REALIZAT DE RAZVAN CALUGARASU	    #####
#####                                       #####
#####  Nu ai voie sa stergi acest mesaj     #####
#####                                       #####
#####  Toate drepturile imi sunt rezervate  #####
#####  Aceasta este doar o licenta. Nu vei  #####
#####  putea redistribui scriptul fara      #####
#####  permisiunea mea.                     #####
#####                                       #####
#####  Pentru nelamuriri contacteaza - ma la  #####
#####  adresa de mail raku_cmr@yahoo.com.   #####
#####                                       #####
#################################################
#################################################
 */
function addEvent(obj, event_name, func_name)
{
   if (obj.attachEvent)
   {
      obj.attachEvent("on" + event_name, func_name);
   }
   else if(obj.addEventListener)
   {
      obj.addEventListener(event_name, func_name, true);
   }
   else
   {
      obj["on" + event_name] = func_name;
   }
}
function removeEvent(obj, event_name, func_name)
{
   if (obj.detachEvent)
   {
      obj.detachEvent("on" + event_name, func_name);
   }
   else if(obj.removeEventListener)
   {
      obj.removeEventListener(event_name, func_name, true);
   }
   else
   {
      obj["on" + event_name] = null;
   }
}
function randOrd ()
{
   return (Math.round(Math.random()) - Math.random());
}

function Scroller(parent, direction, speed, width, height, randomize)
{
   var scrollerParent = parent;
   var speed = speed;
   var speedNormal = speed;
   var direction = direction;
   // directia : TopBottom, BottomTop, LeftRight, RightLeft
   var width = width;
   var height = height;
   var randomize = randomize;
   var content = new Array();
   var container = document.createElement("div");
   var track;
   var trackS;
   var tableHeight = 0;
   var tableWidth = 0;
   var paddingWidth = 10;
   // asta seteaza spatiu
   var interval = null;
   var imgs = new Array();
   addEvent(container, "mouseover", stop);
   addEvent(container, "mouseout", start);
   function stop()
   {
      clearInterval(interval);
   }
   function start()
   {
      interval = setInterval(function()
      {
         setPosition()
      }
      , 100);
   }
   function createTrack()
   {
      switch(direction)
      {
         case "TopBottom" :
         case "BottomTop" :
            track = document.createElement("table");
            track.colspan = "0px";
            track.cellspan = "0px";
            track.style.position = "absolute";
            var trackBody = document.createElement("tbody");
            for(i = 0; i < content.length; i ++ )
            {
               var newTR = document.createElement("tr");
               var newTD = document.createElement("td");
               var newIMG = new Image();
               newIMG.src = content[i][0];
               newIMG.alt = content[i][1];
               newIMG.className = "imgNoBorder";
               // tableHeight += newIMG.height + 2 * paddingWidth;
               var newA = document.createElement("a");
               newA.href = content[i][2];
               newA.target = "_blank";
               newA.appendChild(newIMG);
               newTD.className = "tdScroller";
               width > height ? newTD.style.height = height : newTD.style.width = width;
               newTD.appendChild(newA);
               newTR.appendChild(newTD);
               trackBody.appendChild(newTR);
            }
            track.appendChild(trackBody);
            break;
         case "LeftRight" :
         case "RightLeft" :
            track = document.createElement("table");
            track.colspan = "0px";
            track.cellspan = "0px";
            track.style.position = "absolute";
            var trackBody = document.createElement("tbody");
            var newTR = document.createElement("tr");
            for(i = 0; i < content.length; i ++ )
            {
               var newTD = document.createElement("td");
               var newIMG = new Image();
               newIMG.src = content[i][0];
               newIMG.alt = content[i][1];
               newIMG.className = "imgNoBorder";
               var newA = document.createElement("a");
               newA.href = content[i][2];
               newA.target = "_blank";
               newA.appendChild(newIMG);
               newTD.className = "tdScroller";
               width > height ? newTD.style.height = height : newTD.style.width = width;
               newTD.appendChild(newA);
               newTR.appendChild(newTD);
            }
            trackBody.appendChild(newTR);
            track.appendChild(trackBody);
            break;
      }
   }
   this.addContent = function(newContent)
   {
      // tre sa fie un vector de forma ["imagine", "descriere", "link"]
      content.push(newContent);
   }
   function loadImages(i)
   {
      preIMG = new Image();
      preIMG.onload = function()
      {
         tableHeight += preIMG.height + 2 * paddingWidth;
         tableWidth += preIMG.width + 2 * paddingWidth;
         if(i < content.length - 1)
         {
            loadImages(i + 1);
         }
         else
         {
            switch(direction)
            {
               case "TopBottom" :
               track.style.height = tableHeight + "px";
               trackS = track.cloneNode(true);
               track.style.top = - parseInt(track.style.height) + "px";
               trackS.style.top = - 2 * parseInt(track.style.height) + "px";
               container.appendChild(track);
               container.appendChild(trackS);
               break;
               case "BottomTop" :
               track.style.height = tableHeight + "px";
               trackS = track.cloneNode(true);
               track.style.top = parseInt(height) + "px";
               trackS.style.top = parseInt(height) + parseInt(track.style.height) + "px";
               container.appendChild(track);
               container.appendChild(trackS);
               break;
               case "LeftRight" :
               track.style.width = tableWidth + "px";
               trackS = track.cloneNode(true);
               track.style.left = - parseInt(track.style.width) + "px";
               trackS.style.left = - 2 * parseInt(track.style.width) + "px";
               container.appendChild(track);
               container.appendChild(trackS);
               break;
               case "RightLeft" :
               track.style.width = tableWidth + "px";
               trackS = track.cloneNode(true);
               track.style.left = parseInt(width) + "px";
               trackS.style.left = parseInt(width) + parseInt(track.style.width) + "px";
               container.appendChild(track);
               container.appendChild(trackS);
               break;
            }
            start();
         }
      }
      preIMG.src = content[i][0];
   }
   this.create = function()
   {
      if(randomize)
      {
         content.sort(randOrd);
      }
      createTrack();
      loadImages(0);
      container.style.overflow = "hidden";
      container.style.position = "relative";
      container.style.width = width;
      container.style.height = height;
      container.className = "divContainer";
      document.getElementById(scrollerParent).appendChild(container);
   }
   function setPosition()
   {
      switch(direction)
      {
         case "TopBottom" :
         track.style.top = parseInt(track.style.top) + speed + "px";
         trackS.style.top = parseInt(trackS.style.top) + speed + "px";
         if(parseInt(track.style.top) >= parseInt(height))
         {
            track.style.top = parseInt(trackS.style.top) - parseInt(track.style.height) + "px";
         }
         if(parseInt(trackS.style.top) >= parseInt(height))
         {
            trackS.style.top = parseInt(track.style.top) - parseInt(track.style.height) + "px";
         }
         break;
         case "BottomTop" :
         track.style.top = parseInt(track.style.top) - speed + "px";
         trackS.style.top = parseInt(trackS.style.top) - speed + "px";
         if(parseInt(track.style.top) <= - parseInt(track.style.height))
         {
            track.style.top = parseInt(trackS.style.top) + parseInt(track.style.height) + "px";
         }
         if(parseInt(trackS.style.top) <= - parseInt(track.style.height))
         {
            trackS.style.top = parseInt(track.style.top) + parseInt(track.style.height) + "px";
         }
         break;
         case "LeftRight" :
         track.style.left = parseInt(track.style.left) + speed + "px";
         trackS.style.left = parseInt(trackS.style.left) + speed + "px";
         if(parseInt(track.style.left) >= parseInt(width))
         {
            track.style.left = parseInt(trackS.style.left) - parseInt(track.style.width) + "px";
         }
         if(parseInt(trackS.style.left) >= parseInt(width))
         {
            trackS.style.left = parseInt(track.style.left) - parseInt(track.style.width) + "px";
         }
         break;
         case "RightLeft" :
         track.style.left = parseInt(track.style.left) - speed + "px";
         trackS.style.left = parseInt(trackS.style.left) - speed + "px";
         if(parseInt(track.style.left) <= - parseInt(track.style.width))
         {
            track.style.left = parseInt(trackS.style.left) + parseInt(track.style.width) + "px";
         }
         if(parseInt(trackS.style.left) <= - parseInt(track.style.width))
         {
            trackS.style.left = parseInt(track.style.left) + parseInt(track.style.width) + "px";
         }
         break;
      }
   }
}