$(document).ready(function() {
	
	fb_settings = {
		'width':	'600',
		'height':	'600',
		'autoDimensions': 'false',
		'overlayColor': '#000',
		'overlayOpacity': '0.6'
	};

	// Fancybox for all certificates
	$("a.cert").fancybox(fb_settings);

	// Add class name to odd rows
	$(".grid tr:odd").each(function(index, el) {
		$(el).addClass('odd');										  
	});

	// Hover over for table rows
	$(".grid tr").each(function(index, el) {
								
		$(el).mouseover(function() {								
			$(el).addClass('hover');
		});

		$(el).mouseout(function() {								
			$(el).removeClass('hover');
		});
	});
						   
	// Select all / none for diamond shapes in search						   
	$('#shape_all').click(function() {
		if ($('#shape_all').attr('checked')) {
			$('#shape_all_label').text('Deselect all');
			$(".Shape").checkCheckboxes();
		} else {
			$('#shape_all_label').text('Select all');
			$(".Shape").unCheckCheckboxes();			
		}
	});
	
	// Apply tooltips for every link with class "tip"
	$('a.tip').tipsy({
		gravity: 'w',
		html: true,
		opacity: 0.7
	});
	
	// Ajax load of certificate within diamond_detail
	$('#load_cert').click(function(event) {
		event.preventDefault();
		diamond_id = $('#load_cert').attr('rev');
		img_link = '/cert/'+diamond_id+'.jpg';
		
		a = $('#load_cert');
		img = $('#cert_placeholder');
		
		$(img).attr('src', '/imgs/site/loading.gif');
		
		$.ajax({
			url: img_link,
			success: function(data) {
				img = $('#cert_placeholder');
				a = $('#load_cert');

				var newImg = new Image();
				$(newImg).attr('src', img_link);
				$(newImg).addClass('cert_loading');
				
				$(a).unbind();
				$(a).html($(newImg));				
				$(a).attr('href', img_link);
				$(a).attr('id', '');
				$(a).fancybox(fb_settings);
				
				$(a).click();
			}
		});
	});

	// Format price while typing
	$('.price_format').blur(function() {
		$(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: 0 });
	})
	.keyup(function(e) {
		var e = window.event || e;
		var keyUnicode = e.charCode || e.keyCode;
		if (e !== undefined) {
			switch (keyUnicode) {
				case 16: break; // Shift
				case 17: break; // Ctrl
				case 18: break; // Alt
				case 27: this.value = ''; break; // Esc: clear entry
				case 35: break; // End
				case 36: break; // Home
				case 37: break; // cursor left
				case 38: break; // cursor up
				case 39: break; // cursor right
				case 40: break; // cursor down
				case 78: break; // N (Opera 9.63+ maps the "." from the number key section to the "N" key too!) (See: http://unixpapa.com/js/key.html search for ". Del")
				case 110: break; // . number block (Opera 9.63+ maps the "." from the number block to the "N" key (78) !!!)
				case 190: break; // .
				default: $(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: 0 });
			}
		}
	})
	
	$("#enquiry_form").validate();		
});
