$(document).ready(function(){
 
	if ($.cookie('plateHideOptions') == 1) { $('.plateOptions .input, .plateOptions p').toggle(); }
	
	$('#tabs').tabs();
	
	$('.plateOptions legend').click(function(){
		$('.plateOptions .input, .plateOptions p').slideToggle();		
		
		if ($(this).children('.showArrow:first').attr('src') == '/img/down.gif') {
			$(this).children('.showArrow:first').attr('src', '/img/up.gif');
			$.cookie('plateHideOptions', 1, { expires: 999 });
		} else {
			$(this).children('.showArrow:first').attr('src', '/img/down.gif');	
			$.cookie('plateHideOptions', 0);			
		}
		
	}).css('cursor', 'pointer').append('<img src="/img/down.gif" alt="show" class="showArrow" />');
 
	$('table.plateTable a.deleteRow').click(function(){
		ele = $(this).parent().parent();
		$(ele).fadeOut('slow', function(){
			$(ele).remove();
			$('table.plateTable tr').removeClass('alt');
			$('table.plateTable tr:even').addClass('alt');
		});	
		
		return false;		
	});
	
	$('table.plateTable a.addNotes').click(function(){	
		ele = $(this).parent().parent();
		
		if ($(this).text() == 'Remove notes') {
			$(this).text('Add notes');
			
			$(ele).next('.notesRow').fadeOut('slow', function(){
				$(this).remove();
			});
			
			return false;
		}
		
		row = $(ele).attr('id');
		row = row.replace('row', '');
		newHTML = '<tr class="' + $(ele).attr('class') + ' notesRow"><td colspan="2" align="right" style="border-top: 1px solid #EEE;">Notes:</td><td colspan="6" style="border-right: 1px solid #666; border-top: 1px solid #EEE"><input type="text" name="data[Plate][' + row + '][notes]" style="width: 400px" /></td></tr>';
		$(ele).children('td').css('border-bottom-width', '0px');		
		$(ele).children('td:last').css('border-right', '1px solid #666');		
		
		$(this).text('Remove notes');
			
		$(ele).after(newHTML);		
		return false;		
	});
	
	$('table.plateTable .lippedInput, table.plateTable .motorcycleInput, table.plateTable .logoInput').change(function(){

		
		logo = '';
		lipped = false;
		motorcycle = false;
		
		row = $(this).parent().parent().parent();		
		
		lipped = ($(row).children().children().children('.lippedInput').attr('checked'));
		motorcycle = ($(row).children().children().children('.motorcycleInput').attr('checked'));
		logo = ($(row).children().children().children('.logoInput').val());

		image = '/img/plates/' + logo.toLowerCase() + '/';
		
		if (lipped && motorcycle) {
			image = image + 'motorcycle-lipped';								
		} else if (motorcycle && !lipped) {
			image = image + 'motorcycle';
		} else if (lipped && !motorcycle) {
			image = image + 'lipped';								
		} else {
			image = image + 'normal';								
		}
		
		image = image + '.gif';
		
		$(row).children().children('img').attr('src', image);

		//return false;
	});

});

