Loader = Class.create();
Loader.DefaultOptions = 
{
	imgPath:	"/images/",
	imgPrefix:	"loading-anim-",
	imgExt:		".png",
	imgTitle:	"loading",
	frames:		"16",
	delay:		"75",
	play:		true,
	playDelay:	"100"
}
Loader.prototype =
{
	initialize : function(options)
	{
		this.options = Object.extend(Object.extend({},Loader.DefaultOptions), options || {});
		this.options.parent = $(this.options.parent);
		
		if (!this.options.id)
		{
			this.options.id = parseInt(new Date().getTime()) * (Math.floor(Math.random()*25));
		}
		
		this._createLoader();
		this._loadFrames();
		this.currentFrame = 1;
		
		if (this.options.play)
		{
			this.show();
		}
		else
		{
			this.hide();
		}
	},
	
	play : function()
	{
		if (this.interval) { window.clearInterval(this.interval); }
		
		var next = (this.currentFrame == 16) ? 1 : this.currentFrame + 1;
		
		$(this.options.id + "LoadingFrame_" + this.currentFrame).style.display = "none";
		$(this.options.id + "LoadingFrame_" + next).style.display = "block";
		
		this.currentFrame = (this.currentFrame == 16) ? 1 : this.currentFrame + 1;
		this.interval = window.setInterval(this.play.bind(this), this.options.delay);
	},
	
	stop : function()
	{
		if (this.interval) { window.clearInterval(this.interval); }
		$(this.options.id + "LoadingFrame_" + this.currentFrame).style.display = "none";
		this.currentFrame = 1;
	},
	
	pause : function()
	{
		if (this.interval) { window.clearInterval(this.interval); }
	},
	
	show : function()
	{
		if (this.doNotPlay)
		{
			this.doNotPlay = false;
			return false;
		}
		Element.setStyle(this.container, { opacity: 0 });
		if (Effect.fade)
		{
			Effect.fade(this.container,
			{
				duration: 300,
				acceleration: 1,
				from: 0,
				to: 1
			});
		}
		else
		{
			Element.setStyle(this.container, { display: "block", opacity: 1 });
		}
		this.play();
	},
	
	delayShow: function(delay)
	{
		if (!delay) { delay = this.options.playDelay; }
		this.delayPlay = true;
		this.doNotPlay = false;
		window.setTimeout(this.show.bind(this), delay);
	},
	
	hide : function()
	{
		if (this.delayPlay)
		{
			this.delayPlay = false;
			this.doNotPlay = true;
		}
		if (Effect.fade)
		{
			Effect.fade(this.container,
			{
				duration: 300,
				acceleration: 1,
				from: parseInt(Element.getStyle(this.container, "opacity")),
				to: 0
			});
		}
		else
		{
			Element.setStyle(this.container, { display: "none", opacity: 0 });
		}
		this.stop();
	},
	
	remove: function()
	{
		this.stop();
		this.options.parent.removeChild(this.container);
	},
	
	_loadFrames : function()
	{
		var imgs = [];
		var div = null;
		
		for (var i = 0; i<this.options.frames; i++)
		{
			imgs[i] = new Image();
			imgs[i].src = this.options.imgPath + this.options.imgPrefix + (i+1) + this.options.imgExt;
			
			div = document.createElement("div");
			if(/MSIE/.test(navigator.userAgent))
			{
				div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='" + imgs[i].src + "')";
			}
			else
			{
				div.style.backgroundImage = "url(" + imgs[i].src + ")";
			}
			Element.setStyle(div, { display: "none", width: "32px", height: "32px", position: "absolute" });
			
			div.id = this.options.id + "LoadingFrame_" + (i+1);
			this.animation.appendChild(div);
		}
	},
	
	_createLoader : function()
	{
		this.container = document.createElement("div");
		this.title     = document.createElement("div");
		this.animation = document.createElement("div");
		
		this.container.setAttribute("id", this.options.id + "LoadingContainer");
		this.title.setAttribute("id", this.options.id + "LoadingTitle");
		this.animation.setAttribute("id", this.options.id + "LoadingAnimation");
		
		var top = (this.options.parent.offsetHeight/2) - 23;
		var left = (this.options.parent.offsetWidth/2) - 22;
		Element.setStyle(this.container, { position: "absolute", top: top+"px", left: left+"px", width: "43px", height: "46px" });
		
		if (/MSIE/.test(navigator.userAgent))
		{
			var path = this.options.imgPath + this.options.imgTitle + this.options.imgExt;
			this.title.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='" + path + "')";
			Element.setStyle(this.title, { fontSize: "1px", lineHeight: "0px" });
		}
		else
		{
			this.title.style.background = "url(" + this.options.imgPath + this.options.imgTitle + this.options.imgExt + ")";
		}
		Element.setStyle(this.title, {  position: "absolute", top: "0px", left: "0px", width: "43px", height: "8px" });
		
		Element.setStyle(this.animation, { position: "absolute", top: "14px", left: "6px", width: "32px", height: "32px" });
		
		this.options.parent.appendChild(this.container);
		this.container.appendChild(this.title);
		this.container.appendChild(this.animation);
	}
}