//A simple loader object
function mLoader(elId,sprite) {
	this._sprite = (sprite == null) ? 1 : sprite;
	this._elId = elId;
	this._srcImg = 'images/ajax-loader.gif';
	//The loader element
	this._loader = $('<div>').addClass('loader').css({
		'display':'none',
		'position':'absolute'
	});
	this._loaderImg = $('<img /\>').attr('src', this._srcImg);
	this._loaderImg.appendTo(this._loader);
	/**
	* This holds the css selector for child elements that use transparent png will
	* require a hack to work around IEs png with opacity parent elements.
	*/
	this._ieFixEl = null;
	
	this.run = function() {
		var el = $(this._elId);
		/**
		* Because IE sucks and those bums at MS couldn't be asked to fix the
		* transparent PNG with opacity set parent bug in IE8 (despite being
		* known in IE7) we have to do this to work around it.
		*/
		if ( this._ieFixEl != null && jQuery.browser.msie ) {
			$(this._ieFixEl,el).css({
				'background':'none',
				'filter':"progid:DXImageTransform.Microsoft.alpha(opacity=100) "
					+"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='assets/images/prod-thumb-bg.png', "
					+"sizingMethod='scale')"});
			/*
			$("img",el).css({
				'position':'relative'
			}).fadeTo("slow", 0.33);
			*/
		}
		//el.children(':not(div.loader)').fadeTo("slow", 0.33);
		el.fadeTo("slow", 0.33);
		//this._loader.appendTo(this._elId);
		el.after(this._loader);
		var pos = el.position();
		var pHeight = $(this._elId).height();
		var pWidth = $(this._elId).width();
		var top = Math.round(((pHeight / 2) + pos.top) - (this._loader.height() / 2) )+'px';
		var left = Math.round(((pWidth / 2) + pos.left) - (this._loader.width() / 2) )+'px';
		
		this._loader.css({
			'top':top,
			'left':left
		});
		
		this._loader.show();
		
	}
	//If true is passed then an error message will be shown
	this.stop =  function(fail) {
		fail = (fail == null) ? false : fail;
		if ( fail ) {
			alert('Failed request!');
		} else {
			this._loader.hide();
			$(this._elId).fadeTo("fast", 1,function(){
				$(this).css("opacity",'');
			});
		}
	}
	
	this.setImgSrc = function(srcImg) {
		this._srcImg = srcImg;
		this._loaderImg.attr('src', this._srcImg);
	}
	
	/**
	* Sets the selectors to use if some elements require the PNG fix for IE.
	* Note that this should only be a selector relative to the parent defined
	* in elId.
	*/
	this.setIeFixSelector = function(selector) {
		this._ieFixEl = selector;
	}
	
}
//Simple utility function so we can easily pass a method as a callback
function bind(toObject, methodName){
    return function(){toObject[methodName]()}
}
