$(document).ready(function(){
	
	var t1 = $.template($("#tmpl-rounded-header").html())
	var t2 = $.template($("#tmpl-rounded").html())
	var t3 = $.template($("#tmpl-subrounded").html())
	
	$('.rounded').each(function(){
		if ($(this).hasClass("no-auto")) return;
		var header = $(this).find("> h4").html()
		var content = $(this).find("> ins").html()
		var template
		if (header) {
			template = t1
		} else {
			template = t2
			if (!content)
				content = $(this).html()
		}
		$(this).html(template, {
			header: header,
			content: content
		})
	})
	$('.subrounded').each(function(){
		var content = $(this).html()
		var template = t3
		$(this).html(template, {
			content: content
		})
	})
	$.fn.stdhover = function(){
		$(this).hover(function(){ $(this).addClass("hover") },
			function(){ $(this).removeClass("hover") })
		return this
	}
	$.fn.unstdhover = function() {
		$(this).unbind('hover')
		return this
	}
	$.fn.stdlivehover = function(){
		$(this).live('mouseenter', function(){ $(this).addClass("hover") })
		$(this).live('mouseleave', function(){ $(this).removeClass("hover") })
	}
	/* editable { */
	$.fn.myeditable = function(){return this.each(function(i, editable){
		$(editable).stdhover()
		$(editable).find("li").unstdhover().stdhover()
		$(editable).find("li:not(.adder)").live("click", function(){
			$(this).remove()
			var __items = new Array()
			$(editable).find("li:not(.adder)").each(function(){ __items.push($(this).html()); })
			$(editable).find("input[type=hidden]").val(__items.join(' '))
			$(editable).find("li").unstdhover().stdhover()
		})
		/*$(this).find("li.adder").bind('keypress', function(event){
			if (event.keyCode == '13') { $(this).click(); }
			else if (event.keyCode == '27') $(this).find("> input[type=text]").val("")
		})*/
		$(editable).find("li.adder").bind('click', function(event){
			if ($(this).find("input[type=text]").val() != '') {
				$(this).before("<li>" + $(this).find("> input[type=text]").val() + "</li>")
				$(this).find("input[type=text]").val("")
			}
			var __items = new Array()
			$(editable).find("li:not(.adder)").each(function(){ __items.push($(this).html()) })
			$(editable).find("input[type=hidden]").val(__items.join(' '))
			$(editable).find("li").unstdhover().stdhover()
		})
		var __items = new Array()
		$(editable).find("> li:not(.adder)").each(function(){ __items.push($(this).html()) })
		$(editable).find("input[type=hidden]").val(__items.join(' '))
	})}
	$("ul.editable").myeditable()
	/* } */
	

	$("#login-form a#login-open").click(function(){
		$(this).toggleClass("show")
		$("#login-form a#register-open").removeClass("show")
		if ($(this).hasClass("show")) {
			$("#login-form").addClass("opened")
			$("#login-form").animate({ height: '100px' }, "slow")
			$("#login-form #vertic_line").animate({ height: '78px' }, "slow")
			$("#login-form div.hd").hide()
			$("#login-form #login").fadeIn("slow")
		} else {
			$("#login-form").removeClass("opened")
			$("#login-form").animate({ height: '20px' }, "slow")
			$("#login-form #vertic_line").animate({ height: '19px' }, "slow")
			$("#login-form div.hd").fadeOut("fast")
		}
		
		return false
	})
	
	$("#login-form a#register-open").click(function(){
		$(this).toggleClass("show")
		$("#login-form a#login-open").removeClass("show")
		if ($(this).hasClass("show")) {
			$("#login-form").addClass("opened")
			$("#login-form").animate({ height: '100px' }, "slow")
			$("#login-form #vertic_line").animate({ height: '78px' }, "slow")
			$("#login-form div.hd").hide()
			$("#login-form #register").fadeIn("slow")
		} else {
			$("#login-form").removeClass("opened")
			$("#login-form").animate({ height: '20px' }, "slow")
			$("#login-form #vertic_line").animate({ height: '19px' }, "slow")
			$("#login-form div.hd").fadeOut("fast")
		}
		
		return false
	})
	
	$("#login-form a#plus").click(function(){
		$("#login-form").toggleClass("opened")
		$("#login-form a#login-open").removeClass("show")
		$("#login-form a#register-open").addClass("show")
		if ($("#login-form").hasClass("opened")) {
			$("#login-form div.hd").hide()
			$("#login-form").animate({ height: '100px' }, "slow")
			$("#login-form #vertic_line").animate({ height: '78px' }, "slow")
			$("#login-form #register").fadeIn("slow")
		} else {
			$("#login-form a#login-open, #login-form a#register-open").removeClass("show")
			$("#login-form").animate({ height: '20px' }, "slow")
			$("#login-form #vertic_line").animate({ height: '19px' }, "slow")
			$("#login-form div.hd").fadeOut("fast")
		}
		return false
	})
	
	$("#login-form a#forget").click(function(){
		$("#login-form div.hd").hide()
		$("#login-form div#forgotten-password").fadeIn("slow")
	})
	
	$("a").live('focusin', function(){ $(this).blur() })
	$("input, select, textarea").live('focusin', function(){
		$(this).removeClass("hover")
		$(this).addClass("focus")
	})
	$("input, select, textarea").live('focusout', function(){
		$(this).removeClass("focus")
	})
	$("input, select, textarea, span.menuButton").stdlivehover()
	$("ul, li").stdlivehover()
	$("div.subrounded div.rounded").stdlivehover()
	$("input.enterme").live("focusin", function(){
		$(this).removeClass("enterme")
		$(this).addClass("wasenterme")
		$(this).data("enterme", $(this).val())
		$(this).val("")
	})
	$("input.wasenterme").live("focusout", function(){
		if ($(this).val() == '') {
			$(this).removeClass("wasenterme")
			$(this).addClass("enterme")
			$(this).val($(this).data("enterme"))
		}
	})
	
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	})
	
	var cssFix = function(){
	  var u = navigator.userAgent.toLowerCase(),
	  is = function(t){return (u.indexOf(t)!=-1)};
	  $("html").addClass([
	    (!(/opera|webtv/i.test(u))&&/msie (\d)/.test(u))?('ie ie'+RegExp.$1)
	      :is('firefox/2')?'gecko ff2'
	      :is('firefox/3')?'gecko ff3'
	      :is('gecko/')?'gecko'
	      :is('opera/9')?'opera opera9':/opera (\d)/.test(u)?'opera opera'+RegExp.$1
	      :is('konqueror')?'konqueror'
	      :is('applewebkit/')?'webkit safari'
	      :is('mozilla/')?'gecko':'',
	    (is('x11')||is('linux'))?' linux'
	      :is('mac')?' mac'
	      :is('win')?' win':''
	  ].join(''));
	}();

	$("#login form").ajaxForm({
		beforeSubmit: function() {
			if ($("#login form .enterme").length > 0)
				return false
			else
				return true
		},
		success: function(response) {
			if (response.error) {
				alert(response.error)
			} else if (response.success) {
				window.location = response.uri
			} else {
				alert("Unknown error")
			}
		}
	})
	$("#login a.submit_").click(function(){ $("#login form").submit(); })
	$("#login input").keydown(function(event) {
		if (event.keyCode == '13') {
			event.preventDefault();
			$("#login form").submit();
		}
	})
	$("#forgotten-password a.submit_").click(function(){
		$("#forgotten-password form").ajaxSubmit({
			success: function(response) {
				if (response.error) {
					alert(response.error)
				} else if (response.success) {
					window.location = response.uri
				} else {
					alert("Unknown error")
				}
			}
		})
	})
	$("#register a.submit_").click(function(){
		$("#register form").submit()
	})
	$("a.confirm").click(function(){ return confirm('Sind Sie sicher?') })
	$(".green-button").hover(function(){ $(this).addClass("hover") }, function(){ $(this).removeClass("hover") })
	var $categoryList = $("div.category-list")
	if ($categoryList.hasClass("active")) {
		var computedHeight = $categoryList.outerHeight() +
			$("> ul > li.active > ul", $categoryList).outerHeight()
		$categoryList.css("height", computedHeight + "px")
	}

	var triggers = $(".modal-trigger").overlay({
		mask: {
			color: '#ebecff',
			loadSpeed: 200,
			opacity: 0.1
		},
		closeOnClick: false
	});
	
	$("a[title], img[alt], form *[title]").tooltip({
		position: "bottom center",
		effect: 'fade',
		fadeOutSpeed: 100,
		predelay: 400,
		opacity:0.9
	})
	
	$("form.email-subscribe-form").ajaxForm({
		beforeSubmit: function(formData, $jqForm, options){
			if ($("form.email-subscribe-form input.enterme").length > 0 || $("form.email-subscribe-form input.enterme").val() == '')
				return false
			$("form.email-subscribe-form input").addClass("loading")
			$("form.email-subscribe-form input").removeClass("error")
		},
		success: function(response){
			$("form.email-subscribe-form input").removeClass("loading")
			if (response.success)
				$("#big-subscribe-form, #block-gratis").addClass("subscribed")
			else {
				$("form.email-subscribe-form input[type=text]").addClass("error").addClass("enterme").removeClass("wasenterme")
				if (response.error)
					$("form.email-subscribe-form input").val(response.error)
				else
					alert('Unknown error')
			}
		}
	})
	$("form.email-subscribe-form .green-button").click(function(){
		$("form.email-subscribe-form").submit()
	})
	
	setTimeout(function(){
		$(".discontinued img").desaturate()
		var h1 = $("#main-column").height()
		var h2 = $("#right-column").height()
		if (h1 > h2) {
			$("#right-column #block-beliebtesten").css("height", (
				$("#right-column #block-beliebtesten").height() + (h1-h2-16)) + "px")
		} else if (h2 > h1) {
			$("#main-column > div:last .reducer:first").css("height", (
				$("#main-column > div:last .reducer:first").height() + (h2-h1+16)) + "px")
		}
	}, (($("html").hasClass("webkit")) ? 1042 : 42)) // magic
	
	setTimeout(function(){
		$(".categories-page .category-list ul").css("visibility", "visible")
	}, 1000)
	
	$(".code-trigger").hover(function(){
		$(this).data("unhover", $(this).html())
		$(">span>span>span", $(this)).html(
			$(this).parent().find("span.code").text())
	}, function(){
		if ($(this).data("unhover"))
			$(this).html($(this).data("unhover"))
		$(this).data("unhover", null)
	}).click(function(){
		$(">span>span>span", $(this)).html(
			$(this).parent().find("span.code").text())
		$(this).data("unhover", null)
	})
	
	$('#cats li').hover(
		function () {
			$('ul', this).fadeIn("fast")
		},
		function () {
			$('ul', this).fadeOut("fast")
		}
	)
	
	if ($("#falseDue:checkbox:checked").length > 0) {
		$("#due_day, #due_month, #due_year").attr("disabled", "disabled")
	} else {
		$("#due_day, #due_month, #due_year").removeAttr("disabled")
	}
	$("#falseDue:checkbox").click(function(){
		if ($("#falseDue:checkbox:checked").length > 0) {
			$("#due_day, #due_month, #due_year").attr("disabled", "disabled")
		} else {
			$("#due_day, #due_month, #due_year").removeAttr("disabled")
		}
	})
	
	$(".examples-company a").click(function(){
			$("input[name=q]").removeClass('enterme').val($(this).text())
			return false
	})
	$(".examples-town a").click(function(){
			$("input[name=ql]").removeClass('enterme').val($(this).text())
			return false
	})
	
	$("a.submit").click(function(){
			var $form = $(this).parent()
			while ($form) {
				if ($form.find("form").length > 0) {
						$form = $form.find("form")
						$form.submit()
						return false
				}
				$form = $form.parent()
			}
			
			return false
	})
})
