$().ready (function (){
	carregaTweets();
	$(".miniatura").colorbox({slideshow:false});
	$(".ver_mapa").colorbox({iframe:true,width:610,height:400});
	
	/*// We only want these styles applied when javascript is enabled
	$('div.navigation').css({'width' : '100px', 'float' : 'left'});
	$('div.content').css('display', 'block');

	// Initially set opacity on thumbs and add
	// additional styling for hover effect on thumbs
	var onMouseOutOpacity = 0.67;
	$('#thumbs ul.thumbs li').opacityrollover({
		mouseOutOpacity:   onMouseOutOpacity,
		mouseOverOpacity:  1.0,
		fadeSpeed:         'fast',
		exemptionSelector: '.selected'
	});
	
	// Initialize Advanced Galleriffic Gallery
	var gallery = $('#thumbs').galleriffic({
		delay:                     2500,
		numThumbs:                 3,
		preloadAhead:              10,
		enableTopPager:            true,
		enableBottomPager:         true,
		maxPagesToShow:            7,
		imageContainerSel:         '#slideshow',
		controlsContainerSel:      '#controls',
		captionContainerSel:       '#caption',
		loadingContainerSel:       '#loading',
		renderSSControls:          true,
		renderNavControls:         true,
		playLinkText:              'Iniciar Slideshow',
		pauseLinkText:             'Pausar Slideshow',
		prevLinkText:              '&lsaquo; Anterior',
		nextLinkText:              'Próxima &rsaquo;',
		nextPageLinkText:          'Próxima &rsaquo;',
		prevPageLinkText:          '&lsaquo; Anterior',
		enableHistory:             false,
		autoStart:                 false,
		syncTransitions:           true,
		defaultTransitionDuration: 300,
		onSlideChange:             function(prevIndex, nextIndex) {
			// 'this' refers to the gallery, which is an extension of $('#thumbs')
			this.find('ul.thumbs').children()
				.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
				.eq(nextIndex).fadeTo('fast', 1.0);
		},
		onPageTransitionOut:       function(callback) {
			this.fadeTo('fast', 0.0, callback);
		},
		onPageTransitionIn:        function() {
			this.fadeTo('fast', 1.0);
		}
	});*/
	
	$('#destaques_itens').anythingSlider({
		 // Appearance
		  width               : 450,      // Override the default CSS width
		  height              : 190,      // Override the default CSS height
		  resizeContents      : true,      // If true, solitary images/objects in the panel will expand to fit the viewport
		
		  // Navigation
		  startPanel          : 1,         // This sets the initial panel
		  hashTags            : false,      // Should links change the hashtag in the URL?
		  buildArrows         : true,      // If true, builds the forwards and backwards buttons
		  buildNavigation     : false,      // If true, buildsa list of anchor links to link to each panel
		  navigationFormatter : null,      // Details at the top of the file on this use (advanced use)
		  forwardText         : "", // Link text used to move the slider forward (hidden by CSS, replaced with arrow image)
		  backText            : "", // Link text used to move the slider back (hidden by CSS, replace with arrow image)
		
		  // Slideshow options
		  autoPlay            : true,      // This turns off the entire slideshow FUNCTIONALY, not just if it starts running or not
		  startStopped        : false,     // If autoPlay is on, this can force it to start stopped
		  pauseOnHover        : true,      // If true & the slideshow is active, the slideshow will pause on hover
		  resumeOnVideoEnd    : true,      // If true & the slideshow is active & a youtube video is playing, it will pause the autoplay until the video has completed
		  stopAtEnd           : false,     // If true & the slideshow is active, the slideshow will stop on the last page
		  playRtl             : false,     // If true, the slideshow will move right-to-left
		  startText           : "",   // Start button text
		  stopText            : "",    // Stop button text
		  delay               : 4000,      // How long between slideshow transitions in AutoPlay mode (in milliseconds)
		  animationTime       : 600,       // How long the slideshow transition takes (in milliseconds)
		  easing              : "easeInOutExpo"    // Anything other than "linear" or "swing" requires the easing plugin

	});

	$("form").submit(function (){
		$(".mensagem_obrigatorio,.mensagem_invalido").css("display","none");
		$(".obrigatorio").removeClass("erro_validacao");
		var campos=$(".obrigatorio");
		var sucesso=true;
		$.each(campos,function (){
			if ($.trim($(this).val())==""){
				$(this).addClass("erro_validacao");
				$(this).parent().find(".mensagem_obrigatorio").css("display","inline");
				$(this).val($.trim($(this).val()));	
				
				sucesso=false;
			}else if($(this).attr("name")=="email"){
				if (!/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($(this).val())){
					$(this).addClass("erro_validacao");
					$(this).parent().find(".mensagem_invalido").css("display","inline");
					sucesso=false;
				}	
			}
			
			
		});
		
		$.each(campos,function (){
			if ($.trim($(this).val())==""){
				$(this).focus();
				return false;
			}else if($(this).attr("name")=="email"){
				if (!/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($(this).val())){
					$(this).focus();
					return false;
				}	
			}
			
		});
		
		return sucesso;
	});

});

function carregaTweets(){
	$("#tweets_container").html("Carregando tweets...<br /><br />");  
	$.ajax({  
        url : "http://twitter.com/statuses/user_timeline/ccbeuj.json?callback=?&count=2",  
        dataType : "json",  
        timeout:15000,  
        success : function(data) {  
			$("#tweets_container").html("");  
			  for (i=0; i<data.length; i++) {  
						 $("#tweets_container").append('<div class="tweet'+(i==data.length-1?" ultimo":"")+'"> '+ '<p class="texto">' + data[i].text+' <span class="data">' + twitter_relative_time(data[i].created_at) +'</span></p></div>');  
						 
			  }  
		 },  
		 error : function() {  
					 $("#tweets_container").html("Não foi possível carregar tweets.<br /><br />");  
		 }
    });  	
}

function twitter_relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}
