$(document).ready(function() {
	/* 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();
	}

	/* 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]");
		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;
	});
	
	// Prevent submission of empty forms
	$(":submit.notempty").click(function() {
		var inputs = $(this).closest("form").find("[rel=check]");
		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;
	});
	
	
	/*$('#sidemenu li a').click(function () {
		var checkElement = $(this).next();
		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			return false;
		}
		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$('#sidemenu ul:visible').slideUp('normal');
			checkElement.slideDown('normal');
			return false;
		}
	});*/	
});

function getUrlVars(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
