function cufon()
{
	Cufon.replace('ul#navigation li a', {hover: true});
	Cufon.replace('h2');
	Cufon.replace('h3');
	Cufon.replace('a.news');
	
	$("ul#navigation li").show();
	
}

function searchIt()
{
	var val = $("input#search-input").val();
	
	var genres = "";

	if (val && val != "zoek in de site") {
		val = val.split(" ").join("_");
		val = escape(val);
		val = val.split("+").join("&plus;");
	
		var location = "/zoeken/" + val + "/";
		
		if (genres) {
			location += "geavanceerd/" + genres + "/";
		}
		
		window.location = location;
	} else {
		$("input#search-input").removeClass("normal");
		$("input#search-input").addClass("error");
		$("input#search-input").focus();
	}
}


$(document).ready( function() {
	cufon();
	
	$(this).mousemove(function(e){
		var posLeft = e.pageX - $("#tooltip-holder").width() - 15;
		if (posLeft < 10) {
			posLeft = e.pageX+10;
		}

		posLeft += $("#tooltip-holder").width() + 30;
		$("#tooltip-holder").css("left", posLeft);
		$("#tooltip-holder").css("top", e.pageY - 30);
		$("#tooltip-bottom").css("left", - $("#tooltip").width()-10);
		
	});
	
	$("#submit_form").click(function() {
		validate_form("form_contact");
	});
	
	$.fn.colorbox.settings.bgOpacity = "0.9";
	$("a.colorbox").colorbox({transition:"fade"});
	
	$("img.tiny").hover(function(){
		$("#tooltip").html($(this).attr("rel"));
		$("#tooltip-holder").show();
	},function(){
		$("#tooltip-holder").hide();
	});
	
	$("li.fiche").click(function(){
		var id = $(this).attr("id");
		id = id.split("li-").join("");
		var href = $("#a-" + id).attr("href");
		window.location = href;
	});
	
	
	$("ul li").hover(function() {
		$(this).addClass("over");
	}, function() {
		$(this).removeClass("over");
	});
	
	$("ul#navigation li").hover(function(){
		var id = $(this).attr("id").split("li-").join("");
		$("#sub-" + id).show();
		$(this).addClass("bg");
	}, function(){
		var id = $(this).attr("id").split("li-").join("");
		$("#sub-" + id).hide();
		$(this).removeClass("bg");
	});
	
	$("li.fiche").hover(function(){
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
	
	$("input#search-input").click(function(){
		$(this).val("");	
	});

	$("input#search-input").keypress(function(e){
		
		$(this).removeClass("error");
		$(this).addClass("normal");
		if (e.which == 13) {
			searchIt();	
		}
	});
	
	$("#search-arrow").click(function(){
		searchIt();
	});
	
	$("#search-img").hover(function(){
		var src = $(this).attr("src");
		src = src.split("white").join("blue");
		$(this).attr("src", src);
	},function(){
		var src = $(this).attr("src");
		src = src.split("blue").join("white");
		$(this).attr("src", src);
	});
	
	$("#search-input").autocomplete(data, {matchContains:true});
	
	$("#search-input").result(function(event, data, formatted) {
		searchIt();
	});
});


function validate_form(form) {
	var validate = true;
	
	if (validate) {
		var params = $("#" + form).serialize();
		params += "&form_id=" + form;
		params += "&id=" + id;
		
		$("#submit_form").attr("disabled", true);
		$("#submit_form").hide();
				
		$.ajax( {
			type : "POST",
			url : "/ajax/validate-form/",
			data : params,
			success : function(msg) {
				validate = true;
				$("#submit_form").removeAttr("disabled");
			
				msg = eval(msg);
	
				$("input, textarea, select").each( function() {
					if (this.id.indexOf(form) != -1) {
			
						$(this).removeClass("error");
						$(this).removeClass("error_text");
						$(this).addClass($(this).attr("type"));
					}
				});
	
				if (msg) {
					var goNext = true;
					
									
					if (msg[0]) {
						
						$("#submit_form").show();
					
						for ( var i = 0; i < msg[1].length; i++) {
							var obj = $("#" + msg[1][i]);
							
							if (!obj.attr("id")) {
								$('input').each( function() {
									if (this.id.indexOf(msg[1][i])!=-1) {	
										
										obj = $("#" + this.id);
									}
								});
								
							}
								
							if (i == 0) {
								obj.focus();
								obj.select();
							}
							
							var error = "error";
							var type = obj.attr("type");
							
							if (type == "text" || type == "password") {
								error += "_" + type;
							}
	
							if (obj.attr("type") == "checkbox") {
								obj.parent().attr("class", error);			
							}
							obj.attr("class", error);
						}
					} else if (goNext) {
						$("#" + form).hide();
						$("#form_success").show();
					} else {
						window.location = msg[1];						
					}
				}
			}
	});
	}
	
	return false;
}
