// JavaScript Document

	$(function() {
			   /*
		$('.alphabetlinks a').bind('click', function() {
									var letter = $(this).html();
									$('.alphabetlinks a').removeClass('active');
									$(this).addClass('active');
									$('.alphabetdiv').hide();
									$('#letter_'+letter).show();													
									return false;
								});		
			*/		
		$('#history h2[rel]').bind('click', function() {										  
			var id = $(this).attr('rel');
			if ($('#'+id+':visible').length == 0) {
				$('.order-details').slideUp();				
				$('#'+id).slideToggle();
			}
			else {
				$('.order-details').slideUp();				
			}
		});
		
		// Hide/show invoice fields
		if ($('#on_invoice_1:checked').length == 0) {
			$('#invoice_fields').hide();
		}
		
		$('#on_invoice_1').bind('change', function() {
			if ($('#on_invoice_1:checked').length == 0) {
				$('#invoice_fields').slideUp();
			}
			else {
				$('#invoice_fields').slideDown();
			}
		});
		
		// If we are in the address book, bind some functions to select boxes.
		path_array = window.location.pathname.split("/");
		if (path_array[2] == 'addressbook') {
			if (path_array[3] == 'new') {
				$('select[name="country_id"]').bind('change', switchCountryNew);
				
				$('select[name="state"]').bind('change', function() {
					$('input[name="state"]').val($('select[name="state"] option:selected').text());
				});
				
				// Also run country switch on page load
				switchCountryNew();
			}
			else {
				$('form[id^="address"] select[name="country_id"]').bind('change', switchCountryEdit);
				
				$('form[id^="address"] select[name="state"]').bind('change', function(event) {
					container_id = '#' + $('#' + event.target.id).parent().parent().attr('id');
					$(container_id + ' input[name="state"]').val($(container_id + ' select[name="state"] option:selected').text());
				});
			}
		}
	});
	
	function switchCountryNew() {
		state_ctl_select = $('select[name="state"]');
		state_row_select = state_ctl_select.parent();
		state_ctl_text   = $('input[name="state"]');
		state_row_text   = state_ctl_text.parent();
		
		// If country == 230 (USA), display select and hide text box
		if ($('select[name="country_id"] option:selected').val() == 230) {
			state_ctl_text.val('');
			state_row_select.show();
			state_row_text.hide();
			state_ctl_select.trigger('change');
		}
		else {
			state_ctl_text.val('');
			state_row_select.hide();
			state_row_text.show();
		}
	}
	
	function switchCountryEdit(event) {
		container_id = '#' + $('#' + event.target.id).parent().parent().attr('id');
		country_ctl_select = $(container_id + ' select[name="country_id"]');
		state_ctl_select   = $(container_id + ' select[name="state"]');
		state_row_select   = state_ctl_select.parent();
		state_ctl_text     = $(container_id + ' input[name="state"]');
		state_row_text     = state_ctl_text.parent();
		
		// If country == 230 (USA), display select and hide text box
		if ($(container_id + ' select[name="country_id"] option:selected').val() == 230) {
			state_ctl_text.val('');
			state_row_select.show();
			state_row_text.hide();
			state_ctl_select.trigger('change');
		}
		else {
			state_ctl_text.val('');
			state_row_select.hide();
			state_row_text.show();
		}
	}
