﻿var ajax = { 

    postRequest: function(url, vars, values, handler) {
        var params = this.paras(vars,values);
        xmlhttp = this.getxmlhttpObj();
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length);
        xmlhttp.setRequestHeader("Connection", "close");   
        _this = this;
        _handler = handler.bind(this);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    var data = xmlhttp.responseText;
                    if(data != null)                   
                    
                    _handler(data);
                    
            }
        }

        xmlhttp.send(params);    
     },
 
    getRequest: function(url, vars, values, handler) {
        xmlhttp = this.getxmlhttpObj();
        url = url+"?"+this.paras(vars,values);
        xmlhttp.open("GET", url,true);
        
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    var data = xmlhttp.responseText;
                    if(data != null) 
                    handler(data);
            }
        }
        
        xmlhttp.send(null);
    },

    paras: function(vars, values) {
    var par = vars[0]+"="+values[0];
    for(i=1;i<vars.length;i++)  {
          par += "&"+vars[i]+"="+values[i];
    }
    return par;
    },

    getxmlhttpObj: function (){
    return (window.XMLHttpRequest) ? new XMLHttpRequest() : ((window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : null);

    }

}


function getstyle(elem, prop) {
		if(document.defaultView){
			return document.defaultView.getComputedStyle(elem, null).getPropertyValue(prop);
		}
		else if(elem.currentStyle){
			var prop = prop.replace(/-(\w)/gi, function($0,$1) {
				return $1.toUpperCase();
			});
			return elem.currentStyle[prop];
		}
		else return null;
	}
	
Function.prototype.bind = function(obj) {	
    var _method = this;
    return function() {
        return _method.apply(obj, arguments);
    };    
} 	

var ttt = [];
	
function $(id) {
var $ =  (typeof id  =="string") ? document.getElementById(id) : id;
$.start ={};

$.stop = function(){
     for (i in ttt) {
        clearTimeout(ttt[i]);
     }
};

$.move = function(settings,callbk) {
    var _this = this;
    var left = getstyle(_this,"left");
    var top =  getstyle(_this,"top");
 
     _this.start.left = (left=="auto") ? 0 : parseInt(left);
    _this.start.top = (top=="auto") ? 0 : parseInt(top);
    if (settings.to.top == _this.start.top) { // x 
         
        var descend = (settings.to.left>_this.start.left) ? false : true; 
       
        var s = Math.min(_this.start.left,settings.to.left); 
        var d = Math.max(_this.start.left,settings.to.left); 
            for (i = s; i <= d; i++) { 
              (function(j) { 
				    
				    var delay = (descend==true) ? (d-j)*settings.delay/(d - s) : (j-s)*settings.delay/(d - s);
				    
                   ttt[i] =  setTimeout(function() { 
                         _this.style.left = j+"px"; 
                          
                          if (descend==false&&j==d&&callbk!=undefined) {callbk.call(_this);} 
                          else if (descend==true&&callbk!=undefined&&j==s) {callbk.call(_this);} 
                           
                          },delay); 
                })(i); 
            } 
           
    } 
  
};

return $;
}

function ticker(id){
this.ul = document.getElementById(id);
this.lis = this.ul.getElementsByTagName("LI");
this.len = this.lis.length;
this.width = 0;
for(i=0;i<this.len;i++) {
var dd = this.lis[i].offsetWidth;
this.width +=this.lis[i].offsetWidth;
}
this.ul.style.width = this.width+"px";
this.left = this.ul.parentNode.offsetWidth;
this.ul.style.left = this.left+"px";
this.t=0;
this.delay =30000;
this.speed = (this.left+this.width)/this.delay;

}

ticker.prototype = {
repeat: function(fn,del){
    var _this = this;
    (function() { 
    _this.t = setInterval(fn,del);
    })();

},

too: function(){
     _this = this;
     left = _this.width;
     delay = Math.abs(parseInt((parseInt(this.ul.style.left)+left)/this.speed));
     $(this.ul).move({delay:delay,to:{left:-left,top:0}},function(){
     this.style.left = _this.left+"px";
     _this.go();    
    });    
},

attach:function(){

this.ul.onmouseover = function(){
    $(this).stop();
    _stop();
};

this.ul.onmouseout = function(){
if (_this.t!=0) return;
_stop();
_too();
}

},

go: function(){
    _this = this;
    _stop = this.stop.bind(this);
    _too = this.too.bind(this);
    this.attach();
    clearInterval(this.t);

    $(this.ul).move({delay:this.delay,to:{left:-this.width,top:0}},function(){       
       this.style.left = _this.left+"px";
    });
       
  this.go_on();
},

go_on:function() {

this.repeat(this.ticker1.bind(this),this.delay+10);
},

ticker1: function(){

 $(_this.ul).move({delay:_this.delay,to:{left:-_this.width,top:0}},function(){    
            this.style.left = _this.left+"px";    
 });
    
},

stop:function(){
$(this.ul).stop();
clearInterval(this.t);
this.t = 0;
}

}
