/**
 * @author ltarantino
 * @Version  $LastChangedRevision: 6739 $
 */
var messages = {entry:[]};
var scriptsCache = new Array();

if($) {
	$.extend ({
		getMessagePosition: function(messageKey){
			for(var l = 0; l < messages.entry.length;l++){
				if(messages.entry[l].key == messageKey)return l;
			}
			return -1;
		}
	});
	$.extend ({
		getMessage: function(messageKey){
			position = $.getMessagePosition(messageKey);
			if(position == -1){
				messageBundleLoader();
				position = $.getMessagePosition(messageKey);
			}
			if(position == -1){
				return '$'+messageKey;
			} else {
				return messages.entry[position].Text;
			}
		}
	});
	$.extend ({
     loadMessageBoundle: function (messageBoundle){
		$.ajax({type: "GET",
				url: messageBoundle,
				dataType: ($.browser.msie) ? "text" : "xml",
				async: false,
				success: function(data,testStatus) {
					var xml;
    			    if (typeof data == "string") {
       					xml = new ActiveXObject("Microsoft.XMLDOM");
       					xml.async = false;
       					xml.loadXML(data);
     				} else {
       					xml = data;
     				}
					 newBoundle = $.xmlToJSON(xml);
					 if(newBoundle == null){
;;					 		$(document).log("Got null response! for:"+messageBoundle+" Try to reload whitout cache!");
;;					 		alert("Unable to load "+messageBoundle);
					 } else {
;;					 		$(document).log("Got response! for:"+messageBoundle);
					 	for(var l = 0; l < newBoundle.entry.length;l++){
			 				position = $.getMessagePosition(newBoundle.entry[l].key);
			 				if(position == -1){
								messages.entry[messages.entry.length]=newBoundle.entry[l];
							} else {
								messages.entry[position]=newBoundle.entry[l];
							}
			 			}
			 		}
				}
		});
		}
	});

  	$.extend ({
		jsRegister: function(config){
			scriptsCache[config.id]=new Object();
			scriptsCache[config.id].url = config.url;
			scriptsCache[config.id].callback = config.callback;
			scriptsCache[config.id].cache = config.cache;
			scriptsCache[config.id].depends = config.depends;
			if(config.lazy == false && !scriptsCache[config.id].lock){
				for(var depend in scriptsCache[config.id].depends){
					$.jsInclude(depend);
				}
				scriptsCache[config.id].lock = true;
				$.getScript(scriptsCache[config.id].url,function(){
					scriptsCache[config.id].loaded = true;
					if($.isFunction(scriptsCache[config.id].callback))scriptsCache[config.id].callback();
				},scriptsCache[config.id].cache);
			}
		}
	});
  	$.extend ({
		jsInclude: function(id){
			if (scriptsCache[id] == undefined || scriptsCache[id].url == undefined ) {
				$(document).error("Script "+id+" not registered!");
			}
			else if(scriptsCache[id].loaded != true && !scriptsCache[id].lock){
			 try {
				scriptsCache[id].lock = true;
				for(var depend in scriptsCache[id].depends){
					$.jsInclude(scriptsCache[id].depends[depend]);
				}
 				$.ajaxSetup({async: false});
				$.getScript(scriptsCache[id].url,function(){
					scriptsCache[id].loaded = true;
					if($.isFunction(scriptsCache[id].callback))scriptsCache[id].callback();
				},scriptsCache[id].cache);
				$.ajaxSetup({async: true});
			 } catch (e){
	    			$(document).error(e);
	  		 }

			}
		}
	});

	$.extend ({
		addSheet:function ( url, doc ) {
       	 doc = doc || document;
      	  var link = doc.createElement( 'link' );
       	 link.rel = 'stylesheet';
       	 link.type = 'text/css';
       	 link.href = url;
       	 doc.getElementsByTagName('head')[0].appendChild(  link );
		}
	});
	$.extend({
		  getUrlVars: function(){
		    var vars = [], hash;
		    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		    for(var i = 0; i < hashes.length; i++)
		    {
		      hash = hashes[i].split('=');
		      vars.push(hash[0]);
		      vars[hash[0]] = hash[1];
		    }
		    return vars;
		  },
		  getUrlVar: function(name){
		    return $.getUrlVars()[name];
		  }
	});


} else {
	alert("jQuery library is not present");
}
