/*
 * file   : utils.js
 * author : Kwame Ansong-Dwamena
 */

// globals
var i = 0;
var posx = 0;
var posy = 0;
var e = window.onmouseover;
var feedbackformState = "OFF";

// this function obtains the location of the mouse in the browser
// function obtained from http://www.quirksmode.org/
function getMouseLocation(e) 
{
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	
	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
}

/*
 * this function checks to see if the feedback from is visible,
 * then hides it so that it does not interfere with the image popup;
 */
function hideFeedbackbox()
{
	document.getElementById("feedbackbox").style.display = "none";
	document.getElementById("feedbackbox").style.visibility = "hidden";
}

/*
 * this function checks the state of the feedback box;
 * if it is ON, this function re-displays it, otherwise, it is OFF;
 * by so-doing, this function ensures that the feedback form shows up
 * after the image popup is hidden. this function also turns the state OFF
 */
function showFeedbackbox()
{
	document.getElementById("feedbackbox").display = "block";
	document.getElementById("feedbackbox").visibility = "visibile";
}

/*
 * this function removes the 'onmousemove' event
 * handler from a thumb and then hides the collage popup
 */
function hideCollagePopup()
{
	this.onmousemove = null;
	var popup = document.getElementById("popup");
	popup.style.visibility = "hidden";
	popup.style.display = "none";
	document.getElementById("popupImage").src = "";
	showFeedbackbox();
	if(isFeedbackboxVisible) showFeedbackbox();
}

// this function inserts the popup image into the popup div
function insertPopupImage(thumbnail)
{
	var thumbnailID = thumbnail.id;
	var thumbnailNum = thumbnailID.charAt(5);
	document.getElementById("popupImage").src = "img/popup" + thumbnailNum + ".png";
}

// this function 'draws' the collage popup at the current mouse position
function showCollagePopupAtMousePsn(e)
{
	//if(isFeedbackboxVisibile) hideFeedbackBox();
	getMouseLocation(e);
	var popup = document.getElementById("popup");
	popup.style.position = "relative";
	popup.style.left = posx + "px";
	var top = document.body.clientHeight - posy;
	popup.style.top = "-" + top + "px";
	popup.style.visibility = "visible";
	popup.style.display = "block";
	insertPopupImage(this);
}

/*
 * this function is only called when an 'onmouseover' event is fired over a thumb;
 * this function:
 * - obtains the location of the mouse,
 * - then displays the collage popup,
 * - then attaches an 'onmousemove' event handler to the thumbnail the mouse is over.
 */
function displayCollagePopup(e)
{
	this.onmousemove = showCollagePopupAtMousePsn;
}

/* 
 * this function attaches 'onmouseover' event handlers to the
 * thumbnails; the 'onmouseover' event triggers the collage popups when fired
 */
function addMouseoverEventHandlersToThumbs()
{
	for(j=0;j<3;j++)
	{
		var ovalElem = document.getElementById("thumb"+j);
		ovalElem.onmouseover = showCollagePopupAtMousePsn;
		ovalElem.onmouseout = hideCollagePopup;
	}
}

/*
 * from Stuart Langride, http://www.kryogenix.org/days/2007/09/26/shortloaded;
 * this function detects the "onload" event for all browsers and fires up the
 * controls for the website
 */
(function(i) {
  var u = navigator.userAgent.toLowerCase();
  var ie = /*@cc_on!@*/false;
  if (/webkit/.test(u)) {
    // safari
    timeout = setTimeout(function(){
			if ( document.readyState == "loaded" || 
				document.readyState == "complete" ) {
				i();
			} else {
			  setTimeout(arguments.callee,10);
			}
		}, 10); 
  } else if ((/mozilla/.test(u) && !/(compatible)/.test(u)) ||
             (/opera/.test(u))) {
    // opera/moz
    document.addEventListener("DOMContentLoaded",i,false);
  } else if (ie) {
    // IE
    (function (){ 
      var tempNode = document.createElement('document:ready'); 
      try {
        tempNode.doScroll('left'); 
        i(); 
        tempNode = null; 
      } catch(e) { 
        setTimeout(arguments.callee, 0); 
      } 
    })();
  } else {
    window.onload = i;
  }
})(addMouseoverEventHandlersToThumbs);
