var onDocumentReadyFuncs = [];

var altSrcKeys = {"link": "rel", "button": "value"};

var swapImages = function() {
	var type = getElementType(this);
	if(altSrcKeys[type] && this[altSrcKeys[type]]) {
		var img = (("link" == type) ? $(this).find("img")[0] : (("button" == type) ? this : null));
		
		if(img) {
			var temp = img.src;
			img.src = this[altSrcKeys[type]];
			this[altSrcKeys[type]] = temp;
		}
	}
};

var isLink = function(element) {
	return ("a" == element.tagName.toLowerCase());
};

var isButton = function(element) {
	return (("input" == element.tagName.toLowerCase()) && ("image" == element.type));
};

var getElementType = function(element) {
	return ((isLink(element)) ? "link" : ((isButton(element)) ? "button" : element.tagName));
};

window.tempImages = [];
var preloadImage = function(src) {
	var img = new Image();
	img.src = src;
	window.tempImages = window.tempImages.concat([img]);
};

function fixColumnHeights() {
	var greatestHeight = 0;
	
	var columnLeft = $('#main .columnLeft');
	
	if('undefined' != typeof columnLeft[0]) {
		if(greatestHeight < columnLeft[0].clientHeight) {
			greatestHeight = columnLeft[0].clientHeight;
		}
	}
	
	var centerColumn = $('#main .centerColumn');
	
	if('undefined' != typeof centerColumn[0]) {
		if(greatestHeight < centerColumn[0].clientHeight) {
			greatestHeight = centerColumn[0].clientHeight;
		}
	}
	
	var columnRight = $('#main .columnRight');
	
	if('undefined' != typeof columnRight[0]) {
		if(greatestHeight < columnRight[0].clientHeight) {
			greatestHeight = columnRight[0].clientHeight;
		}
	}
	
	if('undefined' != typeof columnLeft[0]) {
		columnLeft[0].style.height = greatestHeight + 'px';
	}
	
	if('undefined' != typeof centerColumn[0]) {
		centerColumn[0].style.height = greatestHeight + 'px';
	}
	
	if('undefined' != typeof columnRight[0]) {
		columnRight[0].style.height = greatestHeight + 'px';
	}
}

$(document).ready(function() {
	$("a.categoryHeading, a.magenta").each(function() {
		$(this).removeClass('magenta');
		this.style.color = '#666';
		$(this).hover(
			function() {
				this.style.color = '#c81588';
			}, function() {
				this.style.color = '#666';
			}
		);
		/*$(this).contents().filter(
			function() {
				return (
					(3 == this.nodeType) || (
						(1 == this.nodeType) &&
						('SPAN' == this.tagName.toUpperCase())
					)
				);
			}
		).wrap('<a></a>')*/
	});
	
	$("a.hover, input.hover").each(function() {
		var type = getElementType(this);
		
		if(altSrcKeys[type] && this[altSrcKeys[type]]) {
			preloadImage(this[altSrcKeys[type]]);
			$(this).hover(swapImages, swapImages);
		}
	});
	
	$("a.popup").each(function() {
		$(this).click(function(event) {
			event.preventDefault();
			window.open(this.href);
			//this.href = "#";
		});
	});
	
	for(var i=0; i < window.onDocumentReadyFuncs.length; ++i) {
		window.onDocumentReadyFuncs[i]();
	}
});

$(window).load(function() {
	fixColumnHeights();
});
