var popupTimer = 0, 
popupLast = "", 
viewportwidth, 
viewportheight;
 
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined') {
  	viewportwidth = window.innerWidth, viewportheight = window.innerHeight;
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
   viewportwidth = document.documentElement.clientWidth, viewportheight = document.documentElement.clientHeight;
}
// older versions of IE 
else {
   viewportwidth = document.getElementsByTagName('body')[0].clientWidth, viewportheight = document.getElementsByTagName('body')[0].clientHeight;
}

function $(id)
{
	return document.getElementById(id);
}

// get by tag
function byTag(tag, obj)
{
	if (obj == null) {
		obj = document;
	}
	
	var col = [];
	tag = tag.split(",");
	for (var g = 0, l = tag.length; g < l; g ++) {
		var w = obj.getElementsByTagName(tag[g].toUpperCase());
		for (var q = 0, l2 = w.length; q < l2; q ++) {
			col.push(w[q]);
		}
	}		
	return col;
}

// returns objs document co-ordinates
function position(obj, type)
{
	var orig = obj;
	var retval = 0;
	if (type == "Left" && obj.x) {
		return obj.x;
	}
	else if (type == "Top" && obj.y) {
		return obj.y;
	}
	else {
		var useType = (type == "Left" || type == "Right" || type == "Center") ? "Left" : "Top";
		while (obj.offsetParent) {
			//if (obj && obj.style && obj.style.position) obj.style.position = "relative";
			retval += eval("obj.offset" + useType);
			obj = obj.offsetParent;
		}
		switch (type) {
			case "Center" : return retval + (orig.offsetWidth / 2); break;
			case "Right" : return retval + (orig.offsetWidth); break;
			default: return retval;
		}			
	}
	return false;

}

function findParent(obj, tag)
{
	tag = tag.toUpperCase();
	while (obj && obj.tagName != tag) {
		obj = obj.parentNode;
	}
	return obj;
}

// determine scroll amount
function getScroll()
{
	if (document.documentElement && document.documentElement.scrollTop) {
		return document.documentElement.scrollTop;
	}
	return (document.body.scrollTop) ? document.body.scrollTop : ((window.pageYOffset) ? window.pageYOffset : 0);	
}

// show popup
function showPopup(id, a)
{
	clearTimeout(popupTimer);
	if (id != popupLast) {
		removePopup();
	}	
	var s = getScroll(), 
	t = (position(a, "Top")), 
	l = position(a, "Left"), 
	c = document.body.appendChild($("moreInfo").cloneNode(true)), 
	d = null, 
	vs = viewportheight + s;
		
	for (var q = 0, x = popupItems.length; q < x; q ++) {
		if (popupItems[q].id == id) {
			d = popupItems[q];
			break;
		}
	}
	
	with (c) {
		for (var prop in d) {
			innerHTML = innerHTML.replace(new RegExp('{data:' + prop + "}", "g"), d[prop]);
			innerHTML = innerHTML.replace(/&lt;/g, "<");
			innerHTML = innerHTML.replace(/&gt;/g, ">");
		}
		id = "moreInfoClone";
		style.display = "block";
		style.left = (l - offsetWidth) + "px";
		style.top = (((t + offsetHeight) > vs) ? vs - offsetHeight : t) + "px";
		popupLast = id;
	}	
	var ds = byTag("DIV", $("moreInfoClone"));
	ds[ds.length - 1].style.display = d.special_offer.length ? "block" : "none";
	byTag("SPAN", ds[0])[0].style.display = (d.img_card.length || d.img_bank.length) ? "" : "none";
}

// hide popup
function hidePopup()
{
	popupTimer = setTimeout(removePopup, 500);
}

function removePopup()
{
	if ($("moreInfoClone")) $("moreInfoClone").parentNode.removeChild($("moreInfoClone"));
}

function trSetup()
{
	if ($("compare")) {
		for (var q = 1, tr = byTag("TR", $("compare")), l = tr.length; q < l; q ++) {
			Event.add(tr[q], "mouseover", function(e) { findParent(Event.target(e), "TR").className += " over" }, false);
			Event.add(tr[q], "mouseout", function(e) { findParent(Event.target(e), "TR").className = findParent(Event.target(e), "TR").className.replace(/ ?over/g, ""); }, false);
		}
		for (var i = 0; i < 2; i ++) {
			for (var q = (i ? 0 : 2), td = byTag("TH", tr[i]), l = td.length; q < (i ? l : l - 1); q ++) {
				if (td[q].colSpan == 1 || i) {
					Event.add(td[q], "mouseover", function(e) { findParent(Event.target(e), "TH").className += " over" }, false);
					Event.add(td[q], "mouseout", function(e) { findParent(Event.target(e), "TH").className = findParent(Event.target(e), "TH").className.replace(/ ?over/g, ""); }, false);
				}
			}
		}
	}
}

// extend objects
function extend(obj, props, force)
{
	for (var x in props) {
		if (typeof(obj[x]) == "undefined" || force) {
			obj[x] = props[x];
		}
	}
}

function showError(name, msg, f)
{
	var s = $(name + "_error");
	if (! s) {
		if (! msg.length) {
			return false;
		}
		s = document.createElement("span");
		s.id = name + "_error";
		$(f).elements[name].parentNode.appendChild(s);
	}
	s.className = (msg.length) ? "formError" : "formOk";
	s.innerHTML = msg;
}

// Add easier event handling
if (! window.Event) {
	var Event = {};
}

// Event object extending
var ev = {
	
	get: function(e)
	{
		return (! e) ? window.event : e;
	},

	target: function(e)
	{
		e = this.get(e);
		return (e.target) ? e.target : (e.srcElement) ? e.srcElement : false;
	},

	add: function(elm, evType, fn, useCapture)
	{
		// cross-browser event handling for IE5+, NS6 and Mozilla By Scott Andrew
		if (elm.addEventListener) {
			elm.addEventListener(evType, fn, useCapture);
			return elm;
		} 
		else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return elm;
		} 
		else {
			elm['on' + evType] = fn;
			return elm;
		}	
	},
		
	key: function(e)
	{
		e = this.get(e);
		return (e.keyCode) ? e.keyCode : (e.which) ? e.which : e.charCode;
	},
		
	stop: function(e)
	{
		e = this.get(e);
		if (document.all) {
			e.returnValue = false;
			e.cancelBubble = true;
		}
		else {
			e.preventDefault();
			e.stopPropagation();
		}		
	}
	
};
extend(Event, ev);

Event.add(window, "load", trSetup, false);