$(document).ready(function(){
						   
	// shoutbox
	var maxChar = 300;
	$('form#daddy-shoutbox-form textarea').keyup(function(event)
	{
		var length = $(this).attr('value').length;
		$('div.aantal span.size').html(length);
		if(length > maxChar)
		{
			$('div.aantal span').css('color','red');
			$('div.aantal span.error').html('Te veel tekens!');
		}
		else
		{
			$('div.aantal span').css('color','black');
			$('div.aantal span.error').html('');
		}
	});
	
	var count = 0;
	var files = '/files/jquery-shoutbox/';
	var lastTime = 0;
	
	// functions van de shoutbox
	function prepare(response) {
	  var d = new Date();
	  count++;
	  d.setTime(response.time*1000);
	  var mytime = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
	  var string = '<div class="shoutbox-list" id="list-'+count+'">'
		  + '<span class="shoutbox-list-time">'+mytime+'</span>'
		  + '<span class="shoutbox-list-nick">'+response.nickname+':</span><br />'
		  + '<span class="shoutbox-list-message">'+response.message+'</span>'
		  +'</div>';
	  
	  return string;
	}
	
	function success(response, status)  { 
	  if(status == 'success') {
		lastTime = response.time;
		$('#daddy-shoutbox-response').html('<img src="'+files+'images/accept.png" />');
		$('#daddy-shoutbox-list').append(prepare(response));
		$('textarea[name=message]').attr('value', '').focus();
		$('#list-'+count).fadeIn('slow');
		var objDiv = document.getElementById("daddy-shoutbox-list");
		objDiv.scrollTop = objDiv.scrollHeight; 
		timeoutID = setTimeout(refresh, 0);
	  }
	}
	
   function validate(formData, jqForm, options) {
		var valTitel = $('input.titel').attr('value');
		var valText	 = $('textarea').attr('value');
		if(!valTitel){
			alert('geen naam ingevuld!');
			return false;
		} 
		if(!valText){
			alert('geen Bericht ingevuld!');
			return false;
		}
		if(valText.length > 300){
			alert('te veel tekens in bericht!');
			return false;
		}
	  $('#daddy-shoutbox-response').html('<img src="'+files+'images/loader.gif" />');
	  clearTimeout(timeoutID);
	}

	function refresh() 
	{
	  	$.getJSON(files + "daddy-shoutbox.php?action=view&time=" + lastTime, function(json) 
		{
			if(json.length) 
			{
				  for(i=0; i < json.length; i++) 
				  {
						$('#daddy-shoutbox-list').append(prepare(json[i]));
						$('#list-' + count).fadeIn('slow');
				  }
				  var j = i-1;
				  lastTime = json[j].time;
				$('div#daddy-shoutbox-list').scrollTop(2000, 0);
				//alert(lastTime);
			}
		 });
	  	timeoutID = setTimeout(refresh, 2000);
	}
	
	// wait for the DOM to be loaded 		
	var options = { 
	  dataType:       'json',
	  beforeSubmit:   validate,
	  success:        success
	}; 
	$('#daddy-shoutbox-form').ajaxForm(options);
	timeoutID = setTimeout(refresh, 100);
					
	// fancy scroller sponsoren
	$('div.scroller').fancyScroll({
		interval 	: 4000,
		speed		: 1000,
		ease		: 'easeInOutCubic',
		start		: Math.floor(Math.random() * 3)
	});
	

});