jQuery(document).ready(function($) {

	// Cases
	$('.cases').carousel('article');
	init_lightbox('.cases');

	// Portfolio
	$('.portfolio').data('list', $('.portfolio .wrap li')); // Caching
	$('.portfolio').carousel('li');
	
	$('.portfolio footer a').click(function() {
		var $filter = $(this).attr('rel');
		
		// Prevent double click
		if(!$(this).hasClass('active')) {
		
			// Reset elements
			$('footer a').removeClass('active');
			$(this).addClass('active');
			$('.portfolio .wrap').css('margin-left', 0);
			
			var list = $('.portfolio').data('list');
			$('.portfolio .wrap li').remove();
			$('.portfolio .wrap ul').html(list);
			
			// Filter the list
			$('.portfolio .wrap li').each(function() {
				if($filter == '' || $(this).hasClass($filter))
					$(this).hide().fadeIn();
				else
					$(this).remove();
			});
			
			// Restart
			$('.portfolio').carousel('li');	
			init_lightbox('.portfolio');
		}
			
		return false;
	});
	
	init_lightbox('.portfolio');

	// Twitter Feed
	$.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=edir&count=10&callback=?",
		function(data){
			var items = '';
			$(data).each(function(i, item) {
				var text = feed_linkify(item.text);
				var date = feed_datefy(item.created_at);
				items += '<article><p>'+text+'</p><time>'+date+'</time></article>';
			})
			$('.twitter .wrap').html(items);
			$('.twitter').carousel('article');
		});
		
	// Skoob Feed
	$.get("http://edirpedro.com.br/portfolio/skoob.rss.php",
		function(data){
			var items = '';
			$(data).find('item').each(function(i, item) {
				var title = feed_skoobfy($(item).find('title').text(), $(item).find('link').text());
				var time = feed_datefy($(item).find('pubDate').text());
				items += '<article><p>'+title+'</p><time>'+time+'</time></article>';
			});
			$('.skoob .wrap').html(items);
			$('.skoob').carousel('article');
		});
		
	// Menu Portfolio
	$('a#nav-portfolio').click(function() {
		if($('.rank').is(':visible')) {
			$('.rank').animate({
				'height': -10
			}, 500, function() { $(this).hide(); });	
		}
		return false;
	});
	
	// Menu Sobre
	$('.rank').data('height', $('.rank').height()); // Caching height size
	$('a#nav-sobre').click(function() {
		if($('.rank').is(':hidden')) {
			$('.rank').show().height(0).animate({
				'height': $('.rank').data('height')
			}, 500);
		}
		return false;
	});
	

	// Menu
	$('.menu a').click(function() {
		if($(this).attr('id') != 'nav-contato') {
			$('.menu a').removeClass('active');
			$(this).addClass('active');
		}
	});
	
	// Add Odd class to a list
	$('dl').each(function() {
		$('dt:odd, dd:odd', this).addClass('par');
	});
		
});

function init_lightbox(section) {
	$(section + ' a.lightbox').fancybox({
		titlePosition: 'over',
		//titleFormat: fancybox_title,
		overlayColor: '#000',
		padding: 0
	});
	
}

function fancybox_title(title, currentArray, currentIndex, currentOpts) {
	var text = title.replace(/([\w]+:\/\/[\w\.\/\?\-\=]+)/, '<a href="$1">visite o site</a>');
	var html = '<div id="fancybox-title-over">'+text+'</div>';
	return html;
}

// Zoom to Fancybox :o)
$.fn.fancybox.add_zoom = function(selectedIndex, selectedOpts) {
	$('#fancybox-zoom').remove();
	
	var zoom = $('<a id="fancybox-zoom" href="#" />');
	zoom.text($.fn.fancybox.defaults.autoScale ? 'Zoom' : 'Fit');
	zoom.attr('title', $.fn.fancybox.defaults.autoScale ? 'Zoom' : 'Fit');
	zoom.removeClass().addClass($.fn.fancybox.defaults.autoScale ? 'zoom-in' : 'zoom-out');
	zoom.click(function() { return $.fn.fancybox.do_zoom(selectedIndex); });
	zoom.appendTo('#fancybox-outer');
	
	$('#fancybox-overlay').height(0).width(0);
	$('#fancybox-overlay').css('width', $(document).width());
	$.fancybox.resize();
}

$.fn.fancybox.do_zoom = function(pos) {
	$('html, body').scrollTop(0).scrollLeft(0);
	$.fn.fancybox.defaults.autoScale = !$.fn.fancybox.defaults.autoScale;
	$.fancybox.pos(pos);
	return false;
}

/*
$.fn.fancybox.has_zoom = function() {
	var img = new Image();
    img.src = $('#fancybox-img').attr('src');
	content_width = $('#fancybox-content').width();
	content_height = $('#fancybox-content').height();
	if(img.width == content_width && img.height == content_height)
		return false;
	else
		return true;
}
*/

$.fn.fancybox.defaults.onComplete = function(selectedArray, selectedIndex, selectedOpts) {	
	$.fn.fancybox.add_zoom(selectedIndex, selectedOpts);
}

$.fn.fancybox.defaults.onClosed = function() {
	$.fn.fancybox.defaults.autoScale = true;
}

// Create links to urls and user names
function feed_linkify(text) {
	// Replaces any protocol like http:// and ftp://
	var replaced = text.replace(/([\w]+:\/\/[\w\.\/\?\-\=]+)/, '<a href="$1" target="_blank">$1</a>');
	// Replaces www.site.com
	replaced = replaced.replace(/(www[\w\.\/\?\-\=]+)/, '<a href="http://$1" target="_blank">$1</a>');
	// Replaces @user
	replaced = replaced.replace(/@([\w]+)/, '<a href="http://twitter.com/$1" target="_blank">@$1</a>');
	
	return replaced;
}

// Replaces GMT date to String date
function feed_datefy(date) {
	//Wed May 25 03:15:29 +0000 2011
	var cal = new Date(date.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/, "$1 $2 $4 $3 UTC"));
	return cal.getDate()+'.'+cal.getMonth()+'.'+cal.getFullYear()+' '+cal.getHours()+'h'+cal.getMinutes();
}

// Write the link of the book name
function feed_skoobfy(title, link) {
	var output = title.replace('edir ', '');
	output = output.replace(/(avaliou|vai ler|já leu|marcou como tenho|está lendo|marcou como desejado|abandonou|fez resenha para|quer trocar) (.*)/, '$1 <a href="'+link+'" target="_blank">$2</a>');
	return output;
}

