(function($) {
	$('document').ready(function() {
	
	/** Equivalente a constante de projeto URL. */
	url = $('base').attr('href');
	
	/** Placeholder. */
	jQuery.fn.extend({
		printLabel: function( label ) {

			$(this).val(label);
			$(this).focus(function(){ if($(this).val() == label) {$(this).val(''); }});
			$(this).blur(function(){ if($(this).val() == '') {$(this).val(label); }});

			return this;
		}
	});
	
	/**
	 * Popup ouça agora.
	 */
	$('.pop').live('click', function(e) {
		e.preventDefault();
		window.open($(this).attr('href'), $(this).attr('name'), 'width=400,height=150,resizable=no,status=no,scrollbars=no&titlebar' + $(this).attr('name'));
	});
	
	/**
	 * Fancybox.
	 */
	$('.fancybox-img').fancybox({
		'titleShow'	: true,
		'autoscale'	: false,
		'overlayOpacity': 0.85,
		'overlayColor'	: '#f0f0f0',
		'padding'	: 5,
		'margin'	: 5,
		'centerOnScroll' : true
	});
	$('.fancybox-page').fancybox({
		'type'              : 'iframe',
		'titleShow'         : false,
		'hideOnContentClick': false,
		'width'				: 650,
		'height'			: 575,
		'padding'			: 0,
		'margin'			: 0,
		'scrolling'			: 'no'
	});
	
	/**
	 * Adquirindo tweets.
	 */
	twitter_user	= 'nazaradio';
	twitter_tweeets	= $('meta#config-tweets').attr('content') === undefined ? 5 : $('meta#config-tweets').attr('content');
	function formatar_data(data){var mes = [];mes['01'] = 'Jan';mes['02'] = 'Feb';mes['03'] = 'Mar';mes['04'] = 'Apr';mes['05'] = 'May';mes['06'] = 'Jun';mes['07'] = 'Jul';mes['08'] = 'Aug';mes['09'] = 'Sep';mes['10'] = 'Oct';mes['11'] = 'Nov';mes['12']='Dec';for(var r in mes){data=data.replace(mes[r],r);}return data.replace(/^... (\d+) (\d+) ([0-9]{2}:[0-9]{2}):.. [^ ]+ (\d+)$/,'$2/$1/$4 as $3');}
	function tweet_build_links(tweet){var mention={regexp:/@([A-Za-z0-9_]+)( ?)/ig,strrep:'<a href="http://twitter.com/$1">@$1</a>$2'};var hashtag={regexp:/#([^:;., ]+)( ?)/ig,strrep:'<a href="http://api.twitter.com/#!/search?q=$1" class="tweet-link">#$1</a>$2'};var link = {regexp : /(http:\/\/[^ ]+)( ?)/ig,strrep : '<a href="$1" class="tweet-link">$1</a>$2'};	return tweet.replace(link.regexp, link.strrep).replace(mention.regexp,mention.strrep).replace(hashtag.regexp,hashtag.strrep);}
	jQuery.jTwitter(twitter_user, twitter_tweeets, function(data){
		$.each(data, function(i, post){
			$('ul#tweets').append(
				'<li>' 
					+ '<p>' + tweet_build_links(post.text) + '</p>'
					+ '<a class="tweet-time"- target="_blank" href="http://twitter.com/#!/' + twitter_user + '/status/' + post.id_str + '">' + formatar_data(post.created_at) + '</a>'
				+ '</li>'
			);
		});
	});
	
	/**
	 * Input de notas no comentário.
	 */
	$('div.rating-box input').hide();
	$('div.rating-box a.rating-star').show();
	$('div.rating-box a.rating-star').live('click', function(e) 
	{
		e.preventDefault();
		var rate = parseInt($(this).attr('rel'));
		
		$('#rate-' + rate).attr('checked', 'checked');
		
		$('div.rating-box a.rating-star').css('background-position', '0 18px');
		while( rate !== 0 ) {
			$('a.rating-star[rel=' + rate + ']').css('background-position', '0 0');
			rate --;
		}
	});
	
	/**
	 * Tabgroups.
	 */
	$('.tab').hide()
	$('.tab:first').show();
	$('.tab-group a').live('click', function(e) 
	{
		e.preventDefault();
		if ( $(this).hasClass('active') === true ) {
			return;
		}
		
		$('.tab-group a').removeClass('active');
		$(this).addClass('active');
		
		$('.tab').stop(true,true).slideUp(400);
		$ ( $('ul.tab-group a.active').attr('href').replace(/^.*?#([^#]+)$/, '#tab-$1') ).fadeIn(600);
	});
	
	/**
	 * Tooltips.
	 */
	$('.tooltip a').live('click', function(e) {
		e.preventDefault();
	});
	$('.tooltip a').hover(function() {
		$(this).next('div').stop(true, true).fadeIn(300);
	}, function() {
		$(this).next('div').fadeOut(300);
	});
	
	/**
	 * Abrir formulário de recado (mural).
	 */
	$('form#form-mural').hide();
	$('a#abrir-form-mural').live('click', function(e) {
		e.preventDefault();
		$('form#form-mural').slideDown('slow');
	});
	
	/** 
	 * Links sem retorno.
	 */
	$('a.epd').live('click', function(e) { e.preventDefault(); });
	
	});
})(jQuery);
