// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Fake console.log courtesy of
// http://codingforfunandprofit.uglybugger.org/2009/03/11/consolelog-equivalent-for-internet-explorer/

// rudimentary javascript logging to emulate console.log(). If there already exists
// an object named "console" (defined by most *useful* browsers :p) then we
// won't do anything here at all.
if (typeof (console) === 'undefined') {

    // define "console" namespace
    console = new function() {
        // this is the Id of the console div. It doesn't actually need to be a div,
        // as long as it has an innerHTML property.
        this.ConsoleDivId = "JavaScriptConsole";

        // maintains a reference to the console output div, so that we don't have to
        // call document.getElementById a bunch of times.
        this.ConsoleDiv = null;

        // allows us to cache whether or not the console div exists, so that we can
        // just do an early exit from the console.log method and similar if we're not
        // going to put any useful output anywhere.
        this.ConsoleDivExists = null;
    };

    // this is an expensive (really quite expensive) string padding function. Don't use
    // it for large strings.  -andrewh 11/3/09
    console.padString = function(s, padToLength, padCharacter) {
        var response = "" + s;
        while (response.length < padToLength) {
            response = padCharacter + response;
        }

        return response;
    }

    console.log = function(message) {

        // this will be executed once, on first method invocation, to get a reference to the
        // output div if it exists
        if (console.ConsoleDivExists == null) {
            console.ConsoleDiv = document.getElementById(console.ConsoleDivId);
            console.ConsoleDivExists = (console.ConsoleDiv != null);
        }

        // only do any logging if we actually have an output div.  (Check using the cached
        // variable so that we don't end up with a bunch of failed calls to
        // document.getElementById).
        if (console.ConsoleDivExists) {
            var date = new Date();
            var entireMessage =
                console.padString(date.getHours(), 2, "0") + ":" +
                console.padString(date.getMinutes(), 2, "0") + ":" +
                console.padString(date.getSeconds(), 2, "0") + "." +
                console.padString(date.getMilliseconds(), 3, "0") + " " + message;
            delete date;

            // append the message
            console.ConsoleDiv.innerHTML = console.ConsoleDiv.innerHTML + "<br />" + entireMessage;

            // scroll the div to the bottom
            console.ConsoleDiv.scrollTop = console.ConsoleDiv.scrollHeight;
        }
    }
}



// This is intended as a short-term solution so that testing can be done in subdirectories.
var pathPrepend = '';
if ( document.location.toString().match( 'properties/' ) )
{
	pathPrepend = '../../';
}

function initPage () {
	assureMinSidebarHeight();

	MM_preloadImages(
		pathPrepend + 'images/topnav/topnav_over_01.gif',
		pathPrepend + 'images/topnav/topnav_over_02.gif',
		pathPrepend + 'images/topnav/topnav_over_03.gif',
		pathPrepend + 'images/topnav/topnav_over_04.gif',
		pathPrepend + 'images/topnav/topnav_over_05.gif');
}

function assureMinSidebarHeight () {
	var sideBar = document.getElementById ('sidebar');
	var sHeight = sideBar.offsetHeight;
	

	var wHeight = windowHeight();
	
	//console.log ('sHeight='+sHeight+', wHeight='+wHeight);
	
	if (sHeight < wHeight) {
		console.log ('Sidebar (h: '+sHeight+') must expand downward by '+(wHeight - sHeight)+' pixels');
				
		sideBar.style.height = (wHeight - 26)+'px';
	}
	
	var wrapperDiv = document.getElementById( 'wrapper' );
	// in order to avoid having the bottom image appear in one position briefly
	// only to be moved to the bottom of the window, set the background of the #wrapper
	// div here so that the image is placed after any sizing adjustments are made
	if (wrapperDiv.style && YAHOO.env.ua.ie) {
		//console.log( 'Setting wrapperDiv.style.background property' );
		wrapperDiv.style.background = 'background: url('+pathPrepend + 'images/footer.gif) repeat-x bottom left;';
	} else {
		var value = 'background: url('+pathPrepend + 'images/footer.gif) repeat-x bottom left;';
		//console.log( 'Setting wrapperDiv style attribute value: ' + value );
		wrapperDiv.setAttribute( 'style',  value );
	}
}

//YAHOO.util.Event.onContentReady( 'wrapper', assureMinSidebarHeight );

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Misc window, element, and CSS functions

function windowHeight () {
	// return the appropriate value (depending on browser)
	return window.offsetHeight ? window.offsetHeight : 
		window.innerHeight ? window.innerHeight :
			document.documentElement.clientHeight ? document.documentElement.clientHeight : 
				document.body.clientHeight ? document.body.clientHeight : undefined;
}

/*
	Calculate the position of the dom element argument and return an object with both a
 	'top' and a 'left' property.
*/

function getPosition(obj){
	var topValue= 0,leftValue= 0;
	while(obj){
		leftValue+= obj.offsetLeft;
		topValue+= obj.offsetTop;
		obj= obj.offsetParent;
	}
	finalvalue = { top : topValue, left : leftValue};
	return finalvalue;
}

// via http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
// NOTE that this will report a number smaller than the innerHeight of the window if the 
// page/body content does not occupy the entire window height
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

// end www.softcomplex.com functions



