var timerId = null;
var curMonth = null;
var calendarActive = false;
var activeMonth = null;

function showNoFlash()
{
	 jQuery("div.noflash").each(function(){		
		 jQuery(this).show(); 
	 });
	 clearTimeout(timerId)
}

function zoeken()
{
	var val = $("#search-txt").val();
	if (!val) {
		$("#search-txt").attr("class", "error_text");
		$("#search-txt").select();
		alert("je bent vergeten iets in te vullen!");
	} else {
		window.location = "/zoeken/" + val;
	}
}

$(document).ready(function() 
{	
	$("#search-txt").autocomplete(data,{matchContains:true});

	$("img#zap").click(function(){
		window.open("http://www.zap.be");		
	})
	$("#search-txt").select();
	$("#search-button").click(function(){
		zoeken();
	});
	
	$("#search-txt").keypress(function(e){
		if(e.which == 13) {
			zoeken();
		}
	});
	
	timerId = setTimeout("showNoFlash()", 2000)
	
	$("div#calendar-tooltip").hide();
	jQuery("div.noflash").each(function(){
		jQuery(this).hide(); 
		jQuery(this).css({"visibility":"visible"});
	});
	
	$.browser.msie6 = $.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent) && !/MSIE 8\.0/i.test(window.navigator.userAgent);
	
	if ($.browser.msie6) {
		$("div#IE6FIX").show();
	} 

	$("#switch-bg").click(function(){
		$(this).hide();
		$.ajax({
			   type: "POST",
			   url: "/ajax/change-bg/",
			   success: function(msg){
					var background = eval(msg);
					$("#switch-bg").show();
				//	alert(background);
					$('body').css('background-image', 'url(\"/' + background + '\")');
			   }
		 });
	})
	
	$("#switch-bg").hover(
			function () {
				$("#wrapper").hide();
			}, 
			function () {
				$("#wrapper").show();
			}
    );
	 
	$("img#netlog").click(function() {
		window.open("http://www.netlog.be/");		
	})
	
	$("img#facebook").click(function(){
		window.open("http://www.facebook.com/");	
	})
	
	$("div.sub-navigation").hide();
	
	$("ul#navigation li a").click(function(){
		var id = $(this).attr("id");
		id = id.split("-link").join("");
		$("div#sub-" + id).removeAttr("class");
		$("div#sub-" + id).attr("class", "sub-navigation");
		$("div#sub-" + id).slideToggle("fast");
	});
		
	$("table.news-index").click(function(){
		var id = $(this).attr("id");
		window.location = id;
	});
	
	$("table.news-home").click(function(){
		var id = $(this).attr("id");
		window.location = id;
	});
	
	$("table.calendar-home").click(function(){
		var id = $(this).attr("id");
		window.location = id;
	});
	
	$("table.guestbook-home").click(function(){
		window.location = "/planktong";
	});
	
	$("div#guestbook-holder").hide();
	
	$("div#guestbook-add").click(function(){
		$("div#guestbook-holder").slideToggle();
	});
	
	$("img#opendoek").click(function(){
		window.open("http://www.opendoek-vzw.be/");	
	});
	
	$("div.visje-vangen").hover(
			function () {
				$(this).attr("class", "home-box visje-vangen-over")
			}, 
			function () {
				$(this).attr("class", "home-box visje-vangen")
			}
    );
	
	$("div.visje-vangen").click(function(){
		window.location = "/voer/visje_vangen";
	})
	
	$("table.guestbook-home").hover(
			function () {
				$(this).attr("class", "guestbook-home-over")
			}, 
			function () {
				$(this).attr("class", "guestbook-home")
			}
    );
	
	$("table.calendar-home").hover(
			function () {
				$(this).attr("class", "calendar-home-over")
			}, 
			function () {
				$(this).attr("class", "calendar-home")
			}
    );
	
	$("table.news-index").hover(
			function () {
				$(this).attr("class", "news-index-over")
			}, 
			function () {
				$(this).attr("class", "news-index")
			}
    );
	
	$("table.news-home").hover(
			function () {
				$(this).attr("class", "news-home-over")
			}, 
			function () {
				$(this).attr("class", "news-home")
			}
    );
	
	
	$("ul#calendar li").hover(
			function () {
				calendarActive = true;
				var offset = $(this).offset();
				
				activeMonth = $(this);
				
				var x = offset.left;
				var y = offset.top;

				
				
				var month = parseInt($(this).attr("rel"));
				
				
				month = monthIndex+month-1;
				if (month > 12) {
					month-=12;					
				}
				
				var obj = $("div#calendar-tooltip");
				
				if (!obj.html()) {
					obj.html(preloader);
				}
			
				obj.css("left", (x-obj.width()) + "px");
			
				obj.css("top", y + "px");
				obj.show();
				
				if (curMonth != month) {
					obj.html(preloader);
					obj.css("left", (x-obj.width()) + "px");
					
					curMonth = month;
					
					$.ajax( {
						type : "POST",
						url : "/ajax/get-month/",
						data : "month=" + month,
						success : function(msg) {
							msg = eval(msg);
							var month = msg[0];
							
							if (month == curMonth) {
								var code = msg[1];
								var obj = $("div#calendar-tooltip");
								obj.html(code);
								obj.css("left", (x-obj.width()) + "px");
								
								$("div.calendar-tooltip-fiche").hover(function() {
									calendarActive = true;
									clearTimeout(timerId);
									
									$(this).attr("class", "calendar-tooltip-fiche-over");
								
									var obj = $("div#calendar-tooltip");
									obj.show();
									
									/*
									var monthObj = $("a#" + (activeMonth.attr("id") + "-link"));
									monthObj.css(activeMonth.attr("id") + "-over");*/
									
								}, function() {
									calendarActive = false;
									timerId = setTimeout("hideCalendarTooltip()", 200);
									$(this).attr("class", "calendar-tooltip-fiche");
									
								});
								
								$("div.calendar-tooltip-fiche").click(function() {
									window.location = this.id;
								});
							}
						}
					});
				}
			}, function() {
				calendarActive = false;
				timerId = setTimeout("hideCalendarTooltip()", 200)
			}
    );
	
	$("input#captcha-renew").click(function(){
		$("td#captcha-holder").hide();
		$.ajax({
			   type: "POST",
			   data: "id=form_guestbook_captcha",
			   url: "/ajax/change-captcha/",
			   success: function(msg){
					$("div#captcha").html(eval(msg));
					$("td#captcha-holder").show();
			   }
		 });
	});
	
	$("div#calendar-tooltip").mouseout(function() {
		
	});
	
	$('#' + form).submit( function() {	
		return validate_form(form);
	});
	
	$('#' + form + '_submit').click( function() {
		return validate_form(form);
	});
	
	/*$("div.plankton-home, div#header1, div#header2").click(function(){
		window.location = "/home";
	});*/
	/*$("div#header1").click(function(){
		alert("test");
		window.location = "/home";
	});*/
	

	
	$("div.plankton-home").hover(
			function () {
				$(this).attr("class", "plankton-home-over")
			}, 
			function () {
				$(this).attr("class", "plankton-home")
			}
    );
	
	$("#news").click(function(){
		window.location = "/vers-voer"
	});
	
	$("#guestbook").click(function(){
		window.location = "/planktong"
	});
	
	$("#calendar").click(function(){
		window.location = "/kalender/eventkalender"
	});
	
	$("#photo-strip-title").click(function(){
		window.location = "/herinneringen"
	});
	
	$("table.fiche").hover(
			function () {
				$(this).css("background-image", "url('/media/images/interface/bg.png')");
			}, 
			function () {
				$(this).css("background-image", "");
			}
    );
	
	$("tr.list-fiche").hover(
			function () {
				$(this).attr("class", "list-fiche-over")
			}, 
			function () {
				$(this).attr("class", "list-fiche");
			}
    );
	
	$("tr.list-fiche").click(function(){
		window.location = this.id;
	});
	
	$("table.fiche").click(function(){
		window.location = this.id.split("table-fiche-").join("");
	});
	
	$("div#back").click(function() {
		window.location = $(this).attr("rel");		
	})
	
	$("a.external").click(function(event){
		event.preventDefault();
	});
	
	$("a.external img").hover(
			
			function () {
				var id = $(this).attr("src");
		
			//	alert(id);
				$("a.external img").each(function(){
					//alert(this.id +  "->" + id);
					//alert($(this).attr("src"));
					if ($(this).attr("src") != id) {
						//$(this).fadeTo("fast", 0.6);
					} else {
						//$(this).fadeTo("fast", 1);
					}
				});
			}, 
			function () {
				$("a.external img").each(function(){
					//$(this).fadeTo("fast",1);
				});
			}
    );
	
	$("table.fotos").hover(
			
			function () {
				
			}, 
			function () {
				$("a.external img").each(function(){
					$(this).fadeTo("fast",1);
				});
			}
    );
	
	$.fn.colorbox.settings.bgOpacity = "0.9";
	$("a[rel='set']").colorbox({transition:"fade"});
	
	/*
	$("div.sub-navigation ul li a img").click(function(){
		var id = $(this).attr("id").split("-li").join("");
		id = id.split("-").join("/");
		window.location = "/" + id;
	})
	*/
	
	$("a.sub-navigation-link img").hover(	
		function () {
		
			var id = this.id.split("-li").join("");

			$("#" + id + "-star").attr("src", "/media/images/interface/navigation/star-over.png");
		}, 
		function () {

			var id = this.id.split("-li").join("");

			if (id.indexOf("-" + action) == -1 || action == "") {
				$("#" + id + "-star").attr("src", "/media/images/interface/navigation/star.png");
			}
		}
    );
	/*
	$("div.sub-navigation ul li a").each(function(){
		var parent = $(this).parent().parent().parent().attr("id").split("sub-").join("");
		var id = $(this).html();
		var id = trim(parent) + "-" + trim(id);
		id = id.split(" ").join("_");
		id = id.split("'").join("");
		id = id.toLowerCase();
		
		var file = id + ".png";
		file = file.split(" ").join("_");
		file = file.split("'").join("");
		file = file.toLowerCase();
		
		$(this).attr("id", id + "-li");
		var star = "<img id='" + id + "-star' class='star' src='/media/images/interface/navigation/star.png' alt='star' />";
		$(this).html("<table><tr><td>" + star + "</td><td><img id='" + id + "' src='/media/images/interface/navigation/" + file + "' alt='" + id + "' /></td></tr></table>");
	})
	*/
	$("img.navigation").each(function(){
		var id = this.id;
		if (id == controller) {
			$("div#sub-" + id).toggle();
			$("div#sub-" + id).attr("class", "sub-navigation");
		}
	})
	
	$("img.star").each(function(){
		var id = this.id.split(controller + "-").join("").split("-star").join("");
	
		if (id == action) {
			$(this).attr("src", "/media/images/interface/navigation/star-over.png");
		}
	})
	/*
	$("img.navigation").click(function(){
		var id = this.id;

		$("div#sub-" + id).attr("class", "sub-navigation");
		var kids = $(this).parent().children().length;
		if (kids <= 1) {
			window.location = "/" + id;
		}
	})*/

});

function openHome()
{
	window.location = "/home";
}
function hideCalendarTooltip()
{
	if (!calendarActive) {
		var obj = $("div#calendar-tooltip");
		obj.hide();
	}

	clearTimeout(timerId)
}
function validate_form(form) {
	
	var params = $("#" + form).serialize();
	params += "&form_id=" + form;
	
	jQuery("#" + form + "_submit").attr("disabled", true);
	jQuery("#" + form + "_submit").hide();

	var validate = true;

	if (validate) {
		jQuery.ajax( {
			type : "POST",
			url : "/ajax/validate-form/",
			data : params,
			success : function(msg) {
				jQuery("#" + form + "_submit").removeAttr("disabled");
				
				msg = eval(msg);
	
				jQuery("input").each( function() {
					if (this.id.indexOf(form) != -1) {
						jQuery(this).removeAttr("class");
						jQuery(this).attr("class", jQuery(this).attr("type"));
					}
				});
	
				jQuery("textarea").each( function() {
					if (this.id.indexOf(form) != -1) {
						jQuery(this).removeAttr("class");
						jQuery(this).attr("class", jQuery(this).attr("type"));
					}
				});
				
				if (msg[0]) {
					jQuery("#" + form + "_submit").show();
					
					var idArray = new Array();
					
					for ( var i = 0; i < msg[1].length; i++) {
						var obj = jQuery("#" + msg[1][i]);
					
						if (!obj.attr("id")) {
							var once = false;
							jQuery('input').each( function() {
								if (this.id.indexOf(msg[1][i])!=-1) {	
									obj = jQuery("#" + this.id);
									if (!once) {
										once = true;
										idArray.push(obj.attr("id"));
									}
								}
							});
							
						} else {
							idArray.push(obj.attr("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);
					}
					
					var errorMsg = "je bent de volgende verplichte zaken vergeten in te vullen:";
					

					for ( var i = 0; i < idArray.length; i++) {
						var obj = jQuery("#" + idArray[i])
						var tag = "";
						var tries = 0;
						while (tag != "td" || tries > 5) {
							obj = obj.parent();
							tag = obj.get(0).tagName.toLowerCase();
							tries++;
						}
						errorMsg += " - " + obj.siblings("td").html();
						
					}
	
					
					alert(errorMsg);
				} else {
					
					if (msg[1].indexOf("--->")!=-1) {
						window.location = msg[1].split("--->").join("");
					} else if (msg[1].indexOf("home")==-1) {
						history.go(-1);	
					} else {
						window.location = msg[1];
					}
				}
			}
	});
	}
	
	return false;
}

function trim(value) {
	  value = value.replace(/^\s+/,'');
	  value = value.replace(/\s+$/,'');
	  return value;
}