Ext.namespace("WebGenie.utility");
Ext.namespace("WebGenie.bm");

// source: ExtJS\examples\shared\examples.js
WebGenie.utility.message = function(){
	var msgCt;

	function createBox(t, s){
		return ['<div class="msg">',
				'<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
				'<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', t, '</h3>', s, '</div></div></div>',
				'<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
				'</div>'].join('');
	}
	return {
		messageBox : function(title, format){
			if(!msgCt){
				msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
			}
			msgCt.alignTo(document, 't-t');
			var s = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
			var m = Ext.DomHelper.append(msgCt, {html:createBox(title, s)}, true);
			m.slideIn('t').pause(2).ghost("t", {remove:true});
		},

		messageBoxByArray : function(parameterArray){
			if(!msgCt){
				msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
			}
			msgCt.alignTo(document, 't-t');
			var s = String.format.apply(String, Array.prototype.slice.call(parameterArray, 1));
			var m = Ext.DomHelper.append(msgCt, {html:createBox(parameterArray[0], s)}, true);
			m.slideIn('t').pause(2).ghost("t", {remove:true});
		}
	}
}();

WebGenie.utility.log = function(){
	var enable = true;
	var enableMessageBox = false;

	return {
		log : function(level, message){
			if (enable)
			{
				if (enableMessageBox)
				{
					WebGenie.utility.message.messageBox(level, message);
				}
				if(window.console && window.console.firebug)
				{
					console.debug(level + ' ' + new Date().format('Y-m-d H:i:s u') + '\n' + message);
				}
			}
		},
		debug: function(message){
			this.log('DEBUG', message);
		},
		info: function(message){
			this.log('INFO', message);
		},
		warn : function(message){
			this.log('WARN', message);
		},
		error : function(message){
			this.log('ERROR', message);
		},
		fatal : function(message){
			this.log('FATAL', message);
		}
	};
}();

WebGenie.utility.firebug = function(){
	var enable = false;

	return {
		alertFirebug : function(){
			if (enable)
			{
				if(window.console && window.console.firebug){
					Ext.Msg.alert('Warning', 'Firebug is known to cause performance issues with Ext JS.');
				}
			}
		}
	}
}();

LOG = WebGenie.utility.log;

// http://stuck.myweb.hinet.net/c/js/js_cookies.htm
WebGenie.utility.cookie = function()
{
	return {
		setCookie: function ( name, value, expires, path){
			var today = new Date();
			today.setTime( today.getTime() );
			
			/*
			if the expires variable is set, make the correct 
			expires time, the current script below will set 
			it for x number of days, to make it for hours, 
			delete * 24, for minutes, delete * 60 * 24
			*/
			if( expires ){
				expires = expires * 1000 * 60 * 60 * 24;
			}
			
			var expires_date = new Date( today.getTime() + (expires) );
			var cookieStr = name + "=" +escape( value ) +
			(( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
			( ( path ) ? ";path=" + path : "" );
			
			document.cookie = cookieStr;
		},
		
		getCookie: function (name){
			var start = document.cookie.indexOf( name + "=" );
			var len = start + name.length + 1;
			if ( ( !start ) &&	( name != document.cookie.substring( 0, name.length ) ) ){
				return null;
			}
			if ( start == -1 ) return null;
			var end = document.cookie.indexOf( ";", len );
			if ( end == -1 ) end = document.cookie.length;
			return unescape( document.cookie.substring( len, end ) );
		},
		
		deleteCookie: function ( name, path, domain ) {
			if ( GetCookie( name ) ) 
				document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") +( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}
		
	}
}();

function Minimize()
{
	window.innerWidth = 100;
	window.innerHeight = 100;
	window.screenX = screen.width;
	window.screenY = screen.height;
	alwaysLowered = true;
}

function Maximize()
{
	window.moveTo(0,0);
	window.resizeTo(screen.width,screen.height);
}

function isTrueValue(booleanValue)
{
	return (booleanValue == true || booleanValue == 'true');
}