$(function(){
    	
    //Events right column
    $('#slide-show').cycle({ 
        fx:     'turnDown', 
        //easing: 'bounceout', 
        delay:  -2000 
    });

    //Fix for ie7 and ie8 because it shows a white background, weird!!!
    $('#slide-show div').each(function(){
       this.style.background = 'transparent';
    });

    //Fix for cycle plugin when there is just one photo
    var tem_img = $('div#slide-show img');
    if (tem_img.length == 1)
        tem_img[0].style.marginTop = 0;
    
    $('#gallery-photos-slide').cycle({
        fx:    'scrollRight', 
        delay: -8000,
        next:   '#next-button', 
        prev:   '#prev-button'  
    });

    $(".tv-live-link").colorbox({
        href: "/home/live?ajax=true",
        onCleanup: function(){
            $('#gallery-photos-slide').cycle('resume');
            $('#slide-show').cycle('resume');
        }
    });

    $('#left-col').jScrollPane({
        showArrows: true,
        horizontalGutter: 30,
        verticalGutter: 30
     });


    $(".play-video").click(function(){
        this.href += '&ajax=true';
    });
    $(".play-video").colorbox();

    $(".play-audio").click(function(){
        this.href += '&ajax=true';
    });

    $(".play-audio").colorbox();

    $('.tv-live-link').click(function(){
        $('#gallery-photos-slide').cycle('pause');
        $('#slide-show').cycle('pause');
    });
    
    $('#pause-button').click(function(){
        $('#gallery-photos-slide').cycle('pause');
    });
    
    $('#play-button').click(function(){
        $('#gallery-photos-slide').cycle('resume');
    });
    
    var indicador = $('#item-pointer');
    var actual = $('.current-item');

    indicador.css('left', actual.position().left);
    
    $('div#pointer-right').width(actual.width() - 24  )

    $('ul#main-nav li a').hover(function(){
    	//Mouse enter
    	$li = $(this).parent();
    	indicador.stop().animate({
    		'left': $li.position().left
    	},{
    		duration:400,
    		easing:'easeInOutBack'
    	});
        
        $('div#pointer-right').stop().animate({
    		'width': $li.width() - 24
    	},{
    		duration:400,
    		easing:'easeInOutBack'
    	});
    },function(){
    	//Mouse leave
    	$li = $(this).parent();
    	indicador.stop().animate({
    		'left':actual.position().left
    	},{
    		duration:500,
    		easing: 'easeInOutBack'
    	});
        
        $('div#pointer-right').stop().animate({
    		'width': actual.width() - 24
    	},{
    		duration:400,
    		easing:'easeInOutBack'
    	});
        
    });
	
    //SERMONS
    
    $('#sermons-search form').submit(function(){
       if($('#sermon_key_words').val() === ''){
           $('#sermon_key_words').focus();
           return false;
       }
          
    });	

    //CONTACT US
    $('#message').val('');
    //Regular expression verification for email address
    function check_email(email){
        return (/^[\w-\.]+@\w+\.[a-zA-Z]{2,}$/).test(email);
    }

    $('#contact-form').submit(function(){
        
       if($('#name').val() == ''){
           alert('Ingrese su nombre')
           return false;
       }

       if(!check_email($('#email').val())){
           alert('Ingrese un correo válido')
           return false;
       }

       if($('#subject').val() == ''){
           alert('Ingrese el asunto de este mensaje')
           return false;
       }

       if($('#message').val() == ''){
           alert('Ingrese sus comentarios')
           return false;
       }
       //Showing a loading image	
       $('<div id="Cargando"></div>').appendTo('body').hide().fadeIn('500');	
       $.post(this.action, $(this).serialize(),function(response){
            if(response.success){
                alert('Gracias por escribirnos, muy pronto le contestaremos')
            }else{
                alert('Surgió un error inesperado, por favor vuelva a enviar su mensaje')
            }
            
            //Hidding loading image	
	    $('#Cargando').fadeOut(500,function(){
		$(this).remove();
	    });
       });

       return false;
       
    });
	
    

    //**************************************************************************
    //MINISTERIES

    $('#ministeries-container ul li h1').each(function(){
        $(this).css('top', '-' + $(this).outerHeight() + 'px');
	console.log('-' + $(this).outerHeight() + 'px')
    });

    $('#ministeries-container a').hover(function(){
        $(this).parent().find('h1').stop().animate({
            'top':0
        })
    }, function(){
        $(this).parent().find('h1').stop().animate({
            'top': (-$(this).outerHeight() - 40)
        })
    })
	

    
});

