(function($){var a={},c="doTimeout",d=Array.prototype.slice;$[c]=function(){return b.apply(window,[0].concat(d.call(arguments)))};$.fn[c]=function(){var f=d.call(arguments),e=b.apply(this,[c+f[0]].concat(f));return typeof f[0]==="number"||typeof f[1]==="number"?this:e};function b(l){var m=this,h,k={},g=l?$.fn:$,n=arguments,i=4,f=n[1],j=n[2],p=n[3];if(typeof f!=="string"){i--;f=l=0;j=n[1];p=n[2]}if(l){h=m.eq(0);h.data(l,k=h.data(l)||{})}else{if(f){k=a[f]||(a[f]={})}}k.id&&clearTimeout(k.id);delete k.id;function e(){if(l){h.removeData(l)}else{if(f){delete a[f]}}}function o(){k.id=setTimeout(function(){k.fn()},j)}if(p){k.fn=function(q){if(typeof p==="string"){p=g[p]}p.apply(m,d.call(n,i))===true&&!q?o():e()};o()}else{if(k.fn){j===undefined?e():k.fn(j===false);return true}else{e()}}}})(jQuery);
var bridge_url = 'http://www.marcianoarte.com/bridge/';


function initialize () {
	
	var view = $('#view').attr('rel');
	
	switch (view) {	
		case 'blog':
			return 0;
		break;
		
		case 'article':
			return 1;
		break;
		
		default:
			return 0;
		break;	
	}
	
}



function call (author, number) {
	
	$.ajax({
		url: bridge_url,
		data: 'a='+author+'&n='+number,
		crossDomain: true,
		dataType: 'html',
		type: 'POST',
		success: function(data) {
			show(data);
		}
	});
	
}

function show (obj) {

	var target = $('#bridge_content');
	var spinner = target.children('#spinner');
		
	if (obj != 'false') {
		spinner.fadeOut(function() {
			target.append(obj);
			target.children('#content').css('display', 'block');
			animate();
		});
	}
}


function animate() {

	var target = $('#bridge_content');
	var content = target.children('#content');
	var w_content = content.width() + 50;
	var objects = new Array();
	var positions = new Array();
	var count=0;
	var tot_width=0;
	
	$('.img_element').each(function() {
		objects[count] = $(this);
		if (count==0) {
			positions[count] = 0;
		} else {
			var last = objects[count-1];
			positions[count] = positions[count-1] + 170;
		}
		$(this).css('left', w_content);
	count++;});
	
	$('.img_single').mouseenter(function() {
		$(this).animate({
			opacity: 1
		}, 300)
	});
	
	$('.img_single').mouseleave(function() {
		$(this).animate({
			opacity: 0.8
		}, 300)
	});
	
	function loop(objects, n) {
		$.doTimeout(200, function() {
			var inn_count = n;
			var x = n + 1;
			if (inn_count<count) {
				var in_obj = objects[n];
				in_obj.css('visibility', 'visible');
				var pos = positions[n];
				in_obj.animate({
					left: pos
				}, 700, loop(objects, x));
			}
		});
	}
	
	loop(objects, 0);

}

$(document).ready(function() {
	
	var pattern = /\{autore=(.*)\}/i;
	var object = $('.bridge_article');
	var target = $('#bridge_content');
	var spinner = target.children('div#spinner');
	var title = $('#bridge_title');
	
	
	if (initialize()) {
		
		//perform the action, replace the pattern
		var content = object.html();		
		
		if (content.match(pattern)) {
						
			var matches = content.match(pattern);
			var author = matches[1];			
			var spinner_text = spinner.html();
			
			target.show();
			title.show();
			spinner.empty();
			spinner.append(spinner_text.replace(/%autore%/, author));
			spinner.show();
			object.replaceWith(content.replace(pattern, ''));			
			call(author, 5);
			
		}
		
		
		
	} else {
		
		// only remove pattern
		object.each(function() {			
			$(this).replaceWith($(this).html().replace(pattern, ''));			
		});
		
	}
	
});
