$(document).ready(function() {
	/* Use sliced images and nested div trickery if browser
	 * doesn't support CSS3 */
	if(!Modernizr.borderradius || !Modernizr.boxshadow) {
		$(".decoratedbox").decorateboxes();

		/* apply decorations to internal h2 elements (not the main header) */
		$(".decoratedheaders h2").each(function() {
			if($(this).parent().is(":not(.decorboxheader)")) {
				$(this)
					.wrap('<div class="decorboxheader"></div>')
					.before('<div class="leftcap"></div>')
					.after('<div class="rightcap"></div>');
			}
		});
	}

	/* emulate placeholders for older browsers */
	if(!Modernizr.input.placeholder) {
	$("input[placeholder],textarea[placeholder]")
		.blur(function() {
			if($(this).val()=="") {
				$(this).val($(this).attr("placeholder")).addClass("placeholder");
			}
		})
		.focus(function() {
			if($(this).val()==$(this).attr("placeholder")) {
				$(this).val("").removeClass("placeholder");
			}
		}).blur();
	}

	/* Deobfuscate addresses */
	$("a[href^=mailto:]").each(function() {
		var addr = $(this).attr("href").substring(7);
		var a = addr.replace(/ \[ät\] /g, "@").replace(/ \[piste\] /g, ".");
		$(this).attr("href", "mailto:" + a);
		if($(this).text()==addr) { $(this).text(a); }
	});

	/* Rich tooltips */
	$("[title].richtip")
		.mouseover(function(ev) {
			var tip = $("#tooltip");
			if(tip.size()==0) {
				tip = $('<div id="tooltip" class="decoratedbox tooltipbox"><div class="tooltipboxarrow"></div><div id="tooltipbody"></div></div>');
				if(!Modernizr.borderradius || !Modernizr.boxshadow) {
					tip.decorateboxes();
				}
				$("body").append(tip);
			}
			tip.css({left: ev.pageX-30, top: ev.pageY+35});
			$("#tooltipbody").html($(this).data("richtip"));
			tip.stop(true,true).fadeIn('fast');
		}).mousemove(function(ev) {
			$("#tooltip").css({left: ev.pageX-10, top: ev.pageY+25});
		}).mouseout(function() {
			$("#tooltip").stop(true,true).fadeOut('fast');
		}).each(function() {
			$(this).data("richtip", $(this).attr("title")).attr("title","");
		});

	/* AJAX contact forms */
	$("form.contactform").submit(function() {
		var f = $(this);
		var action = f.attr('action');
		var params = f.serialize();

		$.post(action, params + '&_json', function(data) {
			f.hide('fast', function() {
				f.replaceWith('<p>' + data.message + '</p>');
			});

			}, 'json');

		f.css('opacity', '0.5');
		$('<div class="ajaxprogress"></div>').css({
			width: f.width() + 10,
			height: f.height() + 10,
			top: f.position().top - 5,
			left: f.position().left - 5
			}).appendTo(f);

		if(typeof _gaq !== 'undefined') {
			var path = "/lomake" +
				action.substring(action.lastIndexOf('/')) +
				window.location.pathname;
			_gaq.push(['_trackPageview', path]);
		}

		return false;
	});

	// Prevent submission of empty forms
	$(":submit.noempty").click(function() {
		var inputs = $(this).closest("form").find("input[type=text],textarea");
		var ok = true;
		inputs.each(function() {
			if($(this).val()==="" || $(this).val() === $(this).attr("placeholder")) {
				ok = false;
				$(this)
					.attr("data-oldborder", $(this).css('border-color'))
					.css('border-color', 'red')
					.one('change', function() {
						$(this).css('border-color', $(this).attr('data-oldborder'));
					});

			}
			return true;
		});
		return ok;
	});
});

/* Add border decoration elements. This is used only if CSS3 borders are not available */
jQuery.fn.decorateboxes = function() {
	return this.each(function() {
		var header=$(this).children(":header:first-child");
		if(header.size()>0) {
			header
				.wrap('<div class="decorboxheader"></div>')
				.before('<div class="leftcap"></div>')
				.after('<div class="rightcap"></div>');
		} else {
			$(this).prepend('<div class="decorboxtopleft"><div class="decorboxtop"></div><div class="decorboxtopright"></div></div>');
		}

		$(this)
			.children(".decorboxheader,.decorboxtopleft")
			.nextAll().wrapAll('<div class="decorboxbody"><div class="decorboxright"><div class="decorboxcontent"></div></div></div>');
		$(this).append('<div class="decorboxbl"></div><div class="decorboxbtm"></div><div class="decorboxbr"></div>');
	});
};

