/*
alert('www.motherandlanello.com/common/');
*/

/* derived from "Cool DHTML tooltip script" and additional souces as indicated
 * http://www.dynamicdrive.com/dynamicindex5/dhtmltooltip.htm
 * Cool DHTML tooltip script- © Dynamic Drive DHTML code library (www.dynamicdrive.com) * This notice MUST stay intact for legal use * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code */

/* POSITIONS TIP RELATIVE to RIGHT,BOTTOM of REFERENCE
 * offsetX,offsetY RELATIVE OFFSET TO TOP,LEFT of TIP
 *	must be edited to position tip somewhere else */
var offsetX=0 //X offset of tooltip
var offsetY=10 //Y offset of tooltip

var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false

/* tip TIP OBJECT (tip box)
 *	must NOT be declared as var which makes it local
 * 	must be defined just by "name =" to be global (tip = x) */
function settipobj(){
	if (ie||ns6)
		tip=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""
	tip.style.visibility="hidden"
}
/* display tip */
function ddrivetip(thetext, thecolor, thewidth){
	if (ns6||ie){
		if (typeof thewidth!="undefined") tip.style.width=thewidth+"px"
		if (typeof thecolor!="undefined" && thecolor!="") tip.style.backgroundColor=thecolor
		tip.innerHTML=thetext
		enabletip=true
		return false
	}
}
/* position the tip */
function positiontip(e){
	if (enabletip) {
/* determine mouse position, window dimensions,scrolling offset
 ----------------------------------------------------------------
 * docX,docY MOUSE POSITION RELATIVE to the DOCUMENT
 * 	replaced with
 * 	http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
----------------------------------------------------------------- */
	var docX = 0, docY = 0;
	if (!e) 
		var e = window.event;
	if (e.pageX || e.pageY) {
		docX = e.pageX;
		docY = e.pageY;
	}
	else 
		if (e.clientX || e.clientY) {
			docX = e.clientX + document.body.scrollLeft +
			document.documentElement.scrollLeft;
			docY = e.clientY + document.body.scrollTop +
			document.documentElement.scrollTop;
		}
/* ---------------------------------------------------------------
 *  windWidth,winHeight WINDOW DIMENSIONS
 * 	replaced with
 *	http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
------------------------------------------------------------------ */
	var winWidth = 0, winHeight = 0;
	if (typeof(window.innerWidth) == 'number') {
		//Non-IE
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
	}
	else 
		if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			//IE 6+ in 'standards compliant mode'
			winWidth = document.documentElement.clientWidth;
			winHeight = document.documentElement.clientHeight;
		}
		else 
			if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
				//IE 4 compatible
				winWidth = document.body.clientWidth;
				winHeight = document.body.clientHeight;
			}
/* ---------------------------------------------------------------
 * scrX,scrY WINDOW SCROLLING OFFSET
 * 	replaced with
 *	http://www.howtocreate.co.uk/tutorials/javascript/browserwindow 
------------------------------------------------------------------ */
	var scrX = 0, scrY = 0;
	if (typeof(window.pageYOffset) == 'number') {
		//Netscape compliant
		scrY = window.pageYOffset;
		scrX = window.pageXOffset;
	}
	else 
		if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
			//DOM compliant
			scrY = document.body.scrollTop;
			scrX = document.body.scrollLeft;
		}
		else 
			if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
				//IE6 standards compliant mode
				scrY = document.documentElement.scrollTop;
				scrX = document.documentElement.scrollLeft;
			}
/* determine how close mouse is to edges of window and adjust
/* ---------------------------------------------------------------
 * spaceX MOUSE DISTANCE from WINDOW RIGHT
 * winX MOUSE POSITION RELATIVE TO WINDOW
 * tipX TIP WIDTH INCLUDING OFFSET
 * tip.offsetWidth TIP WIDTH
---------------------------------------------------------------- */
	var winX = docX - scrX;
	var spaceX = winWidth - winX;
	var tipX = tip.offsetWidth + offsetX +20
//	***forced 20px correction for firefox ***
/* ---------------------------------------------------------------
  *	check if there is not enough room for tip right
---------------------------------------------------------------- */
	if (spaceX < tipX) {
	// not enough room - move tip left bt its width
		tip.style.left = (docX - tipX) +"px";
	}
	else {
	// enough room - put tip right
		tip.style.left = (docX + offsetX) +"px";
	}
/*
alert($style("dhtmltooltip", "left"))
alert(spaceX +" = "+ winWidth +" - "+ winX +"\n"+ tipX )
*/
/* ---------------------------------------------------------------
 * spaceY MOUSE DISTANCE from WINDOW BOTTOM
 * winY MOUSE POSITION RELATIVE TO WINDOW
 * tipY TIP HEIGHT INCLUDING OFFSET/
 * tip.offsetHeight TIP HEIGHT
---------------------------------------------------------------- */
	var winY = docY - scrY;
	var spaceY = winHeight - winY;
	var tipY = tip.offsetHeight + offsetY
/* ---------------------------------------------------------------
 *	check if there is not enough room for tip below
---------------------------------------------------------------- */
	if (spaceY < tipY) {
	// not enough room - move tip up bt its height
		tip.style.top = (docY - tipY) +"px";
	}
	else {
	// enough room - put tip below
		tip.style.top = (docY + offsetY) +"px";
	}
	tip.style.visibility = "visible"

/*
alert($style("dhtmltooltip", "top"))
*/

/*
alert("spaceY = winHeight - winY = winHeight - (docY - scrY) +\n"+
spaceY +"\t = "+ winHeight +" - "+ winY +"\n"+
"\t = "+ winHeight +" -( "+ docY +" - "+ scrY +" )\n"+
"tip.style.top = docY - tipY\n" +
$style("dhtmltooltip", "top") +"\t = "+ docY +" - "+ tipY +"\n"+
tmp);
*/

}
}
function $style(ElementId, CssProperty)
{
    function $(stringId)
    {
        return document.getElementById(stringId);
     }   

    if($(ElementId).currentStyle)
   {
        var convertToCamelCase = CssProperty.replace(/\-(.)/g, function(m, l){return l.toUpperCase()});
        return $(ElementId).currentStyle[convertToCamelCase];
    }
    else if (window.getComputedStyle)
   {
        var elementStyle = window.getComputedStyle($(ElementId), "");
        return elementStyle.getPropertyValue(CssProperty);
    }
}	
function hideddrivetip(){
	if (ns6||ie){
		enabletip=false
		tip.style.visibility="hidden"
		tip.style.left="-1000px"
		tip.style.backgroundColor=''
		tip.style.width=''
	}
}
document.onmousemove=positiontip;

/*
window.onload=settipobj;
used addDOMLoadEvent from common_am.js instead of window.onlosd
*/
addDOMLoadEvent(settipobj);

/***********************************************
* Get Elements By Class Name
* 	Returns a list of child nodes with the supplied class name
* http://snipplr.com/view/1696/get-elements-by-class-name/
* 
* wrapper to allow for browser variation
* 	getElementsByClassName() is now natively supported by 
* 	the most recent versions of Firefox, Safari, and Opera
* http://www.quirksmode.org/blog/archives/2008/05/getelementsbycl.html 
***********************************************/
/* returns an array of elements which are children of node (object)
 * which have the class classname (text) */
function getElementsByClassName(classname, node){
	node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for (var i = 0, j = els.length; i < j; i++) {
		if (re.test(els[i].className)) {
			a.push(els[i]);
		}
	}
	return a;
}
/***********************************************
* onload function to getElementsByClassName
*  loads all elements with classname into tips a global array derived from
*  http://snipplr.com/view/1696/get-elements-by-class-name/

* pass array element innerHTML to PHP using callPHP derived from
* http://www.devshed.com/c/a/PHP/PHP-and-JavaScript-Pooling-Your-Resources/
*  PHP and JavaScript, Pooling Your Resources
* http://www.devshed.com/c/a/PHP/PHP-and-JavaScript-Pooling-Your-Resources-continued/
*  HP and JavaScript, Pooling Your Resources (continued)
* 
* using javascript and PHP wiyh MySQL derived from
* http://www.geekpedia.com/tutorial145_PHP-and-Javascript-Dynamic-update-with-MySQL.html
*  PHP and Javascript: Dynamic update with MySQL
***********************************************/
function getTipData(){
	var classname = "tip";
	var node = document;
	tips = [];
	tips = getElementsByClassName(classname, node);
	for (var i = 0, j = tips.length; i < j; i++) {
		tips[i].setAttribute("id", "sid" + i);
		// get tip info from DB and insert into page
//		callPHP(tips[i].innerHTML, tips[i].id);
	}
}
/* Wait for window to be loaded */
addDOMLoadEvent(getTipData);

/* call PHP file jsdb.php with variables  */
function callPHP() {
	var ref = "";

	for (var i = 0, j = tips.length; i < j; i++) {
		//avoid trailing delimiter (#)
		if (i == 0) {
			var ref = ref + tips[i].innerHTML;
		}
		else {
			var ref = ref + "#" + tips[i].innerHTML;
		}
	};
var encoded = encodeURIComponent(ref);
	if (encoded != "")	attach_file('http://www.motherandlanello.com/common/jsdb.php?u_tip=' + encoded);
	/* alert('http://www.motherandlanello.com/common/jsdb.php?u_tip=' + encoded); */
}
/* Wait for window to be loaded 
 * Must be loaded AFTER showTips */
addDOMLoadEvent(callPHP);

/* javascript to PHP */
function attach_file( p_script_url ) {
	// create new script element, set its relative URL, and load it
	script = document.createElement( 'script' );
	script.src = p_script_url;
	document.getElementsByTagName( 'head' )[0].appendChild( script );
}

