
$(document).ready(function() {

//add close x to message boxes

	$('.success').prepend('<a href="" style="float:right; padding:0 20px; text-decoration:none; font-size:9px;">[Close]</a>');
	$('.success a').click(function(){
		$('.success').slideUp(100);
		return false;
	});
	
	$('a[href$="remove"]').click(function(){
  	var answer = confirm('Are you sure you want to remove this address?');
  	return answer // answer is a boolean
	});
	
//top search field text


$('#query').val('Keyword or SKU').css("color", "#ccc");

$('#query').click(function() {
	$(this).val('').css("color", "#222");
});



//add datepicker to date fields
$('input.date').datepicker();

});


// position images

window.onload = (function() {

	$('.tiny-holder img').each(function()	{

							  							  
		var width = $(this).width();
		var height = $(this).height();
		
		if (width > height) {
			
			var position_top = Math.floor((120 - height)/2);
			
			$(this)
				.css("position","relative")
				.css("top",position_top + "px");

		} else {
			
			var position_left = Math.floor((120 - width)/2);
			
			$(this)
				.css("position","relative")
				.css("left",position_left + "px");
		}
	});
						  
	$('.small-holder img').each(function()	{

							  							  
		var width = $(this).width();
		var height = $(this).height();
		
		if (width > height) {
			
			var position_top = Math.floor((200 - height)/2);
			
			$(this)
				.css("position","relative")
				.css("top",position_top + "px");

		} else {
			
			var position_left = Math.floor((200 - width)/2);
			
			$(this)
				.css("position","relative")
				.css("left",position_left + "px");
		}
	});
	
	$('.medium-holder img').each(function()	{

							  							  
		var width = $(this).width();
		var height = $(this).height();
		
		if (width > height) {
			
			var position_top = Math.floor((300 - height)/2);
			
			$(this)
				.css("position","relative")
				.css("top",position_top + "px");

		} else {
			
			var position_left = Math.floor((300 - width)/2);
			
			$(this)
				.css("position","relative")
				.css("left",position_left + "px");
		}
	});
	
	$('.medium-holder').hide().filter(':first').show();

	$('.thumb-gallery a').click(function () {
		var n = $(this).index();
		$('.medium-holder').hide().filter(':eq('+n+')').show();
		var newlink = $('.medium-holder:visible').attr('href');
		$('.enlarge a').attr("href", newlink);
		//$('.medium-holder').eq(''+n+'').show();
		$('#nnn').text(''+n+'');
		return false;
	});	
	
	// setting footer divs to the same height	
	$('#footer .one-fourth').css('height', largest_height('#footer .one-fourth') + 'px');

	//apply fancybox to images on content areas
	$('.holder, .enlarge a').fancybox();
	
	//show hide tax exempt options
	
	if ($('#tax_exempt').attr("checked")==true) {
		$('#tax_exempt_options').show();
	} else {
		$('#tax_exempt_options').hide();
	}

	$('#tax_exempt').click(function(){
		$('#tax_exempt_options').toggle();
	});

});
		
// pass in jQuery selector and it will return the largest height for the selected elements
function largest_height(elements){
		var largest = 0;
	
		$(elements).each(function(){
			if ($(this).height() > largest)
				largest = $(this).height();
		});
		return largest;
    }
