$(document).ready(function() {
	
	var kaart = 'kaart';
	$('#Kaart_NL area').click(function() {
	    kaart = String($(this).attr('href')).substr(1, ($(this).attr('href').length - 1));
		var active = false;
		$('#accordion .titel').each(function() {
			if($(this).attr('class') == 'titel') {
				active = true;	
			}
		});
		var obj = $($(this).attr('href'));
		if(active) {
			$('#accordion .item').slideUp(500);
			window.setTimeout(function() {
				$('#accordion .titel').attr('class', 'titel closed');
				obj.next().slideDown(500);
				obj.removeClass('closed');
		    }, 500);
		}
		else {
			obj.next().slideDown(500);
			obj.removeClass('closed');
		}
		return false;
	}).mouseover(function() {
		var provincie = String($(this).attr('href')).substr(1, ($(this).attr('href').length - 1));
		$('#kaart_').attr('src', 'images/' + provincie + '.png');
	}).mouseout(function() {
		$('#kaart_').attr('src', 'images/' + kaart + '.png');
	});
	
	$('.item .sub-accordion').each(function() {
		$(this).children(':first').css({'margin':'0px'});
	});
	
	$('.stel_vraag').click(function() {
		var begeleider = $(this).attr('id').split('-')[1];
		$("#stel_een_vraag").dialog({			
			title			 : 'Stel een vraag aan ' + $(this).attr('name'),
			draggable		 : true,
			autoOpen		 : false,
			resizable		 : false,
			height			 : 250,
			width			 : 600,
			modal			 : true,
			closeOnEscape	 : false,
			open			 : function(event, ui) {
				$.ajax({
					type	 :	'GET',
					url		 : 	'request.php?stel_een_vraag=' + begeleider,
					success	 : 	function(response) {
						$('#stel_een_vraag').html(response);
					}
				})
			},
			close			 : function(event, ui) {
				$('#stel_een_vraag').dialog('destory');	
			}
		});
		$('#stel_een_vraag').dialog('open');	
		return false;
	});
	
	$('.sub-titel').click(function() {
	   	var active = false;
		
		//$('.stel_vraag').hide();
		$(this).parent().children('.sub-item').each(function() {
			if($(this).css('display') == 'block') {
				active = true;
			}
		});
		if(active) {
			$(this).parent().children('.sub-item').slideUp(500);
			var obj = $(this);
			window.setTimeout(function() {
				obj.next().slideDown(500);
				obj.children().show();
			}, 500);
		}
		else {
			$(this).next().slideDown(500);
			$(this).children().show();
		}
	});
	
	$('#accordion span.titel').click(function() {
		
		if($(this).parent().attr('class') == 'accordion-kaart') {
			kaart	=	String($(this).text()).substr(1, ($(this).text().length - 1)).split(' ')[0].toLowerCase();
			$('#kaart_').attr('src', 'images/' + kaart + '.png');
		}
		
		var active = false;
		$('#accordion .titel').each(function() {
			if($(this).attr('class') == 'titel') {
				active = true;	
			}
		});
		if(active) {
			$('#accordion .item').slideUp(500);
			window.setTimeout(function() {
				$('#accordion .titel').attr('class', 'titel closed');
		    }, 500);
		}
		
		if($(this).next().css('display') == 'none') {
	
			var status 		= 'closed';
			$('#accordion .item').each(function() {
				if($(this).css('display') == 'block') {
					status	= 'open';
				}
			});
			
			if(status == 'open') {
				var el = $(this);
				window.setTimeout(function() {
					el.next().slideDown(500);
					el.removeClass('closed');
				}, 500);
			}
			else {
				$(this).next().slideDown(500);
				$(this).removeClass('closed');
			}
			
		}
		
	});
	
	$('.button').mouseover(function() {

		var page = $(this).attr('class').split(' ')[1];
		
		if($('#sub_' + page).length == 1) {
			$('.submenu').hide();
			$('#sub_' + page).show();
		}
		else {
			$('.submenu').hide();			
		}
	});
	
	var top_menu_width 		=	763;
	var top_menu_slide 		=	$('#b_table').width();
	var top_menu_begin		=	0;
	var speed				=	7;
	var top_menu_des		=	top_menu_slide - top_menu_width;
	var top_menu_pos 		= 	$('#buttons').position();
	
	if($('#top_nav').length == 1) {
		var active			=	'';
		$('#top_nav div').mouseover(function() {
			if(active == '') {
				active		=	$('#top_nav div.active').attr('id');
			}
			$('#top_nav div').attr('class', '');
			$(this).attr('class', 'active');
		}).mouseout(function() {
			$('#top_nav div').attr('class', '');
			$('#' + active).attr('class', 'active');
			active = '';
		});
	}
	
	$('#slide_middle').hide();
	
	$('#slide_right').mouseover(function() {
		top_menu_pos 		= 	$('#buttons').position();
		$('#buttons').stop();
		moveTopMenu('right', speed);
		$('#slide_right').hide();
		$('#slide_left').hide();
		$('#slide_middle').show();
	});
	
	$('#slide_left').mouseover(function() {
		top_menu_pos 		= 	$('#buttons').position();
		$('#buttons').stop();
		moveTopMenu('left', speed);
		$('#slide_right').hide();
		$('#slide_left').hide();
		$('#slide_middle').show();
	});
	
	$('#menu_slide').mouseleave(function() {
		 stopTopMenu();
	});
	
	$('#slide_middle').mouseover(function() {
		 stopTopMenu();										  
	});
	
	function moveTopMenu(direction, speed) {
		if(top_menu_slide > top_menu_width) {
			if(direction == 'right') {
				menu_scroll 	= 	String(Math.round(top_menu_pos.left * 1) / 1);
				menu_scroll 	= 	parseInt(menu_scroll.replace('-', ''));
				menu_scroll 	= 	top_menu_des - menu_scroll;
				$('#buttons').animate({left: '-' + top_menu_des + 'px'}, (menu_scroll * speed), 'linear');
			}
			else {
				menu_scroll 	= 	String(Math.round(top_menu_pos.left * 1) / 1);
				menu_scroll 	= 	parseInt(menu_scroll.replace('-', ''));
				$('#buttons').animate({left: top_menu_begin + 'px'}, (menu_scroll * speed), 'linear');				
			}
		}
	}
	
	function stopTopMenu() {
		$('#slide_left').show();
		$('#slide_right').show();
		$('#slide_middle').hide();
		top_menu_pos 		= 	$('#buttons').position();
		$('#buttons').stop();		
	}

});


/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}
function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function validateNotEmpty( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }
   return false;
}



function validateEmail( strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/
var objRegExp  = /^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/;

  //check for valid email
  return objRegExp.test(strValue);
}

