/**********************/
/* Behaviour for MBFR */
/**********************/


/***************************************************************/
/* PLUGIN: setDefaultFocus                                     */
/* focuses the input/select/textarea with the highest priority */
/***************************************************************/

$.fn.setDefaultFocus = function() {
	return this.each(function() {
		var result = null;
		// Determine first not disabled form input element
		$("input:enabled:visible, textarea:enabled:visible, select:enabled:visible", this).eq(0).each(function() {
			result = $(this);
		});
		// Search for focus marked elements
		$("input.focus:enabled:visible, textarea.focus:enabled:visible, select.focus:enabled:visible, div.focus>input:enabled:visible, div.focus>textarea:enabled:visible, div.focus>select:enabled:visible", this).eq(0).each(function() {
			result = $(this);
		});
		// Search for error marked elements
		$("input.error:enabled:visible, textarea.error:enabled:visible, select.error:enabled:visible", this).eq(0).each(function() {
			result = $(this);
		});
		// Set the focus and select content
		if(result != null) {
			result.focus();
			result.select();
		}		
	});
};



/*******************/
/* MBFR Util Class */
/*******************/

var mbfr = {

	removeValidationRules: function(formnode) {
		if($(formnode).validate) {
			$('input, select, textarea', formnode).each(function() {
				if($(this).rules) {
					$(this).rules("remove");
				}
			});
		}
	},

	resetSelection: function() {
		if(document.selection) {
			document.selection.empty();
		} else {
			window.getSelection().removeAllRanges();
		}
	},

	isLength: function(v, l) {
		return ($.trim(v).length == l);
	},

	isNumeric: function(v) {
		var regex = /^\d+$/;
		return regex.test(v);
	},

	isDate: function(y, m, d) {
		y = parseInt(y, 10);
		m = parseInt(m, 10);
		d = parseInt(d, 10);
		var c = new Date(y, m - 1, d);
		var yt = c.getFullYear();
		var mt = c.getMonth() + 1;
		var dt = c.getDate();
		return (y == yt && m == mt && d == dt);
	}

};


/*******************************/
/* Wait until page has loaded! */
/*******************************/
$(document).ready(function() {


/**************************/
/* Login Anywhere Handler */
/**************************/
$('#mbfr-login').click(function(e) {
	e.stopPropagation();
	e.preventDefault();
	var dlg = $('<div id="login-dialog" title="MBFR Login">'
		+ '<form id="login-form" action="/smf/index.php?action=login2" method="post">'
		+ '<label for="login-form-user">Benutzer:</label>'
		+ '<input id="login-form-user" type="text" name="user" size="20" value="" /><br />'
		+ '<label for="login-form-password">Passwort:</label>'
		+ '<input id="login-form-password" type="password" name="passwrd" size="20" value="" /><br />'
		+ '<label for="login-form-length">Dauer:</label>'
		+ '<select id="login-form-length" name="cookielength">'
		+ '<option value="60">1 Stunde</option>'
		+ '<option value="1440">1 Tag</option>'
		+ '<option value="10080">1 Woche</option>'
		+ '<option value="43200">1 Monat</option>'
		+ '<option value="-1" selected="selected">Unbegrenzt</option>'
		+ '</select><br />'
		+ '<input type="hidden" name="hash_passwrd" value="nohash" />'
		+ '</form>'
		+ '</div>');
	$('input, select', dlg)
		.focus(function() {
			$(this).addClass('focus');
		})
		.blur(function() {
			$(this).removeClass('focus');
		});
	$('input', dlg)
		.keydown(function(e) {
			if(e.which == 13) {
				$('#login-form').submit();
			}
		});
	$('form', dlg)
		.submit(function() {
			var frm = $('#login-form').get(0);
			hashLoginPassword(frm, smf_session_id);
			return true;
		});
	$('option:even', dlg)
		.css('background-color', '#f6f6f6');
	$(dlg).dialog({
		dialogClass: 'mbfr-login-dialog',
		autoOpen: true,
		modal: true,
		resizable: false,
		buttons: {
			'Einloggen': function() {
				$('#login-form').submit();
				//$(this).dialog('close');
			}

		}
	});
	$('#login-form-user').focus().select();
});


/*************************/
/* Flowplayer for Videos */
/*************************/
$("#videoplayer").flowplayer("/swf/flowplayer-3.1.5.swf", {
	clip: {
		autoPlay: true,
		autoBuffering: true
	},
	canvas:  {
		background: '#000000',
		backgroundGradient: 'none'
	},
	plugins: {
		controls: {
			all: false,
			play: true,
			scrubber: true,
			time: true,
			backgroundColor: '#000000',
			backgroundGradient: 'none',
			timeColor: '#DDF0DC',
			durationColor: '#DDF0DC',
			timeBgColor: '#154305',
			sliderColor: '#154305',
			progressColor: '#5A934C',
			bufferColor: '#1D5B07',
			buttonColor: '#317721',
			buttonOverColor: '#5A934C',
			volumeSliderColor: '#1D5B07',
			tooltipColor: '#1D5B07'
		}
	}
});


/***********************************/
/* Form Element Focus Highlighting */
/***********************************/

$("input[type='text'], input[type='password'], textarea, select").focus(
	function() {
		$(this).addClass("focused");
	}).blur(
	function() {
		$(this).removeClass("focused");
	}
);

$("form").submit(
	function() {
		$("input[type='text'], input[type='password'], textarea, select").each(
			function() {
				$(this).removeClass("focused");
			}
		);
	}
);


/*********************************************************/
/* Set Validation Framework Defaults  & Validation Rules */
/*********************************************************/

$.validator.setDefaults({
	errorElement: "p",
	errorClass: "client-error"
});

$.validator.addMethod("mbfr_date",
	function(v, e) {
		if(mbfr.isLength(v, 0)) return true;
		if(!mbfr.isLength(v, 10)) return false;
		if(v.substr(2, 1) != '.' || v.substr(5, 1) != '.') return false;
		var d = v.substr(0,2);
		var m = v.substr(3,2);
		var y = v.substr(6,4);
		if(!mbfr.isNumeric(d) || !mbfr.isNumeric(m) || !mbfr.isNumeric(y)) return false;
		return mbfr.isDate(y, m, d);
	},
	"Bitte gib ein Datum im Format TT.MM.JJJJ ein."
);


/***************************************************/
/* Form Validation and Handlers for mitglied_d.php */
/***************************************************/

// New Security Code Button
$('#mitgliedsantrag #newcode').click(function() {
	var pform = $(this).parents("form").get(0);
	mbfr.removeValidationRules(pform);
	$('#mitgliedsantrag #action').val('new');
	$('#mitgliedsantrag').submit();
});

// Reset Button
$('#mitgliedsantrag input[type=reset]').click(function() {
	$("#mitgliedsantrag").setDefaultFocus().validate().resetForm();
});

// Form Focus and Validation
$("#mitgliedsantrag").setDefaultFocus().validate({
    rules: {
        vorname: {
            required: true
        },
        name: {
            required: true
        },
        gebdatum: {
            required: true,
            mbfr_date: true
        },
        strassenr: {
            required: true
        },
        plz: {
            required: true,
            digits: true
        },
        ort: {
            required: true
        },
        mail: {
            required: true,
            email: true
        },
        telefon: {
            required: true
        },
        mitteilung: {
            required: true
        },
        secCode: {
            required: true,
            digits: true,
            rangelength: [5,5]
        }
    },
    messages: {
    	secCode: 'Bitte gib den angezeigten Zahlencode ein.'
    }
});


/*********************/
/* End of page ready */
/*********************/
});


