(function($){
 	$.fn.extend({ 
 		//plugin name - animatemenu
 		slideshow: function(options) {
			var defaults = {
			    animatePadding: 60
			};
			var options = $.extend(defaults, options);
    		return this.each(function() {
				  var o = options;
				  var obj = $(this);				
				  var items = $("li", obj);
				  var delay = function() { slideSwitch(obj,items); };
				  D = setInterval(delay, 10000 );
    		});
			
			function slideSwitch(obj,items) {
				var $active = obj.find('li.active');
				if ( $active.length == 0 ) $active = obj.find('li:last');
				
				var $next =  $active.next().length ? $active.next() : obj.find('li:first');
				$active.addClass('last-active');
				
				 $next.css({opacity: 0.0})
					.addClass('active')
					.animate({opacity: 1.0}, 1000, function() {
						$active.removeClass('active last-active');
				});
		
			};
    	}
	});
})(jQuery);



var functions = {
	internalFunctions : function()
	{
		$('ul.slideshow').slideshow({animatePadding: 30, defaultPadding:10});
	},
	// external links
	externalLinks : function(){
		
		function mycarousel_itemLoadCallback(carousel, state)
		{
			if (carousel.has(carousel.first, carousel.last)) {	return;	}
			jQuery.get(baseurl+'links-uteis/get-links',
				{
					first: carousel.first,
					last: carousel.last
				},
				function(data) {
					mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, data);
				},'json'
			);
		};
		
		function mycarousel_itemAddCallback(carousel, first, last, data)
		{
			carousel.size(parseInt(data.total));
			jQuery(data.items).each(function(i,val) {
				var obj = jQuery(this);
				carousel.add(first + i, mycarousel_getItemHTML(val.url,val.alink));
			});
		};
		
		function mycarousel_getItemHTML(url,alink)
		{
			return '<a title="visitar este site" target="_blank" href="'+alink+'"><img src="' + url + '" alt="" /></a>';
		};
		
		 jQuery('#links').jcarousel({
			// itemVisibleOutCallback: {onAfterAnimation: function(carousel, item, i, state, evt) { carousel.remove(i); }},
			itemLoadCallback: mycarousel_itemLoadCallback
		});
	},
	
	
	
	tabs:function()	{
		$('div.tabs li a').click(function()	{
			$('div.tabs ul li').removeClass('active');
			$(this).parent().addClass('active');
			$('div.tabs .item:visible').hide().removeClass('active');
			$('div.tabs div[id="'+$(this).attr('rel')+'"]').show().addClass('active');
		});
	},
	
	twitter:function()	{
		
		var username='JangadaSC'; // set user name
		var format='json'; // set format, you really don't have an option on this one
		var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?count=2&callback=?'; // make the url
		
		$('#twitter .inner').addClass('loading');
		
		$.getJSON(
			url,
			function(tweet){ 
			$.each(tweet,function(key,value) {
				var text  = toLink(value.text);
				$('#twitter .inner').append('<div class="item row'+key+'"><p>'+text+'</p><span>'+parseTwitterDate(value.created_at)+'</span></div>');
				//console.log(key,value);
				
			});
			
			//$('#twitter .inner').html(text); 
			 $('#twitter .inner').removeClass('loading');
			// get the first tweet in the response and place it inside the div
		});
		
		function parseTwitterDate(tdate) {
			var system_date = new Date(Date.parse(tdate));
			var user_date = new Date();
			if (K.ie) {
				system_date = Date.parse(tdate.replace(/( \+)/, ' UTC$1'))
			}
			var diff = Math.floor((user_date - system_date) / 1000);
			if (diff <= 1) {return "Agora";}
			if (diff < 20) {return diff + " segundos atrás";}
			if (diff < 40) {return "meio minuto atrás";}
			if (diff < 60) {return "menos de um minuto atrás";}
			if (diff <= 90) {return "um minuto atrás";}
			if (diff <= 3540) {return Math.round(diff / 60) + " minutos atrás";}
			if (diff <= 5400) {return "1 hora atrás";}
			if (diff <= 86400) {return Math.round(diff / 3600) + " horas atrás";}
			if (diff <= 129600) {return "1 dia atrás";}
			if (diff < 604800) {return Math.round(diff / 86400) + " dias atrás";}
			if (diff <= 777600) {return "1 semana atrás";}
			return "on " + system_date;
		}
		
		// from http://widgets.twimg.com/j/1/widget.js
		var K = function () {
			var a = navigator.userAgent;
			return {
				ie: a.match(/MSIE\s([^;]*)/)
			}
		}();


		
		function toLink(text)
		{
			if( !text ) return text;
			 
			text = text.replace(/((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,function(url){
			
			nice = url;
			if( url.match('^https?:\/\/') ) {
				nice = nice.replace(/^https?:\/\//i,'')
			}
			else
				url = 'http://'+url;
				return '<a target="_blank" rel="nofollow" href="'+ url +'">'+ nice.replace(/^www./i,'') +'</a>';
			});
			 
			return text;
		}	
	},
	
	galeria : function()
	{
		$("#galeria a").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	false
		});
	},
	
	formfunctions : function()
	{
		$(".filestyle").filestyle({
			image: baseurl+"images/layout/upload.gif",
			imageheight: 30,
			imagewidth: 80,
			width: 250
		});
	}
}


jQuery(document).ready(function() {
	functions.externalLinks();
	functions.tabs();
	functions.internalFunctions();
	functions.galeria();
	
	
});
