// global custom javascript functions

// onload event (jQuery method)
$(document).ready(function(){
	
	// apply ie png fix to elements with a class of transpng
	$('.transpng').ifixpng(); 


	// search box behavior
	$('#srch_q').click(function(){
		if($(this).val()=='Search');
		$(this).val('');
	});

	$('#srch_q').blur(function(){
		if($(this).val()==''){
			$(this).val('Search');
		}
	});


	// apply round corners to search box
	$('#searchbox form').corner("round 8px").parent().css('padding', '2px').corner("round 8px");
	
	// autocomplete search
	function findValueCallback(event, data, formatted) {
		$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
	}
	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}
	$("#srch_q").autocomplete("/scripts/ajaxSearch.php", {
		width: 180,
		selectFirst: false
	});
	$("#srch_q").result(function(event, data, formatted) {
		if (data)
			$(this).parent().next().find("input").val(data[1]);
	});
	
	
	// table striping
	$('.striped tr:even').addClass('altrow');
	
	
	// button rollovers 
		// preload rollover images
		$('.rollover').children('img').each(function() {
			rollsrc = $(this).attr('src');
			rollON = rollsrc.replace(/(.*)\.(jpg|gif|png)$/i, '$1_over.$2');
			$('<img>').attr('src', rollON);
		});
	
		// check for rollover
		$('.rollover').mouseover(function(){
			imgsrc=$(this).children('img').attr('src');
			matches = imgsrc.match(/_over/);

			// don't do the rollover if state is already ON
			if (!matches) {
				imgsrcOn = imgsrc.replace(/(.*)\.(jpg|gif|png)$/i, '$1_over.$2');
				$(this).children('img').attr('src', imgsrcOn);
			}
			
			$('.rollover').mouseout(function(){
				$(this).children('img').attr('src', imgsrc);
			});
		});
		
		
	// product thumbnail/image swapping
	$('.imglink').click(function(){
		$('.prodimage').attr('src',$(this).attr('href'));
		return false;
	});
		
	
});
