/**
 * @author admin
 * @copyright igg.com
 */
    $=function(id)
    {
    	return document.getElementById(id);
    }
    var timeObj = {};
    timeObj.obj = null;
    timeObj.start = 0;
    timeObj.step = 5;
    timeObj.end = 0;
    timeObj.runtime = 100;
    timeObj.timer;
    timeObj.runat = 0;
    timeObj.stopevt = function(){};
    timeObj.setting = function(){};
    timeObj.check = function() {
        if (!timeObj.stopevt()) {
            if (timeObj.runat < 100) {
                timeObj.setting();
                timeObj.runat++;
            } else {
                window.clearInterval(timeObj.timer);
            }
        } else {
            window.clearInterval(timeObj.timer);
        }
    };
    timeObj.init = function(){
        timeObj.start = 0;
        timeObj.step = 5;
        timeObj.end = 0;
        timeObj.runat = 0;
    };
    
    var scrollObject = {};
        scrollObject.childtagname = 'div';//default
        scrollObject.direction = 0;//the move direction,0 horizontal ,1 vertical,default is 0
        scrollObject._dobj = null; //the move object
        scrollObject._automoveno = 0; //the current
        scrollObject._addmoveno = 1;  //1 positive ,-1 negative
        scrollObject._objno = 0;
        scrollObject._objname = null;
        scrollObject._automoveobj = null;
        scrollObject.automovetimer = null;
        scrollObject._objWidth = 0;
        scrollObject._objHeight = 0;
        scrollObject._moveHeigh = 0; //normally the same to _objHeight
        scrollObject._targetP = []; //store the target positions
        scrollObject.automovedelay = 3000;//default 3 seconds
    
    scrollObject.outHandler = function(){
        scrollObject._automoveno -= scrollObject._addmoveno;
    	scrollObject.autoMove();
    };
    scrollObject.inHandler = function(){
        scrollObject.clearAutoMove();
    };
    //
    scrollObject._addEvent = function(target, handler, tasktype){
    	if (target.addEventListener)
    		target.addEventListener(tasktype, handler, false);
    	else if (target.attachEvent)
    		target.attachEvent('on'+tasktype, function(){return handler.call(target, window.event)});
    };
    //call this before call initDraw
    scrollObject.set = function(){
        
    };
    scrollObject.initDraw = function(id) {
       
        this._objname = id;
        this._dobj = $(id);
        //add event
        scrollObject._addEvent(this._dobj,scrollObject.inHandler,'mouseover');
        scrollObject._addEvent(this._dobj,scrollObject.outHandler,'mouseout');
        
    	var tmp = this._dobj.getElementsByTagName(scrollObject.childtagname);
    	var length = tmp.length;
    	//just store the items,and count
    	if(length > 0){
    	    var s = '';
    	    if (scrollObject.direction == 1){
    	        this._dobj.style.marginTop = '0px';
    	        scrollObject._moveHeight = tmp[0].clientHeight||tmp[0].offsetHeight;
    	        var tmp_l = 0;
        		for(var i=0; i < length ; ++i){
        			tmp_l = tmp[i].clientTop||tmp[i].offsetTop;
        			scrollObject._targetP.push(tmp_l);
        			s += ' '+tmp_l+' ';
        		}
    	    }else{
    	        this._dobj.style.marginLeft = '0px';
    	        scrollObject._moveHeight = tmp[0].clientWidth||tmp[0].clientWidth;
    	        var tmp_l = 0;
        		for(var i=0; i < length ; ++i){
        			tmp_l = tmp[i].clientLeft||tmp[i].offsetLeft;
        			scrollObject._targetP.push(tmp_l);
        			s += ' '+tmp_l+' ';
        		}
    	    }
    	    //set the num
    		scrollObject._objno = length;
    	}
    	else return;
    	
    	//if have set the second argument,it is the priority
    	if (arguments.length > 1){
            scrollObject._objno = arguments[1];
        }
        //set it for timer
        timeObj.obj = scrollObject._dobj;
        
        //start the auto move
        scrollObject.checkAutoMove();
    };
    scrollObject.checkAutoMove = function() {
        if (scrollObject._automoveno > scrollObject._objno - 2){
            scrollObject._addmoveno = -1;
        }
        if (scrollObject._automoveno < 1) {
            scrollObject._addmoveno = 1;
        }
        scrollObject.automovetimer = window.setTimeout(scrollObject.autoMove,scrollObject.automovedelay);
    };
    scrollObject.clearAutoMove = function() {
        if (scrollObject.automovetimer) {
            window.clearInterval(scrollObject.automovetimer);
        }
        if (timeObj.timer) {
            window.clearInterval(timeObj.timer);
        }
    };
    scrollObject.autoMove = function() {
        //rest
        timeObj.init();
        timeObj.step = 15;
        //calculate the index
        scrollObject._automoveno += scrollObject._addmoveno;
        //calculate the range
        timeObj.end = 0 - parseInt(scrollObject._targetP[scrollObject._automoveno]);
        
    	//set the handler
    	if (scrollObject.direction == 1){
    	    //vertical
    	    timeObj.start = parseInt(scrollObject._dobj.style.marginTop);
    	    timeObj.stopevt = function() {
                var l = parseInt(this.obj.style.marginTop);
                if (this.start > this.end) {
                    if (l <= this.end) {
                        scrollObject.checkAutoMove();
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    if (l >= this.end) {
                        scrollObject.checkAutoMove();
                        return true;
                    } else {
                        return false;
                    }
                }
            };
            timeObj.setting = function() {
                var l = parseInt(this.obj.style.marginTop);
                var step = (this.end - l)/this.step;
                if(Math.abs(step) <1){
                    if(step>0)step = 1;
                    else step = -1;
                }
                l += step;
                this.obj.style.marginTop = l + "px";
            };
    	}else{
    	    //default
    	    timeObj.start = parseInt(scrollObject._dobj.style.marginLeft);
    	    timeObj.stopevt = function() {
                var l = parseInt(this.obj.style.marginLeft);
                if (this.start > this.end) {
                    if (l <= this.end) {
                        scrollObject.checkAutoMove();
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    if (l >= this.end) {
                        scrollObject.checkAutoMove();
                        return true;
                    } else {
                        return false;
                    }
                }
            };
            timeObj.setting = function() {
                var l = parseInt(this.obj.style.marginLeft);
                var step = (this.end - l)/this.step;
                if(Math.abs(step) <1){
                    if(step>0)step = 1;
                    else step = -1;
                }
                l += step;
                this.obj.style.marginLeft = l + "px";
            };
    	}
        //start the move
       timeObj.timer = window.setInterval(timeObj.check,10);
    }