/* Author: 
Francis Rebouças
*/
Visao = {
	init:function(){
			
		$("#tabs").tabs();
		//* Máscaras de Formulários/*/
		$("input.cpf").setMask('cpf');
		$("input.cep").setMask('cep');
		$("input.telefone").setMask('phone');	
		$("input.cnpj").setMask('cnpj');
		$("input.time").setMask('time');
		$("input.date").setMask('date');
		$("input.validade").setMask('99/9999');
		$("input.decimal").setMask('decimal');
		
		
		
		
		$(".news-slide").jcarousel({
			auto: 5,
			wrap: 'last',
			scroll:1,
			initCallback: Visao.carouselCallback
		});
		
		/*Notificação Ajax*/
		$(".loading-notification").ajaxStart(function(){
			$(this).show();
		});
		$(".loading-notification").ajaxStop(function(){
			$(this).hide();
		});		
		
		$("#flashMessage").show(500).delay(1800).fadeOut(400);
	
		return false;
	},
	carouselCallback : function(carousel){
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	},
	home : function(){
		$('.form-enquete').bind('submit',function(){	
			$url = $(this).attr('action');
			$voto =   $('input[type=radio]:checked').val();
			if($voto != undefined){
				$.post($url, {
					opcao : $voto
				}, function(data){
					resultado = $.parseJSON(data);
					html = '';
					if(resultado.success == 'true'){
						resultado = resultado.respostas;
						$.each(resultado,function(k, v){						
							html += v.pergunta + "<div class='barra-100'><div class='porcentagem' style='width: " + v.porcentagem + "'></div></div><div style='margin-bottom:10px;'> <small>"+ v.porcentagem +"</small> </div>";
						});
						//console.log(html);
						$('.form-enquete fieldset').html(html);
						$('.form-enquete input[type=image]').attr({
							'disabled' : 'disabled'
						});
					
					}else{
						alert(resultado.msg);	
					}
				});
			}else{
				alert('Selecione pelo menos um 1 resposta!');
			}
			
			return false;
			
		});
		$('.slides').cycle({
			timeout: 8000,
			pager:'.pages'
		});
		var inc = 0;
		$.doTimeout('loop', 8000, function(){
			$.get(webroot + 'presentes/random.html', function(data){
				$('.catalogo').html(data);
			});			
			inc++;
			return true;
		});
	},
	apadrinhamento: function(){
		$.validator.addMethod(
			"date",
			function ( value, element ) {
				var bits = value.match( /([0-9]+)/gi ), str;
				if ( ! bits )
					return this.optional(element) || false;
				str = bits[ 1 ] + '/' + bits[ 0 ] + '/' + bits[ 2 ];
				return this.optional(element) || !/Invalid|NaN/.test(new Date( str ));
			},
			"Favor colocar uma data no formato dd/mm/yyyy"
			);
		$(".form-validate").validate();		
	},
	zoom:function(){
		$(".zoom").fancybox({
			'titleShow'     : false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack'
		});	
	},
	galerias: function(){
		$(".galeria-fotos").jcarousel();	
	}
	
};
$(function(){
	
	
	Visao.init();
	Visao.zoom();
	Cufon.replace('h1,h2,h3,h4,h5, .gilsans',{
		fontFamily: 'Gill Sans' , 
		hover: true
	});
	if(!Modernizr.input.placeholder){
		$("input").each(
			function(){
				if($(this).val()=="" && $(this).attr("placeholder")!=""){
					$(this).val($(this).attr("placeholder"));
					$(this).focus(function(){
						if($(this).val()==$(this).attr("placeholder")) $(this).val("");
					});
					$(this).blur(function(){
						if($(this).val()=="") $(this).val($(this).attr("placeholder"));
					});
				}
			});
	}
});



//Twitter Plataform
(function () {
	if (window.__twitterIntentHandler) return;
	var intentRegex = /twitter\.com(\:\d{2,4})?\/intent\/(\w+)/,
	windowOptions = 'scrollbars=yes,resizable=yes,toolbar=no,location=yes',
	width = 550,
	height = 420,
	winHeight = screen.height,
	winWidth = screen.width;

	function handleIntent(e) {
		e = e || window.event;
		var target = e.target || e.srcElement,
		m, left, top;
		while (target && target.nodeName.toLowerCase() !== 'a') {
			target = target.parentNode;
		}
		if (target && target.nodeName.toLowerCase() === 'a' && target.href) {
			m = target.href.match(intentRegex);
			if (m) {
				left = Math.round((winWidth / 2) - (width / 2));
				top = 0;
				if (winHeight > height) {
					top = Math.round((winHeight / 2) - (height / 2));
				}
				window.open(target.href, 'intent', windowOptions + ',width=' + width + ',height=' + height + ',left=' + left + ',top=' + top);
				e.returnValue = false;
				e.preventDefault && e.preventDefault();
			}
		}
	}
	if (document.addEventListener) {
		document.addEventListener('click', handleIntent, false);
	} else if (document.attachEvent) {
		document.attachEvent('onclick', handleIntent);
	}
	window.__twitterIntentHandler = true;
}());


















