/**
 * (c) 2006 k3o Tomek Grzechowski
 * author: Tomek [miniman] Grzechowski
 */

ie5 = (document.all && document.getElementById);
ns6 = (!document.all && document.getElementById);

function MeritoHeadControl() {
	
	this.addInlineJScript = AddInlineJScript;
	this.addFileJScript = AddFileJScript;
	this.addFileStyleSheet = AddFileStyleSheet;
	
	function AddInlineJScript( JScriptText ) {
		
		var head = document.getElementsByTagName("head")[0];
		
		var jscript = document.createElement('script');
		jscript.type = 'text/javascript';
		jscript.text = JScriptText;
		
		head.appendChild( jscript );
	}
	
	function AddFileStyleSheet( CSSFile ) {
		
		var head = document.getElementsByTagName("head")[0];
		
		var hlinks = head.getElementsByTagName( "link" );
		var count = hlinks.length;
		var found = false;
		
		var hlink = document.createElement('link');
		hlink.type = 'text/css';
		hlink.rel = 'stylesheet';
		hlink.href = CSSFile;
		
		//because the baseurl is added to hlink.src automaticaly 
		// [or not if CSSFilename has URL like struct]
		//the compare is set here
		
		if ( count ) {
			
			for (i = 0; i < count; i++ ) {
				
				if ( hlinks[i].href == hlink.href ) {
					
					found = true;
					break;
				}
			}
		}
		
		//if css not found
		if ( !found ) {
			
			//add css to document's head
			head.appendChild( hlink );
		}
		
		return true;  
	}
	
	function AddFileJScript( JSFilename, recursive ) {
		
		var head = document.getElementsByTagName("head")[0];
		
		var jscripts = head.getElementsByTagName( "script" );
		var count = jscripts.length;
		var found = false;
		
		var jscript = document.createElement('script');
		jscript.type = 'text/javascript';
		jscript.src = JSFilename;
		
		//because the baseurl is added to jscript.src automaticaly 
		// [or not if JSFilename has URL like struct]
		//the compare is set here
		
		if ( count ) {
			
			for (i = 0; i < count; i++ ) {
				
				if ( jscripts[i].src == jscript.src ) {
					
					found = true;
					break;
				}
			}
		}
		
		//if script not found
		if ( !found ) {
			
			//add script to document's head
			head.appendChild( jscript );
		}
		
		return true;  
	}
}

function MeritoControl( ) {
	
	this.head = new MeritoHeadControl();
	this.version = '0.2';
	 
}

if ( !document.getElementById ) {
	
	throw 'merito: document method getElementById() is required';
} else {
	
	merito = new MeritoControl();

	//remote control setup
	merito.head.addFileJScript( '/inc.js/merito/meritoRemoteControl.js' );
	merito.head.addFileJScript( '/inc.js/merito/meritoTabsControl.js' );
	merito.head.addFileJScript( '/inc.js/merito/meritoFormControl.js' );
	merito.head.addFileJScript( '/inc.js/merito/meritoDebugControl.js' );
	merito.head.addFileJScript( '/inc.js/merito/meritoTooltipControl.js' );
}

