//************************************************************
//************************************************************
//floating box
function CFloatBox(id){
	//properies
	this.cnt = 0;
	this.divId = id;
    this.div = null;
    
	this.floatable=false;
	this.animated = true;
	
	this.jumpCount= 15;
	this.floatTime= 15;
	this.interval = 3000;

    this._isFloating=false;

	this.show = function(){
	    if(!this.getBox())return;
        this.div = document.getElementById(this.divId);
        if(!this.div) return;
        this.div.style.position = 'absolute';
        this.div.style.zindex = '100';
        $('fon_popup_window').style.display='block';
        this.div.style.display = 'block';
        this.setFloatable(true);

        if ((parent_popup_obj && this.divId != parent_popup_obj.divId && this.divId != error_popup_obj.divId ) || (!parent_popup_obj && this.divId != error_popup_obj.divId))
        {
            eval("parent_popup_obj = popup_obj_"+this.divId);
        }
        this.div.style.position = 'fixed';
	}


	this.hide = function(){
	    if(!this.getBox())return;
        this.setFloatable(false);
        this.div.style.display = 'none';
        $('fon_popup_window').style.display='none';
	}
	
	this.setFloatable = function(floatable){
	   this._isFloating=false;
	   this.floatable = floatable;
	   if(this.floatable) this.startFloat(true);
	}
	
	
	this.checkFloat = function(){
	    this.startFloat();
		window.setTimeout(function(){oThis.checkFloat();},this.interval);
	}
	
	this.startFloat = function(){
	    if(!this.floatable) return;
		var oThis = this;
	    if(!this.getBox())return;
		var x = this.getBoxX();
		var y = this.getBoxY();
		var cx = parseInt(this.div.style.left); 
		var cy = parseInt(this.div.style.top);
		if(!this._isFloating && (cx != x || cy != y  || isNaN(cx) || isNaN(cy) ) ){
			this._isFloating = true;
			if(this.animated)
			 this.float();
			else
			 this.jumpTo(x,y);
		}
	}
	this.float = function(){
	    if(!this.floatable) return;
		var oThis = this;
	    if(!this.getBox())return;
		var x = this.getBoxX();
		var y = this.getBoxY();
		var cx = parseInt(this.div.style.left);cx = isNaN(cx)?x:cx;
		var cy = parseInt(this.div.style.top);cy = isNaN(cy)?y:cy;
		var dx = (x-cx)/this.jumpCount; dx = Math.floor(dx);
		var dy = (y-cy)/this.jumpCount; dy = Math.floor(dy);
        this.jumpTo(cx+dx, cy+dy);
        this.cnt++
		if(dx==0 && dy==0)
		{
			this._isFloating = false;
		}
		else
			window.setTimeout(function(){oThis.float();},this.floatTime);
	}
	
	this.jumpTo = function(x, y){
	    if(!this.floatable)return;
	    if(!this.getBox())return;
		this.div.style.left = x+'px';
		this.div.style.top = y+'px';
		this._isFloating = false;
	}

	this.getBox = function(){
	    if(!this.div) this.div = document.getElementById(this.divId);
	    if(!this.div || !this.div.tagName) this.div = null;
	    return this.div;
	}
	
	this.getBoxX = function(){
		ax = (document.all? parseInt(document.body.leftMargin) : 0);
        var winWidth = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement.clientWidth: document.body.clientWidth;
		var objWidth = this.div.offsetWidth;
		var scrollLeft = document.body.parentNode.scrollLeft + document.body.scrollLeft;
		var theLeft = (winWidth-objWidth)/2;
		if(theLeft<0) theLeft=0;
		return  scrollLeft+ax+theLeft;
	}

	this.getBoxY = function(){
		ay = (document.all? parseInt(document.body.topMargin) : 0);
        var winHeight = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement.clientHeight : document.body.clientHeight;
		var objHeight = this.div.offsetHeight;
		var scrollTop = this.div.scrollTop;
		//var scrollTop = document.body.parentNode.scrollTop + document.body.scrollTop;
		var theTop = (winHeight-objHeight)/2;
		if(theTop<0) theTop=0;
		return  scrollTop+ay+theTop;
	}
	
	this.init = function(){
		var oThis = this;
		this.div = document.getElementById(this.divId);
		if(this.div) this.div.setAttribute("object", this);
		if(document.all){
			window.attachEvent ('onresize', function(){oThis.startFloat();});
			window.attachEvent ('onscroll', function(){oThis.startFloat();});
		}
		else{
			window.addEventListener("resize", function(){oThis.startFloat();}, false); 
			window.addEventListener("scroll", function(){oThis.startFloat();}, false); 
			window.scroll = function(){oThis.startFloat();}
			window.onscroll = function(){oThis.startFloat();}
		}
	}
	//init code
	this.init();
	return this;
}