/**********************************************************************
 * This file is part of the RaidNinja project.
 **********************************************************************
 * Copyright (C) 2005-2007 Nate Bundy <msu.falcon@gmail.com>
 * Copyright (C) 2006-2007 Derek Lee <beldak.sd@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $HeadURL: https://raidninja.svn.sourceforge.net/svnroot/raidninja/rn2/trunk/scripts/mouseover.js $
 * $Revision: 183 $
 * $Author: beldak $
 * $Date: 2007-06-29 13:37:18 -0500 (Fri, 29 Jun 2007) $
**********************************************************************/
var enablepopup = false;
var popupObject = "";
function prepareMouseovers()
{
	if (!document.getElementsByTagName) return false;
	document.onmousemove=positiontip;
	var spans = document.getElementsByTagName("span");
	for (var i=0; i < spans.length; i++) {
		var span_class = (spans[i].getAttribute("class")) ? spans[i].getAttribute("class") : spans[i].getAttribute("className");
		if (span_class == 'raidname') {
			spans[i].onmouseover = function() {
				this.nextSibling.nextSibling.style.display = "block";
				this.nextSibling.nextSibling.style.position = "absolute";
				enablepopup = true;
				popupObject = this.nextSibling.nextSibling;
			}
			spans[i].onmouseout = function() {
				this.nextSibling.nextSibling.style.display = "none";
				enablepopup = false;
				popupObject = "";
			}
		}
	}
}
function prepareMouseovers2()
{
	if (!document.getElementsByTagName) return false;
	var spans = document.getElementsByTagName("span");
	for (var i=0; i < spans.length; i++) {
		var span_class = (spans[i].getAttribute("class")) ? spans[i].getAttribute("class") : spans[i].getAttribute("className");
		if (span_class == 'raidname') {
			spans[i].onmouseover = function() {
				popup(this.nextSibling.nextSibling);
			}
			//alert(spans[i].nextSibling.nextSibling.innerHTML);
			spans[i].onmouseout = function() {
				this.nextSibling.nextSibling.style.display = "none";
			}
		}
	}
}
function prepareMouseoversCharsTest()
{
	if (!document.getElementsByTagName) return false;
	var divs = document.getElementsByTagName("div");
	for (var i=0; i < divs.length; i++) {
		var div_class = (divs[i].getAttribute("class")) ? divs[i].getAttribute("class") : divs[i].getAttribute("className");
		var temp = new Array();
		if (div_class != null)
		{
			temp = div_class.split(' ');
			for (var j=0; j < temp.length; j++)
			{
				if (temp[j] == 'character')
				{
					div_class = 'character';
					break;
				}
			}
		}
		if (div_class == 'character' &&  divs[i].getElementsByTagName("div")[0].firstChild != null) {
				divs[i].onmouseover = function() {
					this.getElementsByTagName("div")[0].style.display = "block";
					this.getElementsByTagName("div")[0].style.position = "absolute";
					enablepopup = true;
					popupObject = this.getElementsByTagName("div")[0];
				}
			divs[i].onmouseout = function() {
				this.getElementsByTagName("div")[0].style.display = "none";
				enablepopup = false;
				popupObject = "";
			}
		}
	}
}
function prepareMouseoversCharsTest2()
{
	if (!document.getElementsByTagName) return false;
	var lis = document.getElementsByTagName("li");
	for (var i=0; i < lis.length; i++) {
		var li_class = (lis[i].getAttribute("class")) ? lis[i].getAttribute("class") : lis[i].getAttribute("className");
		if (li_class == 'character' && lis[i].getElementsByTagName("div")[0].firstChild != null) {
			lis[i].onmouseover = function() {
				this.getElementsByTagName("div")[0].style.display = "block";
				this.getElementsByTagName("div")[0].style.position = "absolute";
				enablepopup = true;
				popupObject = this.getElementsByTagName("div")[0];
			}
			lis[i].onmouseout = function() {
				this.getElementsByTagName("div")[0].style.display = "none";
				enablepopup = false;
				popupObject = "";
			}
		}
	}
}
function prepareMouseoversClassIcons()
{
	if (!document.getElementsByTagName) return false;
	var divs = document.getElementsByTagName("div");
	for (var i=0; i < divs.length; i++) {
		var div_class = (divs[i].getAttribute("class")) ? divs[i].getAttribute("class") : divs[i].getAttribute("className");
		var temp = new Array();
		if (div_class != null)
		{
			temp = div_class.split(' ');
			for (var j=0; j < temp.length; j++)
			{
				if (temp[j] == 'classIcon')
				{
					div_class = 'classIcon';
					break;
				}
			}
		}
		if (div_class == 'classIcon' &&  divs[i].getElementsByTagName("div")[0].firstChild != null) {
				divs[i].onmouseover = function() {
					this.getElementsByTagName("div")[0].style.display = "block";
					this.getElementsByTagName("div")[0].style.position = "absolute";
					enablepopup = true;
					popupObject = this.getElementsByTagName("div")[0];
				}
			divs[i].onmouseout = function() {
				this.getElementsByTagName("div")[0].style.display = "none";
				enablepopup = false;
				popupObject = "";
			}
		}
	}
}


	/***********************************************
	* Portions from Cool DHTML tooltip script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
	* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
	* Ripped apart, beaten, and modified heavily by Falcon (Nate Bundy) for use in RN
	***********************************************/
	var offsetxpoint= -10; //Customize x offset of tooltip
	var offsetypoint=20; //Customize y offset of tooltip
	var ie=document.all;
	var ns6=document.getElementById && !document.all;
	var enabletip=false;
	if (ie||ns6)
	function browsercheck()
	{
	   ie=document.all;
	   ns6=document.getElementById && !document.all;
	}
	
	function ietruebody(){
		return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
	}
	
	function positiontip(e){
		if (enablepopup){
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20;
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20;
		
		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000;
		
		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<popupObject.offsetWidth)
		{
			//move the horizontal position of the menu to the left by it's width
			// Falcon's edit:
			// Buggy for lots of text, so check to make sure this won't push off left of screen
			// If this would push some of the text off-screen, just throw the tip to the left edge of the screen
			// The size should dynamically change to fit as much on-screen as possible
			if ( (ie ? ietruebody().scrollLeft+event.clientX-popupObject.offsetWidth : window.pageXOffset+e.clientX-popupObject.offsetWidth) < 0)
				popupObject.style.left = ie ? ietruebody().scrollLeft+"px" : window.pageXOffset+"px";
			else
			popupObject.style.left=ie? ietruebody().scrollLeft+event.clientX-popupObject.offsetWidth+"px" : window.pageXOffset+e.clientX-popupObject.offsetWidth+"px";
		}
		else if (curX<leftedge)
		popupObject.style.left="5px";
		else
		//position the horizontal position of the menu where the mouse is positioned
		popupObject.style.left=curX+offsetxpoint+"px";
		
		//same concept with the vertical position
		if (bottomedge<popupObject.offsetHeight)
		{
			// Falcon's edit:
			// Same modification for vertical as horizontal
			// If this would push the element off-screen, throw it to the top edge of the screen
			// Only problem with this is if there isn't room, the mouse will no longer be over the original element, thus a mouseover mouseout cycle begins causing flickering of the popup element
			if ( (ie? ietruebody().scrollTop+event.clientY-popupObject.offsetHeight-offsetypoint : window.pageYOffset+e.clientY-popupObject.offsetHeight-offsetypoint) < 0)
			{
				popupObject.style.top = ie ? ietruebody().scrollTop + "px" : window.pageYOffset + "px";
				//alert("trying");
			}
			else
				popupObject.style.top=ie? ietruebody().scrollTop+event.clientY-popupObject.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-popupObject.offsetHeight-offsetypoint+"px";
		}
		else
		popupObject.style.top=curY+offsetypoint+"px";
		popupObject.style.visibility="visible";
		}
	}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// addLoadEvent(prepareMouseovers);
addLoadEvent(prepareMouseoversCharsTest);
addLoadEvent(prepareMouseoversCharsTest2);
addLoadEvent(prepareMouseoversClassIcons);
addLoadEvent(browsercheck);
