/***************************************************************
*  Copyright notice
*
*  (c) 2009 Stephan Schuler <stephan.schuler@netlogix.de>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*  A copy is found in the textfile GPL.txt and important notices to the license
*  from the author is found in LICENSE.txt distributed with these scripts.
*
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/




jQuery.event.special.nxformsync = {
	setup: function(data, namespaces) {
	var elem = this;
	},

	teardown: function(namespaces) {
	var elem = this;
	}
};






/**
 * Do some client side form processing when booking events
 *
 * $Id$
 */
var nxformsync = {




	/**
	 * Prefix of the id attribute of the source elements
	 */
	sourcePrefix: 'tx_nxgpmevents_data',




	/**
	 * Prefix of the id attribute of the target elements
	 */
	targetPrefix: 'tx_nxgpmevents_data_billing',




	/**
	 * Sync target and source elements? Change this value to enable or disable auto sync
	 */
	syncAddresses: true,




	/**
	 * Holds all syncable inputs to auto-sync all elements when the "syncAddress" checkbox changes to "yes"
	 */
	syncStack: [],




	/**
	 * Init script. Finds source and target inputs and initializes sync state
	 */
	init: function() {

		jQuery('.nx-tabform').each(function(id, form) {

			var subscribeAddress = jQuery(form).find('#tx-nxgpmevents-subscribe-address');
			var billingAddress = jQuery(form).find('#tx-nxgpmevents-subscribe-billing');
			if (subscribeAddress.length == 1 && billingAddress.length == 1) {
				nxformsync.findSyncableInputs(subscribeAddress, billingAddress);
				nxformsync.findCheckboxForSyncState(jQuery(form));
			}
		});

	},




	/**
	 * Looks for sync state changing checkbox
	 */
	findCheckboxForSyncState: function(form) {
		var checkbox = form.find('#tx_nxgpmevents_billingisaddress');
		if (checkbox.length == 1) {
			checkbox.click(function() {
				nxformsync.checkboxForSyncStateChanged(checkbox, form);
			});
			nxformsync.checkboxForSyncStateChanged(checkbox, form);
		}
	},




	/**
	 * OnChange event of the sync state changeing checkbox
	 */
	checkboxForSyncStateChanged: function(checkbox, form) {
		nxformsync.syncAddresses = checkbox.attr('checked');
		if (nxformsync.syncAddresses) {
			form.addClass('nx-gpmbuchungsformular-syncenabled');
			form.removeClass('nx-gpmbuchungsformular-syncdisabled');
			nxformsync.performCompleteSync();
		}
		else {
			form.addClass('nx-gpmbuchungsformular-syncdisabled');
			form.removeClass('nx-gpmbuchungsformular-syncenabled');
		}
	},




	/**
	 * Looks for syncable inputs and adds sync events
	 */
	findSyncableInputs: function(sources, targets) {

		nxformsync.syncStack = [];
		sources.find('input, select').each(function(key, sourceInput) {
			if (
				jQuery(sourceInput).attr('id')
				&&
				jQuery(sourceInput).attr('id').substr(0, nxformsync.sourcePrefix.length) == nxformsync.sourcePrefix
			) {
				var id = jQuery(sourceInput).attr('id').substr(nxformsync.sourcePrefix.length);
				if(id.substr(id.length-8) == '_private') {
					id = id.substr(0, id.length-8); // clearing of "_private" sufixes, database stuff
				}
				var sourceId = nxformsync.sourcePrefix+id;
				var targetId = nxformsync.targetPrefix+id;
				if (jQuery('#'+targetId).length == 1) {
					nxformsync.addSyncJob(jQuery(sourceInput), jQuery('#'+targetId));
				}
			}
		});

	},




	/**
	 * Adds a single {onChange=>sync} to a pair of source and target input fields
	 */
	addSyncJob: function(sourceInput, targetInput) {

		if (sourceInput.hasClass('nx-gpmbuchungsformular-syncjobadded')) {
			return false;
		}
		sourceInput.addClass('nx-gpmbuchungsformular-syncjobadded');

		if (targetInput[0].tagName != sourceInput[0].tagName) {
			return false;
		}

		nxformsync.syncStack.push({sourceInput:sourceInput, targetInput:targetInput});

		sourceInput.change(function() {
			nxformsync.performSync(sourceInput, targetInput);
		});

		targetInput.change(function() {
			nxformsync.performSync(targetInput, sourceInput);
		});

	},




	/**
	 * Auto-Syncs all input elements defined as being pairs
	 */
	performCompleteSync: function() {

		jQuery.each(nxformsync.syncStack, function(id, jobs) {
			nxformsync.performSync(jobs.sourceInput, jobs.targetInput);
		});
	},




	/**
	 * Syncs the values of a single pair of source and target element
	 */
	performSync: function(sourceInput, targetInput) {

		if (!nxformsync.syncAddresses) {
			return false;
		}

		var syncType = 'default';
		switch ((targetInput[0].tagName).toLowerCase()) {
			default:
				syncType = 'default';
				nxformsync.performSyncDefault(sourceInput, targetInput);
			break;
		}

		var syncEvent = new jQuery.Event('nxformsync');
		syncEvent.nxformsync = {
				sourceInput: sourceInput,
				targetInput: targetInput,
				syncType: syncType
		};
		targetInput.trigger(syncEvent);

	},




	/**
	 * The default action of syncing is to copy values. This will work for input and selects.
	 */
	performSyncDefault: function(sourceInput, targetInput) {

		targetInput.attr('value', sourceInput.attr('value'));

	}




};




jQuery(document).ready(function () {
	nxformsync.init();
});



