﻿// JavaScript Document
/*
//浮动的层//
*/
	function FloatLayer(layerid,speed,x,y)//layerid浮动层ID，必须
	{
		this.Speed=speed;//移动速度
		this.X = x;//层的坐标
		this.Y = y;
		this.IntervalId = null;
		this.Layer = document.getElementById(layerid);//浮动层对象
		this.FloatMethod="y";//浮动方式，沿XY轴方向浮动，x只在X轴上浮动，y只在Y轴上浮动
//		this.Amount = 3;//移动的步进像素，必须为大于1的整数;
		this.Stepped = 0.06;//加速度比率
	}
	FloatLayer.prototype.initialize = function ()
	{
		if(this.Layer)
		{
			this.Layer.style.position = "absolute";
			if(this.Speed==null)
			{
				this.Speed = 1;
			}
			if(this.X)
			{
				this.Layer.style.left = this.X+"px";
			}
			if(this.Y)
			{
				this.Layer.style.top = this.Y+"px";
			}
			this.X = this.Layer.offsetLeft;
			this.Y = this.Layer.offsetTop;
			this.Layer.FloatControl = this;
			if(!window.Layers)
			{
				window.Layers = new Array();
			}
			var _NewIndex = window.Layers.push(this.Layer)-1;
			//初始化时，先触发一次
			window.Layers[_NewIndex].FloatControl.IntervalId = setInterval("window.Layers["+_NewIndex+"].FloatControl.Float(window.Layers["+_NewIndex+"])",window.Layers[_NewIndex].FloatControl.Speed)
			window.onresize=window.onscroll = function ()
			{
				for(var i=0;i<window.Layers.length;i++)
				{
					try
					{
						if(window.Layers[i].FloatControl.IntervalId!=null)
						{
							clearInterval(window.Layers[i].FloatControl.IntervalId)
						}
						window.Layers[i].FloatControl.IntervalId = setInterval("window.Layers["+i+"].FloatControl.Float(window.Layers["+i+"])",window.Layers[i].FloatControl.Speed)
					}catch(e){
						delete window.Layers[i];
					}
				}
			}
		}
	}
	FloatLayer.prototype.ScrollTop = function ()//滚动条的偏移量
	{
		if(window.pageYOffset)
		{
			return window.pageYOffset;
		}else{
			if(document.documentElement && document.documentElement.scrollTop)
			{
				return document.documentElement.scrollTop
			}else{
				return document.body.scrollTop
			}
		}
	}
	FloatLayer.prototype.ScrollLeft = function ()//滚动条的偏移量
	{
		if(window.pageXOffset){
			return window.pageXOffset;
		}else{
			if(document.documentElement && document.documentElement.scrollLeft){
				return document.documentElement.scrollLeft
			}else{
				return document.body.scrollLeft
			}
		}
	}
	
	FloatLayer.prototype.Float = function (sender)//滚动条的偏移量
	{
//		document.title=this.FloatMethod;
		switch(this.FloatMethod.toLowerCase())
		{
			case "x":
				this.FloatX(sender);
				break;
			case "y":
				this.FloatY(sender);
				break;
			default:
				this.FloatX(sender);
				this.FloatY(sender);
		}
	}
	
	FloatLayer.prototype.FloatY = function (sender)//滚动条的偏移量
	{
		var _direction = this.Y+this.ScrollTop() - sender.offsetTop;
		if(_direction==0)
		{
			return true;
		}
		var _amount = parseInt(_direction * this.Stepped);
		if(_amount==0)
		{
			if(_direction>0)
			{
				_amount=1
			}else{
				_amount=-1
			}
		}
		sender.style.top = sender.offsetTop+_amount+"px";
	}
	FloatLayer.prototype.FloatX = function (sender)//滚动条的偏移量
	{
		var _direction =this.X+this.ScrollLeft() - sender.offsetLeft;
		if(_direction==0)
		{
			return true;
		}
		var _amount = parseInt(_direction * this.Stepped);
		if(_amount==0)
		{
			if(_direction>0)
			{
				_amount=1
			}else{
				_amount=-1
			}
		}
		sender.style.left = sender.offsetLeft+_amount+"px";
	}
	