$(document).ready(function(){
    
    $carregaImagem = function($url, $titulo, $descricao, $scroll){
        $('#midiaDestaque .info h3, #midiaDestaque .info p').text('');
        $('#midiaDestaque .img').html('<div id="imgLoading"></div>').animate({opacity: 1.0}, 1000, function(){
            $img = new Image();
            $($img).load(function(){
                $(this).css('display', 'none');
                $('#midiaDestaque .img').html(this);
                $(this).fadeIn();
                $('#midiaDestaque .info h3').text($titulo);
                $('#midiaDestaque .info p').text($descricao);
                if($scroll){
                    $top = $('#midiaDestaque').offset().top - 10;
                    $('html, body').animate({ scrollTop: $top }, 'slow');
                }
            }).error(function () {
                $('#midiaDestaque .img').append('<p>Ocorreu um erro durante o carregamento da imagem. Por favor, tente novamente.</p>');
            }).attr('src', $url);
        });
    };
    
    $('#formContato label').hover(function(){
        $id = $(this).attr('for');
        $('#' + $id).addClass('hover');
    }, function(){
        $('#' + $id).removeClass('hover');
    });
    
    $('#formContato .form').hover(function(){
        $(this).addClass('hover');
    }, function(){
        $(this).removeClass('hover');    
    });
    
    $('#formContato .form').focus(function(){
        $('p.msgErrorServer').hide();
        $('#formContato .form').removeClass('over');
        $(this).addClass('over');
    });
    
    $('#formContato .form').blur(function(){
       $(this).removeClass('over');
    });
    
    $('input#url').focus(function(){
        $val = $(this).val();
        if($val.indexOf('http://') == '-1'){
            $(this).val('http://' + $val);
        }
    }).blur(function(){
        if($.trim($(this).val()) == 'http://'){
            $(this).val('').removeClass('error');
            $('p.msgError').hide();
        }
    });
    
    $('#formContato a.btnVoltar').click(function(){
        $('.msgSucesso').hide();
        $('span.min').removeClass('hidden');
        $('.formInner').fadeIn('slow');
        return false;
    });
    
    $("#formContato").validate({ errorContainer: $(".msgError"), errorLabelContainer: $(".msgErrorLabel"),
        submitHandler: function() {
            $('p.msgErrorServer').hide();
            $('.loading').fadeIn();
            $wait = setTimeout(function(){
                $.ajax({
                    type: "POST",
                    data: $("#formContato").serialize(),
                    dataType: "json",
                    url: $("#formContato").attr('action'),
                    success: function(json){
                        if(json.status == "ok"){
                            $('.formInner, .loading').hide();
                            $('span.min').addClass('hidden');
                            $('.formInner .form').val('');
                            $('.msgSucesso').fadeIn();
                        } else {
                            $('.loading').hide();
                            $('p.msgErrorServer').fadeIn('fast');
                        }
                    }
                });
            }, 1000);
        },
        error: function(xhr, text, err){
            alert('bla');
            $('.loading').hide();
            $('p.msgErrorServer').fadeIn('fast');
        }
   });
    
    $('ul#listAlbumHome li a').hoverIntent(function(){
        $(this).parent().fadeTo('fast', 1.0);
    }, function(){
        $(this).parent().fadeTo('fast', 0.7);    
    });
    
    $('ul.jcarousel').removeClass('hide').jcarousel({ scroll: 2, animation: 'slow' });
    
    $('#midiaDestaque').each(function(){
        $url = $(this).attr('rel').split('|')[0];
        $titulo = $(this).attr('rel').split('|')[1];
        $descricao = $(this).attr('rel').split('|')[2];
        $carregaImagem($url, $titulo, $descricao, false);
    });
    
    $('.navGaleria ul li a').hoverIntent(function(){
        $(this).fadeTo('fast', 1.0);
    }, function(){
        if(!($(this).hasClass('hover'))){
            $(this).fadeTo('fast', 0.7);    
        }        
    }).click(function(){
        if($(this).parents('.navGaleria').hasClass('navGaleriaFotos')){
           $('.navGaleria ul li a.hover').removeClass('hover').fadeTo('fast', 0.7);
            $(this).addClass('hover');
            $url = $(this).attr('href');
            $titulo = $(this).find('img').attr('alt');
            $descricao = $(this).find('img').attr('rel');
            $carregaImagem($url, $titulo, $descricao, true);
            return false;
        }        
    });
    
    $('.post-view a').each(function(){
        $href = $(this).attr('href');
        if($href.indexOf('/uploads/') > 0){
            $(this).fancybox({ 'overlayColor': '#14212e', 'overlayOpacity': '0.6' });
        } else if($href.indexOf('www.youtube.com') > 0){
            $title = $(this).text();
            $id = $href.split('watch?v=')[1].split('&')[0];
            $(this).after('<iframe title="' + $title + '" class="youtube-player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/' + $id + '?rel=0" frameborder="0"></iframe>')
            $(this).remove();
        }
    });
    
    $('#formBusca').submit(function(){
        $val = $(this).find('.form').val();
        $title = $(this).find('.form').attr('title');
        if($.trim($val) == '' || $.trim($val) == $title){
            $(this).find('.form').val('').focus();
            return false;
        }
    });
    
    $('ul#listVideosHome li a').hover(function(){
        $(this).find('.img span').addClass('hover');
        $(this).find('h3').addClass('hover');
    }, function(){
        $(this).find('.img span').removeClass('hover');
        $(this).find('h3').removeClass('hover');
    });
    
    $('.post:odd, .comment:odd').addClass('odd');
    $('#sidebar ul').children('li:odd').addClass('odd');
    $('#sidebar ul li:first-child, #sidebar ul li:first-child a').addClass('noborder');
    
    $('.backtotop').click(function(){
        $('html, body').animate({ scrollTop: 0 }, 'slow');
        return false;
    });
   
    $('.hint').hint();
    
});

/* HINT */
/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/
(function($) {

	$.fn.hint = function(blurClass) {
		if (!blurClass) {
			blurClass = 'blur';
		}

		return this.each(function() {
			// get jQuery version of 'this'
			var $input = $(this),

			// capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = $(this.form),
      $win = $(window);

			function remove() {
				if ($input.val() === title && $input.hasClass(blurClass)) {
					$input.val('').removeClass(blurClass);
				}
			}

			// only apply logic if the element has the attribute
			if (title) {
				// on blur, set value to title attr if text is blank
				$input.blur(function() {
					if (this.value === '') {
						$input.val(title).addClass(blurClass);
					}
				}).focus(remove).blur(); // now change all inputs to title

				// clear the pre-defined text when form is submitted
				$form.submit(remove);
				$win.unload(remove); // handles Firefox's autocomplete
			}
		});
	};

})(jQuery);
/* HINT */

