String.prototype.format = function() {
    var args = arguments; 
	return this.replace(/\{(\d+)\}/g, 
		function(m,i){ 
			return args[i]; 
	}); 
}

function CloseDisplay(adid){
	document.getElementById(adid).style.display = "none";
}

var codetemp = '<div style="width:{0}px;height:{1}px;position:absolute;top:0px;left:0px;z-index:9999;" id="{4}">';
	codetemp += '<div style="text-align:right;"><span style="font-sie:14px;padding:0 3px 0 3px;border:solid 1px #61666D; cursor:pointer;" onclick="CloseDisplay(\'{4}\');">×</span></div>';
	codetemp += '<div><a href="{3}" target="_blank"><img src="{2}" border="0" style="width:{0}px;height:{1}px;" /></a></div>';
	codetemp += '</div>';


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;
	var Ads = [];
	var 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) ? args.LinkUrl : "#";
		this.Panel = args.Panel;
		var PanelID = "panelad_" + Math.round(Math.random()* 100);
		var adCode = codetemp.format(this.Width,this.Height,this.ImageUrl,this.LinkUrl,PanelID);
		document.getElementById(this.Panel).innerHTML +=  adCode;
		var ad = document.getElementById(PanelID);
		ad.style.top = this.Top +'px';
		ad.style.left = this.Left +'px';
		Ads.push(ad);
		Args.push(args)
	}

	var scroll = function()
	{
		var doc = (document.compatMode && document.compatMode.toLowerCase() == "css1compat") ? document.documentElement : document.body;
		return { 'x' : doc.scrollLeft, 'y' : doc.scrollTop };
	};
	var $get = function(obj){ return typeof(obj)=='string' ? document.getElementById(obj) : obj;}
	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 Ads)
		{
			var obj = $get(Ads[ad].id);
			if (Args[ad].Speed==0)
			{
				Fixed(obj, Args[ad]);
			}
			else
			{
				this.Float(obj, Args[ad]);
			}
		}
		window.onscroll = function(){
			for(var ad in Ads)
			{
				var obj = $get(Ads[ad].id);
				if (Args[ad].Speed==0)
				{
					obj.style.left = parseInt(Args[ad].Left) + scroll().x + 'px';
					obj.style.top = parseInt(Args[ad].Top) + scroll().y + 'px';
				}
			}
		}
	}

	Fixed = function(obj, args)
	{
		obj.style.left = parseInt(args.Left) + 'px';
		obj.style.top = parseInt(args.Top) + 'px';
	};
	
	this.Float = function(obj, args)
	{
		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 + scroll().x - 1)) 
		{ 
			curLeft = scroll().x + this.client().x - curWidth; 
			isLeft = false; 
		} 
		if((curHeight+ curTop) > (this.client().y + scroll().y - 1)) 
		{ 
			curTop = scroll().y + this.client().y - curHeight; 
			isTop = false; 
		} 
		if(curLeft < scroll().x) 
		{ 
			curLeft = scroll().x; 
			isLeft = true; 
		}
		if(curTop < scroll().y) 
		{ 
			curTop = scroll().y; 
			isTop = true; 
		} 
		
		curLeft = curLeft + (isLeft ? + 1 : -1);
		curTop = curTop + ((isTop) ? + 1 : -1);
		obj.style.left = curLeft + 'px';
		obj.style.top = curTop + 'px';
	}
}

