$(function() {

	// Tooltip for wiki consument
	function wiki_tooltip(target_items, name){
		$(target_items).each(function(i){
			$(this).parent().css('z-index', '999');
			$(this).parent().append("<span class='"+name+"' id='"+name+i+"'><span>"+$(this).attr('title')+"</span></span>");
			var my_tooltip = $("#"+name+i);
			$(this).removeAttr("title").mouseover(function(){
					my_tooltip.css({display:"none"}).fadeIn(400);
			}).mouseout(function(){
					my_tooltip.fadeOut(400);
			});
		});
	}
	wiki_tooltip("a.wikiTip","tooltip");

	// Tooltip for all other elements
	function simple_tooltip(target_items, name){
		$(target_items).each(function(i){
			$("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
			var my_tooltip = $("#"+name+i);
			$(this).removeAttr("title").mouseover(function(){
					my_tooltip.css({display:"none"}).fadeIn(400);
			}).mousemove(function(kmouse){
					my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
			}).mouseout(function(){
					my_tooltip.fadeOut(400);
			});
		});
	}
	simple_tooltip(".wToolTip","wuglyTip");


	 // Controls active textfields and textareas
	$(".textFieldLarge, .textAreaLarge, .textFieldMedium, .textAreaMedium, .textFieldSmall, .textAreaSmall").focus(function() {
		$(this).addClass("formFocus");
	});
	
	$(".textFieldLarge, .textAreaLarge, .textFieldMedium, .textAreaMedium, .textFieldSmall, .textAreaSmall").blur(function() {
		$(this).removeClass("formFocus");
	});

	 // Sucky MSIE requires extra click on formselects
	if( !$.browser.msie ) {
		$(".formSelectLarge, .formSelectMedium, .formSelectSmall, .formSelect").focus(function() {
			$(this).addClass("formFocus");
		});
	
		$(".formSelectLarge, .formSelectMedium, .formSelectSmall, .formSelect").blur(function() {
			$(this).removeClass("formFocus");
		});
	}

	// NEW style for the fields (wiki consument)

	 // Controls active textfields and textareas
	$(".formElements input[type=text], .formElements input[type=password], .formElements textarea").focus(function() {
		$(this).addClass("formFocus");
	});

	$(".formElements input[type=text], .formElements input[type=password], .formElements textarea").blur(function() {
		$(this).removeClass("formFocus");
	});

	 // Sucky MSIE requires extra click on formselects
	if( !$.browser.msie ) {
		$(".formElements select").focus(function() {
			$(this).addClass("formFocus");
		});

		$(".formElements select").blur(function() {
			$(this).removeClass("formFocus");
		});
	}

	 
	 // Controls submit buttons
	$(".submitButton").hover(
		function() {
			$(this).addClass("submitButtonHover");
		},
		function() {
			$(this).removeClass("submitButtonHover");
		});

	 // Controls the toggling of windows
	$('a.toggle').click(function() {
		var parentDiv = $(this).parent().parent();
		$(parentDiv).find('div').toggle('fast');
		$(parentDiv).toggleClass('closedScreen');
	});

    //controls the toggling of windows (toggles div.elementWrap)
    $('a.toggleElement').click(function() {
		var parentDiv = $(this).parent().parent().parent();
		$(parentDiv).find('div.elementWrap').toggle('fast');
		$(parentDiv).toggleClass('closedScreen');
	});
	
	// Controls the search field
	$("#searchField").focus(function() {
		if($("#searchField").val() == $("#searchField").attr("title"))
		{
			$("#searchField").val('');
			$("#searchField").removeClass('greyTxt');
		}
	});
	
	$('#searchField').keydown(function() {
		$("#searchField").removeClass('greyTxt');		
	});
	
	$("#searchField").blur(function() {
		if($("#searchField").val() == '')
		{
			$("#searchField").val($("#searchField").attr("title"));
			$("#searchField").addClass('greyTxt');
		}
	});

	// Controls the city search field
	$("#searchField2").focus(function() {
		if($("#searchField2").val() == $("#searchField2").attr("title"))
		{
			$("#searchField2").val('');
			$("#searchField2").removeClass('greyTxt');
		}
	});
	
	$('#searchField2').keydown(function() {
		$("#searchField2").removeClass('greyTxt');		
	});
	
	$("#searchField2").blur(function() {
		if($("#searchField2").val() == '')
		{
			$("#searchField2").val($("#searchField2").attr("title"));
			$("#searchField2").addClass('greyTxt');
		}
	});
	
	$('input.searchButton').click(function() {
		if($("#searchField").val() != $("#searchField").attr("title") || $("#searchField2").val() != $("#searchField2").attr("title"))
		{
			if($("#searchField").val() == $("#searchField").attr("title"))
			{
				$("#searchField").val('');
			}
			if($("#searchField2").val() == $("#searchField2").attr("title"))
			{
				$("#searchField2").val('');
			}
			$('#searchForm').submit();
		}
		else
		{
			return false;
		}
	});




    // Inner search form
    // Controls the search field
	$("#innersearchField, #innnersearchField2").focus(function() {
		if($(this).val() == $(this).attr("title")) 		{
			$(this).val('');
		}
	});

    $("#innersearchField, #innnersearchField2").blur(function() {
		if($(this).val() == '') 		{
			$(this).val($(this).attr("title"));
		}
	});

    $('input.innerSearchButton').click(function() {
		if($("#innersearchField").val() != $("#innersearchField").attr("title") || $("#innnersearchField2").val() != $("#innnersearchField2").attr("title")) {

            if($("#innersearchField").val() == $("#innersearchField").attr("title")) {
				$("#innersearchField").val('');
			}
			
            if($("#innnersearchField2").val() == $("#innnersearchField2").attr("title")) {
				$("#innnersearchField2").val('');
			}

            $('#searchForm').submit();
            return true;
		} else {
			return false;
		}
	});


    // Custom search form
    // Controls the search field
	$("#customsearchField, #customsearchField2").focus(function() {
		if($(this).val() == $(this).attr("title")) 		{
			$(this).val('');
		}
	});

    $("#customsearchField, #customsearchField2").blur(function() {
		if($(this).val() == '') 		{
			$(this).val($(this).attr("title"));
		}
	});

    $('input.customSearchButton').click(function() {
		if($("#customsearchField").val() != $("#customsearchField").attr("title") || $("#customsearchField2").val() != $("#customsearchField2").attr("title")) {

            if($("#customsearchField").val() == $("#customsearchField").attr("title")) {
				$("#customsearchField").val('');
			}

            if($("#customsearchField2").val() == $("#customsearchField2").attr("title")) {
				$("#customsearchField2").val('');
			}

            $('#customsearchForm').submit();
            return true;
		} else {
			return false;
		}
	});



	
	 // Do not display the final seperator
	$("div.wrap div.scheidingForEach:last-child").css({display:"none"});
	
	 /** 
	  * Handle FAQ folding
	  */
	$("#faq p").not("#faqIntro").hide();
	$("h4").wrapInner("<span>");
	
	$("h4 span").click(function() {
		$(this).parent().nextAll().each( function() {
			if($(this).filter('h4').length) 
			{
				return false;
			}
			
			$(this).filter('p').toggle();
            $(this).filter('table').toggle();
		});
		
		$(this).toggleClass('expanded');
	});
	
	 /** 
	  * Characters left for input and textarea fields
	  */
	$("#shortDescription, #longDescription, #swapTerms").each(function() {
		 // Create an unique id for the status span
		var statusId  = $(this).attr('id')+'_status';
		
		 // Find maximum amount of characters allowed
		var maxLength = $('#'+$(this).attr('id')+'_maxlength').val();
		
		 // Append a span to display the status in
		$(this).after('<small id="'+statusId+'">');
		
		 // Set initial characters left 
		$('#'+statusId).html(maxLength - $(this).val().length +' tekens over');
		
		 // Update amount of characters on keyup
		$(this).keyup(function() {
			 // Set current amount of characters
			var curLength = $(this).val().length;
			
			 // Notify the user that too much chars are used
			if( curLength > maxLength )
			{
				 // Display error state
                $('#'+statusId).css('color', 'red');

                 // Update status message
                $('#'+statusId).html(curLength - maxLength +' tekens te veel');
			}
			else
			{
				 // Display natural state
                $('#'+statusId).css('color', '#555');

                 // Update status message
                $('#'+statusId).html(maxLength - curLength +' tekens over');
			}
		});
	});

    $('a.print').click(function() {
       window.print();
       return false;
    });

});
