// define namespace
var bytepark = bytepark || {};

bytepark.move = function() {
	var that = {},
		targetFieldset,
		objOptions,
		oldText;
	
	/* public methods */
	that.init = function() {
		/* German initialisation for the jQuery UI date picker plugin. */
		/* Written by Milian Wolff (mail@milianw.de). */
		
		window.setTimeout(function() {
			$.datepicker.regional['de'] = {
				closeText: 'Schließen',
				prevText: '&#x3c; zurück',
				nextText: 'Vor &#x3e;',
				currentText: 'Heute',
				monthNames: ['Januar','Februar','März','April','Mai','Juni',
				'Juli','August','September','Oktober','November','Dezember'],
				monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
				'Jul','Aug','Sep','Okt','Nov','Dez'],
				dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
				dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
				dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
				weekHeader: 'Wo',
				dateFormat: 'dd.mm.yy',
				firstDay: 1,
				isRTL: false,
				showMonthAfterYear: false,
				yearSuffix: ''
			};
			$.datepicker.setDefaults($.datepicker.regional['de']);
			console.log(this);
			/* + config */
			$( ".datepicker" ).datepicker({
				showOn: "button",
				buttonImage: "files/site/images/lekker_datepicker.gif",
				buttonImageOnly: true,
				buttonText : 'Bitte Datum wählen',
				onClose : function() {
					$(this).trigger('focusout');
				},
				beforeShow : function() {
					window.setTimeout(function() {
						$('input.datepicker').removeClass('required validateLekkervalidatormovedate');					
					}, 50);
				}
			});
		}, 10);
	};
	
	/*
	 * toggles legend text of last fieldset
	 * @param obj - jQuery Object
	 * @param tg - jQuery Object
	 */
	that.toggleEnergyProduct = function(obj, tg) {
		targetFieldset = tg;
		objOptions = [];
		oldText = targetFieldset.find('legend').text();
		
		obj.find('option').each(function(i) {
			objOptions[i] = $(this).val();
		});
		
		obj.change(setEnergyProduct);
		if($.inArray(obj.find(':selected').val(), objOptions) !== 0) obj.trigger('change');
	};
	
	that.checkChoose = function(obj) {
		obj.click(toogleChoose);
		
		$(obj.filter(":checked")).trigger('click'); // the checked html if validaton error
	};
	
	that.checkReasonMessage = function(obj) {
		obj.click(toggleReasonMessage);
		$(obj.filter(":checked")).trigger('click'); // the checked html if validaton error
	};
	
	that.checkBillingAddress = function(obj) {
		useBillingAddress.call(obj); // needed for inital check 
		obj.click(useBillingAddress);
	};
		
	that.checkValue = function(obj1, obj2) {
		obj1.focusout(function() {
			if(!obj2.val() && obj1.val()) {
				obj2.val(obj1.val());
			}
		} );
		
	};
		
	/* private methods */
	/**
	 * Sets text of last fieldsets legend
	 * @param e - Event
	 */
	var setEnergyProduct = function(e) {
		var selected = $(e.target).find(':selected').val();
		
		if($.inArray(selected, objOptions) === 0) {
			targetFieldset.find('legend').text(oldText);
		} else {
			targetFieldset.find('legend').text('Möchten Sie auch zukünftig mit ' + objOptions[$.inArray(selected, objOptions)] + ' von lekker versorgt werden?');
		}
	};
	
	/**
	 * toogles the choose content
	 */
	var toogleChoose = function() {
		// hide 
		$('table.choose').addClass('hide');
		
		// (this.value) = this because of click function, html element. $(this) is the jquery object
		var t = $(this);  // we need the fresh html element
		t.parents().next('table').removeClass('hide');
		
		checkValidation.call($('input[name="move[move_at]"]'),'required', 'validateLekkervalidatormovedate');
	};
	
	/**
	 * toggles the message textarea
	 */
	var toggleReasonMessage = function() {
		var t = $(this);
		var textarea = t.parents().next('tr.textarea'); // textarea for user content

		if(t.val() == 'Sonstiges') {
			textarea.removeClass('hide');
		}
		else {
			// hide
			textarea.addClass('hide');
		}
	};
	
	/**
	 * shows the inputs if shipping address is not the billing address 
	 */
	var useBillingAddress = function() {
		var t = $(this);
		var shippingAddress = $('.shippingLikeBilling');
		
		if(t.is(':checked')) {
			shippingAddress.addClass('hide');
		}
		else {
			shippingAddress.removeClass('hide');
			
		}
		
		checkValidation.call($('input[name="move[shipping_street_name]"]'),'required');
		checkValidation.call($('input[name="move[shipping_street_number]"]'),'required');
		checkValidation.call($('input[name="move[shipping_postcode]"]'),'required','validateLekkervalidatorpostcode');
		checkValidation.call($('input[name="move[shipping_city]"]'),'required','validateLekkervalidatorcity');
	};
	
	/**
	 * checks if validation classes have to be added or removed
	 */
	var checkValidation = function(required,validator) {
		var t = $(this);
		
		// check if element/this is visible
		if(false == t.is(':visible')) {
			if(required) {
				t.removeClass(required);
			}
			if(validator) {
				t.removeClass(validator);
			}
		}
		else {
			if(required) {
				t.addClass(required);
			}
			if(validator) {
				t.addClass(validator);
			}
		}
	};
			
	return that;
}();

// jquery ready function
$(function() {	
	
	/* = config */
	bytepark.move.toggleEnergyProduct($('select[name="move[product_type]"]'), $('.toggle'));
	bytepark.move.checkChoose($('.choose_future'));
	bytepark.move.checkReasonMessage($('input[name="move[reason]"]'));
	bytepark.move.checkBillingAddress($('input[name="move[like_billing]"]'));
	bytepark.move.checkValue($('input[name="move[last_name]"]'), $('input[name="move[billing_last_name]"]'));
	bytepark.move.checkValue($('input[name="move[first_name]"]'), $('input[name="move[billing_first_name]"]'));
	bytepark.move.init();
});



