/*
UI.JS
Programming for UI effects
*/
var menubar;
var event_scroll;
var thumbnail;


/* Menu bar */
(isIE) ? window.attachEvent("onload", menuInit) : window.addEventListener("load", menuInit, false);


function menuInit() {
	menubar           = document.getElementById("nav");
	menubar.className = "content inplace";
	event_scroll      = (isIE) ? window.attachEvent("onscroll", positionMenu) : window.addEventListener("scroll", positionMenu, false);
}

function positionMenu() {
	var pageScroll = ((isIE) ? document.documentElement.scrollTop : window.pageYOffset) - 110;
	
	menubar.className = (pageScroll < 0) ? "content inplace" : "content floating";
}


/* Product thumbnail */
function init_thumbnail() {
	thumbnail = document.createElement('div');
	thumbnail.id = "thumbnail";
	
	document.body.appendChild(thumbnail);
}

function showThumbnail(a, img) {
	thumbnail.style.display = "block";
	thumbnail.innerHTML     = "<h3>" + a.innerHTML + "</h3>\n<img src=\"/product_shots/thumbnail/" + img + "\" />";
}

function hideThumbnail() {
	thumbnail.style.display = "none";
}

function moveThumbnail(e) {
	var posX = 0;
	var posY = 0;
	
	if (!e || e == undefined) e = window.event;
	
	if (isIE) {
		posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft + 10;
		posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		
		view_bottom  = document.documentElement.scrollTop + document.documentElement.clientHeight;
	}
	else {
		posX = e.pageX + 10;
		posY = e.pageY;
		
		view_bottom  = window.pageYOffset + window.innerHeight;
	}
	
	thumb_bottom = thumbnail.offsetHeight + posY;
	posY         = (thumb_bottom > view_bottom) ? posY - (thumb_bottom - view_bottom) : posY + 10;
	
	
	thumbnail.style.left = posX + "px";
	thumbnail.style.top  = posY + "px";
}


/* Widgets */
function toggle(target, obj, show, dismiss) {
	var css = (show) ? "toggle on" : "toggle off";
	
	document.getElementById(target).className = css;
	
	if (dismiss) { obj.parentNode.removeChild(obj) }
	
	return false;
}

function mailPanel(obj) {
	var panel = getParentObject(obj, "DIV");
	
	panel.className = (panel.className == "show") ? "hide" : "show";
	
	return false;
}


/* Operations */
function replaceControls(obj, which) {
	// replacing parent is a one-shot deal
	switch (which) {
		case "parent":
			obj.parentNode.innerHTML = "<img src=\"/lib/img/ui/ui_throb32.gif\" alt=\"Please wait\" />";
			
			break;
		
		case "parent_small":
			obj.parentNode.innerHTML = "<img src=\"/lib/img/ui/ui_throb26.gif\" alt=\"Please wait\" />";
			
			break;
		
		default:
			var throbber       = document.createElement("span");
			throbber.innerHTML = "&nbsp;";
			throbber.className = "throbber " + which;
			
			// back up original
			original = obj;
			switched = throbber;
			
			obj.parentNode.replaceChild(throbber, obj);
			
			break;
	}
}

function restoreControls() {
	switched.parentNode.replaceChild(original, switched);
}

function dismiss(obj) {
	obj.parentNode.removeChild(obj);
	
	return false;
}


/* Utilities */
function addCSSClass(obj, css) {
	if (!hasCSSClass(obj, css)) { obj.className += (" " + css) }
}

function removeCSSClass(obj, css) {
	if (hasCSSClass(obj, css)) {
		var reg = new RegExp('(\\s|^)' + css + '(\\s|$)');
		obj.className = obj.className.replace(reg, ' ');
	}
}

function hasCSSClass(obj, css) {
	return obj.className.match(new RegExp('(\\s|^)' + css + '(\\s|$)'));
}

function getParentObject(obj, type) {
	while (obj.nodeName != type) {
		obj = obj.parentNode;
	}

	return obj;
}
