(function($) {
	$.nb = {
		language: 'no'
	}
	
	//STRING METHODS
	//String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,''); };
	//String.prototype.normalize = String.prototype.normalise = function() { return this.trim().replace(/\s+/g,' '); };
	//String.prototype.startsWith = function(str,i){ i=(i)?'i':'';var re=new RegExp('^'+str,i);return (this.normalize().match(re)) ? true : false ; };
	//String.prototype.endsWith = function(str,i){ i=(i)?'gi':'g';var re=new RegExp(str+'$',i);return (this.normalize().match(re)) ? true : false ; };
	//String.prototype.isEmpty = function(){return this.replace(/^\s+|\s+$/g,'').length == 0;}
	
	$.extend({
		log : function(message,type,parameters){			
			try {
				switch(type){
					case 'log' : {
						console.log(message);
						break;	
					}	
					case 'debug' : {
						console.debug(message);	
						break;
					}
					case 'info' : {
						console.info(message);
						break;	
					}
					case 'warn' : {
						console.warn(message);	
						break;
					}
					case 'error' : {
						console.error(message);	
						break;
					}
					default : {
						console.log(message);
						break;
					}					
					
				}
			} catch(e) {
				//DO NOTHING	
			}			
		},
		
		isset : function(variable){
			return variable != undefined
		},
    	
		hasValue : function(variable){
			return $.isset(variable) && variable!=''
		},
		
    	exists : function(element) {
			return element != "" || $(element).length > 0			
		},
		
		parameterize : function(object,insideobject){
			var output = "";					
			if(insideobject){
				for(key in object){					
					if(typeof(object[key]) == "object" && !$.isArray(object[key])){			
						output += (output == "" ? "" : ",") + "\""+key+"\":{"+ $.parameterize(object[key],true) +"}";				
					} else if($.isArray(object[key])){		
						if(object[key].length == 0)
							output += (output == "" ? "" : ",") + "\""+key+"\":[]";
						else
							output += (output == "" ? "" : ",") + "\""+key+"\":\""+object[key]+"\"";
					} else {
						output += (output == "" ? "" : ",") + "\""+key+"\":\""+object[key]+"\"";
					}
				}
				
			} else {								
				for(key in object){
					if(typeof(object[key]) == "object" && !jQuery.isArray(object[key])){						
						output += "&"+key+"={"+ $.parameterize(object[key],true) +"}";				
					} else if($.isArray(object[key])){						
						if(object[key].length == 0)
							output += "&"+key+"=[]";
						else
							output += "&"+key+"="+object[key];
					} else {
						if(output == "")
							output += "&"+key+"="+object[key];
						else
							output += "&"+key+"="+object[key];					
					}        
				}
			}			
			return output;
		},
		
		locale: function(){
			if ( navigator ) {
				if ( navigator.language ) {
					return navigator.language;
				}
				else if ( navigator.browserLanguage ) {
					return navigator.browserLanguage;
				}
				else if ( navigator.systemLanguage ) {
					return navigator.systemLanguage;
				}
				else if ( navigator.userLanguage ) {
					return navigator.userLanguage;
				}
			}
		},

    	highlight : function(haystack,needle){					
        	var regex = new RegExp(">([^<]*)?("+needle+")([^>]*)?<","ig");
        	return haystack.replace(regex,'>$1<span class="netblast-highlight">$2</span>$3<');
    	}
	});
	
	$.fn.extend({
		busy : function(parameters){					
			
			var options = {
				background : 'transparent',
				indicator : 'http://www.muligheter.no/Webdesk/bilde/200787.gif',
				hideIndicator : false 
			};	
			
			if(parameters) $.extend(options, parameters);
									
			return this.each(function(){ 
				var self = this;
				
				var position = $(self).position();
	            if(options.hideIndicator){
			     options.indicator = '';   		
				}
				var mask = $("<div></div>")
					.addClass("netblast-element-loader")
					.css({
						'position':'absolute',
						'background-color':options.background,
						'background-image': 'url('+options.indicator+')',
						'height':'150px', 
                        'width':$(self).width(),
						'display':'block',
						'z-index':'700',
						'background-repeat':'no-repeat',
						'background-position':'50% 50%'
						});
					
				
				$(self).hide().before($(mask));			
			});
		},
		
		finished : function(){
			return this.each(function(){
			   var self = $(this);
			   $(self).parent().find(".netblast-element-loader").fadeOut('fast');	
			   $(self).hide().fadeIn('fast');					  
			});	
		}					
	});
	
	
	jQuery.fn.fadeIn = function(speed, callback) { 
		return this.animate({opacity: 'show'}, speed, function() { 
			if (jQuery.browser.msie) 
				this.style.removeAttribute('filter');  
			if (jQuery.isFunction(callback)) 
				callback();  
		}); 
	}; 
	 
	jQuery.fn.fadeOut = function(speed, callback) { 
		return this.animate({opacity: 'hide'}, speed, function() { 
			if (jQuery.browser.msie)  
				this.style.removeAttribute('filter');  
			if (jQuery.isFunction(callback)) 
				callback();  
		}); 
	}; 
	 
	jQuery.fn.fadeTo = function(speed,to,callback) { 
		return this.animate({opacity: to}, speed, function() { 
			if (to == 1 && jQuery.browser.msie)  
				this.style.removeAttribute('filter');  
			if (jQuery.isFunction(callback)) 
				callback();  
		}); 
	}; 	
	
})(jQuery);
