 /**
 * Form-Styling
 * 
 * 
 * @projectDescription	Form-Styling
 * @namespace			audi_ngw.form.*
 *
 * @author 				$Author: smueller $
 * @version				$Revision: 1 $
 * @copyright			NEUE DIGITALE GmbH, Berlin
 * 
 * @jslint: 2008-10-31
 * 
 * @file:				audi_ngw.form.js
 * $URL: https://svn.pvtool.org/svn/day_audi_ngw/trunk/ngw_base/frontend/js/audi/audi.form.js $
 */



/* create namespace */
audi_ngw.namespace(audi_ngw,'form');
 
audi_ngw.form.tabOrderCounter = 0;
 
 /**
  * Wrapper for custom events, 
  *  decreases number of registered events and fires oncomplete event 
  *  which checks for registered events == 0 before running oncomplete actions
  * @param {String} sEvent
  * @return {Void}
  */
audi_ngw.form.initiate = function ($scope) {
	audi_ngw.form.styleFormUploads($scope);
	audi_ngw.form.initErrors($scope);
	audi_ngw.form.initDisabledForms($scope);
	audi_ngw.form.initTabOrder($scope);
};

audi_ngw.form.initTabOrder = function($scope) {
	jQuery('form').each(function() {
		var $form = jQuery(this);
		jQuery('input, textarea, select, button', $form).each(function() {
			audi_ngw.form.tabOrderCounter++;
			jQuery(this).attr('tabindex', audi_ngw.form.tabOrderCounter);
		});
	});
}

audi_ngw.form.initDisabledForms = function($scope) {
	jQuery('form.disabled_until_changed', $scope).each(function() {
		var $form = jQuery(this);
		$form.attr('predata', jQuery.md5($form.serialize()));
		$form.bind('submit.isDisabled', function() {
			$form = jQuery(this);
			var $predata = $form.attr('predata');
			var $postdata = jQuery.md5($form.serialize());
			if ($form.attr('predata') && (jQuery.md5($form.serialize()) != $form.attr('predata'))) {
				return true;
			} else {
				return false;
			}
		});
		$form.bind('form.checkDisabled', function() {
			$form = jQuery(this);
			if ($form.attr('predata') && (jQuery.md5($form.serialize()) != $form.attr('predata'))) {
				jQuery('button', $form).removeClass('disabled');
			} else {
				jQuery('button', $form).addClass('disabled');
			}
		});
		jQuery('input, textarea', $scope).bind('keyup', audi_ngw.form.checkFormDisabled);
		jQuery('input, textarea', $scope).bind('keydown', audi_ngw.form.checkFormDisabled);
		jQuery('input, textarea', $scope).bind('blur', audi_ngw.form.checkFormDisabled);
		jQuery('input, textarea', $scope).bind('focus', audi_ngw.form.checkFormDisabled);
		jQuery('input, textarea', $scope).bind('click', audi_ngw.form.checkFormDisabled);
	});
};

audi_ngw.form.checkFormDisabled = function() {
	$elm = jQuery(this);
	$form = $elm.parents('form');
	$form.trigger('form.checkDisabled');
};

audi_ngw.form.initErrors = function($scope) {
	jQuery('form.template-c-3 div.error ul.error', $scope).each(function() {
		var err = jQuery(this);
		var div = err.parent('.row');
		if ((div.length === 1) && (err.height() > div.height())) {
			div.height(err.height());
		}
	});
};

audi_ngw.form.styleFormUploads = function ($scope) {
	jQuery('div.fileupload').addClass('fileuploadjs').each(function() {
		var div = jQuery(this);
		var upload = div.find(':file');
		upload.change(function() {
			_file = upload.attr('value');
			div.find('span.fileupload').text(_file);
		});	
	});
};

audi_ngw.form.onAjaxSnippetReady = function(event, data) {
	var $scope = jQuery(data.sId);
	audi_ngw.form.initiate($scope);
	audi_ngw.event.trigger('ajaxSnippetPrepared');
};

audi_ngw.form.onAjaxModalContentReady = function(event, data) {
	var $scope = jQuery(data.sId);
	audi_ngw.form.initiate($scope);
	audi_ngw.event.trigger('ajaxModalContentPrepared');
};

audi_ngw.form.prepare = function() {
	audi_ngw.event.bind('ajaxSnippetReady',audi_ngw.form.onAjaxSnippetReady);
	audi_ngw.event.bind('ajaxModalContentReady.formUpdater', audi_ngw.form.onAjaxModalContentReady);
	audi_ngw.form.initiate(jQuery('body'));
};

