$(document).ready(function(){
   
   // ON RESIZE, AND ON LOAD, FOR RIGHT TICKER
   $(window).resize(ticker); 
   ticker();
   
});

function ticker(){
   
   // IF PAGE IS WIDTH ENOUGH, SHOW OR LOAD TICKER
   if($("body").width() >= 1380){
      
      // DOES NOT EXCIST, BUILT AND LOAD THE TICKER
      if(!$("#rightTicker").length){
         $("body").append("<div id=\"rightTicker\"></div>");
         $('#rightTicker').load('public/wall/ajax.php').fadeIn(1000);
         var tickerUpdates = setInterval(checkTicker, 5000); 
         
      // DOES EXCIST, SHOW THE TICKER
      }else{
         $('#rightTicker').fadeIn(1000);
      }
   // TO SMALL TO BE VISIBLE, FIDE IF EXCIST
   }else{
      if($("#rightTicker").length){
         $('#rightTicker').fadeOut(250);
      }
   }
}

function checkTicker(){
   $('#rightTicker').load('public/wall/ajax.php');
}




function set_ajax_link( el, event ){
	var url = el.attr("href");
	load_page_content( url );
}
function load_page_content( url ){
	$("#loadAgenda").load( url, { 'ajax': 'true' }, function(){
		$('#loadAgenda .agenda-nav a').click( function( event ) {
			set_ajax_link( $(this), event );
		});
	});
}

function set_ajax_link_sec( el, event ){
	var url = el.attr("href");
	load_page_content_sec( url );
}
function load_page_content_sec( url ){
	$("#columnLike").load( url, { 'ajax': 'true' }, function(){
		$('#columnLike a').click( function( event ) {
			set_ajax_link_sec( $(this), event );
		});
	});
}

$().ready(function() {
	function formatItem(row) {
		return row[0] + " (<strong>" + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}
	$("#message").autocomplete('public/search.php', {
		width: 356,
		max: 50,
		multiple: true,
		matchContains: true,
		multipleSeparator: " ",
		scrollHeight: 140,
		formatItem: formatItem,
		formatResult: formatResult
	});
	/*$("#suggest4").result(function(event, data, formatted) {
		var hidden = $(this).parent().next().find(">:input");
		hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
	});*/
});

$(document).ready(function(){
   /* agenda */
   $('#loadAgenda').load('public/agenda.php').fadeIn(1000);
   
   $('.agenda-nav a').live('click', function(event) {
      event.preventDefault();
      set_ajax_link($(this), event);
      return false;
   });
   
   /* column */
   $('#columnLike span').fadeIn(1000);
   
   $('.readmore a').click(function(event){
      event.preventDefault();
      $('.readmore a').slideUp(100);
      $('.columnmore').slideDown(1000);
   });
   
   $('#loadAgenda .agenda-nav a').click( function( event ) {
   	set_ajax_link( $(this), event );
   });
   
   
   /* rel url */
   $('a[rel*=external]').click( function() {
      window.open(this.href);
      return false;
   });
   
   /* autoResize */
   $('textarea#message').autoResize({
       // On resize:
       onResize : function() {
           $(this).css({opacity:0.8});
       },
       // After resize:
       animateCallback : function() {
           $(this).css({opacity:1});
       },
       // Quite slow animation:
       animateDuration : 100,
       // More extra space:
       extraSpace : 30
   });
   
   /* placeHolder */
   textReplacement($('#name'));
   textReplacement($('#email'));
   textReplacement($('#onderwerp'));
   textReplacement($('#message'));
   
   textReplacement($('#name-r'));
   textReplacement($('#email-r'));
   textReplacement($('#onderwerp-r'));
   textReplacement($('#message-r'));
      
   textReplacement($('#search'));
   
   // SET AGENDA HOVER
   $("#loadAgenda").ajaxComplete(function(){
      $('.agendaItem').tipsy({gravity: 'se', html: true, title: 'title'});
    });

   /* tooltip */
   $('.tooltip-results').tipsy({gravity: 's', html: true, title: 'title'});
   
   $('.tooltip-medal').tipsy({gravity: 's', html: true, title: 'alt'});
   
   $('.tooltip-smile').tipsy({gravity: 's', html: true, title: 'alt'});
   
   $('.tooltip-rss').tipsy({gravity: 'se', fade: true, title: 'title'});
   $('.tooltip-news').tipsy({gravity: 'e', fade: true, title: 'title'});
   
   $('.agendaItem').tipsy({gravity: 'n', html: true, title: 'title'});
   
   $('.dashboard').tipsy({gravity: 'n', fade: true, title: 'title'});
   $('.rss').tipsy({gravity: 'n', fade: true, title: 'title'});
   $('.login').tipsy({gravity: 'ne', fade: true, title: 'title'});
   
   $('#kletsdoosGameStart').tipsy({gravity: 's', fade: true, title: 'title'});
   
   $('.tooltip-kd-twitter').tipsy({gravity: 's', fade: true, title: 'title'});
   
   // COLUMN LIKE
   var column = $('#columnLike');
   
   $('.columnLike a').live('click', function(event) {
      event.preventDefault();
      set_ajax_link_sec($(this), event);
      return false;
   });
   
   // POLL
	var loader=$('#loader');
	var pollcontainer=$('#pollcontainer');
	loader.fadeIn();
	//Load the poll form
	$.get('public/poll.php', '', function(data, status){
		pollcontainer.html(data);
		animateResults(pollcontainer);
		pollcontainer.find('#viewresult').click(function(){
			//if user wants to see result
			loader.fadeIn();
			$.get('public/poll.php', 'result=1', function(data,status){
				pollcontainer.fadeOut(1000, function(){
					$(this).html(data);
					animateResults(this);
				});
				loader.fadeOut();
			});
			//prevent default behavior
			return false;
		}).end()
		.find('#pollform').submit(function(){
			var selected_val=$(this).find('input[name=poll]:checked').val();
			if(selected_val!=''){
				//post data only if a value is selected
				loader.fadeIn();
				$.post('public/poll.php', $(this).serialize(), function(data, status){
					$('#formcontainer').fadeOut(300, function(){
						$(this).html(data);
						animateResults(this);
						loader.fadeOut();
					});
				});
			}
			//prevent form default behavior
			return false;
		});
		loader.fadeOut();
	});
	
	function animateResults(data){
		$(data).find('.bar').hide().end().fadeIn('slow', function(){
							$(this).find('.bar').each(function(){
								var bar_width=$(this).css('width');
								$(this).css('width', '0').animate({ width: bar_width }, 1000);
							});
						});
	}
	
});
