var animMutex = false;
var hideTimeout = null;

function pos(el) {
	var left = 0;
	var top = 0;
	
	do {
		
		left += el.offsetLeft;
		top += el.offsetTop;
		
	} while(el = el.offsetParent);
	
	return [ left, top ];
	
}

// shamelessly stolen from 
// http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function portImgMove(event, image, prev_id, next_id) {
	if(!event) { event = window.event; }
	
	var width = image.width;
	var height = image.height;

	var imgLoc = pos(image);
	
	var left = imgLoc[0];
	var top = imgLoc[1];
	
	var x = event.clientX - left;
	var y = event.clientY + getScrollXY()[1];

	if(x < (width / 2)) {
		if(prev_id != null) {
			showTooltip("prev", left - 31, y);
		}
	} else {
		if(next_id != null) {
			showTooltip("next", left + width + 5, y);
		}
	}
}

function showTooltip(message, x, y) {
	var el = $("tooltip");
	if(!el) {
		el = document.createElement("div");
		el.id = "tooltip";
		el.style.display = "none";
		document.body.appendChild(el);
	}
	
	el.style.left = x + "px";
	el.style.top = (y - 15) + "px";
  el.className = "tooltip" + message;
	
	if(!animMutex) {
		
		if(hideTimeout) {
			clearTimeout(hideTimeout);
		}
	
		
		animMutex = true;
		
		new Effect.Appear(
			'tooltip',
			{ 
				afterFinish:function(obj) { 
					hideTimeout = setTimeout(function() { hideTooltip(false); }, 3000);
				}
			}
		);
	}
}

function hideTooltip(resetMutex) {
	var el = $("tooltip");
	if(el) {
		new Effect.Fade(
			'tooltip', 
			{ 
				afterFinish:function(obj) {
					if(resetMutex) {
						animMutex = false;
					}
				}
			}
		);
	}
}

function portImgClick(event, image, prev_id, next_id) {
	if(!event) { event = window.event; }
	
	var width = image.width;
	var height = image.height;

	var imgLoc = pos(image);
	
	var left = imgLoc[0];
	var top = imgLoc[1];
	
	var x = event.clientX - left;
	var y = event.clientY - top;
	
	if(x < (width / 2)) {
		if(prev_id != null) {
			window.location = prev_id;
		}
	} else {
		if(next_id != null) {
			window.location = next_id;
		}
	}
}

function showAbout() {
	var links = $('links');
	var contact = $('contact');
	var about = $('about');
	
	if(contact.style.display == 'none' && links.style.display == "none") {
		Effect.toggle(about, "slide", { duration:0.5 });
	} else 
	if(contact.style.display == "none") {
		Effect.toggle(
			links,
			"slide",
			{
				duration:0.5,
				afterFinish:function() {
					Effect.toggle(about, "slide", { duration:0.5 });
				}
			}
		);
	} else {
		Effect.toggle(
			contact,
			"slide",
			{
				duration:0.5,
				afterFinish:function() {
					Effect.toggle(about, "slide", { duration:0.5 });
				}
			}
		);
	}
}

function showLinks() {
	var links = $('links');
	var contact = $('contact');
	var about = $('about');
	
	if(contact.style.display == 'none' && about.style.display == "none") {
		Effect.toggle(links, "slide", { duration:0.5 });
	} else 
	if(contact.style.display == "none") {
		Effect.toggle(
			about,
			"slide",
			{
				duration:0.5,
				afterFinish:function() {
					Effect.toggle(links, "slide", { duration:0.5 });
				}
			}
		);
	} else {
		Effect.toggle(
			contact,
			"slide",
			{
				duration:0.5,
				afterFinish:function() {
					Effect.toggle(links, "slide", { duration:0.5 });
				}
			}
		);
	}
}

function showContact() {
	var links = $('links');
	var contact = $('contact');
	var about = $('about');
	
	if(links.style.display == 'none' && about.style.display == "none") {
		Effect.toggle(contact, "slide", { duration:0.5 });
	} else 
	if(links.style.display == "none") {
		Effect.toggle(
			about,
			"slide",
			{
				duration:0.5,
				afterFinish:function() {
					Effect.toggle(contact, "slide", { duration:0.5 });
				}
			}
		);
	} else {
		Effect.toggle(
			links,
			"slide",
			{
				duration:0.5,
				afterFinish:function() {
					Effect.toggle(contact, "slide", { duration:0.5 });
				}
			}
		);
	}
}

