var j = jQuery.noConflict();

j(document).ready(function() {
	j.app.init();
});

if(typeof console == 'undefined')
{
	//var console = {log: function () { }};
}

j.app = {
	loadedScripts: [],
	loadedCss: [],
	loadedHtml: {},
	loadedJson: {},
	langCache: {},
	
	init: function () {
		
		j('body')
			.addClass(j.client.os.toLowerCase())
			.addClass(j.client.browser.toLowerCase())
			.addClass(j.client.browser.toLowerCase()+'-'+j.client.version);
	},
	
	/**
     * to make sure we wont have any cache problems when we update our system, for every javascript / css file we dynamically load we add a session
	 * we set a cookie with this random number, so that we can cache it for our current session however if someone restarts his browser he starts with a new cache.
	 * 
     * @lastedit 2011-03-22 Jeffrey de Vreede
     * @param void
     * @return int session
     */
	getSession: function () {
		if(j.cookie('cms-session'))
		{
			return j.cookie('cms-session');
		}
		else
		{
			var session = Math.floor(Math.random()*10000000);
			j.cookie('cms-session', session, {path:'/'});
			return session;
		}
	},
	
	/**
     * load the html into the cache using the getSession method
	 * 
     * @lastedit 2011-03-22 Jeffrey de Vreede
     * @param string url
     * @return string
     */
	loadHtml: function ( url ) {
		// skip if file is already loaded
		if ( ! this.loadedHtml[url] ) {
			// load html file
			var html;
			j.ajax({
				url: url + '?' + this.getSession(),
				async: false,
				type: 'POST',
				success: function (data) {
					html = data;
				}
			});

			// add to loaded array
			this.loadedHtml[url] = html;
			return html;
		} else {
			return this.loadedHtml[url];
		}
	},
	
	/**
     * load the html into the cache using the getSession method
	 * 
     * @lastedit 2011-03-22 Jeffrey de Vreede
     * @param string url
     * @return string
     */
	loadJson: function ( url, noCache ) {
		// skip if file is already loaded
		if ( ! this.loadedJson[url] || noCache ) {
			// load json file
			var json;
			j.ajax({
				url: url + '?' + this.getSession(),
				async: false,
				type: 'POST',
				dataType: 'json',
				success: function (data) {
					json = data;
				}
			});

			// add to loaded array
			this.loadedJson[url] = json;
			return json;
		} else {
			return this.loadedJson[url];
		}
	},
	
	/**
     * dynamically load the css using the getSession method
	 * 
     * @lastedit 2011-03-22 Jeffrey de Vreede
     * @param string url
     * @return object this for chaining 
     */
	loadCss: function ( url ) {
		// skip if css already loaded
		if ( j.inArray( url, this.loadedCss ) === -1 ) {
			// load css file
			j( 'head' )
				.append( '<link>' )
				.children( ':last' )
				.attr({
				  rel: 'stylesheet',
				  type: 'text/css',
				  href: url + '?' + this.getSession()
				});

			// add to loaded array
			this.loadedCss.push( url );
		}

		return this;
	},
	
	/**
     * dynamically load the js using the getSession method
	 * 
     * @lastedit 2011-03-22 Jeffrey de Vreede
     * @param string url
     * @return object this for chaining 
     */
	loadScript: function ( url, isAsync, callback ) {
		if(isAsync === null)
		{
			isAsync = true;
		}
		
		if ( j.inArray( url, this.loadedScripts ) === -1 ) {
			j.ajax({
				url: url + '?' + this.getSession(),
				dataType: 'script',
				async: isAsync,
				success: function () {
					j.app.loadedScripts.push( url );
					if ( j.isFunction( callback ) )
						callback.apply( this, arguments );
				}
			});
		} else {
			if ( j.isFunction( callback ) )
				callback.call();
		}
	},
	
	/**
     * load language file and cache the category
	 * 
     * @lastedit 2011-04-07 Jeffrey de Vreede
     * @param string name
     * @return string
     */
	lang: function (name) {
		var self = this;
		var tmp = name.split('/');
		var variable = tmp.pop();
		var file = tmp.join('-|-|-');
		// skip if file is already loaded
		if ( ! this.langCache[file] ) {
			// load lang file
			j.ajax({
				url: '/'+(j.app.name == 'site' ? 'ajax' : j.app.name)+'/lang:get:' + encodeURIComponent(file) + '?' + this.getSession(),
				async: false,
				type: 'GET',
				dataType: 'json',
				success: function (data) {
					self.langCache[file] = data;
				}
			});
		}
		if(this.langCache[file] && this.langCache[file][variable])
		{
			return this.langCache[file][variable];
		} else {
			return variable;
		}
	},
	
	/**
     * validate a date string
	 * 
     * @lastedit 2011-12-19 Jeffrey de Vreede
     * @param string name
     * @return string
     */
	isValidDate: function(date)
	{
		var date = date.split('-');
		var dateFormat = j.app.lang('settings/jsDateFormat').split('-');
		
		if(date[0] && date[1] && date[2] && date[1].length == 2)
		{
			
			if((dateFormat[0] == 'yy' && date[0].length == 4 && date[2].length == 2) || (dateFormat[2] == 'yy' && date[2].length == 4 && date[0].length == 2))
			{
				return true;
			}
		}
		return false;
	},
	
	/**
     * pause the js compiler
	 * 
     * @lastedit 2011-04-07 Jeffrey de Vreede
     * @param int miliseconds
     * @return void
     */
	pause: function(ms) {
		ms += new Date().getTime();
		while (new Date() < ms){}
	},
	getRandomNum: function(lbound, ubound) 
	{
		return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
	},
	getRandomChar: function(type) 
	{
		var numberChars = "0123456789";
		var lowerChars 	= "abcdefghijklmnopqrstuvwxyz";
		var upperChars 	= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var otherChars 	= "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
		var charSet		= '';
		
		if (type == 'number')
			charSet += numberChars;
			
		if (type == 'alphaLowerNum')
			charSet += numberChars+lowerChars;
			
		if (type == 'alphaUpperNum')
			charSet += numberChars+upperChars;
			
		if (type == 'alphaMixNum')
			charSet += upperChars+lowerChars+numberChars;
		
		if (type == 'alphaAllNum')
			charSet += upperChars+lowerChars+numberChars+otherChars;
		
		return charSet.charAt(this.getRandomNum(0, charSet.length));
	}
	,getPassword: function(length,type) 
	{
		if(!length)
			lengt = 8;
		
		if(!type)
			type = 'alphaMixNum';
		
		var password = "";
		var i		 = 0;
		
		while(length != i)
		{
			i++
			
			password+= this.getRandomChar(type);
		}
		
		return password;
	}
}
