/*
* 多个浮动单个飘浮广告
* by goslam.cn
* 2009年10月26日
*/

var Holy = function()
{
	this.Panel = null;
	this.Speed = 10;
	this.Left = 0;
	this.Top = 0;
	this.Width = 137;
	this.Height = 60;
	this.ImageUrl = '';
	this.LinkUrl = '';
	var isLeft = true;
	var isTop = true;
	this.Ads = [];
	this.Args = [];
	this.Add = function(args)
	{
		this.Speed = args.Speed;
		this.Left = args.Left;
		this.Top = args.Top;
		this.Width = args.Width;
		this.Height = args.Height;
		this.ImageUrl = args.ImageUrl;
		this.LinkUrl = args.LinkUrl;
		this.Panel = args.Panel;
		var ad = document.createElement("a"); 
		ad.Image = new Image; 
		ad.style.position = "absolute"; 
		ad.style.left = args.Left; 
		ad.style.top = args.Top;
		ad.style.width = args.Width;
		ad.style.height = args.Height;
		ad.Image.src = args.ImageUrl;
		ad.Image.style.width = args.Width;
		ad.Image.style.height = args.Height;
		if(this.LinkUrl) 
		{ 
			ad.href = this.LinkUrl; 
			ad.Image.border = 0; 
			ad.target = "_blank"; 
		} 
		ad.appendChild(ad.Image); 
		document.getElementById(this.Panel).appendChild(ad); 
		this.Ads.push(ad);
		this.Args.push(args)
	}

	this.scroll = function()
	{
		var doc = (document.compatMode && document.compatMode.toLowerCase() == "css1compat") ? document.documentElement : document.body;
		return { 'x' : doc.scrollLeft, 'y' : doc.scrollTop };
	};

	this.client = function()
	{
		var doc = (document.compatMode && document.compatMode.toLowerCase() == "css1compat") ? document.documentElement : document.body;
		return { 'x' : doc.clientWidth, 'y' : doc.clientHeight };
	};

	this.append = function()
	{
		for(var ad in this.Ads)
		{
			if (this.Args[ad].Speed==0)
			{
				this.Fixed(this.Ads[ad], this.Args[ad]);
			}
			else
			{
				this.Float(this.Ads[ad], this.Args[ad]);
			}
		}

		var me = this;
		window.onscroll = function(){
			var arg = me.Args;
			for(var ad in me.Ads)
			{
				if (me.Args[ad].Speed==0)
				{
					me.Ads[ad].style.left = me.Args[ad].Left + me.scroll().x;
					me.Ads[ad].style.top = me.Args[ad].Top + me.scroll().y;
				}
			}
		}
	}

	this.Fixed = function(obj, args)
	{
		obj.id = 'Fixed_' + Math.random();
		obj.style.left = args.Left;
		obj.style.top = args.Top;
	};
	
	this.Float = function(obj, args)
	{
		obj.id = 'Float_' + Math.random();
		var me = this;
		var imgInterval = setInterval(function(){ me.Start(obj); }, args.Speed);
		obj.onmouseover = function(){ clearInterval(imgInterval); } 
		obj.onmouseout = function(){ imgInterval = setInterval(function(){ me.Start(obj); }, args.Speed) } 
	};

	this.Start = function(obj)
	{
		var curLeft = parseInt(obj.style.left); 
		var curTop = parseInt(obj.style.top);
		var curWidth = parseInt(obj.style.width);
		var curHeight = parseInt(obj.style.height);
		if((curWidth + curLeft) > (this.client().x + this.scroll().x - 1)) 
		{ 
			curLeft = this.scroll().x + this.client().x - curWidth; 
			isLeft = false; 
		} 
		if((curHeight+ curTop) > (this.client().y + this.scroll().y - 1)) 
		{ 
			curTop = this.scroll().y + this.client().y - curHeight; 
			isTop = false; 
		} 
		if(curLeft < this.scroll().x) 
		{ 
			curLeft = this.scroll().x; 
			isLeft = true; 
		}
		if(curTop < this.scroll().y) 
		{ 
			curTop = this.scroll().y; 
			isTop = true; 
		} 
		
		curLeft = curLeft + (isLeft ? + 1 : -1);
		curTop = curTop + ((isTop) ? + 1 : -1);
		obj.style.left = curLeft;
		obj.style.top = curTop;
	}
}
