﻿var isIE = (document.all) ? true : false;

var $ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};

if(!isIE){
HTMLElement.prototype.__defineGetter__("currentStyle", function () {
   return this.ownerDocument.defaultView.getComputedStyle(this, null);
});
}

function getStyleWidth(obj)
{
if(!obj) return;
var w =obj.currentStyle.width? obj.currentStyle.width:null;
w = obj.style.width ? obj.style.width : w;
if(!w || w == 'auto'){
   w = obj.offsetWidth ? obj.offsetWidth : null;
}
w = parseInt(w);
return w;
}
function getStyleHeight(obj)
{
if(!obj) return;
var h =obj.currentStyle.height? obj.currentStyle.height:null;
h = obj.style.height ? obj.style.height : h;
if(!h || h == 'auto'){
   h = obj.offsetHeight ? obj.offsetHeight : null;
}
w = parseInt(h);
return h;
}

function trace(str)
{
if(! $('DebugBox')) return;
$('DebugBox').value += str + '\n'; 
}



function Xpnew_Marqueen(){

var _this = this;   //??????this?????this??,????????????_this????
this.step = 2;
this.Speed = 25;
this.Direction = 'left';
//????ID?????????
this.ID = document.getElementById(arguments[0]);
if(!this.ID)
{
   alert("?????\"" + arguments[0] + "\"?????\r\n?????ID??????!");
   this.ID = -1;
   return;
}
//??div???,?????ul?li
if(!this.ID.hasChildNodes()){
   alert("?????:????????????");//?????,?????????????,??????????
   return;
}
var nodeList01 = this.ID.childNodes; //nodeList01 ?div????????
var ULNode;    //ul??
//alert( this.ID.innerHTML);
//alert(nodeList01);

//????,??????????,??????xpath?? .//li,???????

for(var i =0; i< nodeList01.length ; i ++){
   if("UL" == nodeList01[i].nodeName){
    ULNode = nodeList01[i];
   }
}

//??li?????ul
//??div?????ul,????????div?li
var UL_new = document.createElement("UL");    //??ul

if(ULNode)
{
   var nodeList02 = ULNode.childNodes;
   var i = 0;
   while(nodeList02.length > 0)
   {
    trace('???????:' + i + ' name: ' + nodeList02[0].tagName);
    trace('\n******************************************\n');
    trace('ULNode???:' + nodeList02.length);
    if("LI" == nodeList02[0].tagName){
     UL_new.appendChild(nodeList02[0]);
    }
    else
    {
     ULNode.removeChild(nodeList02[0]);
    }
   
   }

  
}
else 
{
   for(var i =0; i< nodeList01.length ; i ++){
    if("LI" == nodeList01[i].tagName){
     UL_new.appendChild(nodeList01[i]);
     this.ID.removeChild(nodeList01[i]);//????lis
    }
   }
}

if(!UL_new.hasChildNodes())
{
   alert("?????:??????????????");//?????,?????????????,??????????
   return;
}


this.Player = document.createElement("DIV"); 
this.Box = document.createElement("DIV"); 
this.Player.appendChild(this.Box);
this.Box.appendChild(UL_new);
if(ULNode)
{
   ULNode.replaceNode(this.Player);
}
else
{
   this.ID.appendChild(this.Player);
}


//??div??????li???
var w1,h1,w2;
w1 = getStyleWidth(this.ID);
h1 = getStyleHeight(this.ID);
w2 = getStyleWidth(UL_new.childNodes[0]);
h2 = getStyleHeight(UL_new.childNodes[0]);

var w3 = w2 * UL_new.childNodes.length; //?????li???

var CopyTotal = Math.floor(parseInt(w1)/parseInt(w3)); 

var CopyHTML = UL_new.innerHTML;
w3 = w3 * ( CopyTotal + 1); 
for(var i = 0 ; i < CopyTotal; i ++)
{
   UL_new.innerHTML += CopyHTML;
} 


trace('w1: ' + w1 + ' h1: ' + h1 + ' w2: ' + w2 + ' h2: ' + h2 + ' w3: ' + w3 );
h1 = h1 > h2 ? h2 : h1;

this.Player.style.width = w1;
this.Player.style.height = h1;
this.Player.style.overflow = "hidden";
UL_new.innerHTML += UL_new.innerHTML; //????????
w3 = w3 * 2 ;      
this.Box.style.width = w3;
UL_new.style.padding = "0px";
UL_new.style.margin = "0px";
//this.Box.style.border = "1px solid #F00";

_this.Player.onmouseover=function() {clearInterval(_this.MyMar);}
_this.Player.onmouseout=function() {_this.Refresh();}


trace('??????:' + this.Box.offsetWidth);
this.MyMar;

}
Xpnew_Marqueen.prototype.Refresh = function()
{
var _this = this;
//this.MyMar=setInterval(function(){this.play},this.Speed);
this.MyMar=setInterval(function(){_this.play();},this.Speed);
//this.MyMar=setTimeout(function(){_this.play();},this.Speed);
}

Xpnew_Marqueen.prototype.play = function(){
// trace('??:' + this.Direction);
switch(this.Direction)
{
  
   case 'left':
   
    if(this.Box.offsetWidth/2 - this.Player.scrollLeft <= 0)
    {
     this.Player.scrollLeft-=this.Box.offsetWidth/2;
     if(this.Player.scrollLeft< this.step){
      this.Player.scrollLeft = this.step;
     }
    }
    else
    {
     this.Player.scrollLeft += this.step;
    
    }
   break;
  
   case 'right':
//    trace(" this.Player.innerHTML:" + this.Player.innerHTML + " this.Player.scrollLeft:" + this.Player.scrollLeft);
//    trace(" this.Player.scrollLeft:" + this.Player.scrollLeft);
    if(this.Player.scrollLeft <= 0)
    {
     this.Player.scrollLeft+=this.Box.offsetWidth/2;
    }
    else
    {
    
     this.Player.scrollLeft -= this.step;
    
    }
   break;
  
   default: 
  
   break;

}


} 


window.onload = function(){

   var m2 = new Xpnew_Marqueen("pj_show");
   m2.Direction = 'left';
   m2.step = 1; //滚动的距离，单位像素。
   m2.Speed = 25; //滚动的频度，单位毫秒。
   m2.Refresh();
}

