/* # $Id: fullermoney.js 18015 2010-07-18 17:16:32Z chris $
 */

function go(url) {
    window.location = url;
} 

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent) {
	while (obj.offsetParent) {
	    curtop += obj.offsetTop;
	    obj = obj.offsetParent;
	}
    } else if (obj.y)
	curtop += obj.y;
    return curtop;
}

function findPosX(obj)
{
    var x = 0;
    if (obj.offsetParent) {
	while (obj.offsetParent) {
	    x += obj.offsetLeft;
	    obj = obj.offsetParent;
	}
    } else if (obj.x)
	x += obj.x;
    return x;
}

function windowHeight() {
    var myHeight = 0;
    if( typeof( window.innerHeight ) == 'number' ) {
	//Non-IE
	myHeight = window.innerHeight;
    } else if( document.documentElement && document.documentElement.clientHeight) {
	//IE 6+ in 'standards compliant mode'
	myHeight = document.documentElement.clientHeight;
    } else if( document.body && document.body.clientHeight) {
	//IE 4 compatible
	myHeight = document.body.clientHeight;
    }
    return myHeight;
}

function windowWidth() {
    var myWidth = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
    } else if( document.body && document.body.clientWidth) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
    }
    return myWidth;
}

// http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function addEvent( obj, type, fn )
{
    if (obj.addEventListener)
	obj.addEventListener( type, fn, false );
    else if (obj.attachEvent) {
	obj["e"+type+fn] = fn;
	obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
	obj.attachEvent( "on"+type, obj[type+fn] );
    }
}

function removeEvent( obj, type, fn )
{
    if (obj.removeEventListener)
	obj.removeEventListener( type, fn, false );
    else if (obj.detachEvent) {
	obj.detachEvent( "on"+type, obj[type+fn] );
	obj[type+fn] = null;
	obj["e"+type+fn] = null;
    }
}

/* -- Search Highlighting -- */
var HghlightNum = 0;

function doScrollToMatch(elnum) { 

    HighlightNum = elnum+1; 
    var el = document.getElementById("hl"+elnum);
    if(el) {
	var ypos = findPosY(el)-100;
	if (ypos < 0) ypos = 0;
	self.scrollTo(0, ypos);
	el.style.backgroundColor = "red";
	el.style.border = "2px solid black";
	
	var idiv = null;
	if (document.getElementById("hl1")) {
	    idiv = document.getElementById("searchTip");
	    if (idiv) {
		idiv.id = "";
		idiv.style.display = "none";
	    }
	    idiv = document.createElement("DIV");
	    idiv.id = "searchTip";
	    if (document.getElementById("hl" + (elnum+1)))
		idiv.innerHTML = "Press 1 for next match";
	    else
		idiv.innerHTML = "Press 1 for first match";
		
	    idiv.style.position = "absolute";
	    idiv.style.background = '#ffffc0';
	    idiv.style.padding = '2px';
	    idiv.style.border = 'solid #888 1px';
	    document.body.appendChild(idiv);

	    idiv.style.top = (ypos + 124) + "px";
	    idiv.style.left = findPosX(el) + "px";
	    idiv.style.display = 'block';
	}

	setTimeout(function() { 
		if (idiv)
		    document.body.removeChild(idiv);
		el.style.backgroundColor = "#ff9933";
		el.style.border = "0px";
	    } , 1500);
    } else if (elnum > 0)
	doScrollToMatch(0);
}

function doHighlight(bodyText, searchTerm) 
{
    var highlightEndTag = "</span>";
    var newText = "";
    var i = -1;
    var lcSearchTerm = searchTerm.toLowerCase();
    var lcBodyText = bodyText.toLowerCase();
    var count=0;
    while (bodyText.length > 0) {
	i = lcBodyText.indexOf(lcSearchTerm, i+1);
	if (i < 0) {
	    newText += bodyText;
	    bodyText = "";
	} else {
	    // skip anything inside an HTML tag
	    if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
		// skip anything inside a <script> block
		if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
		    var highlightStartTag = "<span style='color:blue; background-color:#ff9933;' id=hl"+count+">";
		    newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
		    bodyText = bodyText.substr(i + searchTerm.length);
		    lcBodyText = bodyText.toLowerCase();
		    i = -1;
		    count++;
		}
	    }
	}
    }
    
    return newText;
}

function KeyCap(e) {
    var thiskey = window.event ? window.event.keyCode : e.which;
    if (thiskey == 49) 
	doScrollToMatch(HighlightNum);
}

function dohighlightSearchTerms(searchText)
{
    if (!document.body || typeof(document.body.innerHTML) == "undefined")
	return false;

    searchText = searchText.replace("&#39", "'");
    
    var bodyText = document.getElementById("areaContentMain").innerHTML;
   document.getElementById("areaContentMain").innerHTML = doHighlight(bodyText, searchText);

    if (document.getElementById("hl1")) { // More than one match?
	if (navigator.appName == 'Netscape') 
	    window.captureEvents(Event.KEYPRESS);
	
	if(document.all) 
	    document.onkeypress = KeyCap;
	else 
	    window.onkeypress = KeyCap;
    }

    doScrollToMatch(0);
}

function highlightSearchTerms(searchText) {
    addEvent(window, "load", function() { 
	    dohighlightSearchTerms(searchText); 
	});
}

function playlink(d,m) {
    var el = document.getElementById("playFrame");
    el.style.display = "block";
    el.src = "/x/playcomment?d=" + escape(d) + "&m=" + escape(m);
}
