﻿/*
 *	页面自由飘浮效果
 */
/*
 *   构造函数
 * @param	j_layer:JQueray				  一个 jQuery 对象
 * @param	moveRange:Object				 {top:-1, left:-1, width:-1, height:-1} -1表示自动获取极值
 * @param	speed:Number					 
 */
function FloatObject(layer, moveRange, speedx, speedy){
	this._layer = layer instanceof jQuery?layer:$(layer);
	if(this._layer.length>1){this._layer=$(_layer[0]);}
	this._layer.css("position", "absolute");
	//处理 top/left 值. 防止auto值导致脚本出错
	if(isNaN(parseInt(this._layer.css("left")))){this._layer.css("left", this._layer.position().left+"px");}
	if(isNaN(parseInt(this._layer.css("top")))){this._layer.css("top", this._layer.position().top+"px");}
	this._speedx = Math.ceil(speedx > 0 ? speedx : 1);
	this._speedy = speedy ? Math.ceil(speedy) : this._speedx;
	this._rangeSet = moveRange ? moveRange : {top:-1, left:-1, width:-1, height:-1};
	this._range = {top:this._rangeSet.top, left:this._rangeSet.left, width:this._rangeSet.width, height:this._rangeSet.height};
	
	this._moveLayer = createDelegate(this, FloatObject.__moveLayer);
	this._intervalSet = 30;
	this._intervalID = -1;
	this._paused = false;
	this._dirx = 1;
	this._diry = 1;
}
/*
 * 创建移动控制的公用函数.
 * 由于JS函数指向不确定, 所在在单个实例里要使用代理创建移动控制函数
 */
FloatObject.__moveLayer = function (){
	if(this._paused)	return;
	if(this.onRender){
		this.onRender();
	}
	//
	var offset = {left:parseInt(this._layer.css("left")), top:parseInt(this._layer.css("top"))};
	var nx=offset.left, ny=offset.top;
	if(this._rangeSet.left < 0){this._range.left = getScrollPoint().x;}
	if(this._rangeSet.top	< 0){this._range.top = getScrollPoint().y;}
	if(this._rangeSet.width < 0){this._range.width = getWinSize().width;}
	if(this._rangeSet.height < 0){this._range.height = getWinSize().height;}

	this._range.right = this._range.left + this._range.width - this._layer.outerWidth();
	this._range.bottom = this._range.top + this._range.height - this._layer.outerHeight();
	//set x
	if(this._range.right > 0){
		nx += this._speedx * this._dirx;
		if(nx < this._range.left){
			this._dirx *= -1;
			nx = this._range.left;
		}else if(nx > this._range.right){
			this._dirx *= -1;
			nx = this._range.right;
		}
		this._layer.css("left", nx + "px");
	}
	//set y
	if(this._range.bottom > 0){
		ny += this._speedy * this._diry;
		if(ny < this._range.top){
			this._diry *= -1;
			ny = this._range.top;
		}if(ny > this._range.bottom){
			this._diry *= -1;
			ny = this._range.bottom;
		}
		this._layer.css("top", ny + "px");
	}
	
}
FloatObject.prototype.start = function (){
	if(this._intervalID<0)	this._intervalID = setInterval(this._moveLayer, this._intervalSet, this);
}
FloatObject.prototype.startRandom=function(){
	var dir=["left","top","right","bottom"][Math.floor(Math.random()*100%4)];
	switch(dir){
		case "top":
			this._layer.css("top", this._range.top + "px");
			this._layer.css("left", Math.floor(Math.random()*this._range.width) + "px");
			break;
		case "right":
			this._layer.css("top", Math.floor(Math.random()*this._range.height) + "px");
			this._layer.css("left", this._range.right + "px");
			break;
		case "bottom":
			this._layer.css("top", this._range.bottom + "px");
			this._layer.css("left", Math.floor(Math.random()*this._range.width) + "px");
			break;
		case "left":
			this._layer.css("top", Math.floor(Math.random()*this._range.height) + "px");
			this._layer.css("left", this._range.left + "px");
			break;
	}
	this.start();
}
FloatObject.prototype.pause = function (p){
	if(typeof p == "undefined"){
		this._paused = !this._paused;
	}else{
		this._paused = p;
	}
}
FloatObject.prototype.stop = function (){
	clearInterval(this._intervalID);
	this._intervalID=-1;
}
FloatObject.prototype.setRange = function (left, top, width, height){
	this._rangeSet.left = left ? left : -1;
	this._rangeSet.top = top ? top : -1;
}
FloatObject.prototype.setFPS = function (fps){
	this._intervalSet = fps ? Math.floor(1000/fps) : 30;
}