var Image = new function(){
	var _this = this;
	var _imagePath;
	var _baseUrl;
	var _defaultImage;
	var _currentImage;
	var _loading = false;

	this.init = function(){
		this.setImagePath('content/images/backgrounds/' + this.getCssPath() + '/');
	};

	this.loadImage = function(image, callback, donotcheck){
		if(image == undefined || image == null) image = _defaultImage;

		if(_currentImage == null || (_currentImage !== image) && !_loading){
			_loading = true;
			var doneloading = false;
			var checkTimeout = setTimeout(function(){
				//probably IE bug, load image again.
				if(!doneloading && !donotcheck){
					_this.loadImage(image, callback, true);
				}else{
					clearTimeout(checkTimeout);
				}
			}, 1500);
			this.showLoader(true);
			$.backstretch(_baseUrl + _imagePath + image, {speed: 500}, function(){
				doneloading = true;
				_loading = false;
				clearTimeout(checkTimeout);
				_currentImage = image;
				_this.showLoader(false);
				if(typeof callback == "function")
					callback();
			});
		}
	};

	this.showLoader = function(show){
		if(show) document.getElementById('imageloader-container').style.display = '';
		else 	 document.getElementById('imageloader-container').style.display = 'none';
		return;
	}

	this.setBaseUrl =		function(variable){_baseUrl = variable;};
	this.setImagePath =		function(variable){_imagePath = variable;};
	this.setDefaultImage =	function(variable){_defaultImage = variable;};
	this.setCurrentImage =	function(variable){_currentImage = variable;};

	this.getCssPath =		function(){
		var h 	= $(window).height();
		var w	= $(window).width();


		if(w <= 640)				return "640";
		if(w <= 800)				return "800";
		if(w <= 1024)				return "1024";
		if(w <= 1152)				return "1152";
		if(w <= 1280 && h <= 800) 	return "1280_800";
		if(w <= 1280) 				return "1280_1024";
		if(w <= 1600)				return "1600";

		return "1920";
	};

	this.init();
}();




