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 offset = getScrollXY();

	var relX = event.clientX - left;

	var x = event.clientX + offset[0];
	var y = event.clientY + offset[1];

/*

if there's no previous one and there is a next one,
then show next over the whole image.  If there's a
next and a previous, split it in half.  If there's
a previous and no next, then click does nothing.
*/
	
	if(relX < (width / 2) && prev_id != null) {
		showTooltip("prev", x - 15, y + 30);
	} else 
	if(next_id != null) {
		showTooltip("next", x - 15, y + 30);
	}
}

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 + "px";
  el.className = "tooltip" + message;
	
	if(!animMutex) {
		
		if(hideTimeout) {
			clearTimeout(hideTimeout);
		}
	
		
		animMutex = true;
		
		new Effect.Appear(
			'tooltip',
			{ 
				duration: 0.2
//				afterFinish:function(obj) { 
//					hideTimeout = setTimeout(function() { hideTooltip(false); }, 3000);
//				}
			}
		);
	}
}

function hideTooltip(resetMutex) {
	var el = $("tooltip");
	if(el) {
		new Effect.Fade(
			'tooltip', 
			{ 
				duration: 0.2,
				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 showLinks() {
	var links = $('links');
	var contact = $('contact');
	
	if(contact.style.display == 'none') {
		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 toggleItem(id) {
	var item = $(id);

	if(cats) {
		for(var i=0; i<cats.length; i++) {
			$(cats[i]).style.display = "none";
		}
	}

	item.style.display = "block";

	return false;
}

var AddPopup = function(uri, type, addEdit) {
	this.uri = uri;
	this.type = type;
	if(addEdit) {
		this.addEdit = addEdit;
	}
	this.show();
};

AddPopup.prototype = {
	type:null,
	div:null,
	addEdit:"add",
	show:function() {
		if(!this.div) {
			var div = this.div = ce("div", "addPopup");
			var form = ce("form");
			form.action = this.uri;
			form.method = "POST";
			div.appendChild(form);
			var p = ce("p", "addPopup");
			p.appendChild(document.createTextNode(this.addEdit + " "));
			var span = ce("span");
			span.appendChild(document.createTextNode(this.type));
			p.appendChild(span);
			form.appendChild(p);
			var input = ce("input");
			input.name = "name";
			input.type="text";
			input.style.width="75%";
			input.style.cssFloat = "left";
			input.style.styleFloat = "left";
			form.appendChild(input)
			var input = ce("input");
			input.type="submit";
			input.value="Save";
			input.style.width="20%";
			input.style.cssFloat = "left";
			input.style.styleFloat = "left";
			form.appendChild(input)
			var p = ce("p", "addPopup");
			var a = ce("a");
			a.appendChild(document.createTextNode("cancel"));

			var _this = this;
			a.onclick=function() { _this.hide.call(_this); };

			p.appendChild(a);
			div.appendChild(p);
			document.body.appendChild(div);
		}

		this.div.show();

	},
	hide:function() {
		if(this.div) {
			document.body.removeChild(this.div);
		}
	}
};

function ce(el, cl) {
	var ret = document.createElement(el);
	if(cl) { ret.className = cl; }
	return ret;
}
