$(document).ready(function(){
	
	 //$('#sinopse_trimestre_texto').columnize({ columns: 2 });
	var duracao = 300;
	var ease = 'easeOutQuad';
	var spacing = 5;
	
	/*$('#header_img').find('img').imagesLoaded(function ($images) {
		$(this).fadeIn(300);
	});*/
	$('img', '#header_img').one('load', function() {
		$(this).stop().fadeIn(600);
	}).each(function() {
		if(this.complete){ 
			$(this).trigger('load'); 
			this.src = this.src;
		}
	});
	
	var $parent_menus = $("#nav li.parent");
	var $menu_toggle = $("#menu_toggle a");
	var $menu = $("#menu");
	
	var config = {    
		 over: showSub,
		 timeout: 100,
		 sensitivity: 6,
		 interval: 200,
		 out: hideSub 
	};

	$parent_menus.hoverIntent( config );
	
	$parent_menus.click(function(e){
		return false;
	}).find('ul a').click(function(e) {
	   e.stopPropagation();
	});

	
	function showSub(){
		 $(this).addClass('hovering').find('div.submenu_wrapper').stop().show("slide", { direction: "up" }, 200);
		// $(this).find('div.submenu_wrapper').stop().show(200);
	}
	function hideSub(){
		 $(this).removeClass('hovering').find('div.submenu_wrapper').stop().hide("slide", { direction: "up" }, 100);
		 //$(this).find('div.submenu_wrapper').stop().hide(200);
	}
	
	
	$menu_toggle.click(function(){
		$(this).toggleClass('closed');
		$menu.toggle( "slide", { direction: "right" }, 300 );
		return false;
	});
	$menu_toggle.tipsy({gravity: 'ne',fade: true});
	
	
	$("#nav li:not(.active) a").hover(function() {
		//$(this).stop().animate({ color: "#fe6c00" }, 200);
	},function() {
		//$(this).stop().animate({ color: "#222222" }, 500);
	});
	
	$('a[href="#"]').click(function(){return false;});
	
	$(".lupa img").hover(function () {											  
		$(this).stop().animate({
		opacity: .20
		}, 300);
	},			
	function () {				
		$(this).stop().animate({
		opacity: 1.0
		}, 600);
	}); 
	
	$(".album_img").hover(function () {	
		$(this).find('.ver_album').stop(true, true).fadeIn(300);
	},			
	function () {				
		$(this).find('.ver_album').stop(true, true).fadeOut(300);
	}); 
	
	$(".scrollable").scrollable();

	if($('#slider').length > 0){
		var childs = $('#slider').children().size();
		var slider = $('#slider').advancedSlider({
			width:980, 
			height:520, 
			skin: 'minimal-small', 
			shuffle: false, 
			slideshow : (childs > 1) ? true : false,
			fadeSlideshowControls: false, 
			pauseSlideshowOnHover: true, 
			timerAnimation: false, 
			slideshowDelay: 8000,
			sliceDuration : 1000,
			fadeNavigationArrows: false, 
			navigationButtonsNumbers: false,
			navigationButtons : false,
			horizontalSlices: 1, 
			verticalSlices: 4,
			sliceFade : true,
			slideshowControls: false,
			navigationArrows:false,
			effectType : 'simpleSlide',
			slicePattern: 'leftToRight', 
			slicePoint : 'topLeftToBottomRight',
			captionShowEffect: 'fade',
			captionBackgroundOpacity: 0,
			captionHeight: 300,
			simpleSlideEasing : 'easeInCubic',
			simpleSlideDirection: 'leftToRight',
			simpleSlideDuration: 500
		});
		
		if(childs > 1){
			$("a.slideshow_next").live('click', function() {
				slider.nextSlide();
			});
			$("a.slideshow_prev").live('click', function() {
				 slider.previousSlide();
			});
		}
	}
	
	/*$(".album_img").click(function(e) {
		e.preventDefault();
		var link_album = $(this).find('.open_album');
		var ajax_target = link_album.attr('href');
		var pasta = link_album.data('pasta');
		var _items = [];
		$.fancybox.showActivity();
		$.getJSON(ajax_target, function(data) {
			if(data){
				$.each(data, function(key, val) {
					_items.push({'href' : 'uploads/galeria/'+pasta+'/'+val.href, 'title' : val.title});
				});
				$.fancybox(_items, {
					'padding'			: 0,
					'transitionIn'		: 'elastic',
					'transitionOut'		: 'elastic',
					'type'              : 'image',
					'overlayColor'		: '#FFF',
					'overlayOpacity'	: 0.8,
					'cyclic'			: false,
					'showCloseButton'	: false,
					'showNavArrows'		: false,
					'titlePosition' 	: 'outside',
					'titleFormat'		: formatTitle
				});
			}
	});
	}).error(function() { $.fancybox.hideActivity(); });*/
	
	$('#galeria a.open_album').fancybox({
					'padding'			: 0,
					'transitionIn'		: 'elastic',
					'transitionOut'		: 'elastic',
					'type'              : 'image',
					'overlayColor'		: '#FFF',
					'overlayOpacity'	: 0.8,
					'cyclic'			: false,
					'showCloseButton'	: false,
					'showNavArrows'		: false,
					'titlePosition' 	: 'outside',
					'titleFormat'		: formatTitle
	});
	
	$("a.video").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'overlayColor'		: '#FFF',
				'overlayOpacity'	: 0.8,
				'title'			: $(this).attr('title'),
				'width'		: 680,
				'height'		: 495,
				'href'			: $(this).attr('href').replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					 'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				},
				'showCloseButton'	: false,
				'showNavArrows'		: false,
				'titlePosition' 	: 'outside',
				'titleFormat'		: formatTitleYT
			});
	
		return false;
	});
	

			
	$("#produtos .capa").click(function() {
		var grande = $(this).find('a.zoom_produto').attr('href');
		$.fancybox({
					'href'				: $(this).find('a.zoom_produto').attr('href'),
					'padding'			: 0,
					'transitionIn'		: 'elastic',
					'transitionOut'		: 'elastic',
					'type'              : 'image',
					'overlayColor'		: '#FFF',
					'overlayOpacity'	: 0.8
			});
	return false;
	});		
	
			
	$("a.lb").fancybox({
					'padding'			: 0,
					'transitionIn'		: 'elastic',
					'transitionOut'		: 'elastic',
					'type'              : 'image',
					'overlayColor'		: '#FFF',
					'overlayOpacity'	: 0.8
			});
			
	$("#produtos .notas_autor a").click(function() {
		$.fancybox({
				'padding'		: 20,
				'autoScale'		: false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'width'			: 500,
				'height'		: 500,
				'href'			: this.href,
				'type'			: 'iframe',
				'overlayColor'	: '#FFF',
				'overlayOpacity': 0.8
			});
	return false;
	});
	
	$('#header_img').find('img').imagesLoaded(function ($images) {
		$(this).fadeIn(300);
	});
	
	
	$("input[name='secure_newsletter']").val('siteform').hide();
	
	$('.enviar_newsletter').click(
			  function()
			  {
				  var email_newsletter = $('#email_newsletter').val();
				  var secure_newsletter = $("input[name='secure_newsletter']").val();
				  
				  var botao = $(this);
				  
				  botao.attr('disabled', true);
				  
				  if($.trim(email_newsletter)=="" || $.trim(email_newsletter)=="Insira o seu e-mail")
				  {
					$("#email_newsletter").addClass('invalid');
					$('#feedback_newsletter').addClass('erro').show('fast').html("Preencha o seu e-mail");
					botao.attr('disabled', false);
					return false; 
				  }
				  else if(!IsEmail(email_newsletter))
				  {
					$("#email_newsletter").addClass('invalid');
					$('#feedback_newsletter').addClass('erro').show('fast').html("E-mail inválido");
					botao.attr('disabled', false);
					return false; 
				  }
				  else
				  {
				  $('#feedback_newsletter').html("Aguarde").show('fast');
				  var dataString = 'email=' + email_newsletter + '&secure_newsletter=' + secure_newsletter + '&ci_token=' + $.cookie("ci_csrfprotection_cookie");
				  $.ajax({
						url: 'newsletter/subscribe',
						type: 'POST',
						data: dataString,
						dataType: "json",
						success: function(msg){
							if(msg.sent){
								$('#feedback_newsletter').html("Inscrição concluída com sucesso!").delay(6000).hide('slow');
							}
							else{
								$('#feedback_newsletter').html("Este e-mail já está inscrito!").delay(6000).hide('slow');
							}
							botao.attr('disabled', false);
						}
					});
				}
				  return false;
			  }
		);
		
		$('.required').click(function(){
				$(this).removeClass('invalid');  
				$('#feedback').removeClass().hide("fast");
		});
	
	$("a[href$='pdf']").attr('target','_blank');
	
});// fim ready
function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div id="galeria-title">' + (title && title.length ? '<b>' + title + '</b>' : '' ) + '<span class="controls"><span class="pag"><a href="javascript:;" onclick="$.fancybox.prev();"><img src="imagens/prev_fancybox.png" width="16" height="16" /></a><span>' + pad((currentIndex + 1),2) + ' / ' + pad(currentArray.length,2) + '</span><a href="javascript:;" onclick="$.fancybox.next();"><img src="imagens/next_fancybox.png" width="16" height="16" /></a></span><a class="close" href="javascript:;" onclick="$.fancybox.close();"><img src="imagens/close_fancybox.png" width="16" height="16" /></a></span></div>';
}
function formatTitleYT(title, currentArray, currentIndex, currentOpts) {
    return '<div id="galeria-title">' + (title && title.length ? '<b>' + title + '</b>' : '' ) + '<span class="controls"><a class="close" href="javascript:;" onclick="$.fancybox.close();"><img src="imagens/close_fancybox.png" width="16" height="16" /></a></span></div>';
}
function pad(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}
function IsEmail(email) {
	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return regex.test(email);
}
function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
}
