$(document).ready(docFunctions);

function docFunctions() {
	
	if($("form").length>0){
		$("form").validate({
			errorPlacement: function(error, element) {
				error.prependTo( element.parent("div") );
			}
		});
	}
	
	/*
	$('div#content-left').css('margin-left','-36px');
	$('div#content-left').css('width','580px');
	$('div#content-left').css('margin-top','-10px');
	$('div#content-left div').css('padding','0px');
	$('.machine').css('margin-left','6px');
	$('.machine').css('margin-bottom','-5px');
	*/
	$("a.newsthumbs").fancybox({
		'hideOnContentClick' : true,
		'titlePosition'	: 'inside',
		'speedIn'		:	600,
		'speedOut'		:	150
	});
	//GET URL
	function getHost() {
		var a = document.URL.split("//");
		a = (a[1] ? a[1] : a[0]).split("/"); 
		//alert(a.join("\n"));
		currentPage = a[1];
	}
	getHost();
		
	//MAIN NAVIGATION
	//$("li #roll").hide();
	//$("li #roll").children("#ac-c").addClass('ac-cactive');
	$('#search').addClass('searchTrans');
	$('#search').mouseover(function(){
		$(this).removeClass('searchTrans');
	});
	$('#search').mouseout(function(){
		if($('#search input').val()==""){
			$(this).addClass('searchTrans');
		}
	});
	$('#search input').keyup(function(e){
		if($(this).val()==""){
			$('#search').addClass('searchTrans');
		}
		if(e.keyCode == 13) {
			document.location.href = '/?zoek='+$('#search input').val();
		}

	});
	$('#search button').keyup(function(e){
		if(e.keyCode == 13) {
			document.location.href = '/?zoek='+$('#search input').val();
		}

	});
	$('#search button').mousedown(function(){
		if($('#search input').val()!==""){
			document.location.href = '/?zoek='+$('#search input').val();
		}
		else {
			alert('Vul eerst een zoekterm in alvorens te beginnen met zoeken!');	
		}
	});
	
	if($("ul#menu li").hasClass('active')) {
		$("ul#menu li.active").children("#roll").show();
		$("ul#menu li.active").children("#regular").hide();
		$("li.active").children("#roll").children("#ac-c").children("a").children("b").css("color","#35b806");
	};
	
	$("ul#menu li").mouseover(function() {
		if($(this).hasClass('active') == false) {
			$(this).children("#roll").show();
			$(this).children("#regular").hide();
		}
	});
	
	$("ul#menu li").mouseout(function() {
		if($(this).hasClass('active') == false) {
			$(this).children("#roll").hide();
			$(this).children("#regular").show();
		}
	});
	
	//TOP NAVIGATION
	$("ul#menutop li").mouseover(function() {
		if($(this).hasClass('active') == false) {
			$(this).children("#roll").show();
			$(this).children("#regular").hide();
			$(this).children("#roll").children("#act-c").children("a").children("b").css("color","white");
		}
	});
	
	$("ul#menutop li").mouseout(function() {
		if($(this).hasClass('active') == false) {
			$(this).children("#roll").hide();
			$(this).children("#regular").show();
			$(this).children("#regular").children("a").children("b").css("color","#47f808");
		}
	});
	
	if($("ul#menutop li").hasClass('active')) {
		$("ul#menutop li.active").children("#roll").show();
		$("ul#menutop li.active").children("#regular").hide();
		$("ul#menutop li.active").children("#roll").children("#act-c").children("a").children("b").css("color","white");
	};

	
	//RECHTERKANT SUB LINKS
	if($("a#sublink").hasClass('active')) {
		$("a.active").children("#subimg-overlay").hide();
		$("a.active").css("color","black");
	};
	
	$("a#sublink").mouseover(function() {
		if(!$(this).hasClass('active')) {
			$(this).children("#subimg-overlay").hide();
			//$(this).css("color","black");
		}
	});
	
	$("a#sublink").mouseout(function() {
		if(!$(this).hasClass('active')) {
			$(this).children("#subimg-overlay").show();
			//$(this).css("color","#a2a2a2");
		}
	});
	
	
	//VIDEO POPUP
	$(".lightbox").click(function() {
		$.fancybox({
				//'padding'		: 0,
				'href'			: this.href,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'
			});
	
		return false;
	});	
	
	//VIDEO POPUP
	$("#watchvideo").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: true,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 640,
				'height'		: 360,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
	
		return false;
	});
	
	$(".watchvideo").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: true,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 640,
				'height'		: 360,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					 'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
	
		return false;
	});
	
	if(currentPage == 'home' || currentPage == '') startFancybox();
	
	if(currentPage == "natuurbeheer" || currentPage == "groenvoorziening" || currentPage == "cultuurtechniek" || currentPage == "vacatures" || currentPage == "machines" || currentPage == "biomassa") {
		
		//$("p.textbox").hide();
		if(currentPage != 'machines') startFancybox();
		
		if($("div#dropdownlink").hasClass('active')) {
			$("div#dropdownlink.active").children("a").children("h2").css("color","#288f03");
		};
		
		$("div #dropdownlink").mouseover(function() {
			if(!$(this).hasClass('active')) $(this).children("a").children("h2").css("color","#288f03");
		});
			
		$("div #dropdownlink").mouseout(function() {
			if(!$(this).hasClass('active')) $(this).children("a").children("h2").css("color","#616161");
		});
		
		
		$("div #machineImage").mouseover(function() {
			$(this).children("h2").css("color","#3b3b3b");
		});
		
		$("div #machineImage").mouseout(function() {
			$(this).children("h2").css("color","#616161");
		});
		
		
		if(currentPage == "machines") {
			$("li.button").mouseover(function() {
				$roImage = 'layouts/vanstipdonk/images/btn_request_info_ro';
				$(this).children("input").attr("src", String($roImage)+".png");
			});
						
			$("li.button").mouseout(function() {
				$roImage = 'layouts/vanstipdonk/images/btn_request_info';
				$(this).children("input").attr("src", String($roImage)+".png");
			});
			
			/*
			if(document.getElementById("reaction_machine") != null) {
				$("form#reaction").validate({
					onblur: function(el) {
					if(validator.check(el))
						$(el).removeClass(validator.settings.errorClass);
					else
						$(el).addClass(validator.settings.errorClass);
					},
					
					onkeyup: function(el) {
					if(validator.check(el))
						$(el).removeClass(validator.settings.errorClass);
					else
						$(el).addClass(validator.settings.errorClass);
					}
				});
			}
			*/
		}
		
		/*$("div #dropdownlink").click(function() {
			//$(this).next("p").css({height: heightArray[$("div #dropdownlink").index(this)]});
			//$(this).next("p").slideToggle("slow")
			//.siblings("p:visible").slideUp("slow");
			//$(this).toggleClass("active");
			$(this).siblings("div").removeClass("active");
			$(this).siblings("div").children("h2").css("color","#616161");
		});*/
		
	}
	else if(currentPage == "projecten") {
		startFancybox();
		
		$("div #project").mouseover(function() {
			$(this).children("h2").css("color","black");
			//$roImage = $(this).children("a").children(".prjtregular").attr("ro");
			//$(this).children("a").children(".prjtregular").css("background-image", "url("+String($roImage)+"_roll.jpg)");
		});
		
		$("div #project").mouseout(function() {
			if(!$(this).hasClass('active')) $(this).children("h2").css("color","#47f808");
			//$roImage = $(this).children("a").children(".prjtregular").attr("ro");
			//if(!$(this).hasClass('active')) $(this).children("a").children(".prjtregular").css("background-image", "url("+String($roImage)+"_roll.jpg)");
		});
		
		$("div #project").mousedown(function() {
			$(this).addClass("active");
			$(this).siblings("div").removeClass("active");
			$(this).siblings("div").children("h2").css("color","#47f808");
			//$(this).children("a").children(".prjtregular").css("background-image", "url("+String($roImage)+"_roll.jpg)");
		});
		
	}
	else if(currentPage == "machines") {
		//startFancybox();
		
		/*heightArray = new Array();
		$("ul.submenu").each(function(i) {
			theHeight = $(this).height();
			heightArray[i] = theHeight;
		});
		
		$(".submenu").hide();
			
		$urladress = $.address.value();
		$urlarray = $urladress.split("/");
		
		//alert($urlarray[1]+' - '+$urlarray[2]);
		
		if($urlarray[1] == undefined) {
			//$(location).attr('href', 'machines/#/graaf_laadcombinatie/graaflaad_combinatie');
			//$.address.init();
		}
		
		if($("div #headlink").hasClass($urlarray[1])) {
			$("div."+$urlarray[1]).addClass("active");
			$("div."+$urlarray[1]).children("h2").css("color","#3b3b3b");
			$("div."+$urlarray[1]).next("ul").slideToggle("slow");
		
			if($("div."+$urlarray[1]).next("ul").children("div").hasClass($urlarray[2])) {
				$("div."+$urlarray[1]).next("ul").children("div."+$urlarray[2]).addClass('subactive');
				$("div."+$urlarray[1]).next("ul").children("div."+$urlarray[2]).children("h3").children("a").css("color","#3b3b3b");
				
				$contentId = $("div."+$urlarray[1]).next("ul").children("div."+$urlarray[2]).attr("num");
				showContent($contentId);
			}
		}
		
		$("div #headlink").mouseover(function() {
			if(!$(this).hasClass('active')) $(this).children("h2").css("color","#3b3b3b");
		});
			
		$("div #headlink").mouseout(function() {
			if(!$(this).hasClass('active')) $(this).children("h2").css("color","#616161");
		});
		
		$("div #headlink").click(function() {
			$(this).next("ul").css({height: heightArray[$("div #headlink").index(this)]});
			
			$(this).next("ul").slideToggle("slow")
			.siblings("ul:visible").slideUp("slow");
			$(this).toggleClass("active");
			$(this).siblings("div").removeClass("active");
			$(this).siblings("div").children("h2").css("color","#616161");
		});
		
		
		$("div #sublink").mouseover(function() {
			if(!$(this).hasClass('subactive')) $(this).children("h3").children("a").css("color","#3b3b3b");
		});
		
		$("div #sublink").mouseout(function() {
			if(!$(this).hasClass('subactive')) $(this).children("h3").children("a").css("color","#288f03");
		});*/
	
		//URL REFRESH
		/*var log = function(msg) {
			if (!$('.log').size()) {
				$('<div class="log" />').appendTo('.page');
			}
			
			$('.log').append(msg.replace(/^([^:]*):(.*)$/, '<p><b>$1:</b> <span class="$1">$2</span></p>'))
			.attr({scrollTop: $('.log').attr('scrollHeight')})
			.find('p:nth-child(even)').addClass('even');
		};
		
		// Event handlers
		$.address.init(function(event) {
			log('init: "' + event.value + '"');
			$('a#subje').address(function() {
				$(this).parents('#sublink').addClass('subactive');
				$(this).parents('#sublink').siblings("div").removeClass("subactive");
				$(this).parents('#sublink').children("h3").children("a").css("color","#3b3b3b");
				$(this).parents('#sublink').siblings("div").children("h3").children("a").css("color","#288f03");
				return $(this).attr('href').replace(location.pathname, '');
			});
			
			}).change(function(event) {
				log('change: "' + event.value + '"');
				var text = (event.value == '') ? 'Home' : 
				event.pathNames[0].substr(0, 1).toUpperCase() + 
				event.pathNames[0].substr(1);
				$('a').each(function() {
					$(this).toggleClass('selected', $(this).text() == text);
				});
				
				startFancybox();
			
			}).internalChange(function(event) {
				log('internalChange: "' + event.value + '"');
			
			}).bind('externalChange', {msg: 'The value of the event is "{value}".'}, function(event) {
				log('externalChange: ' + event.data.msg.replace(/\{value\}/, event.value));
		});*/
	
	}
	else if(currentPage == "contact") {
		
		imgPersonArray = new Array();
		$("img#person").each(function(i) {
			theimg = $(this);
			imgPersonArray[i] = theimg;
		});
		
		infoPersonArray = new Array();
		$("h2#personlink").each(function(i) {
			theinfo = $(this);
			infoPersonArray[i] = theinfo;
		});
		
		$("img#person").mouseover(function() {
			var src = $(this).attr("src").replace("uit", "over");
            $(this).attr("src", src);
			
			var num = $(this).attr("num");
			var naam = infoPersonArray[num];
			naam.css("color", "#000000");
		});
		
		$("img#person").mouseout(function() {
			var src = $(this).attr("src").replace("over", "uit");
            $(this).attr("src", src);
			
			var num = $(this).attr("num");
			var naam = infoPersonArray[num];
			naam.css("color", "#45F808");
		});
		
		$("h2#personlink").mouseover(function() {
			var num = $(this).attr("num");
			var naam = imgPersonArray[num];
			
			var src = naam.attr("src").replace("uit", "over");
            naam.attr("src", src);
			$(this).css("color", "#000000");
		});
		
		$("h2#personlink").mouseout(function() {
			var num = $(this).attr("num");
			var naam = imgPersonArray[num];
			
			var src = naam.attr("src").replace("over", "uit");
            naam.attr("src", src);
			$(this).css("color", "#45F808");
		});
/*		
		//Contact formulier
		if(document.getElementById("contact") != null) {
			$("form#contact").validate({
				onblur: function(el) {
				if(validator.check(el))
					$(el).removeClass(validator.settings.errorClass);
				else
					$(el).addClass(validator.settings.errorClass);
				},
				
				onkeyup: function(el) {
				if(validator.check(el))
					$(el).removeClass(validator.settings.errorClass);
				else
					$(el).addClass(validator.settings.errorClass);
				}
			});
		}*/
	
	}
	else if(currentPage == 'leerbedrijf') {
		$("p.textbottom a").mouseover(function() {
			$(this).children("b").css("color","black");
		});
		
		$("p.textbottom a").mouseout(function() {
			$(this).children("b").css("color","#616161");
		});
	}
};

function showContent(ID) {
	$('#content-right').html($('#moreInfo'+ID).html());
};

function startFancybox() {
		
	$("a.showimage").fancybox({
		'hideOnContentClick' : true,
		'titlePosition'	: 'inside',
		'speedIn'		:	600,
		'speedOut'		:	150
	});	
};

function submitReactionForm() {
	$('#machine').val($("#reaction_machine").children("b").attr('value'));
	$('form#reaction').submit();
};
