$(document).ready(function(){

    Buzz.twitter.ini();
    Ajax.init();
    
    //Galeria de Photos - PRETTYPHOTO
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});

   // -------------- GALERIA ---------------

	$('.sliderGaleria').slider({
		items : 6,
		mouseMove : false,
		left : '.previousSlider',
		right : '.nextSlider',
		duration : 2000,
		onStart : function(index) {
                        
		},

		onClick : function(index) {


		}
	});

        $(".maskCPF").mask("999.999.999-99");
       
        $('.formVoto').submit(function(){
            var submit =  true;

            // Verifica se todas as categorias foram votadas
            $(".parpr").each(function(index,obj){
                var checked = false;
                var teste = '';
                $(this).find(".cd_parpr").each(function(i,o){
                   if(o.checked== true)
                       checked = true;
                });
                if(checked==false){
                    submit = false;
                }
            });
            if(submit== false){
                $('.formVoto').showMensageError("Preencha todas categorias");
            }
            return submit;
        });
		
		
		// --------- Destaque -----------
		var $lista = $('.destaquePhotos');

		var $items = $lista.find('li');
		var $total = $items.size();
		var atual = 0;

		$items.css({
		'position': 'absolute',
		'top': 0,
		'left': 0
		});

		$items.hide();

		$items.eq(atual).show();

		setInterval(function() {
		atual++;

		if (atual >= $total) { atual = 0; }

		$items.eq(atual).fadeIn('slow', function() {
		$items.eq(atual-1).fadeOut('slow');
		});
		}, 5000);
		
});

(function($) {
    $.fn.extend({
        showMensageError: function(msg) {
            return this.each(function(index,o) {
               // alert(msg);
                $(o).find('.msgForm').html('\n\
                        <ol class="aviso error">\n\
                                    <li>'+msg+'</li>\n\
                                </ol>\n\
                    ').show();
            });
        },
        showMensageSucess: function(msg) {
            return this.each(function(index,o) {
                $(o).find('.msgForm').html('\n\
                        <ol class="aviso sucess">\n\
                                    <li>'+msg+'</li>\n\
                                </ol>\n\
                    ');
            });
        }
    });
})(jQuery);

// ------------------ TWITTER ------------------
var Buzz = {
	twitter : {
		box : '.twitts',
		modelo : '',
		twitts : new Array(),
		data : {},

		buscarTwittsTimeLine : function() {
			twitts = new Array();
			// busca na timeline
			var quant = parseInt($(this.box + ' input[name="quantidade"]')
					.val());
			quant = (quant == 0 ? 5 : quant);

			$
					.ajax({
						// url:
						// 'http://api.twitter.com/1/fivecom/lists/fivecom/statuses.json?per_page=6',

						url : 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=prazerecia&per_page='
								+ quant + '&count=' + quant,
						dataType : 'jsonp',
						success : function(data) {
							Buzz.twitter.data = data;
							Buzz.twitter.escreverTwitts();
							$(Buzz.twitter.box).show();
						}
					});

			// return twitts;
		},
		aplicarLinks : function(text) {

			text = text.replace(
					/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g,
					function(url) {
						return url.link(url);
					});

			text = text.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
				return ('<a target="_blank" href="http://twitter.com/'
						+ u.replace("@", "") + '">' + u + '</a>');
			});

			text = text
					.replace(
							/[#]+[A-Za-z0-9-_]+/g,
							function(u) {
								return ('<a target="_blank" href="http://twitter.com/search?q=%23' + u
										.replace("#", ""))
										+ '">' + u + '</a>';
							});

			return text;
		},
		escreverTwitts : function() {
			var data = Buzz.twitter.data;
			$.each(data, function(i, item) {
				item.text = Buzz.twitter.aplicarLinks(item.text);
				item.data = new Date(item.created_at);
				Buzz.twitter.twitts.push(item);
			});

			$(Buzz.twitter.box).html("&nbsp;");
			$.each(Buzz.twitter.twitts, function(i, item) {
				var model = Buzz.twitter.modelo;

				model = model
						.replace("#USER_IMG#", item.user.profile_image_url);
				model = model.replace("#USER_NAME#", item.user.screen_name);
				model = model.replace("#TEXT#", item.text);

				// var tweet = '<div id="twitterCont"><img
				// src="'+item.user.profile_image_url+'" width="31" height="31"
				// /><p><a
				// href="http://www.twitter.com/'+item.user.screen_name+'"
				// target="_blank"><strong>'+item.user.name+'</strong></a><br
				// />'+item.text+'<span> - at
				// '+item.data.getUTCHours()+':'+item.data.getUTCMinutes()+'</span></p></div>';
				$(Buzz.twitter.box).append(model);
			});
		},
		ini : function() {
			if ($(this.box).size() > 0) {
				this.modelo = $(this.box).html();
				this.buscarTwittsTimeLine();
			}
		}
	}

};

// ============================= fim TWITTER


