var sDomain	= 'http://www.websitesnederland.nl/';

$(document).ready(function() {
	login();
	submenu();
	showLogin();
	clearFields();
});

//function bekijkAlles() {
//	$('#form_bekijk_alles a').click(function() {
//		$(this).parent('#form_bekijk_alles').submit();	
//	});
//}

function uploadButton() {
	$("input[type=file]").filestyle({ 
		 image: sDomain+"img/tpl/bladeren.gif",
		 imageheight : 30,
		 imagewidth : 91,
		 width : 208,
		 padding : 10
	 });
}

function submenu(){
	$('ul#menu li').hover(function(){
		$(this).find('div.submenu').show();
		$(this).addClass('active');
	}, function(){
		$(this).find('div.submenu').hide();
		$(this).removeClass('active');
	});
}

function externalLinks() {  
	if (!document.getElementsByTagName) return;  
	var anchors = document.getElementsByTagName("a");  
	for (var i=0; i<anchors.length; i++) {  
  	var anchor = anchors[i];  
  	if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")  
    	anchor.target = "_blank";
	}
}

if(!bOpen) {
	var bOpen = false;	
}
function showLogin() {
	if(bOpen == true) {
		$('#login-wrapper').css('top','0px');
	}
	$('#klantlogin').click(function() {
		/*$('#user_name').focus();*/
		var cssPosition = $('#login-wrapper').css('top').split('px');
		if(cssPosition[0] < 0){
			$('#login-wrapper').animate({ 
				top: '0px'
			}, 250);
			$('#sGebruikersnaam').focus();
			$('#klantlogin').addClass('active');
		} else {
			$('#login-wrapper').animate({ 
				top: '-137px'
			}, 250, function(){
				$('#klantlogin').removeClass('active');
			});
		}
	});	
}

function clearFields() {
	$('#inloggen label input').focus(function() {
		$(this).prev().fadeTo(150, 0.33);
		$(this).keydown(function() {
			$(this).css('background-repeat', 'repeat');			 
		});
	});
	$('#inloggen label input').blur(function() {
		if ( $(this).val() == '' ) {
			$(this).css('background-repeat', 'no-repeat');
			$(this).prev().fadeTo(150, 1);
		}
	});
}

function initialize() {
	var myLatlng = new google.maps.LatLng(51.813633,4.025255);
	var myOptions = {
	  zoom: 15,
	  center: myLatlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById("google-maps"), myOptions);
	
	var image = new google.maps.MarkerImage(sDomain+'img/icon-wn.png',
		// This marker is 20 pixels wide by 32 pixels tall.
		new google.maps.Size(150,43),
		// The origin for this image is 0,0.
		new google.maps.Point(0,0),
		// The anchor for this image is the base of the flagpole at 0,32.
		new google.maps.Point(63,40)
	);
	var marker = new google.maps.Marker({
		position: myLatlng, 
		map: map,
		icon: image
	});   
}


//function loadingBar(p_sId, p_nWidth) {
//	$(p_sId).animate({
//		width: p_nWidth,
//		timeout: 500
//	},
//	6400
//	);
//}

/*function wners() {
	$('.foto').hover(function(){
		var sFile = $(this).find('img').attr('id');
		$(this).find('img').attr('src', sDomain+'img/tpl/'+sFile+'_roll.jpg');
	}, function(){
		var sFile = $(this).find('img').attr('id');
		$(this).find('img').attr('src', sDomain+'img/tpl/'+sFile+'.jpg');
	});
}*/

/*function vacatureTitel() {
	$('.vac_reageer, .vac_vertel').click(function(){
		var sTitel = $(this).parents('.vacature').find('h5 span').text();
		$('#vertelform, #reageerform').find('.sVacature').val(sTitel);
	});
}
*/
function validateForms(p_sForm) {
	var aMessage = new Array();
	aMessage[0] = 'Vul uw naam in.';
	aMessage[1] = 'Vul uw e-mailadres in.';
	aMessage[2] = 'Vul een geldig e-mailadres in.';
	aMessage[3] = 'Vul uw vraag of opmerking in.';
	aMessage[4] = 'Vul de naam van de ontvanger in.';
	aMessage[5] = 'Vul het e-mailadres van de ontvanger in.';
	aMessage[6] = 'Vul uw telefoonnummer in.';
	aMessage[7] = 'Vul de bedrijfsnaam in.';
	
	$.validator.addMethod("telefoon", function(phone_number, element) {
    	phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 5 &&
			phone_number.match(/^(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*$/);
	}, "Vul a.u.b. een geldig telefoonnummer in.");
	
	$.validator.addMethod("geennummers", function(geennummers, element) {
		   var reg = /[0-9]/;
		   return this.optional(element) || !geennummers.match(reg);
	}, "Getallen zijn niet toegestaan.");
	$('#'+p_sForm).validate({
		rules: {
			sNaam: {
				geennummers: true
			},
			sWoonplaats: {
				geennummers: true	
			},
			sEmail: {
				email: true
			},
			sTelefoon: {
				telefoon: true	
			}
		},
		messages: {
			sNaam: {
				required: aMessage[0]
			},
			sEmail: {
				required: aMessage[1],
				email: aMessage[2]
			},
			sTelefoon: {
				required: aMessage[6]
			},
			sNaamontvanger: {
				required: aMessage[4]
			},
			sEmailontvanger: {
				required: aMessage[5],
				email: aMessage[2]
			},
			sBedrijfsnaam: {
				required: aMessage[7]
			},
			sBericht: {
				required: aMessage[3]	
			}
		},
		errorPlacement: function(error, element) {
			error.insertAfter(element.parent());
		},
		success: function(label) {
			label.remove();
		},
		submitHandler: function(form) {
			
			switch(p_sForm){
				case 'reageer':
					var sUrl = sDomain+'/includes/reageer.inc.php';
					var sPopup = ' padding_popup';
				break;
				case 'vertelverder':
					var sUrl = sDomain+'/includes/vertel.inc.php';
					var sPopup = ' padding_popup';
				break;
				case 'reageervacature':
					var sUrl = '../includes/reageervacature.inc.php';
					var sPopup = ' padding_popup';
				break;
				case 'vertelverdervacature':
					var sUrl = '../includes/vertelvacature.inc.php';
					var sPopup = ' padding_popup';
				break;
				case 'contact':
					var sUrl = 'includes/contact.inc.php';
					var sPopup = ' padding_popup';
				break;
				case 'algemeen':
					var sUrl = sDomain+'/includes/algemeen.inc.php';
					var sPopup = ' padding_popup';
				break;
				case 'klachten':
					var sUrl = 'includes/klachten.inc.php';
					var sPopup = ' padding_popup';
				break;
				case 'vacature':
					form.trigger('submit');
				break;
				case 'webkey':
				case 'mailkey':
				case 'salekey':
				case 'housekey':
				case 'chatkey':
				case 'spamkey':
				case 'e-commerce':
				case 'training':
				case 'cms':
				case 'fotografie':
				case 'e-mailmarketing':
					var sUrl = '../includes/interessekey.inc.php';
					var sPopup = ' padding_popup';
				break;
				case 'ipad':
					var sUrl = sDomain+'/includes/interessekey.inc.php';
					var sPopup = ' padding_popup';
				break;
			}	
			var sDiv = '';

			var options = {
				url: sUrl,
				type: 'POST',
				success: function(sData) {

					var aResponse = sData.split('|');
					if(aResponse[0] == 0){
						$('#'+p_sForm).fadeOut(500, function(){
							$(this).remove();						 
						});
						if(p_sForm == 'contact'){
							location.href = sDomain + 'contact-dank';
						} else if(p_sForm == 'algemeen') {
							location.href = sDomain + 'dank';
						} else if(p_sForm == 'e-commerce') {
							location.href = sDomain + 'dank';
						} else if(p_sForm == 'cms') {
							location.href = sDomain + 'dank';
						} else if(p_sForm == 'fotografie') {
							location.href = sDomain + 'dank';
						} else if(p_sForm == 'e-mailmarketing') {
							location.href = sDomain + 'dank';
						} else if(p_sForm == 'training') {
							location.href = sDomain + 'dank';
						}  else if(p_sForm == 'webkey') {
							location.href = sDomain + 'dank';
						} else if(p_sForm == 'mailkey') {
							location.href = sDomain + 'dank';
						} else if(p_sForm == 'salekey') {
							location.href = sDomain + 'dank';
						} else if(p_sForm == 'housekey') {
							location.href = sDomain + 'dank';
						} else if(p_sForm == 'chatkey') {
							location.href = sDomain + 'dank';
						} else if(p_sForm == 'spamkey') {
							location.href = sDomain + 'dank';
						} else if(p_sForm == 'ipad') {
							location.href = sDomain + 'dank';
						} else if(p_sForm == 'reageervacature') {
							location.href = sDomain + 'dank-vacature.php';
						}
						
						sDiv += '<div class="succes'+sPopup+'">';
						sDiv += aResponse[1];
						sDiv += '</div>';
						
					} else if(aResponse[0] == 1){

						sDiv += '<div class="succes">';
						sDiv += 'Er is iets mis gegaan tijdens het versturen<br/><br/>';
						sDiv += 'Gebruik het volgende e-mail adres om contact op te nemen:<br/>';
						sDiv += '<a href="mailto:info@websitesnederland.nl" title="info@websitesnederland.nl">info@websitesnederland.nl</a><br/><br/>';
						sDiv += 'Vermeld alstublieft dat u deze foutmelding kreeg.';
						sDiv += '</div>';
					}
				
					$('div.melding').html(sDiv);
		
					setTimeout("$('.melding').fadeIn(700)", 800);
					
				}
			}
			if('#'.p_sForm != 'vacature') {
				$(form).ajaxSubmit(options);
				return false;
			}
		}
	});	
}

function popupForm() {
	$('#sharereageer, .vac_reageer').click(function() { 
		$.blockUI({ 
		message: $('#reageerform'),
		css: {
			width: '570px', height: 'auto', 
			top: '10%', left: '28%', 
			textAlign: 'left', 
			color: '#000',
			backgroundColor: '#fff',
			border: 'none', 
			cursor: 'default'
			},
			overlayCSS:  { 
				opacity: 0.85 
			}
		});
		setTimeout("scrolltotop();",750);
	});
	$('.close, .blockOverlay').click(function() { 
		$.unblockUI(); 
		return false; 
	});
	$('#sharevertel, .vac_vertel').click(function() { 
		$.blockUI({ 
		message: $('#vertelform'),
		css: {
			width: '570px', height: 'auto', 
			top: '10%', left: '28%', 
			textAlign: 'left', 
			color: '#000', 
			border: 'none', 
			cursor: 'default'
			},
			overlayCSS:  { 
				opacity: 0.85 
			}
		});
		setTimeout("scrolltotop();",750);
	});
	$('.close, .blockOverlay').click(function() { 
		$.unblockUI(); 
		return false; 
	});
}

function scrolltotop() {
	$('html, body').animate({
		scrollTop:0
	}, 'slow');	
}

//function filterPopup() {
//	$('#filteren a').click(function(){
//		if($(this).next('#filter_container').is(':hidden')) {
//			$(this).next('#filter_container').show();	
//		} else {
//			$(this).next('#filter_container').hide();	
//		}
//		
//	});
//}

var sGebruikersnaam;
function login() {
	$('#btn_login').click(doLogin);
	$('#sWachtwoord').keypress(function(e){
		if(e.which == 13) {
			doLogin();
		}
	});
	$('form:#inloggen').submit(function(){
		if($('#inloggen').attr('action') == '#') {
			return false;
		}
	});
}

function doLogin() {
	var sGebruikersnaam = $('#sGebruikersnaam').val();
	$.ajax({
		type: 'POST',
		url: sDomain+'includes/login.inc.php',
		data: 'sGebruikersnaam='+sGebruikersnaam,
		success: function(sResult){
			var aResult = sResult.split('|');
			if(parseInt(aResult[0]) == 1) {
				$('#inlog_melding').html('');	
				sUrl = aResult[1];
				$('#inloggen').attr('action',sUrl);
				$('form:#inloggen').submit();
			} else {
				$('#inlog_melding').html(aResult[1]);	
			}
		}
	});
}

//function resetForm() {
//	$('#btn_reset').click(function() {
//		$('#resetform').submit();				   
//	});
//}
//
//var sBranche = '';
//var sOplossing = '';
//function filterWerk(){
//	$('.selectbox-wrapper ul li').click(function(){
//		if($(this).parents('.selectbox-wrapper').attr('id') == 'branche_container') {
//			sBranche = $(this).attr('id');
//		} else {
//			sBranche = '';	
//		}
//		if($(this).parents('.selectbox-wrapper').attr('id') == 'oplossing_container') {
//			sOplossing = $(this).attr('id');
//		} else {
//			sOplossing = '';	
//		}
//		$.ajax({
//			type: 'POST',
//			url: sDomain+'includes/werk.inc.php',
//			data: 'sBranche='+sBranche+'&sOplossing='+sOplossing+'&bAjax=true',
//			success: function(result){
//				$('#content').html(result);
//				$('#branche, #oplossing').selectbox();
//				filterWerk();
//			}
//		});
//	});	
//}

function sorteerWebsites() {
	$('.selectbox-wrapper ul li').click(function(){
		var sSorteer = $(this).attr('id');
		$.ajax({
			type: 'POST',
			url: sDomain+'includes/werk.inc.php',
			data: 'sSorteer='+sSorteer,
			success: function(result){
				$('#content').html(result);
				$('#sorteren').selectbox();
				sorteerWebsites();
			}
		});
	});	
}

function initSlideshow(p_nWidth) {
	$('#items')
	.after('<div id="nav">')
	.cycle({
		fx:     'fade',
		timeout: 6000,
		speed:	 900,
		pager: '#nav'
	});
}

function slideCycle(){
	$('#items')
	.after('<div id="nav">')
	.cycle({
		fx:     'fade',
		timeout: 7000,
		speed:	 600,
		pager: '#nav'
	});
}
/*function slideCycle(){
	$('#loader').fadeOut(300, function(){
		$(this).remove();
		slideshow starten
		$('#items')
		.cycle({
			fx:     'fade',
			timeout: 5000,
			speed:	 900,
			pager: '#nav'
		});
		$('#items .item a.big').click(function(e){		
			var nHeightDiv = $('#items').height();
			if(nHeightDiv > 271) {
				var sLink = $(this).attr('href');
				window.location.href = sLink;	
			} else {
				var nHeight = $(this).children('img').height();
				$('#items').cycle('pause');
				$('#slider #nav,#items .item .hint').hide();
				$('#items').animate({
					height: nHeight
				}, 300, function(){
					$('#close').show();
				});	
			}
			return false;
		});
		$('#items').fadeIn(300);
		$('#nav').fadeIn(300);
	});
	closeSlideCycle();
}*/
function closeSlideCycle(){
	$('#close, #container').click(function(e){
		$('#items').animate({
			height: 271
		}, 300, function(){
			$('#close, #overlay_slideshow').hide();
			$('#slider #nav,#items .item .hint').show();
			$('#items').cycle('resume');
		});
	});	
}