addLoadListener(initNewsTicker);

function initNewsTicker()
{
  var newsScroller = document.getElementById("newsScroller");
  var newsScroller2 = document.getElementById("newsScroller2");
  var newsTicker = document.getElementById("newsTicker");

  newsScroller.style.left = 0;
  newsScroller2.style.left = 0;
  newsTicker.stopped = false;
  if (retrieveComputedStyle(newsScroller, "position") == "relative")
  {
    var relativeWidth = newsScroller.offsetWidth;

    newsScroller.style.position = "absolute";
    newsScroller.calculatedWidth = newsScroller.offsetWidth;

    if (relativeWidth > newsScroller.calculatedWidth)
    {
      newsScroller.calculatedWidth = relativeWidth;
    }

    newsScroller.style.position = "relative";
  }
  else
  {
    newsScroller.calculatedWidth = newsScroller.clientWidth;
  }

   if (retrieveComputedStyle(newsScroller2, "position") == "relative")
  {
    var relativeWidth2 = newsScroller2.offsetWidth;

    newsScroller2.style.position = "absolute";
    newsScroller2.calculatedWidth = newsScroller2.offsetWidth;

    if (relativeWidth > newsScroller2.calculatedWidth)
    {
      newsScroller2.calculatedWidth = relativeWidth2;
    }

    newsScroller2.style.position = "relative";
  }
  else
  {
    newsScroller2.calculatedWidth = newsScroller2.clientWidth;
  }
  
   newsScroller2.style.left = (parseInt(newsScroller.style.left)+newsScroller.offsetWidth) + "px";
  
  
   attachEventListener(newsTicker, "mouseover", clickRunLink, false);
   attachEventListener(newsTicker, "mouseout", clickStopLink, false);
  

  //moveNewsScroller();
 clearTimeout(document.getElementById("newsScroller").timeout);
  return true;
}

function moveNewsReverse()
{
  
  var newsScroller = document.getElementById("newsScroller");
  var newsScroller2 = document.getElementById("newsScroller2");
  var stopLink = document.getElementById("newsTicker");
  var increment = 10 -  stopLink.stopped;
	
  var cont = document.getElementById("newsTicker");
  
  var pwidth = cont.offsetWidth;
  
  
  var currLeft = parseInt(newsScroller.style.left);
  var currLeft2 = parseInt(newsScroller2.style.left);
  
  
  
  
  
  if(currLeft<currLeft2)
  { 


  
  if (currLeft2 > pwidth)
  {  
     
     //newsScroller.style.left = 0;
     newsScroller2.style.left = (parseInt(newsScroller.style.left)-newsScroller2.offsetWidth) + "px";
    	 
	 
	 
  }
  else
  {
    newsScroller2.style.left = (parseInt(newsScroller2.style.left) + increment) + "px";
	
    newsScroller.style.left = (parseInt(newsScroller2.style.left)-newsScroller.offsetWidth) + "px";
  }

  
  }else{


     if (currLeft > pwidth)
  {

   //newsScroller2.style.left = 0;
   newsScroller.style.left = (parseInt(newsScroller2.style.left)-newsScroller.offsetWidth) + "px";
	
  }
  else
  {
    newsScroller.style.left = (parseInt(newsScroller.style.left) + increment) + "px";
	
    newsScroller2.style.left = (parseInt(newsScroller.style.left)-newsScroller2.offsetWidth) + "px";
  }

  
  
  
  
  
  
  
  }
  
  
  
  
  
  
  newsScroller.timeout = setTimeout("moveNewsReverse()", 20);

  
  
  
  
  
  
  
  
  
  
  
  return true;
}


function moveNewsScroller()
{
  
  var newsScroller = document.getElementById("newsScroller");
  var newsScroller2 = document.getElementById("newsScroller2");
  var stopLink = document.getElementById("newsTicker");
  var increment = 10 - stopLink.stopped;
  
  var currLeft = parseInt(newsScroller.style.left);
  var currLeft2 = parseInt(newsScroller2.style.left);
  
  
  if(currLeft<currLeft2)
  {
  if (currLeft < newsScroller.calculatedWidth * -1)
  {
  
    newsScroller.style.left = (parseInt(newsScroller2.style.left)+newsScroller2.offsetWidth) + "px";
	 //newsScroller2.style.left = 0;
	 
    	 
	 
	 
  }
  else
  {
    newsScroller.style.left = (parseInt(newsScroller.style.left) - increment) + "px";
	
    newsScroller2.style.left = (parseInt(newsScroller.style.left)+newsScroller.offsetWidth) + "px";
  }

  
  }else{

 
     if (currLeft2 < newsScroller2.calculatedWidth * -1)
  {
  
   newsScroller2.style.left = (parseInt(newsScroller.style.left)+newsScroller.offsetWidth) + "px";
	//newsScroller.style.left = 0;
  }
  else
  {
    newsScroller2.style.left = (parseInt(newsScroller2.style.left) - increment) + "px";
	
    newsScroller.style.left = (parseInt(newsScroller2.style.left)+newsScroller2.offsetWidth) + "px";
  }

  
  
  
  
  
  
  
  }
  
  
  
  
  

  newsScroller.timeout = setTimeout("moveNewsScroller()",20);

  
  
  
  
  
  
  
  
  
  
  
  return true;
}


function clickStopLink()
{
  var stopLink = document.getElementById("newsTicker");

    clearTimeout(document.getElementById("newsScroller").timeout);
    stopLink.stopped = false;
 

  return true;
}


function clickRunLink(event)
{  
  var minspeed = 10;
  var stopLink = document.getElementById("newsTicker");
  clearTimeout(document.getElementById("newsScroller").timeout);
    
    stopLink.stopped = false;
   var scrollingPosition = getScrollingPosition();
  var cursorPosition = [0, 0];
  
  if (typeof event == "undefined")
  {
    event = window.event;
  }
  
  
  
  if (typeof event.pageX != "undefined" && typeof event.x != "undefined")
  {
    cursorPosition[0] = event.pageX;
    cursorPosition[1] = event.pageY;
  }
  else
  {
    cursorPosition[0] = event.clientX + scrollingPosition[0];
    cursorPosition[1] = event.clientY + scrollingPosition[1];
  }
  
  //alert(cursorPosition[0]);
  
  //alert(stopLink.offsetWidth);
  

   var posdiv = getPosition(stopLink);
   posdiv = posdiv[0];
      
  // finding the position on element:
  var onpos = cursorPosition[0] - posdiv;
  
  //alert(onpos);  
  if(onpos<=stopLink.offsetWidth/2){
  
  var wdivhaf = stopLink.offsetWidth/2;
  
  // finding coeficient :
  
  var cof=onpos/wdivhaf*100;
  
 // alert(cof);
  //alert(100 - Math.round(cof) + " left");
    var cof =  minspeed * (Math.round(cof)/100);
	
	//alert(cof);
	if(!stopLink.stopped){
	  
	  stopLink.stopped =  Math.round(cof);
	  
	  //alert(stopLink.stopped);
	  
      moveNewsScroller();
   }else
    stopLink.stopped =  Math.round(cof);
  
  
  
  }else{
  
   var wdivhaf = stopLink.offsetWidth/2;
  
  // finding coeficient :

 onpos = onpos-wdivhaf;
    
  var cof=minspeed - minspeed*(onpos/wdivhaf);
  
 // alert(cof);
   if(!stopLink.stopped){
   stopLink.stopped =  Math.round(cof);
   
     //alert(stopLink.stopped);
   
   
   moveNewsReverse();
   }else 
   stopLink.stopped =  Math.round(cof);
   
  
  }
  
  
  return true;
}

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}

function attachEventListener(target, eventType, functionRef, capture)
{
  if (typeof target.addEventListener != "undefined")
  {
      target.addEventListener(eventType, functionRef, capture);
  }
  else if (typeof target.attachEvent != "undefined")
  {
      target.attachEvent("on" + eventType, functionRef);
  }
  else
  {
    eventType = "on" + eventType;

    if (typeof target[eventType] == "function")
    {
      var oldListener = target[eventType];

      target[eventType] = function()
      {
        oldListener();

        return functionRef();
      }
    }
    else
    {
      target[eventType] = functionRef;
    }
  }

  return true;
}

function retrieveComputedStyle(element, styleProperty)
{
  var computedStyle = null;

  if (typeof element.currentStyle != "undefined")
  {
    computedStyle = element.currentStyle;
  }
  else
  {
    computedStyle = document.defaultView.getComputedStyle(element, null);
  }

  return computedStyle[styleProperty];
  
  
}


function getScrollingPosition()
{
  var position = [0, 0];

  if (typeof window.pageYOffset != 'undefined')
  {
    position = [
        window.pageXOffset,
        window.pageYOffset
    ];
  }

  else if (typeof document.documentElement.scrollTop != 'undefined'
      && document.documentElement.scrollTop > 0)
  {
    position = [
        document.documentElement.scrollLeft,
        document.documentElement.scrollTop
    ];
  }

  else if (typeof document.body.scrollTop != 'undefined')
  {
    position = [
        document.body.scrollLeft,
        document.body.scrollTop
    ];
  }

  return position;
}

function getPosition(theElement)
{
  var positionX = 0;
  var positionY = 0;

  while (theElement != null)
  {
    positionX += theElement.offsetLeft;
    positionY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
  
  return [positionX, positionY];
}

