/**
 * ia.js
 *
 * Current scripts in this file:
 *    + Collape nav
 *    + Addevent
 *    + Sweet titles
 *    + Tooltip
 *
 */


/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
 *  Collapse Nav script
 *  v. 07.05.07
 *
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/

  if (document.getElementsByTagName){
  //append this style to the page. the style hides all of the menu options when page loads.
    var myCSS = ".menu-opt{display:none;} body{display:none;}";
    var hstyle = document.createElement("style");
    hstyle.setAttribute("type", "text/css");
    //only IE uses style.styleSheet, the rest use style.sheet
    if(hstyle.styleSheet){
    //Many browsers won't let you change the style's cssText, but IE does.
      hstyle.styleSheet.cssText = myCSS;
    } else {
    //this is the standard way to do it, which IE does not support.
      myCSS = document.createTextNode(myCSS);
      hstyle.appendChild(myCSS);
    }
    document.getElementsByTagName("head")[0].appendChild(hstyle);
  }


function displayLinks(node) {
  el = node.nextSibling;
  while (el){
    if (el.className == "menu-opt"){
      if (! el.style.display){
        el.style.display = "block";
        node.className = "menu-head active";
      }
      else if (el.style.display == "none"){
        el.style.display = "block";
        node.className = "menu-head active";
      }
      else{
        el.style.display = "none";
        node.className = "menu-head";
      }

    }
    if (el.className && el.className.indexOf("menu-head") > -1 ){
      el=0;
      //stop loop
    }
    el = el.nextSibling;
  }
}

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

addLoadEvent(collapseList);
addLoadEvent(function(){
  var body = document.getElementsByTagName('body')[0];
  body.style.display = "block";
})


function collapseList() {
  var link = window.location;
  var linksplit = link.href.split("/");
  var page = linksplit[linksplit.length-1];
  var pagepath = link.pathname;
  if (page == ""){
    page = "index.html"
  }
  if (document.getElementById) {
    links = document.getElementsByTagName("li");
    var menulinks = new Array();
    for (i=0; i < links.length; i++){
      if (links[i].className.indexOf("menu-head") > -1 || links[i].className == "menu-opt"){
        menulinks.push(links[i]);
      }
    }
    for (i=0; i < menulinks.length; i++) {
      node = menulinks[i];
      if (node.className.indexOf("menu-head") > -1) {
        var ctrigger = 0;
        var uncollapse = new Array();
        if (menulinks[i+1] && menulinks[i+1].className == "menu-opt"){
        //if there are links underneath a menu heading, make the menu click action unhide it's links
          node.onclick = function () {
            displayLinks(this);
            return false;
          }
          for (j=i+1; j<menulinks.length; j++){
          //loop through menu options
               var liNode = menulinks[j];
               if (liNode.className.indexOf( "menu-head" ) > -1 ){
                   break;
               }
               for (var a=0; a < liNode.childNodes.length; a++){
                   if (liNode.childNodes[a].tagName == "A"){
                   //if this list item has a link, store it as linknode
                      var linknode = liNode.childNodes[a];
                   }
               }
               if (linknode && liNode.className == "menu-opt"){//&& liNode.className == "menu-opt"
                   uncollapse.push(liNode);
                   var linkref = linknode.getAttribute('href');
                   linkref = linkref.replace("..", "");
                   var linkrefsplit = linkref.split('?');
                   var linkpath = '/';
                   linkref = linkrefsplit[0];
                   linkref = linkref.replace("http://www.unf.edu", "");
                   if (linkrefsplit.length > 4){
                      for (var a=3; a < linkrefsplit.length; a++){
                         linkpath+=linkrefsplit[a]+'/';
                      }
                      linkpath = linkpath.substring(0, linkpath.length-1);
                   }
                   else
                     linkpath = 'index.html';
                   if (pagepath.indexOf(linkref) > -1){
                   //the current page url is equal to the link url
                       //ctrigger = 1; /* disabled to keep all menus closed even if at one of th epages */
                   }
               }
          }
          if (ctrigger == 1){
            for (j=0; j < uncollapse.length; j++){
              uncollapse[j].style.display = "block";
              node.className = "menu-head active";
            }
          }
        }
      }
    }
  }
}// end func collapse list



/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
 *  addEvent
 *  v. 7.9.07
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	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] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);




/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
 *  sweetTitles
 *  v. 7.9.07
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/

//BUG: when a dialog is up and you accidently mouseover a building, the dialog closes.
//FIX: when dialog=1 place a timer on mouseovers so they won't cancel the dialog immediately.
//BUG: If you click a building before mouseover the mouseover closes the dialog after it was already initialized.
function closeDialog(){
	sweetTitles.tip.style.visibility = "hidden";
	sweetTitles.dialog = 0;
	return false;
}
function showDialog(){
	sweetTitles.tip.innerHTML = '<h4>J. J. Daniel Hall</h4><img src="../map-images/Bldg-1.jpg" class="left" alt="Building 1"/><p>Designed for educational enlightenment, J. J. Daniel Hall stands among the <a href="http://en.wikipedia.org/wiki/Sycamore_maple" target="_blank">sycamores</a> near the library.</p><a id="closeDialog" href="#"><img src="../images/close.gif" alt="close"/></a>';
	sweetTitles.tip.style.width = '350px';
	sweetTitles.tip.style.visibility = 'visible';
	sweetTitles.tip.style.opacity = '.1';
	sweetTitles.tipFade(10);
	sweetTitles.dialog = 1;
	var close = document.getElementById("closeDialog");
	close.onclick = closeDialog;
	return false;
}
/*
Sweet Titles (c) Creative Commons 2005
http://creativecommons.org/licenses/by-sa/2.5/
Author: Dustin Diaz | http://www.dustindiaz.com
*/
var sweetTitles = {
	xCord : 0,								// @Number: x pixel value of current cursor position
	yCord : 0,								// @Number: y pixel value of current cursor position
	tipElements : ['area'],
	obj : Object,							// @Element: That of which you're hovering over
	tip : Object,							// @Element: The actual toolTip itself
	active : 0,								// @Number: 0: Not Active || 1: Active
	dialog : 0,
	init : function() {
		if ( !document.getElementById ||
			!document.createElement ||
			!document.getElementsByTagName ) {
			return;
		}
		var i,j;
		this.tip = document.createElement('div');
		this.tip.id = 'toolTip';
		document.getElementsByTagName('body')[0].appendChild(this.tip);
		this.tip.style.top = '0';
		this.tip.style.visibility = 'hidden';
		var tipLen = this.tipElements.length;
		for ( i=0; i<tipLen; i++ ) {
			var current = document.getElementsByTagName(this.tipElements[i]);
			var curLen = current.length;
			for ( j=0; j<curLen; j++ ) {
				addEvent(current[j],'mouseover',this.tipOver);
				addEvent(current[j],'mouseout',this.tipOut);
				current[j].setAttribute('tip',current[j].title);
				current[j].removeAttribute("title");
			}
		}
	},
	updateXY : function(e) {
		if ( document.captureEvents ) {
			sweetTitles.xCord = e.pageX;
			sweetTitles.yCord = e.pageY;
		} else if ( window.event.clientX ) {
			sweetTitles.xCord = window.event.clientX+document.documentElement.scrollLeft;
			sweetTitles.yCord = window.event.clientY+document.documentElement.scrollTop;
		}
	},
	tipOut: function() {
		if ( window.tID ) {
			clearTimeout(tID);
		}
		if ( window.opacityID ) {
			clearTimeout(opacityID);
		}
		if ( sweetTitles.dialog < 1 ){
			sweetTitles.tip.style.visibility = 'hidden';
		}
	},
	checkNode : function() {
		var trueObj = this.obj;
		if ( this.tipElements.inArray(trueObj.nodeName.toLowerCase()) ) {
			return trueObj;
		} else {
			return trueObj.parentNode;
		}
	},
	tipOver : function(e) {
		sweetTitles.obj = this;
		sweetTitles.dialog = 0;
		tID = window.setTimeout("sweetTitles.tipShow()",500);
		sweetTitles.updateXY(e);
	},
	tipShow : function() {
		var scrX = Number(this.xCord);
		var scrY = Number(this.yCord);
		var tp = parseInt(scrY+15);
		var lt = parseInt(scrX+10);
		var anch = this.checkNode();
		var addy = '';
		var access = '';
		sweetTitles.tip.style.width = "auto";
		if ( anch.nodeName.toLowerCase() == 'a' ) {
			addy = (anch.href.length > 25 ? anch.href.toString().substring(0,25)+"..." : anch.href);
			var access = ( anch.accessKey ? ' <span>['+anch.accessKey+']</span> ' : '' );
		}
		//edit by sandro turriate
		else if (anch.nodeName.toLowerCase() == 'area'){
			addy = "";
		}
		else {
			addy = anch.firstChild.nodeValue;
		}
		this.tip.innerHTML = "<p>"+anch.getAttribute('tip')+"<em>"+access+addy+"</em></p>";
		if ( parseInt(document.documentElement.clientWidth+document.documentElement.scrollLeft) < parseInt(this.tip.offsetWidth+lt) ) {
			this.tip.style.left = parseInt(lt-(this.tip.offsetWidth+10))+'px';
		} else {
			this.tip.style.left = lt+'px';
		}
		if ( parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) < parseInt(this.tip.offsetHeight+tp) ) {
			this.tip.style.top = parseInt(tp-(this.tip.offsetHeight+10))+'px';
		} else {
			this.tip.style.top = tp+'px';
		}
		this.tip.style.visibility = 'visible';
		this.tip.style.opacity = '.1';
		this.tipFade(10);
	},
	tipFade: function(opac) {
		var passed = parseInt(opac);
		var newOpac = parseInt(passed+10);
		if ( newOpac < 80 ) {
			this.tip.style.opacity = '.'+newOpac;
			this.tip.style.filter = "alpha(opacity:"+newOpac+")";
			opacityID = window.setTimeout("sweetTitles.tipFade('"+newOpac+"')",20);
		}
		else {
			if (this.dialog > 0){
				this.tip.style.opacity = '.99';
				this.tip.style.filter = "alpha(opacity:99)";
			}
			else {
				this.tip.style.opacity = '.80';
				this.tip.style.filter = "alpha(opacity:80)";
			}
		}
	},
	showDialog: function() {
		this.tip.innerHTML = "<p>Hello World!</p>";
		this.tip.style.visibility = 'visible';
		this.tip.style.opacity = '.1';
		this.tipFade(10);

	}
};
function pageLoader() {
	sweetTitles.init();
}
addEvent(window,'load',pageLoader);



/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
 *  tooltip
 *  v. 7.9.07
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/

/*
Text Link/Image Map Tooltip Script-
© Dynamic Drive (www.dynamicdrive.com)
For full source code, and 100's more DHTML scripts
Visit http://www.dynamicdrive.com
*/

if (!document.layers&&!document.all&&!document.getElementById)
event="test"
function showtip(current,e,text){

if (document.all||document.getElementById){
thetitle=text.split('<br>')
if (thetitle.length>1){
thetitles=''
for (i=0;i<thetitle.length;i++)
thetitles+=thetitle[i]
current.title=thetitles
}
else
current.title=text
}

else if (document.layers){
document.tooltip.document.write('<layer bgColor="white" style="border:1px solid black;font-size:12px;">'+text+'</layer>')
document.tooltip.document.close()
document.tooltip.left=e.pageX+5
document.tooltip.top=e.pageY+5
document.tooltip.visibility="show"
}
}
function hidetip(){
if (document.layers)
document.tooltip.visibility="hidden"
}

