$(function(){
	
	
	
	$(".bodyImageWrapper").hover(
			function() {
			$(this).find('.opacityLayer').stop().animate({"height": "25"}, "slow");
			},
			function() {
				$(this).find('.opacityLayer').stop().animate({"height": "280"}, "slow");
			});		 

	/*
	 *  Clear input veld wanneer er op wordt gefocused
	 */
	
	$('#saddr').focus(function(){
		$(this).attr('value','');
	});
	
	/*
	 * Set de lengte automatisch gelijk aan die van het bureau
	 */
	var officeWrapperHeight = $('#officeWrapper').height()-5;

	$('#officeRightColumn').height(officeWrapperHeight);
	
	/*
	 * Form Validatie Server-Side op aanmeldformulier nieuwsbrief
	 */
	$('input').blur(function()
	{
		var formElementId = $(this).get(0).id;
		doValidation(formElementId);
	});
	
	/*
	 * Hide alle extra content items 
	 */
	$(".projectExtraInfoWrapper").hide();	
	$('#projectInfoWrapper a').each(function()
	{
		$(this).click(function(){
			var title = this.title;
			$(".projectExtraInfoWrapper[title='"+title+"']").show('10');
			$('html,body').animate({
				scrollTop: $(window).scrollTop() + 275
			});
		});
		
	});
	
	$('.removeNewsletter').click(function(){		
		var id = $(this).parent().attr('id');
		var data = {'id': id};
		var url = '/admin/nieuwsbrief/removenewsletter';
		$.post(url,data,function(resp){$("#newsletterTable tr td a[id="+id+"]").parents('tr').hide();},'json');		
	});
	$("input:checkbox[name=checkall]").click(function(){
			
	
		if($("#newsletterTableTwo input:checkbox:checked").length<=1)
		{
			$("#newsletterTableTwo input:checkbox").attr('checked',true);	
		}else{
			$("#newsletterTableTwo input:checkbox").attr('checked',false);	
		}
		
	});
	
	/*
	 * Pre-Laad alle images uit de css in
	 */
	$.preloadCssImages();	
	
	/*
	 * Resize de opacity layer 
	 */
	setInterval( "slideSwitch(0)", 3000 );
	setInterval( "slideSwitch(1)", 3000 );
	setInterval( "slideSwitch(2)", 3000 );
	setInterval( "slideSwitch(3)", 3000 );
	
	
	
});

function shortenLayer()
{
	$('.opacityLayer').animate({"height": 280}, 400);
}

function resetLayer()
{
	 $('.opacityLayer').animate({"height": 280}, 400);
}
function doValidation(id)
{
	var data = {};
	var url= '/klanten/validateform';
	$('input').each(function()
	{		
		data[$(this).attr('name')] = $(this).val();		
	});

	$.post(url,data,function(resp)
	{
		/*$("#"+id).parent().find('.errors').remove();
		$("#"+id).parent().append(getErrorHtml(resp[id],id));*/
		$('.subscribeError p').remove();
		$('.subscribeError').append(getErrorHtml(resp[id],id));
	},'json');
	

}

function getErrorHtml(formErrors , id)
{	
	var o = '<p>';
	for(errorKey in formErrors)
	{		
		o+= formErrors[errorKey];
	}
	o+= '</p>';
	return o;
}

function slideSwitch(key) {
    var $active = $('#slideshow'+key+' IMG.active');
    var $countimg = $('#slideshow'+key+' IMG');
    if($countimg.length !=1)
    {	   
	    if ( $active.length == 0 ) $active = $('#slideshow'+key+' IMG:last');
	
	    var $next =  $active.next().length ? $active.next()
	        : $('#slideshow'+key+' IMG:first');
	
	    $active.addClass('last-active');
	
	    $next.css({opacity: 0.0})
	        .addClass('active')
	        .animate({opacity: 1.0}, 1000, function() {
	            $active.removeClass('active last-active');
	        });
    }
}



