/**
 * Instantiate a Utility class
 * @class Common utility class - Singleton
 */
Util = new function(){
	var loadCount=0;
	
	this.startLoading = function (){
		loadCount++;
		//slow loading icon
		$('#loader').show();
	};
	this.finishLoading = function(){
		loadCount--;

		if(loadCount==0){
			//done loading -- hide loading icon
			$(document).trigger('load:finished');
			$('#loader').hide();
		}
	};
	this.imageUtil = new function(){
		var loadingImage = 'images/ajax-loader.gif';
		var stack = [];
		var loading = false;
		this.loadImageNCall = function (src,callback){
			Util.startLoading();
			//begin loading image
			$('<img src="'+src+'"/>').load(function (){
				Util.finishLoading();
				//execute the callback that does the actual image display
				callback(src);
			});
		};
		this.loadImage = function(src,lookup){
			setTimeout(function(){
				if(!loading){
					loading=true;
					Util.imageUtil.loadImageNCall(src,function(imgSrc){
						$(lookup()).attr('src',imgSrc);
						loading=false;
						if(stack.length>0){
							var nextImage = stack.pop();
							Util.loadImage(nextImage.src,nextImage.lookup);
						}
					});
				}else{
					stack.push({src:src,lookup:lookup});
				}
			},10);
			return loadingImage;
		};
	};
	this.loadImage = function(src,callback){
		this.imageUtil.loadImage(src,callback);
	};
	this.loadImageNCall = function(src,callback){
		this.imageUtil.loadImageNCall(src,callback);
	};
	/**
    * validate required attributes for configuration parameter
    * @param config the config parameter to check
    * @param props the properties of the config to check
    * @throws {MissingRequiredParameterException} Exception when required parameter is missing
    */
	this.require = function(config,props){
		if(!config){
			throw 'required config is missing';
		}
		for(var i=0;i<props.length;i++){
			if(!config[props[i]]){
				throw 'required ' + props[i] + ' is missing';
			}
		}
	};
	//Util.registerScript('YAHOO.real.tag.BaseComponent','js/component/BaseComponent.js');
	this.requireScript = function(className){
		if(eval(className)){
			//already loaded; do nothing
		}else{
			//load it
			var filePath = scripts[className];
		    var scriptBlock = document.createElement('script');
		    scriptBlock.type = 'text/javascript';
		    scriptBlock.src=filePath;
			var head = document.getElementsByTagName("head")[0];
			head.appendChild(scriptBlock);
		}
	},
	this.registerScript = function(className,path){
		scripts[className]=document.location.pathname.split('js/')[0]+path;
	}
};