var ContentCustomerCenter = {
		setAddressData: function(data) {
			for (var prop in data) {
				$('#add_address_form :input[name="' + prop + '"]').val(data[prop]);
			}
		},
		
		clearAddressData: function() {
			$('#add_address_form :input[name="id"]').val('');
			$('#add_address_form :input[name="company"]').val('');
			$('#add_address_form :input[name="street"]').val('');
			$('#add_address_form :input[name="house_number"]').val('');
			$('#add_address_form :input[name="zip_code"]').val('');
			$('#add_address_form :input[name="city"]').val('');
		}
}

$(document).ready(function() {
	$('h2 > a, .title_wrapper a').click(function(e) {
		var boxname;
		
		e.preventDefault();
		
		boxname = $(this).attr('href');
		if (boxname == "#add_address_form") {
			ContentCustomerCenter.clearAddressData();
		}
		
		$($(this).attr('href')).slideToggle();
	});
	
	
	$('.set_standard').click(function(e) {
		e.preventDefault();
		
		var jqxhr = $.ajax({
			url: $(this).attr('href'),
			type: 'POST'
		});
		
		jqxhr.success(function(data) {
			try {
				data = $.parseJSON(data);
				
				if (data.success) {
					window.location.reload();
				} else {
					alert('ERROR: DATABASE')
				}
			} catch(e) {
				alert("ERROR: JSON");
			}
		});
		
		jqxhr.error(function() {
			alert("ERROR: URL")
		});
	});
	
	$('.edit_address').click(function(e) {
		var jqxhr;
		
		e.preventDefault();
		
		jqxhr = $.ajax({
			url: $(this).attr('href')
		});
		
		jqxhr.success(function(data) {
			var mydata,
				addressdata,
				showform;
			
			try {
				mydata = $.parseJSON(data);
				
				if (mydata.status == 'success') {
					addressdata = mydata.data;
					
					showform = $('#add_address_form').css('display');
					
					if (showform == 'none') {
						$('#add_address_form').slideToggle();
						ContentCustomerCenter.setAddressData(addressdata);
					} else {
						ContentCustomerCenter.setAddressData(addressdata);
					}
				} else {
					alert('ERROR: DATABASE');
				}
			} catch(e) {
				alert('ERROR: JSON');
			}
		});
		
		jqxhr.error(function() {
			alert("ERROR: URL");
		});
	});
	
	$('.delete_address').click(function(e) {
		e.preventDefault();
		
		var jqxhr = $.ajax({
			url: $(this).attr('href'),
			type: 'POST'
		});
		
		jqxhr.success(function(data) {
			try {
				data = $.parseJSON(data);
				
				if (data.success) {
					window.location.reload();
				} else {
					alert('ERROR: DATABASE')
				}
			} catch(e) {
				alert("ERROR: JSON");
			}
		});
		
		jqxhr.error(function() {
			alert("ERROR: URL")
		});
	});
	
	/* hover address-boxes START */
//	$('.address').unbind('mouseenter').bind('mouseenter', function() {
//		$(this).stop().animate({
//			'backgroundColor': '#FFFFFF'
//		}, 175);
//	});
//
//	$('.address').unbind('mouseleave').bind('mouseleave', function() {
//		$(this).stop().animate({
//			'backgroundColor': '#DFDFDF'
//		}, 175);
//	});
	/* hover address-boxes START END */
	
	$('#add_address_form').submit(function(e){
		e.preventDefault();
		
		var jqxhr = $.ajax({
			url: $(this).attr('target'),
			type: 'POST',
			data: $(this).serialize()
		});
		
		jqxhr.success(function(data) {
			window.location.reload();
		});
	});
	
	$('#edit_base_data_form').submit(function(e){
		e.preventDefault();
		
		var jqxhr = $.ajax({
			url: $(this).attr('target'),
			type: 'POST',
			data: $(this).serialize()
		});
		
		jqxhr.success(function(data) {
			window.location.reload();
		});
	});
	
	$('#edit_payment_data_form').submit(function(e) {
		e.preventDefault();
		
		var jqxhr = $.ajax({
			url: $(this).attr('target'),
			type: 'POST',
			data: $(this).serialize()
		});
		
		jqxhr.success(function(data) {
			window.location.reload();
		});
	});
	
	/***********
	 * Show or hide all addresses
	 */
	var addressesBoxHeight = $('#addressesContainer').innerHeight();
	var addressesBoxHeightMin = 150;
	var addressesClosedText = $('#showAllAddressesTrigger').html();
	$('#addressesContainer').css({
		overflow: 'hidden',
		height: addressesBoxHeightMin + 'px'
	});
	$('#showAllAddressesTrigger').click(function(e) {
		e.preventDefault();
		
		if ($('#addressesContainer').hasClass('isOpen')) {
			$('#addressesContainer').stop().animate({
				height: addressesBoxHeightMin + 'px'
			});
			
			$('#addressesContainer').removeClass('isOpen');
			$('#showAllAddressesTrigger').html(addressesClosedText);
		} else {
			$('#addressesContainer').stop().animate({
				height: addressesBoxHeight + 'px'
			});
			
			$('#addressesContainer').addClass('isOpen');
			$('#showAllAddressesTrigger').html('Adressliste schlie&szlig;en');
		}
	});
	
	/**************
	 * Load order details
	 */
	var lastOpenOrderDetails = 0;
	$('.getOrderDetailsTrigger').click(function(e) {
		e.preventDefault();
		
		var orderDetailsRes = $(this).attr('href');
		var parent = $(this).parents('tr');

		$('#content_customer_center_order_table .orderDetails').remove();
		$('#content_customer_center_order_table .isActive').removeClass('isActive');
		
		if (lastOpenOrderDetails !== $(this).attr('rel')) {
			$.get(orderDetailsRes, function(data) {
				$(data).attr({
					rel: orderDetailsRes
				});
				$(data).insertAfter(parent);
			});

			$(parent).addClass('isActive');

			lastOpenOrderDetails = $(this).attr('rel');
		} else {
			lastOpenOrderDetails = 0;
		}
	});
	
});
