jQuery(function(){

    // abre banner pequeno
    $('.mascara-peq').click(function(e){
	if($(this).parent().is('.banner-pequeno')){
	    $(this).toggleClass('aberta')
	    var $info = $(this).siblings('.info');
	    if($info.is(':not(:animated)'))
		$info.slideToggle('normal')
	    e.preventDefault();
	}
    })

    // slide esquerdo
    if($('#slide-ban-grande').children().size() > 1){
	setInterval(function(){
		$slide = $('.current-gslide');
		$fundoPreto = $slide.find('.fundo-preto');
		if($fundoPreto[0]){
		    $fundoPreto.animate({width: 'hide'}, function(){
			moveSlide($slide);
		    });
		}else{
		    moveSlide($slide);
		}
	    }
	, 5000)
    }

	function moveSlide($slide){
	    $slide.hide().removeClass('current-gslide')
		    if($slide.next()[0]){
			$slide = $slide.next();
		    }else{
			$slide = $slide.parent().children(':eq(0)');
		    }
		    $slide.fadeIn().addClass('current-gslide').find('.fundo-preto').hide().animate({width:'show'})
	}

    $('.voltar-ban').click(function(e){
	var el = $('#' + $(this).attr('rel'));
	$('.banner-direito-p').hide();

	el.show().find('.subinfo, .attachment-cabecalho-direito').hide().fadeIn()
	e.preventDefault();
    })

    // shadowbox
    Shadowbox.init({
	//handleOversize: "drag",
	modal: false,
	onOpen: function(slide) {
	    $('#sb-title').after($('#sb-nav-close')).after($('#sb-nav'));

	}
    });

    // close comments
    $('.fechar-comentarios').click(function(e){
	$('.lista-comentario .first').siblings().slideToggle();
	e.preventDefault();
    }).toggle(function(){
	$('.fechar-comentarios').text('Abrir comentários');
    }, function(){
	$('.fechar-comentarios').text('Fechar comentários');
    })

    $('.ver-galeria').click(function(){
	    $(this).closest('li').find('.itens-galeria').find(':first-child').click();
    })



    $('#programacao-box-overlay').live('click', (function(){
	$.hideAllProgramacaoModal();
    }))
    $('.toggle-programacao').click(function(e){
	e.preventDefault();
    }).toggle(function(){
	    $(this).closest('li').showProgramacaoModal();
	}, function(){
	    $(this).closest('li').hideProgramacaoModal();
    })


    $wrapper = $('#container-eventos');
    if($wrapper[0]){
	$thumbs = $wrapper.children();
	itemWidth = $thumbs.filter(':first').outerWidth(true);
	fullWidth = itemWidth * $thumbs.size()
	$wrapper.width(fullWidth+7) //7 = uma pequena "folga"

	// options

	// Cycle dos eventos home
	$('.bt').click(function(e){
	    var operator = '+';
	    var options = {velocity: 1200, stepSize: itemWidth}
	    if($(this).is('.bt-left'))
		operator = '-';

	    $('#overflowed-box').animate({
		scrollLeft: operator + '=' + options.stepSize + 'px'
	    }, options.velocity, 'easeOutBounce')

	    e.preventDefault();
	})
    }


    // all possible validations

	// adiciona validação regex
	jQuery.validator.addMethod(
		"regex",
		function(value, element, regexp) {
		    var check = false;
		    var re = new RegExp(regexp);
		    return this.optional(element) || re.test(value);
		},
		"Por favor, verifique o campo"
	);

        var validation = {
	    ignore: '.ignore',
	    onfocusout: false,
	    rules: {
		nome: 'required',
		evento: 'required',
		author: 'required',
		email: {
		    required: true,
		    email: true
		},
		'amigo-email': {
		    required: true,
		    email: true
		},
		mensagem: 'required',
		cidade: 'required',
		telefone: {
		    required: true,
		    regex: '[0-9()-]{8,}'
		},
		comment: 'required'
	    },
	    messages: {
		telefone: 'Você deve inserir um telefone válido',
		cep: 'Você deve inserir um cep válido',
		nome: 'Você deve inserir um nome',
		evento: 'Você deve inserir um evento',
		author: 'Você deve inserir um nome',
		mensagem: 'Você deve inserir uma mensagem',
		cidade: 'Você deve inserir uma cidade',
		assunto: 'Você deve inserir um assunto',
		email: {
		    required: 'Você deve inserir um e-mail',
		    email: 'Você deve inserir um e-mail válido'
		},
		'amigo-email': {
		    required: 'Você deve inserir o e-mail de seu amigo',
		    email: 'O e-mail de seu amigo deve ser válido'
		},
		comment: 'Você deve inserir um comentário'
	    }
	};



	$('.to-validate').each(function(){
	    form = $(this);
	    validation.invalidHandler = function(f, validator) {
		  var error = $('<li class="erro">Ocorreu um erro no preenchimento do formulário. Por favor preencha novamente.</li>').hide();

		  var firstChild = form.find('ul').find(':first');

		  if(!firstChild.is('.erro, .enviado-sucesso')){
		      firstChild.before(error);
		      error.slideDown();
		  }else if(firstChild.is('.enviado-sucesso')){
		      firstChild.after(error).remove();
		      error.slideDown();
		  }
		},
	    $(this).validate(validation);
	})

})

$.fn.extend({
  showProgramacaoModal:  function(){
	$.hideAllProgramacaoModal();
	$('body').append($('<div id="programacao-box-overlay" />'))
	$(this).each(function(){
	    var $this = $(this);
	    $this.after('<li class="placeholder"><!--placeholder--></li>')
	    $this.find('.more').removeClass().addClass('menos')
	    var pos = $this.offset();
	    $this.css({
		top: pos.top,
		left: pos.left - ($this.width()/4)
	    }).addClass('on').find('.amodal').hide().end().find('.modal').show()
	})
  },
  hideProgramacaoModal:   function(){
	$('.placeholder').remove();
	$('#programacao-box-overlay').remove();
	$(this).each(function(){
	    $(this).find('.menos').removeClass().addClass('more').end().removeClass().find('.amodal').show().end().find('.modal').hide();
	})
  }

})

$.hideAllProgramacaoModal =  function(){
      $('li.on').each(function(){
	    $(this).hideProgramacaoModal();
      })
  }
