﻿/* Script by: www.jtricks.com
 * Version: 20090101
 * Latest version:
 * www.jtricks.com/javascript/navigation/floating.html
 * 2009-01-01 modified by hero_luot@sohu.ocm
 */
function floatingBox(box, options, index)
{
    this.targetX = -250;    //默认x
    this.targetY =  10;     //默认y
    this.interval = 50;     //漂浮速度==越小越快
	this.offset = 5;        //上下左右偏移量
	this.zindex = 1000;     //浮动层的z-index
    this.menu = box;
    this.index = index;
    this.options = options;
    this.hasInner = typeof(window.innerWidth) == 'number';
    this.hasElement =  document.documentElement
        && document.documentElement.clientWidth;    
	this.move = function ()
	{//move function start
		jQuery(this.menu).css('right', this.nextX + 'px').css('top', this.nextY + 'px');
	}; 
	this.computeShifts = function ()
	{
		var de = document.documentElement;

		this.shiftX =  
			this.hasInner  
			? pageXOffset  
			: this.hasElement  
			  ? de.scrollLeft  
			  : document.body.scrollLeft;  
		if (this.targetX < 0)
		{
			this.shiftX +=
				this.hasElement
				? de.clientWidth
				: document.body.clientWidth;
		}

		this.shiftY = 
			this.hasInner
			? pageYOffset
			: this.hasElement
			  ? de.scrollTop
			  : document.body.scrollTop;
		if (this.targetY < 0)
		{
			if (this.hasElement && this.hasInner)
			{
				// Handle Opera 8 problems
				this.shiftY +=
					de.clientHeight > window.innerHeight
					? window.innerHeight
					: de.clientHeight
			}
			else
			{
				this.shiftY +=
					this.hasElement
					? de.clientHeight
					: document.body.clientHeight;
			}
		}
	};
	this.calculateCornerX = function()
	{
		if (this.targetX != 'center')
			return this.shiftX + this.targetX;

		var width = parseInt(jQuery(this.menu).outerWidth()); 

		var cornerX =
			this.hasElement
			? (this.hasInner
			   ? pageXOffset
			   : document.documentElement.scrollLeft) + 
			  (document.documentElement.clientWidth - width)/2
			: document.body.scrollLeft + 
			  (document.body.clientWidth - width)/2;
		return cornerX;
	};
	this.calculateCornerY = function()
	{
		if (this.targetY != 'center')
			return this.shiftY + this.targetY;

		var height = parseInt(jQuery(this.menu).outerHeight()); 

		// Handle Opera 8 problems
		var clientHeight = 
			this.hasElement && this.hasInner
			&& document.documentElement.clientHeight 
				> window.innerHeight
			? window.innerHeight
			: document.documentElement.clientHeight

		var cornerY =
			this.hasElement
			? (this.hasInner  
			   ? pageYOffset
			   : document.documentElement.scrollTop) + 
			  (clientHeight - height)/2
			: document.body.scrollTop + 
			  (document.body.clientHeight - height)/2;
		return cornerY;
	};

	this.doFloat = function()
	{
		var stepX, stepY;

		this.computeShifts();

		var cornerX = this.calculateCornerX();

	    stepX = (cornerX - this.nextX) * .07;
		if (Math.abs(stepX) < .5)
		{
			stepX = cornerX - this.nextX;
		}

		var cornerY = this.calculateCornerY();

	    stepY = (cornerY - this.nextY) * .07;
		if (Math.abs(stepY) < .5)
		{
			stepY = cornerY - this.nextY;
		}

		if (Math.abs(stepX) > 0 ||
			Math.abs(stepY) > 0)
		{
			this.nextX += stepX;
			this.nextY += stepY;
			this.move();
		}

		setTimeout('funcFloating[' + this.index + '].func()', this.interval);
	};

	this.init = function()
	{
		if(typeof(this.options)  == 'object')
		{
			for(var i in this.options)
			{
				switch(i)
				{
					case 'targetX':
					case 'targetY':
						if(typeof(this.options[i]) == 'number')
						{
							this[i] = this.options[i];
						}
						
						else if(typeof(this.options[i]) == 'string')
						{
							
							if(this.options[i] == 'center')
							{
								this[i] = this.options[i];
							}
							else
							{
								switch(this.options[i])
								{
									case 'top':
										this.targetY= this.offset;
										break;
									case 'bottom':
										this.targetY = -((jQuery(this.menu).outerHeight() + this.offset));									
										break;
									case 'left':
										this.targetX = this.offset;
										break;
									case 'right':
										this.targetX = -((jQuery(this.menu).outerWidth()) + this.offset);
										break;	
								}
							}
							
						}
						break;	
					default:
						this[i] = this.options[i];				
				}
			}
		}
		//Set menu z-index attribute
		jQuery(this.menu).css('position', 'absolute').css('z-index', this.zindex);
		this.initSecondary();
		this.doFloat();
	};

	// Some browsers init scrollbars only after
	// full document load.
	this.initSecondary = function()
	{
		this.computeShifts();
		this.nextX = this.calculateCornerX(); 
		this.nextY = this.calculateCornerY(); 
		this.move();
	};

	this.position = function(targetX, targetY)
	{
		if(typeof(targetX) == 'number')
		{
			this.targetX = targetX;
		}else if(typeof(targetX) == 'string')
		{
			if (targetX == 'center')
			{
				this.targetX = targetX;
			}
			else
			{
				switch(targetX)
				{
					case 'left':
						this.targetX = this.offset;
						break;
					case 'right':
						this.targetX = -((jQuery(this.menu).outerWidth()) + this.offset);
						break;
				}
			}
		}	
		if(typeof(targetY) == 'number')
		{
			this.targetY = targetY;
		}else if(typeof(targetY) == 'string')
		{
			if (targetY == 'center')
			{
				this.targetY = targetY;
			}
			else
			{
				switch(targetY)
				{
					case 'top':
						this.targetY= this.offset;
						break;
					case 'bottom':
						this.targetY = -((jQuery(this.menu).outerHeight() + this.offset));
						break;	
				}
			}
		}
	}
};


var funcFloating = {};
jQuery.fn.floating = function(options)
{
	return jQuery(this).each( function(i) {		
			var nextIndex = 0;
			for(var index in funcFloating) {
				nextIndex = parseInt(index);
			}	
			funcFloating[nextIndex + 1] = {};	
			funcFloating[nextIndex + 1].box = this;	
			funcFloating[nextIndex + 1].obj = new floatingBox(this, options, (nextIndex + 1));
			funcFloating[nextIndex + 1].func = function(){ funcFloating[nextIndex + 1].obj.doFloat(); };
			if (document.layers) {
				funcFloating[nextIndex + 1].obj.init();
			}else {
				funcFloating[nextIndex + 1].obj.init();
				funcFloating[nextIndex + 1].obj.initSecondary();
			}	
		}
	);
};
jQuery.fn.floatingPosition = function(targetX, targetY)
{
	return jQuery(this).each(function(i){	
			for(var j in funcFloating) {
				if(funcFloating[j].box == this) {
					funcFloating[j].obj.position(targetX, targetY);
				}
			}	
		});	
};

