/*------------------------------------------------
	By Daggles
	http://tcg.daggles.net/
	Modify and redistribute as you see fit
	
	11/17/2011: 1.1
		- Cards in the found list are now links that will take you directly to the card
	11/15/2011: 1.0
		Initial release
------------------------------------------------*/

function repl(str) {
    return $.trim(str.replace(/(?=[\/\\^$*+?.()|{}[\]])/g, ' '));
}
function clearHighlight() {
	$('img').each(function(index) {
		$(this).removeClass('dimmedCard, highlightCard');
	});
}
function highlightImage() {
	var searchTerm = repl($('#search').val());
	var re = new RegExp(searchTerm  + "[a-zA-Z]*[0-9]*", "i");
	var cardArray = new Array();
	
	if (searchTerm.length >= 3) {
		$('#cardlist').text('');
		$('img').each(function(index) {
			if ($(this).attr('title') != undefined) {
				if ($(this).attr('title').match(re)) {
					$(this).addClass('highlightCard').removeClass('dimmedCard');
					cardArray.push('<a href="#' + $.trim($(this).attr('title')) + '">' + $.trim($(this).attr('title')) + '</a>');
					$(this).attr('id', $.trim($(this).attr('title')));
				} else {
					$(this).removeClass('highlightCard').addClass('dimmedCard');
				}
			}
		});
	var cardArrayCount = cardArray.length;
	$('#cardlist').html('<i>Found ' + cardArrayCount + ' cards.</i><br />' + cardArray.join(', '));
	} else {
		$('#cardlist').html('<i>Please enter 3 or more characters.</i>');
	}
}
function clearHighlight() {
	$('img').each(function(index) {
		$(this).removeClass('highlightCard dimmedCard');
		$('#cardlist').text('');
	});
}
