﻿function Float(){
    var self = this;
    this.x = this.y = this.delay = 0;
	this.step = 1;
	this.xin = this.yin = true;
	this.url = this.image = "";
	this.Browser = "IE";
	this.ilt;
	
	if(navigator.userAgent.indexOf('Chrome') >= 0)
	    this.Browser = "Chrome";
	else if(navigator.userAgent.indexOf('Firefox') >= 0)
	    this.Browser = "Firefox";
	
	if(typeof arguments[0] == "string")
	    this.url = arguments[0];
	if(typeof arguments[1] == "string")
	    this.image = arguments[1];
	if(typeof arguments[2] == "number")
	    this.x = arguments[2];
	if(typeof arguments[3] == "number")
	    this.y = arguments[3];
	if(new Boolean(arguments[4]))
	    this.xin = arguments[4];
	if(new Boolean(arguments[5]))
	    this.yin = arguments[5];
	if(typeof arguments[6] == "number")
	    this.step = arguments[6];
	if(typeof arguments[7] == "number")
	    this.delay = arguments[7];
	    
	this.obj = document.createElement("div");
    this.obj.style.position = "absolute";
    this.obj.style.zIndex = "100";
	this.obj.style.textAlign = "right";
	
	this.CloseDiv = document.createElement("div");
	this.CloseDiv.style.cursor = "pointer";
	this.CloseDiv.style.padding = "4px";
	this.CloseDiv.style.position = "absolute";
	this.CloseDiv.style.fontSize = "12px";
	this.CloseDiv.style.width = "44px";
    this.CloseDiv.style.zIndex = "100";
	this.CloseDiv.innerHTML = "[关闭]";
	
	this.obj.innerHTML += '<a href="' + this.url + '" target="_blank" style="margin-bottom: 10px;"><img border="0" src="' + this.image + '"/></a>';
    document.body.appendChild(this.obj);
    document.body.appendChild(this.CloseDiv);
    
	this.CloseDiv.onclick = function(){
	    document.body.removeChild(self.obj);
	    document.body.removeChild(self.CloseDiv);
	    
	}
    this.CloseDiv.onmouseover = this.obj.onmouseover = function(){
        clearInterval(self.ilt);
    }
    this.CloseDiv.onmouseout = this.obj.onmouseout = function(){
        self.show();
    }
    
    this.floatAD = function(){
        var L=T=0;
        var R = document.documentElement.clientWidth - self.obj.offsetWidth;
        var B = document.documentElement.clientHeight - self.obj.offsetHeight;
        self.obj.style.left = (self.x + document.body.scrollLeft) + "px";
        self.CloseDiv.style.right = R - (self.x + document.body.scrollLeft) + "px";
        
        switch(self.Browser){
            case "IE":
            case "Firefox":
                self.CloseDiv.style.top = self.obj.style.top = (self.y + document.documentElement.scrollTop) + "px";
                break;
            case "Chrome":
                self.CloseDiv.style.top = self.obj.style.top = (self.y + document.body.scrollTop) + "px";
                break;
        }
            
        self.x = self.x + self.step * (self.xin?1:-1);
        if (self.x < L) { self.xin = true; self.x = L;}
        if (self.x > R) { self.xin = false; self.x = R;}
        
        self.y = self.y + self.step * (self.yin?1:-1);
        if (self.y < T) { self.yin = true; self.y = T; }
        if (self.y > B) { self.yin = false; self.y = B; }
    }
    
    this.show = function(){
        self.ilt = setInterval(self.floatAD, this.delay);
    }
}
