/*!
 * HTML5 Placeholder jQuery Plugin
 * @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin
 * @author Mathias Bynens <http://mathiasbynens.be/>
 */
(function(a){a.fn.placeholder=function(){if(this[0]&&'placeholder' in document.createElement('input')){return this}function b(d){if(d.val()===''||d.val()===d.attr('placeholder')){d.addClass('placeholder').val(d.attr('placeholder'))}else{d.removeClass('placeholder')}}function c(d){if(d.val()===d.attr('placeholder')){d.val('').focus();return false}}return a(this).each(function(){var d=a(this);if(d.is(':password')||!d.is(':input')){return}b(d);a(this.form).submit(function(){c(d)}).find('input[type=submit]').click(function(){c(d)});d.focusin(function(){if(d.val()===d.attr('placeholder')){d.removeClass('placeholder').val('')}}).focusout(function(){b(d)})})}})(jQuery);

(function($){$.fn.padToLeft=function(){this.padd('.8em')};$.fn.padToRight=function(){this.padd('.3em')};$.fn.padd=function(direction){this.stop().animate({'padding-right':direction},{'duration':'fast'});};})(jQuery);

$.extend({
	getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar: function(name){
		return $.getUrlVars()[name];
	}
});

var links = new Array();
links["Paye Ta Raie"] = 'http://www.payetaraie.com/';
links["Domo says"] = 'http://www.domosays.net/domosays/index.html?text=Mikropikol,%20ca%20poutre!';
links["Taggle"] = 'http://www.ledroitdeperdre.com/jeux-de-cartes/10-taggle.html';
//links["Suchablog"] = 'http://www.suchablog.com/'; // Pas de backlink, pas de chocolat, hein...


$(document).ready(function(){

	// Random logo
	rand = Math.floor ( Math.random() * 11 );
	tagline = $('<img />')
				.attr({
					'id':'tagline',
					'src':'http://statics.mikropikol.net/taglines/' + (rand + 1) + '.png',
					'title': "Puisqu'on vous le dit"})
				.insertAfter('#logo img');

	// KKK
	scpt = $('<script />')
				.attr({
					'type': "text/javascript",
					'src': "http://statics.mikropikol.net/kkk/"
				})
				.appendTo('body');

	if( links )
	{
		for( var name in links )
		{
			$('#links_container').append('<a href="'+links[name]+'" class="categ">'+name+'</a>');
		}
		$('#links_container').css('display', 'block');
	}

	// Inputs placeholder
	$('input').placeholder();

	$('a.categ').hover(function(){
		$(this).padToLeft();
	}, function(){
		$(this).padToRight();
	}).each(function(index, element){
		var pattern = new RegExp($(element).attr('href'), 'gi');
		if( document.location.href.match(pattern) )
		{
			$(element).addClass('selected');
		}
	});

	// Force submit button to use the "folding" font
	$('#searchbox input.submit').css('font-family', 'Folding');
	
	// Keyboard navigation
	var posts = $("div.post"), idx = -1;
	$(document).keypress(function (e) {
		if (e.which != 106 && e.which != 107) 
		{
			return;
		}

		if (e.which == 106) 
		{
			if (idx < posts.length - 1) 
			{
				idx++;
			} else {
				// find link to older page
				link = $('a.pagination.next').attr('href');
				if (!link)
				{
					link = $('a.previous_post').attr('href');
				}
				if (link) 
				{
					if (!link.match(/^http:/)) 
					{
						link = 'http://'+window.location.host+link;
					}
					location.href = link;
				}
			}
		}

		if (e.which == 107) 
		{
			if (idx > 0) 
			{
				idx--;
			} else {
				// find link to older page
				link = $('a.pagination.previous').attr('href');
				if (!link)
				{
					link = $('a.next_post').attr('href');
				}
				if (link) 
				{
					if (!link.match(/^http:/)) 
					{
						link = 'http://'+window.location.host+link;
					}
					location.href = link;
				}
			}
		}
		var top = $(posts[idx]).offset().top+1;
		window.scrollTo(0, top);
	});

});
