var MAIN_TIMER_TIME=50;
var MAX_ANIM_STEP=2;
var MIN_ANIM_STEP=1;
var BORDER_AROUND=20;
var animStep=MAX_ANIM_STEP;
var mainTimer=null;
var movingPart=null;
var movingContent=null;
var movingContent_help=null;
var contentWidth=0;
var lastPos=null;
var parentElementName="movingPart";

function init(){
  try{
    // CHANGE ANIM STEP WHEN MOUSE IS OVER, OR OUT
    document.getElementById(parentElementName).onmouseover=function(){animStep=MIN_ANIM_STEP;}
    document.getElementById(parentElementName).onmouseout=function(){animStep=MAX_ANIM_STEP;}
  
    //movingPart=document.createElement("div");
    movingPart=document.getElementById("movingPart");
    movingPart.id="movingPart";
    movingPart.style.position="relative";
    movingPart.style.left=0;
    movingPart.style.top=0;
    movingPart.style.width=parseInt(document.getElementById(parentElementName).offsetWidth)-BORDER_AROUND;
    movingPart.style.overflow="hidden";
    
    movingContent=document.createElement("div");
    movingContent.id="movingContent";
    movingContent.style.position="relative";
    movingContent.style.left=0;
    movingContent.style.top=0;
    movingContent.style.width="100000";
    movingContent.style.whiteSpace="nowrap";
    movingContent.style.overflow="hidden";

    movingContent_help=document.createElement("span");
    movingContent_help.id="movingContent_help";
    movingContent_help.style.position="absolute";
    movingContent_help.style.left=0;
    movingContent_help.style.top=0;
    movingContent_help.style.height=0;
    movingContent_help.style.whiteSpace="nowrap";
    movingContent_help.style.visibility="hidden"; 
        
    movingContent.innerHTML=prepareSuitableContent(document.getElementById(parentElementName).innerHTML); // PREPARE CONTENT
    movingContent_help.innerHTML=movingContent.innerHTML;
    
    document.getElementById(parentElementName).style.overflow="hidden";
    document.getElementById(parentElementName).innerHTML="";
    movingPart.appendChild(movingContent);
    movingPart.appendChild(movingContent_help); // APPEND TEMPORARY SPAN WITH CONTENT                    
    //document.getElementById(parentElementName).appendChild(movingPart);
    
    movingContent.removeChild(movingContent.firstChild); // REMOVE FIRST SF
    movingContent.removeChild(movingContent.lastChild); // REMOVE LAST SF
    movingContent_help.removeChild(movingContent_help.firstChild); // REMOVE FIRST SF
    movingContent_help.removeChild(movingContent_help.lastChild); // REMOVE LAST SF

    contentWidth=document.getElementById("movingContent_help").offsetWidth; // GET CONTENT WIDTH    
    movingPart.removeChild(document.getElementById("movingContent_help"));  // REMOVE TEMPORARY SPAN WITH CONTENT
        
    mainTimer=setInterval(function(){startTimer();},MAIN_TIMER_TIME);        
  }
  catch(e){

  }
}
function prepareSuitableContent(mainContent){
  var tagsList=new Array("p","div"); // TAGS TO REMOVING
  for(var i=0;i<tagsList.length;i++){        
    var pattern1=new RegExp("<"+tagsList[i]+"[^<>]*>");
    var pattern2=new RegExp("</"+tagsList[i]+">");
    mainContent=replaceAll(mainContent,pattern1,"");
    mainContent=replaceAll(mainContent,pattern2,"");
  }
  return mainContent;  
}
function replaceAll(content,pattern,newValue){
	var mainContent = content;
	while (mainContent.search(pattern) > -1) mainContent=mainContent.replace(pattern,newValue);
	return mainContent;
}
function startTimer(){
  verifyShifterPosition();
  movingContent.style.left=parseInt(movingContent.style.left)-animStep;
}
function verifyShifterPosition(){
  if(parseInt(movingContent.style.left)+parseInt(contentWidth)<parseInt(movingPart.style.left)){
    movingContent.style.left=parseInt(movingPart.offsetWidth);
  }
}
window.onload=init;
