/**
 * @author fili
 */

$(document).ready(function(){
	// rel=external
	$("a[rel=external]").each(function(){
		this.target="_blank";
    });

	// PrettyPhoto
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	// Red5 player
	/*
	if ($().flash.hasFlash(9)) {
		$('#noflash').hide();
		$('#red5Player').flash({
			src: '/flash/release/Red5DutchStream.swf',
			width: 640,
			height: 480,
			flashvars: {
				config: '/flash/release/config'
			}
		});
	}
	*/
	
	// Shoutbox
	var count = 0;
	function prepare(response) {
	  count = count+1;
	  
	  if (response.nickname == 'Admin') {
	  	nickClass = 'shoutbox-list-admin';
	  } else {
	  	nickClass = 'shoutbox-list-nick';
	  }
	  
	  var string = '<div class="shoutbox-list" id="list-'+count+'" title="'+response.info+'">'
	  	+ '<span class="shoutbox-list-time">'+response.time+'</span>'
	    + '<span class="'+nickClass+'">'+response.nickname+':</span>'
	    + '<span class="shoutbox-list-message">&quot;'+response.message+'&quot;</span>'
	    +'</div>';
      
	  return string;
	}
	
	function showResponse(response, status)  {
	  if(status == 'success') {
		  if (response.response) {
			  alert(response.response);
		  } else {
			  getMessages();
		  }
	  }
	}
	
	function validate(formData, jqForm, options) {
	  for (var i=0; i < formData.length; i++) { 
	      if (!formData[i].value) {
	          alert('U heeft niet alles ingevuld'); 
	          return false;
	      }
	  }
	  $("form input[name=message]").attr("value","");
	}
	
	function getMessages() {
		$.getJSON("shoutbox.php?action=view", function(json) {
			$('#shoutbox-list div').remove();
			messages = json['messages'];
			for(i=0; i < messages.length; i++) {
				$('#shoutbox-list').append(prepare(messages[i]));
			}
			$("div.counter").html(json['visitors']);
			setTimeout(getMessages, 12000);
			
			// hover tooltip
			$(".shoutbox-list").hover(function(e){
				//$(this).find(".shoutbox-list-info").css({'top':e.pageY,'left':e.pageX}).show();
				//$(this).find(".shoutbox-list-info").css({'top':0,'left':0}).show();
			},function(){
				$(this).find(".shoutbox-list-info").hide();
			});
		});
	}
	
	var options = { 
		target:        '#shoutbox-form',
		//beforeSubmit:  showRequest,
		dataType:       'json',
		beforeSubmit:   validate,
		success:        showResponse
	}; 
	$('#shoutbox-form').ajaxForm(options);
	
	// Update shoutbox
	getMessages();
});

