var showloginpopup;
var formdiagsubmit;


$(document).ready(function() {
	$("#loginemail").inputLabel("E-Mail-Adresse");
	$("#loginpassword").inputLabel("*******");
	
	
	$("#loginform").ajaxForm({
		url:		'/loginajax',
		cache:		false,
		async:		false,
		dataType: 	'script',
		type:		'POST',
		success:	function() {
			if(showloginpopup) {
				$("#loginerrors").infopopup({
					target: "#loginbox",
					hoverlap: 720,
					overlap:  70
				});
				$(":input").each(function() {
					$(this).blur();
				});
			}
		}	
	});
	
	$("a.confirmDiag").bind("click", function(e) {
		e.preventDefault();
		
		var $a = $(this);
		var $div = $(document.createElement('div'));
		$div.html($a.attr("title"));
		$div.appendTo("body");
		$div.dialog({
			modal:		true,
			autoOpen:	true,
			width:		460,
			buttons:	{
				Ok:	function() {
					location.href = $a.attr('href');
				},
				Abbrechen: function() {
					$(this).dialog('detroy');
					$(this).remove(); 
				}
			}
		});
	});
	
	$("form.confirmDiag").bind("submit", function(e) {
		var $form = $(this);
		
		if($form[0].confirmed) {
			return true;
		}
		
		e.preventDefault();
		var $div = $(document.createElement('div'));
		$div.html($form.attr("title"));
		$div.appendTo("body");
		$div.dialog({
			modal:		true,
			autoOpen:	true,
			width:		460,
			buttons:	{
				Ok:	function() {
					$form[0].confirmed = true;
					$("input[type='submit']", $form).click();
				},
				Abbrechen: function() {
					$(this).dialog('detroy');
					$(this).remove(); 
				}
			}
		});
		
		return false;
	});
	
	
	$("#recommend").bind("click", function(e) {
		e.preventDefault();
		
		var $div = $(document.createElement("div"));
		$div.attr("id", "recommendDiag");
		$div.appendTo("body");
		
		var loadFunc = function() {
			$($div, ".zend_form").jqTransform();
			$("#"+$div.attr("id") + " form").ajaxForm({
				dataType:	'json',
				success:	function(data) {
					if(data.error) {
						alert(data.error);
					} else if(data.success) {
						$div.dialog('destroy');
						$div.remove();
					}
				}
			});
		};
		
		
		$div.dialog({
			bgiframe:	true,
			resizable: 	false,
			height:		520,
			width:		580,
			modal:		true,
			autoOpen:	true,
			draggable: 	false,
			title:		'Diese Seite weiterempfehlen',
			buttons: {
				Abbrechen: function() {
					$(this).dialog('destroy');
					$(this).remove();
				}
			}
		});
		
		$div.load($(this).attr("href"), loadFunc);
	});
	
	watchInput();

    	$(".entryHelp").each(function() {
		var title = $(this).attr("title");
		$(this).attr("title", "");

		$(this).bind("mouseover", function(e) {
			e.stopPropagation();

			var $tip = $("<div class='entryHelpTip ui-corner-all'></div>");
			$tip.html(title);

			var offset = $(this).offset();
			$tip.css("left", offset.left + $(this).outerWidth() + 10);
			$tip.css("top", offset.top + $(this).outerHeight() + 10);

			$tip.appendTo($("body"));

			$(this).bind("mouseout", function(e) {
				e.stopPropagation();
				$(this).unbind("mouseout");
				$tip.remove();
			});
		});
	});

});

function limitInput() {
	var maxlength = $(this).attr("maxlength");
	var val		  = $(this).val();
	
	if(maxlength > 0 && val.length > maxlength) {
		$(this).val(val.substr(0, maxlength));
	};
}

function watchInput() {
	$("textarea,input").unbind("keydown", limitInput)
					   .bind("keydown", limitInput);
}

function showAjaxErrors(errors) {
	$div = $(document.createElement('div'));
	$div.html('<p>Es sind folgende Fehler aufgetreten:</p>'+errors);
	$div.appendTo("body");
	
	$div.dialog({
		bgiframe:	true,
		resizable: 	false,
		width:		580,
		modal:		true,
		autoOpen:	true,
		draggable: 	false,
		title:		"Bitte Eingaben \u00fcberpr\u00fcfen",
		buttons: {
			Ok: function() {
				$(this).dialog('destroy');
				$(this).remove();
			}
		}
	});
}

function contactBtn(e) {
	e.preventDefault();

	var $div = $(document.createElement('div'));
	var $a   = $(this);
	
	$div.appendTo("body");
	
	$div.dialog({
		bgiframe:	true,
		resizable: 	false,
		width:		580,
		modal:		true,
		autoOpen:	true,
		draggable: 	false,
		title:		"Kontaktformular",
		buttons: {
			Fertig: function() {
				$(this).dialog('destroy');
				$(this).remove();
			}
		}
	});
	
	$div.load($a.attr("href"), function() {
		$(".zend_form,.jq_form").jqTransform();
		
		var $form = $("form", $div);
		
		$form.bind("submit", function(e) {
			e.preventDefault();
			
			$form.ajaxSubmit({
				url: 		$a.attr("href") + '&validate=1',
				dataType: 	'json',
				success: 	function(data) {
					if(data.errors) {
						showAjaxErrors(data.errors);
					} else {
						$form.unbind("submit");
						$form.ajaxSubmit({
							target:	$div
						});
					} 
				}
			});
			
			return false;
		});
	});
}

