/* 

Copyright 2006-2009 Base Station. This code cannot be redistributed without
permission from www.basestationdb.com.

$Id: bsp.js 721 2009-08-27 22:54:29Z scotts $
 
*/

// Need to define bspview here, and bspview.grid in the bspview.js file.
// Then we can check for the existence of bspview.grid in the isScriptLoaded()
// and define jsPath and scriptLoad as bspview methods so they won't clutter
// the javascript namespace. 

var bspview = new Object();

bspview.init = function () {
	// get absolute path of /_js/ directory from the <script src=""> reference to this file.
	var scripts = document.getElementsByTagName( "script" );
	for ( var i = 0; i < scripts.length; i++ ) {
		if( scripts[ i ].src.substr( scripts[ i ].src.length - 11, 11 )=='/_js/bsp.js' ) {
			bspview.jsPath=scripts[ i ].src.substr( 0, scripts[ i ].src.length - 6 );
			break;
		}
	}
	djConfig={
		parseOnLoad: true,
		afterOnLoad: true,
		isDebug: false,
		locale: "en-us",
		baseUrl: bspview.jsPath+'dojo/dojo/',
		addOnLoad: function () {
			bspview.scriptLoad( bspview.jsPath+"dojo/dojo/dojo_bspview.js" );
			bspview.scriptLoad( bspview.jsPath+"bspview.js" );
			bspview.isScriptLoaded( 'window.bspview.viewDetails', 
					'window.dijit.layout.ContentPane' );
		}
	};
	bspview.scriptLoad( bspview.jsPath+"dojo/dojo/dojo.js" );
}

// load a .js file
bspview.scriptLoad = function ( src ) {
	var s=document.createElement( "script" );
	s.type="text/javascript";
	s.src=src;
	document.getElementsByTagName( "head" )[ 0 ].appendChild( s );
}

// from: http://www.oreillynet.com/onlamp/blog/2008/05/dojo_goodness_part_7_injecting.html
bspview.isScriptLoaded = function ( obj, obj2 ) {
	// Check every few milliseconds to see if object is loaded.
	var _interval = setInterval(function() {
		// check that objects from loaded scripts are defined
	    if ( ( eval( obj ) !== undefined ) 
			&& ( eval( obj2 ) !== undefined )
		) {
	        clearInterval( _interval );
			// wait a small time before proceeding, to ensure everything's parsed.
			setTimeout( function () {
				bspview._addOnLoadLoaded = true;
				bspview.addOnLoad();
			}, 100 );
	    //} else {
	    	//console.debug( 'not loaded:', eval( obj ), eval( obj2 ) );
	    }
	}, 50 );
}

bspview._addOnLoadArray = [];
bspview._addOnLoadLoaded = false;
bspview.addOnLoad = function ( fn ) {
	// always add to queue, so functions will execute in order they were 
	// recieved in case we're executing some of them now.
	if ( typeof( fn ) === 'function' ) {
		bspview._addOnLoadArray.push( fn );
	}
	// see if we can execute array functions now
	if ( bspview._addOnLoadLoaded ) {
		for ( i = 0; i < bspview._addOnLoadArray.length; i++ ) {
			// execute and remove from array
			bspview._addOnLoadArray[ i ]();
			delete bspview._addOnLoadArray[ i ];
		}
	}
}

// returns key/value pairs for current URL query string
bspview.getUrlKeys = function () {
	var bspviewUrlTokens = location.search.substr( 1 ).split( '&' );
	var bspviewUrlKeys = new Object();
	for ( i = 0; i < bspviewUrlTokens.length; i++ ) {
		var bspviewUrlToken = bspviewUrlTokens[ i ].split('=');
		bspviewUrlKeys[ bspviewUrlToken[ 0 ] ] = bspviewUrlToken[ 1 ]; 
	}
	return bspviewUrlKeys;	
}

// Works as a "maxlength" attribute to <textarea> elements.
// A work in progress -- doesn't handle special characters (delete, paste, etc).
bspview.maxlength = function ( obj, e, maxlength ) {
	var theString = obj.value;
	if ( e.ctrlKey || e.metaKey || e.altKey ) {
		// see if it's a paste, otherwise ignore
	} else {
		console.debug( e );
		theString += String.fromCharCode( e.charCode );
	}
	obj.value = theString.substring( 0, maxlength ); // in case a long string was pasted
	return false;
}

// if djConfig is already defined, we're in Inspect mode and don't need to initialize.
//if ( ( djConfig === undefined ) 
if ( bspview.getUrlKeys()[ '_design' ] !== 'inspect') {
	var djConfig; // set scope outside of any function
	bspview.init();
}



//
// These are now unused: 
//
/*
function bL(){
	//bspOnLoad();
}

if ( window.addEventListener ) {
	window.addEventListener( "load", bL ,false );
} else {
	window.attachEvent( "onload",bL );
}
*/