
String.prototype.replaceAll = function(s1,s2)
{
    return this.replace(new RegExp(s1, "gm"), s2);
}
String.prototype.trim=function()
{
     return this.replace(/(^\s*)(\s*$)/g, "");
}

if(typeof(HTMLElement) != "undefined"){
   HTMLElement.prototype.__defineSetter__("outerHTML", function(s) {
        var r = this.ownerDocument.createRange();
        r.setStartBefore(this);
        var df = r.createContextualFragment(s);
        this.parentNode.replaceChild(df, this);
        return s;
    });
   HTMLElement.prototype.__defineGetter__("outerHTML", function(){
        var a = this.attributes, str = "<" + this.tagName, i = 0;
        for (; i < a.length; i++)
            if (a[i].specified)
                str += " " + a[i].name + '="' + a[i].value + '"';
        return str+">"+this.innerHTML+"</"+this.tagName+">";
    });
}

var Glider = function(id,scroll,fixheight,speed,panels,waittime,span){
	this.id = id || null;
	this.scroll = scroll;
	this.fixheight = fixheight || 300;
	this.speed = speed || 50;
	this.panels  = panels || 1;
	this.waittime = waittime || 1000;
	this.span    = span || 20;
	this.dstId = null;
	this.srcId = null;
	this.width = 300;
	this.height = 300;
	this.timerId = null;
	this.timerWorker = null;
	this.counter = 0;
	this.diff    = 0;
	this.running = false;
	
	this.load = function(dstId,srcId,width,height){
		this.clear();
		this.dstId = dstId || this.dstId;
		this.srcId = srcId || this.srcId;
		this.width = width || this.width;
		this.height = height || this.height;
		this.timerId = null;
		this.timerWorker = null;
		this.counter = 0;
		this.diff    = 0;
		this.running = false;
	}
	
	this.start = function(){
			this.stop();
			
			if(this.$(this.srcId)==null || this.$(this.dstId) == null){
				alert("srcId or dstId not exists !");
				return ;
			}
			
			var table = this.$(this.srcId).outerHTML;
			var panel = this.getStubPanel( this.getId(this.dstId) );  
			panel = panel.replaceAll("\\{table\\}",table);
			this.$(this.dstId).innerHTML = panel;
			
			var part0 = this.$(this.dstId);
			var glider = this;

			part0.style.display="block";
			var scrollwidth = (this.scroll)?0:15;
			try{part0.style.width=(this.width+scrollwidth);}catch(e){}
			try{part0.style.width=(this.width+scrollwidth)+"px";}catch(e){}
			try{part0.style.height=this.height;}catch(e){}
			try{part0.style.height=this.height+"px";}catch(e){}
			part0.style.position="absolute";
			part0.style.overflowX= "hidden";
			part0.style.overflowY= (this.scroll)?"hidden":"scroll";
			
			var total = 0, single = 0;
			for(var x=1; x<=this.panels; x=x+1){
				var partX = this.$(this.getId(this.dstId)+"_part"+x);
				var vs = partX.childNodes;
				for(var i=0; i<vs.length; i++){
						if(vs[i]!=null && vs[i].tagName!=null ){
								vs[i].style.display="block"; 
								total += partX.offsetHeight;
								single = partX.offsetHeight;
								break;
						}
				}
			}
			
			single = (single<=0 ? this.height:single);
			this.height = (this.fixheight<single ? this.fixheight:single);
			try{part0.style.height=this.height;}catch(e){}
			try{part0.style.height=this.height+"px";}catch(e){}
			
			if(this.fixheight<total+this.span){
					this.timerWorker = function(){
						  if(glider.running ==false) return;
						  var scrollSpan = part0.scrollHeight-glider.height;
						  
						  var diff = scrollSpan - part0.scrollTop;
						  if( glider.diff == diff ) 
						  	glider.counter ++;
						  else  
						  	glider.counter = 0;
						  	
						  glider.diff = diff;
						  
					    if((diff<=0 || glider.counter>1) && scrollSpan>0 ){
					        setTimeout(function(){
						        part0.scrollTop = 0;
						        glider.counter = 0;
					        	glider.running = true;
					        },glider.waittime);
					        glider.running = false;
					    }
					    else { part0.scrollTop++; }
					}
					if(this.scroll){
					  glider.run(glider.waittime);
					}
					glider.running = true;
					part0.onmouseover = function() { glider.running = false; };
					part0.onmouseout =  function() { glider.running = true; };
		}
		
	}

  this.run = function(timeout){
  	var glider = this;
  	timeout = timeout || 0;
  	glider.timerId = 0;
  	setTimeout(function(){
  		if(glider!=null && glider.timerWorker!=null){
  			glider.timerId = setInterval(glider.timerWorker, glider.speed);
  		}
  	},timeout);
  }

  this.$ = function(name){
  	  if(typeof(name)!= 'string') return name; 
	  	return document.getElementById(name);
  }
  
  this.getId = function(name){
  	  if(typeof(name)== 'string') return name; 
	  	return name.id;
  }

  this.getStubPanel = function(id){
	  	var stub = ""+
	  			"	    <table align=\"left\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"> ";	
		for(var i=1; i<=this.panels; i=i+1){
			    stub += "" +
				"	        <tr> " +
				"	            <td id=\""+id+"_part"+i+"\" valign=\"top\"> " +
				"	              {table} " +
				"	            </td> " +
				"	        </tr> " ;
		}		
		stub += "	    </table> ";
		return stub;
  }

  this.clear = function(){
  	  this.stop();
	  	if(this.$(this.dstId)!=null ){
	  		this.$(this.dstId).style.display="none";
	  		this.$(this.dstId).innerHTML = "";
	    }
  }
  
  this.stop = function(){
	  	if(this.timerId !=null ){
	  		clearInterval(this.timerId);
	    }
	    this.timerId = null;
	    this.running=false;
  }
  
  this.stopped = function(){
  	 return this.timerId == null || this.running==false ;
  }
  
  this.clicked = function(event){
		var evt = window.event?window.event:event;
		var target=evt.srcElement||evt.target;
		var i=0;
		do{
			if(target==null)break;
			if(target.id==this.id)return true;
			if(target.nodeName.toLowerCase() == "html")break;
			target = target.parentNode;
			i++;
			if(i>1000)break;
		}while(true);
		return false;
  }
  
}


