/**
 * Main class
 * @class
 */
intelli = function()
{
	return {
		/**
		 * Language array
		 */
		lang: null,
		/**
		 * Configuration array
		 */
		css: new Array(),
		/**
		 *  Exist value in the array
		 *  @param {Array} arr array
		 *  @param {String} value
		 *  @return {Boolean}
		 */
		inArray: function(val, arr)
		{
			if(typeof arr == 'object' && arr)
			{
				for(var i = 0; i < arr.length; i++) 
				{
					if(arr[i] == val) 
					{
						return true;
					}
				}

				return false;
			}

			return false;
		},
		/**
		 * Remove one item in the array
		 * @param {Array} arr array
		 * @param {String} val value
		 * @return {Array}
		 */
		remove: function(arr, val)
		{
			if(typeof arr == 'object')
			{
				for(var i = 0; i < arr.length; i++) 
				{
					if(arr[i] == val)
					{
						arr.splice(i, 1);
					}
				}
			}

			return arr;
		},
		/**
		 *  Load configuration or language phrases
		 *  @param {Array} array of parametrs lang|conf
		 *  TODO: store variables in the session. Use sessvars lib.
		 */
		loader: function(params)
		{
			var out = '';
			var url = '';

			url += (typeof params.conf != 'undefined') ? 'conf=' + params.conf : '';
			url += (typeof params.lang != 'undefined') ? '&lang=' + params.lang : '';

			$.ajax({
				type: 'POST', 
				url: 'loader.php?load=vars', 
				data: url,
				async: false,
				success: function(p)
				{
					out = eval('(' + p + ')');
				}
			});
			
			if(typeof out.conf != 'undefined')
			{
				if(null == intelli.conf)
				{
					intelli.conf = out.conf;
				}
				else
				{
					var keys = params.conf.split(',');
					
					for(var i = 0; i <= keys.length; i++)
					{
						intelli.conf[keys[i]] = out.conf[keys[i]];
					}
				}
			}

			if(typeof out.lang != 'undefined')
			{
				if(null == intelli.lang)
				{
					intelli.lang = out.lang;
				}
				else
				{
					var keys = params.lang.split(',');
					
					for(var i = 0; i <= keys.length; i++)
					{
						intelli.lang[keys[i]] = out.lang[keys[i]];
					}
				}
			}
		},
		/**
		 *  Hidding or showing some element
		 *  @param {String} obj Can be passed with # symbol
		 *  @param {String} action  show|hide|auto
		 */
		display: function(obj, action)
		{
			var obj = (typeof obj == 'object') ? obj : (-1 != obj.indexOf('#')) ? $(obj) : $('#' + obj);
			
			action = action ? action : 'auto';

			if('auto' == action)
			{
				action = ('none' == obj.css('display')) ? 'show' : 'hide';
			}
			
			if('hide' == action)
			{
				if($.browser.msie)
				{
					obj.hide();
				}
				else
				{
					obj.slideUp('fast');
				}
			}

			if('show' == action)
			{
				if($.browser.msie)
				{
					obj.show();
				}
				else
				{
					obj.slideDown('fast');
				}
			}
		},
		/**
		 * Return random letter
		 * TODO: get several letters. get letter in upper case.
		 */
		getRandomLetter: function()
		{
			return String.fromCharCode(97 + Math.round(Math.random() * 25));

			/* For upper case */
			//return String.fromCharCode(65 + Math.round(Math.random() * 25));
		},
		/**
		 * Show error message
		 */
		error: function(error)
		{
			alert(error);
		},
		/**
		 * Create new cookie
		 * @param {String} name The name of cookie
		 * @param {String} value The value of cookie
		 * @param {Integer} days The expire time of cookie
		 */
		createCookie: function(name, value, days)
		{
			if (days)
			{
				var date = new Date();
				date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
				var expires = "; expires=" + date.toGMTString();
			}
			else 
			{
				var expires = "";
			}
			
			document.cookie = name + "=" + value + expires + "; path=/";
		},
		/**
		 * Return the value of cookie
		 * @param {String} name The name of cookie
		 * @return {String}
		 */
		readCookie: function(name)
		{
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i = 0; i < ca.length; i++)
			{
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1, c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
			}
			return null;
		},
		/**
		 * Clear cookie value
		 * @param {String} name The name of cookie
		 */
		eraseCookie: function(name)
		{
			createCookie(name, "", -1);
		},
		cssCapture: function(attr, val)
		{
			this.css.push(attr + ':' + val);
		},
		cssClear: function()
		{
			this.css = [];
		},
		cssExtract: function()
		{
			return this.css.join('; ') + ';';
		},
		urlVal: function( name )
		{
			name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
			
			var regexS = "[\\?&]"+name+"=([^&#]*)";
			var regex = new RegExp( regexS );
			var results = regex.exec(window.location.href);
			
			if(results == null)
			{
				return null;
			}
			else
			{
				return results[1];
			}
		},
		notifBox: function(o)
		{
			var obj = $("#" + o.id);
			var html = '';

			if(!obj.length)
			{
				this.error("Can't find element with ID: " + o.id);

				return false;
			}

			html += '<ul>';
			for(var i = 0; i < o.msg.length; i++)
			{
				html += '<li>' + o.msg[i] + '</li>';
			}
			html += '</ul>';

			obj.css("padding", "0").css("margin", "0").attr("class", "");
			obj.addClass(o.type).html(html).show();

			$('html, body').animate({scrollTop: obj.offset().top}, 'slow');
		},
		copy2clipboard: function(text)
		{
			if(window.clipboardData)
			{
				window.clipboardData.setData('text',text);
			}
			else
			{
				var clipboarddiv = document.getElementById('divclipboardswf');

				if(clipboarddiv == null)
				{
					clipboarddiv=document.createElement('div');
					clipboarddiv.setAttribute("name", "divclipboardswf");
					clipboarddiv.setAttribute("id", "divclipboardswf");
					document.body.appendChild(clipboarddiv);
				}

				clipboarddiv.innerHTML='<embed src="templates/'+ intelli.config.admin_tmpl +'/img/clipboard.swf" FlashVars="clipboard='+ encodeURIComponent(text)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
			}

			alert('The text is copied to your clipboard...');

			return false;
		},
		is_int: function(input)
		{
			return !isNaN(input) && parseInt(input) == input;
		},
		is_email: function(email)
		{
			var result = email.search(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,3})+$/);
			
			if(result > -1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	};
}();

intelli.lang = new Object();

