var showloginpopup;



$(document).ready(function() {
  
  // channel-form change-handler
  $('#channel_selection select').change(function() {
    if ($('#channel_selection select').val() != '0') {
      $('#channel_selection').attr('action', $('#channel_selection').attr('action') + '/' + $('#channel_selection select').val());
      $('#channel_selection').submit();
    }
  });
  
  
	$("#loginemail").inputLabel("E-Mail-Adresse");
	$("#loginpassword").inputLabel("*******");
	
	
	$("#loginform").ajaxForm({
		url:		'/login',
		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,
			title:		"Best\u00e4tigen",
			width:		460,
			buttons:	{
				Ja:	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,
			title:		"Best\u00e4tigen",
			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;
	});
	
	$(".Zend_Tag_Cloud li a").bind("click", function(e) {
		e.preventDefault();
		$("#what").val($(this).html());
		$("#what").effect('highlight', {color: '#E3001A'}, 500);
	});

	$("a.homeboxlink").bind("click", function(e) {
		e.preventDefault();
		$("#h_home_box").toggle('blind');
	});
	

	
	$(".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();
			});
		});
	});
	
	watchInput();
//	rotatePartner();
});


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 limitInput() {
	var maxlength = parseInt($(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 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;
		});
	});
}

function rotatePartner() 
{
	var $container = $("#h_partner .partnerlogos");
	var width	   = 121;//$container.find("a:first").outerWidth();
	var interval   = 50;
	var offset	   = -1;
	
	var timer = function() 
	{
		var current = parseInt($container.css("left").replace('px', ''));
		
		if(current <= -width) {
			$container.find("a:first").appendTo($container);
			$container.css("left", offset);
		} else {
			$container.css("left", current + offset);
		}
		
		window.setTimeout(timer, interval);
	}
	
	timer();
}


function BookmarkLink() {

	 var Title = document.title; 
	 var Url = 'http://www.handwerkerfinden.com/';
	 if (window.sidebar) {  // FF
		window.sidebar.addPanel(Title, Url, "");
	} else if( window.external ) { // IE 
		window.external.AddFavorite(Url, Title); }
	else {
	    alert("Dr�cken Sie Strg+D um einen neuen Bookmark-Eintrag zu erstellen"); 
	}
 }

