J.openPanel = function (url, data, complete) {
	J.closePanel(function () {
		if (data == undefined) data = []; 
		J('#toppanel_inner').load(url, data, function () {
			if (complete != undefined && typeof(complete == 'function')) complete();

			J("#toppanel_inner").slideDown("400", function () {
				if (J('#searchMoreResults').length > 0)  {
					var height = Math.max(J('#searchMainResults').height(),J('#searchMoreResults').height());
					
					if (height == J('#searchMainResults').height()) {
						J('#searchMoreResults').height(height-14);
					} else {
						J('#searchMainResults').height(height+14);
					}
				}				 
			});																		  
		});								
	});
}


J.closePanel = function (complete) {
	J("#toppanel_inner").slideUp("200", function () {
		J('#services_top li.active, #searchform').removeClass('active');
		J(this).empty();
		J('#toppanel').removeClass('contactPanel sugarwallPanel searchPanel');
		
		if (complete != undefined && typeof(complete == 'function')) complete();
	});	
}


// SEARCH SLIDES
J.fn.rsSearch = function () {
	var url = J(this).attr('action');
	var s = J(this).find('input[name=s]').val();

	if (s != '') {
		J.openPanel(url, J(this).serialize(), function () {
			J('#searchform').addClass('active');								
			J("#toppanel").addClass("searchPanel");
		});
	}
}

J.toggleMoreResults = function () {
	if (J('#searchWrapper').hasClass('open')) {	
		J('#searchMoreResults').animate({width: '0'}, 300, function () {
			J('#searchWrapper').removeClass('open');
		});
	} else {
		J('#searchMoreResults').animate({width: '970px'}, 300);
		J('#searchWrapper').addClass('open');
		J('#searchMoreResults').addClass('xopen');
	}
	
}

J(document).ready(function() {
// CLOSE TOPPANEL 
	J('div.mainWrapper').click(function(event) {
	
		if (J('#toppanel_inner').css('display')=='block')
		{
			J.closePanel();	
			
		}
	
	 });
	
// OPEN TOPPANEL EN LOAD CONTENT
	J("#services_top li").click(function(e){
		if(J('a', this).attr('onclick') == undefined && J(this).attr('id') != 'menu-item-1485') {
			e.preventDefault();
									   
			var me = this;
			var url = J('a', this).attr('href');
			
			if (!J(me).hasClass('active')) {
				J.openPanel(url, [], function () {
					J("#toppanel").addClass("contactPanel");
					J(me).addClass('active');
					
				});
			} else {
				J.closePanel();	
			}
			
			
			return false;
		}
		
	});	
	
		
// PANELS VOOR DE RECHTERKANT/SIDEBAR | ONS VAK
    J(".open_child_page").click(function () {
		J(this).flipDown();
    });
	
	J.fn.flipDown = function () {
		//console.log(J(this).closest('li').next('div.flipdown').length);
		J(this).closest('li').find('div.flipdown').slideToggle("400");;
		J(this).toggleClass("open");
	}


// CLIENT SCROLLER | ANIMATIE
	if (J(".clientScroller").length > 0) {
		var countClients = J(".clientScroller ul li").length;
		var currentClient = 1;
		
		J(".clientScroller ul li:first").clone().appendTo(".clientScroller ul");
		
		setInterval(function () {
			if (currentClient > countClients) {
				J(".clientScroller ul").css('left', '0');
				currentClient = 1;
			}
			J(".clientScroller ul").animate(
				{left: '-=215'}, 
				400
			);
			currentClient++;
		}, 3000);
	}
					
});


function trim(value) {
	value = value.replace(/^\s+/,'');
	value = value.replace(/\s+$/,'');
	return value;
}


J.fn.subscribeNewsletter = function () {
	var formAction = J(this).attr('action');
	var formData = J(this).serialize();

	if (validateEmail(J(this).find('#subcriberEmail').val())) {
		J.ajax({
			type: "POST",
			url: formAction,
			data: formData,
			success: function(msg){
				J('#newsletterSubscribeForm').slideUp(150, function () {
					J('#newsletterSubscribeConfirm').slideDown(150);
				});		
			}
		});
	}
	return false;
}

function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      alert('U heeft een ongeldig e-mailadres ingevoerd.');
      return false;
   }
   
   return true;
}
