/*
	API to the prototype.js Ajax magic
*/
function AjaxUpdate(widget, url) {
	new Ajax.ValueUpdater(widget, url,
		{ onComplete: function() { new Effect.Highlight(widget); new CartUpdater('cart') } });
}

/*
	initialize product image switcher
*/
function initImageSwitcher() {

	// a closure to hide all images and deactivate all tabs inside d
	var hide = function(d) {
		return function() {
			for (var i = 0; d.images.length > i; i++) {
				d.images[i].style.visibility = 'hidden';
				d.tabs[i].firstChild.className = '';
			}
		}
	}

	var divs = document.getElementsByTagName('div');

	// image switcher works on images inside div.img blocks
	for (var i = 0; divs.length > i; i++) {
		var div = divs[i];
		if (div.className != 'img')
			continue;

		// store the nodeLists of images and tabs inside the div node
		div.images = div.getElementsByTagName('img');
		div.tabs = div.getElementsByTagName('ol')[0].childNodes;

		// the zoom tab
		var zoom = document.getElementById('zoom');

		// assign onclick events to make images open a new window
		// with dimensions specified in the URI fragment
		for (var j = 0; div.images.length > j; j++) {
			var link = div.images[j].parentNode;
			link.onclick = function(e) {
				if (popup) popup.close();
				var dim = this.href.match(/#(\d+)x(\d+)/);
				var w = 16 + parseInt(dim[1]);
				var h = 16 + parseInt(dim[2]);
				popup = window.open(this.href, '_blank', 'width=' + w + ',height=' + h + ',resizable=no,scrollbars=no');
				return false;
			}
		}

		// set event handlers for each tab but the last one
		for (var k = 0; div.tabs.length > k + 1; k++) {
			var link = div.tabs[k].firstChild;
			var id = link.href.replace(/[^#]+#/, '');

			// find the image associated with this tab and the link around it
			link.img = document.getElementById(id);
			link.imglink = link.img.parentNode;

			// the show method activates this tab, shows the associated image,
			// and assigns the zoom tab the correct href and onclick attributes
			link.show = function() {
				this.className = 'active';
				this.img.style.visibility = 'visible';
				zoom.href = this.imglink.href;
				zoom.onclick = this.imglink.onclick;
			}

			link.hide = hide(div);

			// at onclick, hide all images in this div and
			// use the link fragment to show the corresponding image
			link.onclick = function(e) {
				this.hide();
				this.show();
				return false;
			}

			// show the first tab initially
			if (!k) link.show();
		}
	}
}

/*
	initialize togglable product group nodes
*/
function initProductGroups() {

	var dts = document.getElementsByTagName('dt');

	// product groups are dt.group nodes
	for (var i = 0; dts.length > i; i++) {
		var dt = dts[i];
		if (dt.className != 'group')
			continue;

		// the first child node is the link
		var link = dt.firstChild;

		// the product group is two levels up, 2nd child node
		var group = dt.parentNode.parentNode.childNodes[1];
		try {
			group.style.display = 'none';
			link.group = group;

			link.toggle = function() {
				this.className = this.className == 'closed' ? 'open' : 'closed';
				this.group.style.display = this.group.style.display == 'block'
					? 'none' : 'block';
			}
			link.onclick = function(e) { this.toggle(); }
		} catch(e) { }
	}

	// toggle open the group that matches URI fragment
	var location = new String(window.location);
	var current = location.match(/#(.+)/);
	if (current)
		try {
			document.getElementById(current[1]).toggle();
		} catch(e) { }
}

/*
	focus the first enabled form field on the page
*/
function selectFirst() {
	var widgets = document.getElementsByTagName('INPUT');
	for (var i = 0; i < widgets.length; i++) {
		var w = widgets[i];
		if (w.readOnly || w.disabled ||
			w.type == 'hidden' || w.type == 'submit')
			continue;
		w.focus();
		w.select();
		break;
	}
}

// attach onclick events to all external links inside the given
// element id to make them open a new browser window
function makeExternal(id) {
	var domainPattern = new RegExp('^http://' + document.domain);
	try {
		var elem = document.getElementById(id);
		var links = elem.getElementsByTagName('A');
	} catch (e) {
		return false;
	}
	for (var i = 0; i < links.length; i++) {
		var link = links[i];
		if (link.href.match(domainPattern))
			continue;	// skip local links
		link.onclick = function() {
			window.open(this.href); return false;
		}
	}
}

// attach onmouseover/out event handlers to all
// list item nodes inside the given element id
function activate(id) {
	var elem = document.getElementById(id);
	var topics = elem.getElementsByTagName('LI');
	for (var i = 0; i < topics.length; i++) {
		var node = topics[i];
		node.originalClass = node.className;
		node.onmouseover = function() {
			this.className = this.originalClass + ' active';
		}
		node.onmouseout = function() {
			this.className = this.originalClass;
		}
	}
}

var popup;

window.onload = function() {
	initImageSwitcher();
	initProductGroups();
	makeExternal('verified');
	selectFirst();
	if (document.all && document.getElementById)
		activate('menu');
}
window.onunload = function() {
	if (popup) popup.close();
}
