/*======================================================================*\
|| #################################################################### ||
|| #  				  Imperial Bulletin Board v1.x                    # ||
|| # ---------------------------------------------------------------- # ||
|| #  For license, version and changelog questions or concerns,       # ||
|| #  navigate to the docs/ folder or visit the forums at the		  # ||
|| #  website, http://www.imperialbb.com/forums. with your questions. # ||
|| # ---------------------------------------------------------------- # ||
|| # Name: js_init.js                                                 # ||
|| # ---------------------------------------------------------------- # ||
|| #          "Copyright(c) 2007-2010 Imperial Development"           # ||
|| # ---------------------------------------------------------------- # ||
|| #################################################################### ||
\*======================================================================*/

if ( !window.console || !console.firebug )
{
	window.console = new Array ( );
	var names = new Array ( 'log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 
				  			'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd' );
	for ( var i = 0; i < names.length; ++i )
	{
		window.console[ names[ i ] ] = function ( ) 
		{
	
		};
	}
}

function ibb_custom_event ( )
{ 
	this.fire 	   = function ( ) { }; 
	this.subscribe = function ( ) { };
};
	
// Declare IBB if not already declared, else leave it.
if ( typeof ( IBB ) == 'undefined' )
{
	var IBB = { };
}

IBB.namespace = function ( )
{
	var args 	 = arguments;
	var curr_pos = null;
	var ns_array;
	for ( var i = 0; i < args.length; i++ )
	{
		curr_pos = IBB;
		ns_array = args[ i ].split ( '.' );
		for ( var j = ( ( ns_array[ 0 ] == 'IBB' ) ? 1 : 0 ); j < ns_array.length; j++ )
		{
			curr_pos[ ns_array[ j ] ] = curr_pos[ ns_array[ j ] ] || { };
			curr_pos 				  = curr_pos[ ns_array[ j ] ];
		}
	}
	return true;
}

IBB.init = function ( )
{
	this.ua 			 = navigator.userAgent.toLowerCase ( );
	this['debug']		 = false;
	this['debugjs']		 = true;
	this['overide'] 	 = false;
	this['wkver']	     = this.ua.match ( /applewebkit\/([0-9]+)/ );
	this['is_webtv']  	 = ( ( this.ua.indexOf ( 'webtv' ) != -1 ) );
	this['is_opera']  	 = ( ( this.ua.indexOf ( 'opera' ) != -1 ) );
	this['is_opera5'] 	 = ( ( this.ua.indexOf ( 'opera/5' ) != -1 ) || ( this.ua.indexOf ( 'opera 5' ) != -1 ) );
	this['is_opera6'] 	 = ( ( this.ua.indexOf ( 'opera/6' ) != -1 ) || ( this.ua.indexOf ( 'opera 6' ) != -1 ) );
	this['is_opera7'] 	 = ( ( this.ua.indexOf ( 'opera/7' ) != -1 ) || ( this.ua.indexOf ( 'opera 7' ) != -1 ) );
	this['is_opera8']	 = ( ( this.ua.indexOf ( 'opera/8' ) != -1 ) || ( this.ua.indexOf ( 'opera 8' ) != -1 ) );
	this['is_opera9']	 = ( ( this.ua.indexOf ( 'opera/9' ) != -1 ) || ( this.ua.indexOf ( 'opera 9' ) != -1 ) );
	this['is_opera95']	 = ( ( this.ua.indexOf ( 'opera/9.5' ) != -1 ) || ( this.ua.indexOf ( 'opera 9.5' ) != -1 ) );
	this['is_opera95u']  = this.is_opera95;
	this['is_saf'] 	  	 = ( ( ( this.ua.indexOf ( 'applewebkit' ) != -1 ) || ( this.ua.indexOf ( 'safari' ) != -1 ) ) || 
						   ( ( this.ua.indexOf ( 'konqueror' ) != -1 ) || ( this.ua.indexOf ( 'khtml' ) != -1 ) ) );
	this['is_kon']    	 = this.is_saf;
	this['is_ie']     	 = ( ( this.ua.indexOf ( 'msie' ) != -1 ) && !this.is_opera && !this.is_saf && !this.is_webtv );
	this['is_ie4']    	 = ( this.is_ie && ( this.ua.indexOf ( 'msie 4.' )  != -1 ) );
	this['is_ie5'] 	  	 = ( this.is_ie && ( this.ua.indexOf ( 'msie 5.' )  != -1 ) );
	this['is_ie50']   	 = ( this.is_ie && ( this.ua.indexOf ( 'msie 5.0' )  != -1 ) );
	this['is_ie55']   	 = ( this.is_ie && ( this.ua.indexOf ( 'msie 5.5' )  != -1 ) );
	this['is_ie5u']   	 = ( this.is_ie && !this.is_ie4 );
	this['is_ie6'] 	  	 = ( this.is_ie && ( this.ua.indexOf ( 'msie 6.' )  != -1 ) );
	this['is_ie6u']   	 = ( this.is_ie5u && !this.is_ie55 && !this.is_ie5 );
	this['is_ie7'] 	  	 = ( this.is_ie && ( this.ua.indexOf ( 'msie 7.' )  != -1 ) );
	this['is_ie7u']   	 = ( this.is_ie6u && !this.is_ie6 );
	this['is_ie7d']   	 = ( this.is_ie7 || this.is_ie6 || this.is_ie5 || this.is_ie4 );
	this['is_ie8'] 	  	 = ( this.is_ie && ( this.ua.indexOf ( 'msie 8' ) != -1 ) );
	this['is_ie8u']   	 = ( this.is_ie8 && !this.is_ie7d );
	this['is_ff'] 	  	 = ( ( this.ua.indexOf ( 'firefox' ) != -1 ) && !this.is_opera );
	this['is_moz']    	 = ( ( this.ua.indexOf ( 'gecko' ) != -1 ) && !this.is_saf && !this.is_opera );
	this['is_ns']     	 = ( ( this.ua.indexOf ( 'compatible' ) == -1 ) && ( this.ua.indexOf ( 'mozilla' ) != -1 ) && 
							 ( !this.is_opera ) && ( !this.is_webtv ) && ( !this.is_saf ) );
	this['is_ns4']    	 = ( ( this.is_ns ) && ( parseInt ( navigator.appBVersion ) == 4 ) );
	this['is_mac']    	 = ( ( this.ua.indexOf ( 'mac' ) != -1 ) || ( navigator.vendor == 'Apple Computer, Inc.' ) );
	this['is_win'] 	  	 = ( ( this.ua.indexOf ( 'win' ) != -1 ) || ( this.ua.indexOf ( '16bit' ) !=- 1 ) );
	this['is_chrome'] 	 = ( ( this.ua.indexOf('chrome') != -1 ) );
	this['is_camino'] 	 = ( ( this.ua.indexOf ( 'gecko' ) != -1 ) && ( navigator.vendor == 'Camino' ) );
	this['is_mobile'] 	 = ( ( this.ua.indexOf ( 'applewebkit' ) != -1 ) && ( this.ua.indexOf ( 'mobile' ) != -1 ) );
	this['is_ps3'] 	  	 = ( ( this.ua.indexOf ( 'playstation 3' ) != -1 ) );
	this['is_iphone'] 	 = ( ( this.ua.indexOf ( 'iphone' ) != -1 ) );
	this['is_ipod'] 	 = ( ( this.ua.indexOf ( 'ipod' ) != -1 ) );
	this['cursor']    	 = ( ( this.is_ie ) ? 'hand' : 'pointer' );
	this['ua_version']	 = parseInt ( navigator.appVersion );
	this['isregexp']  	 = ( ( window.RegExp ) ? true : false );
	this.included 		 = new Array ( );
	this.eventary		 = new Array ( );
	
	this.init_system = function ( )
	{
		this.eventary.sysinit.fire ( );
		console.info ( 'IBB System Init Fired' );
	};
	
	this.add_event = function ( name )
	{
		this.eventary[ name ] = new ibb_custom_event ( );
	};
	
	this.add_event ( 'sysinit' );

	this.fetch_obj = function ( id )
	{
		itm = null;
		if ( document.getElementById ) // DOM
		{
			itm = document.getElementById ( id );
		}
		else if ( document.all ) // IE
		{
			itm = document.all[ id ];
		}
		else if ( document.layers ) // NS4
		{
			itm = document.layers[ id ];
		}
		return itm;
	};

	this.fetch_tag = function ( tag )
	{
		if ( document.getElementsByTagName )
		{
			return document.getElementsByTagName ( tag );
		}
		else if (document.all && document.all.tags)
		{
			return document.all.tags ( tag );
		}
		else
		{
			return null;
		}
	};

	this.include_script = function ( url, did )
	{
		if ( this.is_wkit || this.is_ie || this.is_saf || this.is_chrome )
		{
    		document.write ( '<script type="text/javascript" src="'+root_path+'jscripts/'+url+'.js?v'+ibbversion+'"><\/script>' );
		}
		else
		{
			var src = root_path+'jscripts/'+url+'.js?v'+ibbversion;
			var obj = '';
			if ( did )
			{
				obj = this.fetch_obj ( did );
			}
			else
			{
				if ( ( obj = this.fetch_tag ( 'head' ).item ( 0 ) ) == 'undefined' )
				{
					obj = this.fetch_tag ( 'body' ).item ( 0 );
				}
			}
			var js  	= document.createElement ( 'script' );
			js.language = 'javascript';
			js.type 	= 'text/javascript';
			js.src 		= src;
			obj.appendChild ( js );
		}
		this.included[ url ] = true;
		if ( this.debugjs == true )
		{
			console.log ( 'IBB :: LOADED JS FILE: ( '+url+'.js )' );
		}
		if ( this.debug == true )
		{
			document.write ( "<span style='color: #FF0000; font-size: 13px;'>IBB :: LOADED JS FILE: ( "+url+".js )</span><br />" );
		}
		return false;
	};

	this.include_scripts = function ( url, callback )
	{
		if ( this.is_wkit || this.is_ie || this.is_saf || this.is_chrome )
		{
    		document.write ( '<script type="text/javascript" src="'+root_path+'jscripts/'+url+'.js?v'+ibbversion+'"><\/script>' );
			if ( this.debugjs == true )
			{
				console.log ( 'IBB :: LOADED JS FILE: ( '+url+'.js )' );
			}
		}
		else
		{
			var obj 	= null;
			var js  	= document.createElement ( 'script' );
			js.type 	= 'text/javascript';
			js.language = 'javascript';
			obj = this.fetch_tag ( 'head' )[ 0 ];
			if ( typeof ( callback ) != 'undefined' )
			{
				if ( js.readyState )
				{
					js.onreadystatechange = function ( )
					{
						if ( js.readyState == 'loaded' || js.readyState == 'complete' )
						{
							js.onreadystatechange = null;
							callback ( );
						}
					};
				} 
				else 
				{
					js.onload = function ( )
					{
						callback ( );
					};
				}
			}
			js.src = root_path+'jscripts/'+url+'.js?v'+ibbversion;
			obj.appendChild ( js );
		}
		this.included[ url ] = true;
		if ( this.debugjs == true )
		{
			console.log ( 'IBB :: LOADED JS FILE: ( '+url+'.js )' );
		}
		if ( this.debug == true )
		{
			document.write ( "<span style='color: #FF0000; font-size: 13px;'>IBB :: LOADED JS FILE: ( "+url+".js )</span><br />" );
		}
	};

	this.load_js_init = function ( data )
	{
		if ( data.length > 0 )
		{
			this.jsfiles = data;
		}
		else
		{
			this.jsfiles = new Array (
				'js_debug',
				'js_global', 
				'js_language', 
				'js_event', 
				'js_xml',
				'js_ajax',
				'js_cookies', 
				'js_ddmenu', 
				'js_misc', 
				'js_msgbox', 
				'js_toggle',
				'js_drag',
				'js_tabber'
			);
		}
	};

	this.load_js_exec = function ( )
	{
		if ( this.jsfiles.length > 0 )
		{
			for ( var i = 0; i < this.jsfiles.length; i++ )
			{
				if ( this.included[ this.jsfiles[ i ] ] != true )
				{
					this.include_scripts ( this.jsfiles[ i ], function ( ) { } );
				}
			}
		}
	};

	this.load_js_init ( new Array ( 
		'js_debug',
		'js_global', 
		'js_language', 
		'js_event', 
		'js_xml',
		'js_ajax',
		'js_cookies', 
		'js_ddmenu', 
		'js_misc', 
		'js_msgbox', 
		'js_toggle',
		'js_drag',
		'js_tabber'
	) );
	this.load_js_exec ( );
	
	if ( this.debugjs == true )
	{
		console.log ( 'IBB :: CORE JS INITIALIZED' );
	}
};
IBB.init ( );

/*======================================================================*\
|| #################################################################### ||
|| #          "Copyright(c) 2007-2010 Imperial Development"           # ||
|| #################################################################### ||
\*======================================================================*/