var randomInterval = null;
$(document).ready(function(){
	$('#menu a:not(.active)').hover(function(e){
		$(this).stop().animate({borderRightWidth:'15px'},200);	
		
	},function(e){
		$(this).stop().animate({borderRightWidth:'0px'},200);
		
	});
	
	$(window).resize(function() {
		resizeWindow();
	});
	resizeWindow();
	randomInterval = setInterval(showPainting,7000);
	
});

function resizeWindow(){
	
	html = $('#content .scroll').html();
	$("#content").html('<div class="scroll"></div>');
	$("#content .scroll").html(html);
	$('#content').css({width:($(window).width()-80-$("#content").offset().left)+'px',height:($(window).height()-100-$("#content").offset().top)+'px'})
	$("#content .scroll").jScrollPane({scrollbarMargin :'20'});
	
	
	
	$("a[href$=.jpg]").fancybox({
		'titleFormat'	: function(title, currentArray, currentIndex, currentOpts){
			el = $('[rel=fancy]:eq('+currentIndex+')');
			return('<div class="customtitle"><strong>'+el.attr('titel')+'</strong><div>'+el.attr('tekst')+'</div></div>');
		},
		'overlayShow'		:	true,
		'overlayColor'		: '000',
		'overlayOpacity'	:	0.8,
		'autoScale'			: true,
		'height'			: 300
	});
	
	
	$(".ajaxformulier").each(function(i){
		$(this).unbind('submit').bind('submit',function(e){
			submitAjaxForm($(this));
			e.preventDefault();
		});
	});
	
	
	$('#schilderijen a').hover(function(){
		showPainting($(this).attr('medium'),$(this));								
	});
}
function showPainting(src, el){
	$('.painting div').css('height',$('.painting div').height()+'px');
	if(src){
		clearInterval(randomInterval);
		//$(".painting").css({bottom:'auto',top:$(".painting").position().top+'px'}).stop().animate({'top':(el.offset().top-15)+'px'},500);
		fadePainting(src);
	}else{
		var currentID 	= $('.painting div img').attr('src').replace(/[^0-9]/g, "");
	//	if($('#schilderijen').length > 0){
	//		randomnumber	= Math.floor(Math.random()*($('#schilderijen li').length+1));
	//		linkSchilderij = $('#schilderijen li:eq('+randomnumber+') a').attr('medium');
	//		fadePainting(linkSchilderij);
	//	}else{
			ids 			= $('.painting').attr('rel').split(';');
			if(ids.length > 1)
			{
				var randomnumber 	= currentID;
				while(randomnumber == currentID){
					randomnumber	= ids[Math.floor(Math.random()*(ids.length+1))];
				}
				if(!randomnumber){
					randommbumber = ids[0];	
				}
				fadePainting('website_images/schilderijen/schil_'+randomnumber+'_medium.jpg');
			}
	//	}
	}
}

function fadePainting(src){
	$('.painting div img').attr('src2',src);
	$('.painting div img').unbind('load').stop().fadeTo(250, 0, function(){
		$('.painting div img').attr('src','');
		$('.painting div img').bind('load',function(){
			$('.painting div img').css({'marginLeft':((($('.painting div').width()-$(this).width()) / 2)) + 'px'})
			$('.painting div').animate({height:$(this).height()+'px'},250, function(){
				$(this).find('img').stop().fadeTo(250, 1);	 
			});											  
		});
		$('.painting div img').attr('src',$('.painting div img').attr('src2'));
	})
}

function submitAjaxForm(form){
	var form = form;
	var data = form.serialize();
	var buttonlabel = form.find('input[type=submit]').val();
	form.find('input, textarea, checkbox, radio, select').each(function(){
		if($(this).attr('title')){
			ar = $(this).attr('name').split('[]');
			naam = ar[0];
			data += '&veldnamen['+encodeURIComponent(naam)+']=' + encodeURIComponent($(this).attr('title'))
		}
	});
	$.ajax({
		type: 	form.attr('method'),
		url: 	form.attr('action'),
		data: 	data,
		success: function(msg){
			if(form.attr('target')){
				form.find('input[type=submit]').attr('disabled',false).val(buttonlabel);
				$("#"+form.attr('target')).html(msg);
			}
		},
		error: function(xhr, ajaxOptions, thrownError){
			alert(xhr.status);
			alert(thrownError);
		}
	});
	
	form.find('input[type=submit]').attr('disabled',true).val('Bezig met verzenden..');
	return false;	
}
