/*
    Variable: isBubbleWinVisible
    Returns true or false if the bubble window is visible or not

    Variable: leftBubbleWin
    Returns the left position of the bubble window.

    Variable: topBubbleWin
    Returns the top position of the bubble window.

    Variable: widthBubbleWin
    Returns the width position of the bubble window.

    Variable: hightBubbleWin
    Returns the height position of the bubble window.

    Variable: newResults
    Temporary variable that holds the search result data.

    Variable: bodyLoadRan
    Global bodyLoad function.

    Variable: currentHeight
    Returns the current height of the user-notes window.

    Variable: currentWidth
    Returns the current width of the user-notes window.

    Variable: currentTop
    Returns the current top of the user-notes window.

    Variable: currentLeft
    Returns the current left of the user-notes window.

    Variable: currentPositionY
    Returns the current position-y when a user clicks on the user-notes window.

    Variable: currentPositionX
    Returns the current position-x when a user clicks on the user-notes window.

    Variable: newPositionY
    Returns the new position-y when a user moves the user-notes window.

    Variable: newPositionX
    Returns the new position-x when a user moves the user-notes window.

    Variable: direction
    Determine whether a user is releasing or clicking on the mouse.

    Variable: moveType
    Determine whether a user moves or resizes the user-notes window.

    Variable: zoneType
    Determine whether or not a user can moves the user-notes window.
*/
var isBubbleWinVisible;
var leftBubbleWin;
var topBubbleWin;
var widthBubbleWin;
var hightBubbleWin;
var newResults = "";
var bodyLoadRan = false;
var currentHeight=0;
var currentWidth=0;
var currentTop=0;
var currentLeft=0;
var currentPositionY =0;
var currentPositionX =0;
var newPositionY =0;
var newPositionX =0;
var direction='Released';
var moveType='move';
var zoneType = '';


/*
    Function: bodyLoad
    The main purpose is to check for a page specific pageLoad function to run
    this pageLoad function is where to all page specific loading JS.

    Parameters:
        no-params

    Returns:
        nothing.
*/
function bodyLoad() {
	// ensure this function only runs once
	if (! bodyLoadRan)
	{
		if (typeof(pageLoad) != 'undefined')
			pageLoad();

		bodyLoadRan = true;
  }
}


/*
    Function: onMainBodyMouseUp
    Returns the Point(X,Y) where a user clicks on the screen and checks whether or not this point
    falls into the opened bubble window.

    Parameters:
        mouse - click-event.

    Returns:
        nothing.

    See Also:
        <something>
*/
function onMainBodyMouseUp(mouse)
{
    if (isBubbleWinVisible ) 
    {
        var d=document.getElementById("btc");
        try 
        {
			var top = findPositionY(d);
            var left =  findPositionX(d);
            var width =  d.offsetWidth;
            var height = d.offsetHeight;

            var x1=left; 
            var x2=left+width;
            var y1=top; 
            var y2=top+height;
            var bodyY=mouse.clientY;
            var bodyX=mouse.clientX;

            if (! ((bodyX>=x1 && bodyX<=x2) && (bodyY>=y1 && bodyY<=y2) ))
            {
              if (d.firstChild != null)
								d.removeChild(d.firstChild);
								
              isBubbleWinVisible = false;
            }

        }
        catch(err) 
        {
        }
    }
    else
    {
			if (typeof(isDisplayOptionsVisible) != 'undefined')
				isDisplayOptionsVisible = false;
			if (typeof(isAnnotationVisible) != 'undefined')
				isAnnotationVisible = false;
    }
    return;
}


/*
    Function: trim
    Trim the string.

    Parameters:
        no-params.

    Returns:
        string.
*/
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}


/*
    Function: capitalize
    Capitalize a string.

    Parameters:
        no-params.

    Returns:
        string.
*/
String.prototype.capitalize = function() {
    return this.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
    });
};


/*
    Function: searchOnClick
    clear the default value from the textbox.

    Parameters:
        formField - textbox-id.

    Returns:
        nothing.

    See Also:
        <searchOnBlur(formField)>
*/
function searchOnClick(formField) {
    if(formField.value==formField.defaultValue) {
	    formField.value = "";
	    setquicksearch = 1;
    }
}


/*
    Function: searchOnBlur
    Set the default value when the textbox is clear.

    Parameters:
        formField - textbox-id.

    Returns:
        nothing

    See Also:
        <searchOnClick(formField)>
*/
function searchOnBlur(formField) {
    myExp = RegExp(/\w/);
    if(!myExp.test(formField.value)) {
	    formField.value = formField.defaultValue;
    }
    return;
}


/*
    Function: onKeyPressed
    Attaches the keypress event to the click event of an object when a user presses the "enter" key.

    Parameters:
        evt - keypressed event.
        element - name of the object that has the click-event already defined .

    Returns:
        click event of the object "element".
*/
function onKeyPressed(evt, element) {
    var keyCode = evt.keyCode ? evt.keyCode :
                  evt.charCode ? evt.charCode :
		          evt.which ? evt.which : void 0;
    if (keyCode== 13) {
        document.getElementById(element).click();
        return false;
    }
    else
        return true;
}


/*
    Function: hidediv
    Hide the div tag.

    Parameters:
        divName - name of the div.

    Returns:
        nothing

    See Also:
        <showdiv(divName)>
        <hideobject(obj)>
        <showobject(obj)>
*/
function hidediv(divName) {
    if (document.getElementById) { // DOM3 = IE5, NS6
                document.getElementById(divName).style.display = 'none';
        }
    else {
        if (document.layers) { // Netscape 4
            document.divName.display = 'none';
        }
        else { // IE 4
            document.all.divName.style.display = 'none';
        }
    }
    return;
}


/*
    Function: showdiv
    Show the div tag.

    Parameters:
        divName - name of the div.

    Returns:
        nothing

    See Also:
        <hidediv(divName)>
        <hideobject(obj)>
        <showobject(obj)>
*/
function showdiv(divName) {
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(divName).style.display = 'block';
    }
    else {
        if (document.layers) { // Netscape 4
            document.divName.display = 'block';
        }
        else { // IE 4
            document.all.divName.style.display = 'block';
        }
    }
    return;
}


/*
    Function: hideobject
    Makes an object invisible.

    Parameters:
        obj - the object name.

    Returns:
        nothing

    See Also:
        <hidediv(divName)>
        <showdiv(divName)>
        <showobject(obj)>
*/
function hideobject(obj) {
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(obj).style.visibility = 'hidden';
    }
    else {
        if (document.layers) { // Netscape 4
            document.obj.visibility = 'hidden';
        }
        else { // IE 4
            document.all.obj.style.visibility = 'hidden';
        }
    }
    return;
}


/*
    Function: showobject
    Makes an object visible.

    Parameters:
        obj - the object name.

    Returns:
        nothing

    See Also:
        <hidediv(divName)>
        <showdiv(divName)>
        <hideobject(obj)>
*/
function showobject(obj) {
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(obj).style.visibility = 'visible';
    }
    else {
        if (document.layers) { // Netscape 4
            document.obj.visibility = 'visible';
        }
        else { // IE 4
            document.all.obj.style.visibility = 'visible';
        }
    }
    return;
}


/*
    Function: scrollSet
    Scrolls the text within the div to the same line where the row is positioning.

    Parameters:
        row - object name (could be ul/li/div/ect...
        divname - the name of the div where the text is displayed.
        top - top-padding of the div.

    Returns:
        nothing.

    See Also:
        <scrollReset(divname)>
*/
function scrollSet(row,divname,top) 
{
    try 
    {
        var scrollPos = 0;
        var dm = document.getElementById(divname);
        var dv = document.getElementById(row);
        if (dv != null && dm != null)
        {
					scrollPos = dv.offsetTop - top; // the top's value should be the same value of the divname's padding-top
					dm.scrollTop = scrollPos;
        }
    }
    catch(err) 
    {
			alert('scrollSet: ' + err.description); 
		}
    return;
}


/*
    Function: scrollReset
    Scrolls the text within the div to the top.

    Parameters:
        divname - the name of the div where the text is displayed.

    Returns:
        nothing.

    See Also:
        <scrollSet(row,divname,top)>
*/
function scrollReset(divname) {
    var dm = document.getElementById(divname);
    dm.scrollTop = 0;
    return;
}


/*
   Function: getAttribute
   Returns the attribute value from an object.

   Parameters:
      obj - The object name.
      field - The attribute name.

   Returns:
      the attribute value.

   See Also:
      <Divide>
*/
function getAttribute(obj,field) {
    var value;
    var o = document.getElementById(obj);
    value = o.getAttribute(field);
    return value;
}


/*
    Function: returnTrue
    Return true value.

    Parameters:
       no-params.

    Returns:
        true.

    See Also:
        <returnFalse>
*/
function returnTrue() { return true; }


/*
    Function: returnFalse
    Return false value.

    Parameters:
       no-params.

    Returns:
        false.

    See Also:
        <returnTrue>
*/
function returnFalse() { return false }


/*
    Function: enableLink
    Enables or disables a href link.

    Parameters:
        linkid - link id.
        enable - boolean (true or false).

    Returns:
        nothing.

    See Also:
        <returnTrue()>
        <returnFalse()>
*/
function enableLink(linkid,enable) {
    var link=document.getElementById(linkid);
    if(enable) {
        link.onclick=returnTrue;
        link.style.textDecoration="underline";
        link.style.cursor="pointer";
        link.style.hover="none";

    }
    else {
        link.onclick=returnFalse;
        link.style.textDecoration="none";
        link.style.cursor="default";
    }
}


/*
    Function: setOpacity
    Set opacity of an object.

    Parameters:
        el - an object name (can be div or span).

    Returns:
        nothing.
*/
function setOpacity(el){
    el.style.filter="alpha(opacity=95)"; // ORIG: "alpha(opacity:95)"
    // el.style.KHTMLOpacity="0.95";
    // el.style.MozOpacity="0.95";
    el.style.opacity=".95"; // ORIG: "0.95"
}


/*
    Function: CreateEl
    Create an object and set its class-name.

    Parameters:
        t - object name.
        c - classname.

    Returns:
        object of the name t.
*/
function CreateEl(t,c){
    var x=document.createElement(t);
    x.className=c;
    x.style.display="block";
    return(x);
}


/*
    Function: CreateImageTag
    Creat an image as an object.

    Parameters:
        t - image tag name.
        i - full image-file name.

    Returns:
        image tag.
*/
function CreateImageTag(t,i){
    var x=document.createElement(t);
    x.src=i;
    return(x);
}


/*
    Function: activateFlash
    Activate the flash movie.

    Parameters:
        flashname - name of the flash movie.
        width - width of the flash movie.
        height - height of the flash movie.

    Returns:
        nothing.
*/
function activateFlash(flashname, width, height) {
    document.write('<object type="application/x-shockwave-flash" data="' + flashname + '" width="' + width + '" height="' + height + '">');
    document.write('<param name="movie" value="' + flashname + '" />');
    document.write('<param name="wmode" value="transparent" />');
    document.write('<embed src="' + flashname + '" width="'+ width + '" height="' + height + '"/>');
    document.write('</object>');
}


/*
    Function: createCookie
    Create a cookie.

    Parameters:
        name - cookie name.
        value - cookie value.
        days - expiration date.

    Returns:
        nothing.

    See Also:
        <readCookie(name)>
        <eraseCookie(name)>
*/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


/*
    Function: readCookie
    Read Cookie.

    Parameters:
        name - cokie name.

    Returns:
        nothing.

    See Also:
        <createCookie(name,value,days)>
        <eraseCookie(name)>
*/
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


/*
    Function: eraseCookie
    Erase Cookie.

    Parameters:
        name - cokie name.

    Returns:
        nothing.

    See Also:
        <createCookie(name,value,days)>
        <readCookie(name)>
*/
function eraseCookie(name) {
	createCookie(name,"",-1);
}


/*
    Function: querystring
    Returns a named value from the querystring.

    Parameters:
        name - querystring var.

    Returns:
        value of name.
*/
function querystring(name) {
       var tmp = ( location.search.substring(1) );
       var i   = tmp.toUpperCase().indexOf(name.toUpperCase()+"=");

       if ( i >= 0 )
       {
          tmp = tmp.substring( name.length+i+1 );
          i = tmp.indexOf("&");
          return unescape( tmp = tmp.substring( 0, (i>=0) ? i : tmp.length ));
       }

       return("");
    }


/*
    Function: selectDropDownList
    Selects an item within the list object.

    Parameters:
        strObj - the list object name.
        strItemToSelect - the value of the item that need to be selected within the list.

    Returns:
        nothing.

    See Also:
        <something>
*/
function selectDropDownList(strObj, strItemToSelect) 
{
	strItemToSelect = strItemToSelect.toLowerCase();
  var obj = document.getElementById(strObj);
  for (var i=0; i < obj.options.length; i++) 
  {
    if (obj.options[i].value.toLowerCase() == strItemToSelect) 
    {
      obj.selectedIndex = i; 
      return;
    }
  }
}


/*
    Function: getDivPosition
    Calculates the top and left when a user clicks on the screen.
    Also, it calculates the width and height when a user resizes the user-notes window.

    Parameters:
        mouse - click-event.
        type  - any object type that is loacted inside the user-notes window needs to ignore the moving window.

    Returns:
        nothing.

    See Also:
        <SetDivPosition(mouse)>
*/
function getDivPosition(mouse,type) {

    var o = document.getElementById("firstdiv");

    if ( zoneType == 'Text' ) { return; }

    moveType=type;
    // Assign the global variable to 'Pressed'
    direction='Pressed';
    // Save the cursor position to its corresponding global variable
    currentPositionY =mouse.clientY;
    currentPositionX =mouse.clientX;
    if (type == 'move') {
        // Save the Current Div top & left to a local variable
        var currentTop=document.getElementById("firstdiv").offsetTop;
        var currentLeft=document.getElementById('firstdiv').offsetLeft;
    }
    else if (type == 'resize') {
        // Save the Current Div height & width to a local variable
        var tempHeight=document.getElementById('firstdiv').style.height;
        var tempWidth=document.getElementById('firstdiv').style.width;
        //  Remove the px from the value retrieved from above line of code
        var heightArray=tempHeight.split('p');
        var widthArray=tempWidth.split('p');
        currentHeight = parseInt(heightArray[0]);
        currentWidth = parseInt(widthArray[0]);

    }
}


/*
    Function: SetDivPosition
    Moves or resize the user-notes window.

    Parameters:
        mouse - click-event.

    Returns:
        nothing.

    See Also:
        <getDivPosition(mouse)>
*/
function SetDivPosition(mouse) {

    if(direction=='Pressed') {

        if (moveType=='move') {
            /* THIS IS BUGGY, USING YAHOO'S DRAG UTILITY (set in siteinfo/javascript/Notes.js)
            // Set the new cursor location to newPosition global variable
	        newPositionY=mouse.clientY;
	        newPositionX=mouse.clientX;

	        // Calculate The mouse movement in pixels
            var divnewLocationY=newPositionY - currentPositionY + currentTop;
	        var divnewLocationX=newPositionX - currentPositionX + currentLeft;

	        if(divnewLocationY<=0) {
	            divnewLocationY = 10; }
	        if(divnewLocationX<=0) {
	            divnewLocationX = 10; }

	        if(divnewLocationX>350) {
	            divnewLocationX = 350; }

	        document.getElementById('firstdiv').style.top= divnewLocationY+'px';
	        document.getElementById('firstdiv').style.left= divnewLocationX+'px';
	        */
        }
        else if (moveType=='resize') {
	        /* THIS IS BUGGY, DISABLING
	        // Set the new cursor location to newPosition global variable
	        newPositionY=mouse.clientY;
	        newPositionX=mouse.clientX;
	        // Calculate The mouse movement in pixels
	        var movePerPixelsY=parseInt(newPositionY-currentPositionY);
	        var movePerPixelsX=parseInt(newPositionX-currentPositionX);
	        if (movePerPixelsY>500) movePerPixelsY=500;
	        if (movePerPixelsX>400) movePerPixelsX=400;
	        // Add the mouse movement to the first div height
	        var divnewLocationY=parseInt(currentHeight+movePerPixelsY);
	        var divnewLocationX=parseInt(currentWidth+movePerPixelsX);
	        if(divnewLocationY<350) {
	            divnewLocationY = 350; }
	        if(divnewLocationX<420) {
	            divnewLocationX = 420; }

    	    //document.getElementById('noteTitle').value = currentHeight + ' - ' + currentWidth;
    	    //document.getElementById('frmT').style.height= divnewLocationY+'px';
		    //document.getElementById('rtbFrame').style.height= divnewLocationY+'px';
		    document.getElementById('firstdiv').style.height= divnewLocationY+'px';
	        document.getElementById('firstdiv').style.width= divnewLocationX+'px';
	        */
	    }
    }
}


/*
    Function: onclickFlipFlopImageSize
    This function is used when viewing maps in the verse text panel to change the image size.

    Parameters:
        style - the style property of the image object.
        thumbpercent - percentage.

    Returns:
        nothing.
*/
function onclickFlipFlopImageSize(style, thumbpercent){
    if(style.width == thumbpercent+"%"){
        style.width="";
        style.height="";
    } else {
        style.width=thumbpercent+"%";
        style.height=thumbpercent+"%";
    }
}




/*
    Function: findPositionX
    positioning functions courtesy of Peter-Paul Koch - http://www.quirksmode.org/.

    Parameters:
        obj - object.

    Returns:
        integer.

    See Also:
        <findPosY(obj)>
*/
function findPositionX(obj) 
{
	var curleft = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft;
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft;
		}
	}
	return curleft;
}


/*
    Function: findPosY
    positioning functions courtesy of Peter-Paul Koch - http://www.quirksmode.org/.

    Parameters:
        obj - object.

    Returns:
        integer.

    See Also:
        <findPosX(obj)>
*/
function findPositionY(obj) 
{
	var curtop = 0;
	if (obj.offsetParent) 
	{
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) 
		{
			curtop += obj.offsetTop;
		}
	}
	return curtop;
}