/**
*	@auth Logan Cai
*	@email cailongqun#yahoo.com.cn  (replace # with @)
*	@ this plugin is based on the nice javascript provided by http://www.jtricks.com/javascript/navigation/floating.html
*   @version 1.0
*	
*/
function floatingBox(box, options, index) {
	this.floatIn = true;
    this.targetX = -250;
    this.targetY = 200;
    this.interval = 50;
    this.menu = box;
    this.index = index;
    this.options = options;
    this.hasInner = typeof(window.innerWidth) == 'number';
    this.hasElement =  document.documentElement && document.documentElement.clientWidth;
	this.myScrollLeft;
	
	this.getDocumentElement = function () { return document.documentElement; }
	this.move = function (){
		//move function start
		jQuery(this.menu).css('left', this.nextX + 'px').css('top', this.nextY + 'px');
	};
	
	this.computeShifts = function () {
	    var de = this.getDocumentElement();
		this.myScrollLeft = this.hasElement ? de.scrollLeft : document.body.scrollLeft;
		this.myScrollTop = this.hasElement ? de.scrollTop : document.body.scrollTop;
//	    this.shiftX = this.hasInner ? pageXOffset : ((this.myScrollLeft > 0) ? 0 : this.myScrollLeft);
		if( this.myScrollLeft > 0 ){
			this.shiftX = 0;
		} else {
	    this.shiftX = this.hasInner ? pageXOffset : this.myScrollLeft;
		}
	    if (this.targetX < 0) {
	        if (this.hasElement && this.hasInner) {
	            // Handle Opera 8 problems
	            this.shiftX += de.clientWidth > window.innerWidth ? window.innerWidth : de.clientWidth;
	        } else {
	            this.shiftX += this.hasElement ? de.clientWidth : this.hasInner ? window.innerWidth : document.body.clientWidth;
	        }
	    }
	    this.shiftY = this.hasInner ? pageYOffset : this.myScrollTop;
	    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 ? document.documentElement.clientHeight : this.hasInner ? window.innerHeight : document.body.clientHeight;
	        }
	    }
	};
	this.doFloat = function() {
	    var stepX, stepY;
	    this.computeShifts();
	    stepX = (this.shiftX + this.targetX - this.nextX) * .07;
	    if (Math.abs(stepX) < .5) {
	        stepX = this.shiftX + this.targetX - this.nextX;
	    }
	    stepY = (this.shiftY + this.targetY - this.nextY) * .07;
	    if (Math.abs(stepY) < .5) {
	        stepY = this.shiftY + this.targetY - 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.floatToPos = function() {
	    var stepX, stepY;
	    this.computeShifts();
		var t_X = this.getTargetPos_X();
        stepX = this.shiftX + t_X - this.nextX;
		stepY = (this.shiftY + this.targetY - this.nextY) * .07;
	    if (Math.abs(stepY) < .5) {
	        stepY = this.shiftY + this.targetY - this.nextY;
	    }
	    if (Math.abs(stepX) > 0 || Math.abs(stepY) > 0) {
	        this.nextX += stepX;
	        this.nextY += stepY;
	        this.move();
		    setTimeout('funcFloating[' + this.index + '].floatToPos()', this.interval);
	    } else {
			this.doneFloatToPos();
		}
	};	

	this.doneFloatToPos = function () {
		this.floatIn = false;
	};

	this.doSetPos = function() {
		if(this.floatIn){
			return;	
		}
	    this.computeShifts();
		var t_X = this.getTargetPos_X();
        var stepX = this.shiftX + t_X - this.nextX;
        var stepY = this.shiftY + this.targetY - this.nextY;
/*
		var te = $('#bgHeader')
		var te_w = te.width();
		var te_p = te.position();
		//
		var t = '';
		t += 'shiftX: ' + this.shiftX;
		t += '<br/>shiftY: ' + this.shiftY;
		t += '<br/>stepX: ' + stepX;
		t += '<br/>stepY: ' + stepY;
		t += '<br/>myScrollLeft: ' + this.myScrollLeft;
		t += '<br/>myScrollTop: ' + this.myScrollTop;
		t += '<br/>te_w: ' + te_w;
		t += '<br/>te_p.left: ' + te_p.left + ' top: ' + te_p.top;
		t += '<br/>box.width: ' + jQuery(this.menu).width();
		t += '<br/>t_X: ' + t_X;
		t += '<br/>nextX: ' + (this.nextX + stepX);
		$('#infoFB').empty();
		$('#infoFB').append(t);
*/
		if (Math.abs(stepX) > 0 || Math.abs(stepY) > 0) {
	        this.nextX += stepX;
    	    this.nextY += stepY;
        	this.move();
		}
	};	
	
	this.getTargetPos_X = function () {
		var te = $('#bgHeader');
		var te_p = te.position();
		var te_w = 990;
		/*	works for IE 8/7/6, FF 3/2, Safari 4 	*/
		return te_p.left + te_w - jQuery(this.menu).width() - 20;
	};
	
	this.initSecondary = function() {
	    var de = this.getDocumentElement();
	    this.computeShifts();
		//
		if (this.hasElement && this.hasInner){
			// Handle Opera 8 problems
			var t_h = de.clientHeight > window.innerHeight ? window.innerHeight : de.clientHeight;
		} else {
			var t_h = this.hasElement ? document.documentElement.clientHeight : this.hasInner ? window.innerHeight : document.body.clientHeight;
		}
		//
	    this.nextX = this.shiftX + this.getTargetPos_X();
	    this.nextY = this.shiftY + t_h;
		//
		this.move();
	};	

	this.position = function(targetX, targetY) {
		if(typeof(targetX) == 'number') {
			this.targetX = targetX;
		} else if(typeof(targetX) == 'string') {
			switch(targetX) {
				case 'left':
					this.targetX = 5;
					break;
				case 'right':
					this.targetX = -((jQuery(this.menu).outerWidth()) + 5);
					break;									
			}
		}	
		if(typeof(targetY) == 'number') {
			this.targetY = targetY;
		} else if(typeof(targetY) == 'string') {
			switch(targetY) {
				case 'top':
					this.targetY= 5;
					break;
				case 'bottom':
					this.targetY = -((jQuery(this.menu).outerHeight() + 5));
					break;								
			}
		}				
	};
	
	this.init = function(){
		jQuery(this.menu).css('position', 'absolute');
		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') {
							switch(this.options[i]) {
								case 'top':
									this.targetY= 5;
									break;
								case 'bottom':
									this.targetY = -((jQuery(this.menu).outerHeight() + 5));									
									break;
								case 'left':
									this.targetX = 5;
									break;
								case 'right':
									this.targetX = -((jQuery(this.menu).outerWidth()) + 5);
									break;									
							}
						}
						break;	
					default:
						this[i] = this.options[i];				
				}
			}
		}
	    this.initSecondary();
		this.floatToPos();
	}
};

// Some browsers init scrollbars only after
// full document load.
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(); };
			funcFloating[nextIndex + 1].func = function(){ funcFloating[nextIndex + 1].obj.doSetPos(); };
			funcFloating[nextIndex + 1].floatToPos = function(){ funcFloating[nextIndex + 1].obj.floatToPos(); };
			//
			$(window).bind('resize', function(){ funcFloating[nextIndex + 1].obj.doSetPos(); } );
			$(window).bind('scroll', function(){ funcFloating[nextIndex + 1].obj.doSetPos(); } );
			//
			if (document.layers){
				funcFloating[nextIndex + 1].obj.init();
			} else {
				funcFloating[nextIndex + 1].obj.init();
				funcFloating[nextIndex + 1].obj.initSecondary();
			}
		}
	);
};