$(document).ready(function(){

	//
	// AddThis fix
	var addthis_config = {
		data_use_flash: false
	}
	
	// Create alert tool tips met link title als inhoud
	tooltip();
	
	// Maak mouse over thumbs groter
	$('.politicusFoto').hoverpulse({size: 15, speed: 200});
	
	// link list to link in li
	$('.links li').bind("click", function(e){
		window.location =  $(this).find("a:first").attr("href");
		return false;
	});
	
	// CMS 
	$('.verwijder').bind("click", function(e){
		//alert ( $(this).attr('item_id') );
		msg = ajaxAction( $(this).attr('item_id'), "del");

		$(this).hide();
		return false;
	});
	
	// maak sugestie zoek veld	
	$('input#q').each(function() {
		if($(this).val() === '') {
			$(this).val($(this).attr('title'));
		}

		$(this).focus(function() {
			if($(this).val() === $(this).attr('title')) {
				$(this).val('').css("color","#000");
			}
		});

		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).css("color","#BBB0A6").val($(this).attr('title'));
			}
		});
	});
	
	$('input#q').jsonSuggest(jsonData.politici, {maxResults:16, maxHeight:600 ,onSelect:callbackSearch});
});

function callbackSearch(item){
	//alert('You selected \'' + item.text + '\'\n\nHere is the full selected JSON object;\n');
	$('input#q').attr("disabled","disabled");
	$('input#navZoek').attr("disabled","disabled").val("laden...");
	window.location = "http://publitiek.nl/"+item.id;
}

this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("a.alert_tt").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip' class='alertBody'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("div.alert_tt").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip' class='alertBody'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("div.alert_pol").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip' class='alertBodyPol'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });
	$("a.alert_pol").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip' class='alertBodyPol'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.alert_tt").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
	$("div.alert_tt").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
	$("a.alert_pol").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
	$("div.alert_pol").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});	
};

function ajaxAction(item_id, action, dag, maand, jaar, dezeMaand, ditJaar){
	$.ajax({
		  url: "http://publitiek.nl/ajax.php",
		  type: "POST",
		  data: ({item_id: item_id, action: action, dag: dag, maand: maand, jaar: jaar, dezeMaand: dezeMaand, ditJaar: ditJaar}),
		  dataType: "text",
		  success: function(msg){
		  	 if(action == 'kalender'){
				$('#ajaxKalender').html(msg);
			 }else{
				return msg;
			}
		}
	});
}

// highlight search plugin
jQuery.fn.extend({
    highlight: function(search, insensitive, hls_class){
      var regex = new RegExp("(<[^>]*>)|(\\b"+ search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") +")", insensitive ? "ig" : "g");
      return this.html(this.html().replace(regex, function(a, b, c){
        return (a.charAt(0) == "<") ? a : "<strong class=\""+ hls_class +"\">" + c + "</strong>";
      }));
    }
});
