/**
 * |*** 版权所有,侵权必究 ***|
 * @author 彭伟林
 */

/**
 * Advertisement是一个自己写的浮动广告的对象,基于面向对象的编程思想开发,可重用性高.
 * <br>
 * 首次开发,如有漏动,请指教.
 */
function Advertisement(){
    this.obj = null;
    this.left = 0;
    this.top = 0;
    this.lSpeed = 10;
    this.tSpeed = 5;
    this.winWidth = 0;
    this.winHeight = 0;
    this.time = 20;
    this.timeout = null;
    this.resizeFlag = 0;
    this.movingFunction = null;
}
/**
 * <h2>初始化广告对象.</h2><br><b>
 *  objID{String} 广告所需的DIV元素ID<br>
 *  initLeft{Ingeger} 广告的起始X坐标<br>
 *  initTop{Ingeger} 广告的起始Y坐标<br>
 *  lSpeed{Ingeger} 广告的横向移动速度<br>
 *  tSpeed{Ingeger} 广告的纵向移动速度<br>
 *  time{Ingeger} 广告移动的时间延迟<br></b>
 * @param {String} objID
 * @param {Ingeger} initLeft
 * @param {Ingeger} initTop
 * @param {Ingeger} lSpeed
 * @param {Ingeger} tSpeed
 * @param {Ingeger} time
 */
Advertisement.prototype.init = function(objID, initLeft, initTop, lSpeed, tSpeed, time){
    if (!this.getObject(objID)) 
        return;
    
    this.top = initTop;
    this.left = initLeft;
    this.lSpeed = lSpeed;
    this.tSpeed = tSpeed;
    this.time = time;
	
    this.getBrowerDimensions();
	
    this.moveStart(this);
	
    var elem = this;
    
    this.obj.onmouseout = function(){
        elem.moveStart(elem);
    };
    this.obj.onmouseover = function(){
        elem.moveStop();
    };
    window.onresize = function(){
        elem.resize();
    };
     window.onscroll = function(){
		elem.scrolling();
	};
}
/**
 * <p>跟据元素的ID获取广告DIV</p>
 * <b>如果找到元素则返回true,否则返回false;</b>
 * @param {String} objID
 * @return {boolean}
 */
Advertisement.prototype.getObject = function(objID){
    this.obj = document.getElementById(objID);
    if (this.obj == null) {
        alert("/***** 警告,没有设置广告元素 *****/");
        return false;
    }    
    return true;
}
/**
 * 浏览器的滚动条,的滚动事件.
 */
Advertisement.prototype.scrolling = function(){
    this.obj.style.pixelLeft = document.body.scrollLeft + this.left;
    this.obj.style.pixelTop = document.body.scrollTop + this.top;
}
/**
 * 广告的移动
 * @param {Element} obj
 */
Advertisement.prototype.moveStart = function(obj){

	/* 判断是否碰到边缘,如果是,则改变方向 */
    if (parseInt(this.obj.style.pixelTop) -parseInt(document.body.scrollTop)+
			this.obj.offsetHeight + this.tSpeed + 5 > this.winHeight || this.top < 0) {
				
        if (parseInt(this.obj.style.pixelTop) > this.winHeight) {
            this.obj.style.pixelTop = this.winHeight - this.obj.offsetHeight - 10;
        }
		
        this.tSpeed = -this.tSpeed;
    }
	/* 判断是否碰到边缘,如果是,则改变方向 */
    if (parseInt(this.obj.style.pixelLeft) -parseInt(document.body.scrollLeft)+ 
			this.obj.offsetWidth + this.lSpeed + 5 > this.winWidth || this.left < 0) {
				
        if (parseInt(this.obj.style.pixelLeft) > this.winHeight) {
            this.obj.style.pixelLeft = this.winWidth - this.obj.offsetWidth - 10;
        }
		
        this.lSpeed = -this.lSpeed;
    }
	//重新设置广告的位置.
    this.obj.style.pixelLeft = parseInt(document.body.scrollLeft) + this.left;
    this.obj.style.pixelTop = parseInt(document.body.scrollTop) + this.top;
    
    this.top += this.tSpeed;
    this.left += this.lSpeed;
    //令广告不停地飘动.
    this.timeout = window.setTimeout(function(){
        obj.moveStart(obj)
    }, this.time);
}
/**
 * 广告停止移动
 */
Advertisement.prototype.moveStop = function(){
    window.clearTimeout(this.timeout);
}
/**
 * 浏览器改变大小时的事件代码
 */
Advertisement.prototype.resize = function(){
	
    if ((++this.resizeFlag) % 2 == 1) {
    
        var differWidth, differHeight;		//用来临时保存浏览器变动的百分比
        
        differWidth = this.winWidth;
        differHeight = this.winHeight;
        
        this.getBrowerDimensions();		//重新设置浏览器的宽度与高度值.
        
        differWidth = this.winWidth / differWidth + (this.winWidth < differWidth ? -0.1 : 0);
        differHeight = this.winHeight / differHeight + (this.winHeight < differHeight ? -0.1 : 0);
        
        this.left *= differWidth;
        this.top *= differHeight;
    }
    
}
/**
 * 获取浏览器的宽度与高度
 */
Advertisement.prototype.getBrowerDimensions = function(){
    if (window.innerHeight) {
        this.winWidth = window.innerWidth;
        this.winHeight = window.innerHeight;
    }
    else 
        if ((document.body) && (document.body.clientHeight)) {
            this.winWidth = document.body.clientWidth;
            this.winHeight = document.body.clientHeight;
            
        }
        else 
            if (document.documentElement && document.documentElement.clientHeight) {
                this.winWidth = document.documentElement.clientWidth;
                this.winHeight = document.documentElement.clientHeight;
            }
}
