﻿var Logic = function($){
	/**
	 * private methods and variables
	 */
	var priv = {
		debug	: true,		//enables/disables the error logging
		debugSeverity : 0,	//level of error logging
		console	: $("<ul>").addClass("debug-console"),
				
		initConsole	: function(){
			//if the console has not yet been added to the document, do so now
			if(!priv.console.parentNode){
				$("body").append(priv.console);
			} 
		}

	};
	
	/**
	 * public methods
	 */
	return {
		
		/**
		 * initializes the page logic
		 * to be called on $(document).ready
		 */
		OnReady	: function(){
			//add trim method to the string object
			String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");}
			String.prototype.trimBetween = function(){
		        var vals = this.split(" ");
		        var newString = '';
		        for(var i=0;i<vals.length;i++) { 
		            var s = vals[i];
			        if(s.trim().length > 0){
				        newString += s + " ";
			        }
		        }
		        return newString.trim().replace(/\n/g,'');
			}
			String.prototype.stripTags = function() {return this.replace(/<(.|\n)*?>/g, "");}
			String.format = function()
            {
                if( arguments.length == 0 )
                    return null;

                var str = arguments[0];
                for(var i=1;i<arguments.length;i++)
                {
                    var re = new RegExp('\\{' + (i-1) + '\\}','gm');
                    str = str.replace(re, arguments[i]);
                }

                return str;
            }            

			if(typeof(Search) != "undefined"){
				Search.OnReady();
			}
			
						
			$(window).bind("load",
				function() {
					
				}
			);
		},
		
		getJsParams : function (javascriptfile){
            var params = new Object();

            javascriptfile = javascriptfile + '?';
            var tags = document.getElementsByTagName('script');
            for (var n = 0; n < tags.length; n++)
            {
                var startIndex = tags[n].src.indexOf(javascriptfile);
                if(startIndex != -1){
                   var urlparams = tags[n].src.substring(javascriptfile.length+startIndex);
                   var params = urlparams.split('&');
                   for(var i = 0;i<params.length;i++){
                     var param = params[i].split('=');
                     if(param[1] != null && param[1] != ''){
                        params[param[0]] = param[1];
                     }
                   }
                }
            }
            
            return params;
        },
        		
		/**
		 * handles the writing of debug messages
		 * @param {String} msg
		 * @param {Int} severity: 0 info, 1 wanring, 2 error, 3 critical
		 */
		writeDebug	: function(msg, severity){
			if(typeof(severity) == "undefined"){
				severity = 0;
			}
			
			if(priv.debug && severity <= priv.debugSeverity){
				//check whether debug console exists
				if(typeof(console) != "undefined" && console.log){
					console.log(msg);
				}
				//if no debug console exists: create debug div
				else {
					priv.initConsole();
					$(priv.console).append(
						$("<li>").html(msg)
					);
				}
			}
		},
		
		/**
		 * updates urls when user clicks the remember choice button on the homepage
		 */
		
		updateHomepageUrl : function()
		{
		    if ($('#remember-choice').is(':checked'))
		    {
		        $('.link-home').each(function() {
		            $(this).attr('href', $(this).attr('href') + '?remember=1');
		        });
		    }
		    else
		    {
		        $('.link-home').each(function() {
		            $(this).attr('href', $(this).attr('href').replace('?remember=1', ''));
		        });
		    }
		}
	}
}(jQuery);

/**
 * Initiate onload methods and functions
 */
$(document).ready(
	function(){
		Logic.OnReady();
	}
);