(function($) {
	var cache = [];
	$.preloadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = "media/img/" + arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery);

function slideSwitch() {

    var $active = $('div.slideshow-active');
	
	if( $active.hasClass("destaque-05") )
		var $next = $("div.destaque-01");
	else
		var $next = $active.next();

    $active.addClass('slideshow-last-active');

    $next.css({opacity: 0.0})
        .addClass('slideshow-active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('slideshow-active slideshow-last-active');
			$("li.slideshow-nav-item").each(function(){
				$(this).css("text-decoration", "none");
			});
			$("li#item-" + $next.attr("rel")).css("text-decoration", "underline");
        });
}

$("document").ready(function() {
	
	$("a.fancybox").fancybox({'titlePosition': 'inside'});
	
	jQuery.preloadImages("img-pag-programa-01.png", "img-pag-programa-02.png", "img-pag-programa-03.png", "img-pag-programa-04.png", "img-pag-programa-05.png");
	jQuery.preloadImages("img-pag-diferenciais-01.png", "img-pag-diferenciais-02.png", "img-pag-diferenciais-03.png", "img-pag-diferenciais-04.png", "img-pag-diferenciais-05.png", "img-pag-diferenciais-06.png", "img-pag-diferenciais-07.png", "img-pag-diferenciais-08.png", "img-pag-diferenciais-09.png", "img-pag-diferenciais-10.png");
	jQuery.preloadImages("img-pag-apoio-01.png", "img-pag-apoio-02.png", "img-pag-apoio-03.png", "img-pag-apoio-04.png", "img-pag-apoio-05.png", "img-pag-apoio-06.png", "img-pag-apoio-07.png", "img-pag-apoio-08.png", "img-pag-apoio-09.png", "img-pag-apoio-10.png","img-pag-apoio-11.png", "img-pag-apoio-12.png", "img-pag-apoio-13.png", "img-pag-apoio-14.png", "img-pag-apoio-15.png", "img-pag-apoio-16.png");
	
	var slideshowInterval = setInterval( "slideSwitch()", 5000 );
	
	$("#ulItens li").click(function(){
		window.clearInterval(slideshowInterval);
		
		/*$("div.destaque").each(function(){
			$(this).addClass('slideshow-last-active');
		});*/
		
		var $active = $('div.slideshow-active');
		
		var $next = $("div.destaque-" + $(this).attr("rel"));
		
		$next.css({opacity: 0.0})
        .addClass('slideshow-active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('slideshow-active slideshow-last-active');
			$("li.slideshow-nav-item").each(function(){
				$(this).css("text-decoration", "none");
			});
			$("li#item-" + $active.attr("rel")).css("text-decoration", "underline");
        });
		
		slideshowInterval = setInterval( "slideSwitch()", 5000 );
	});
	
	$("a[href^='http'][rel!='interno']").attr('target','_blank');
	
	$("#ulMenu li").mouseenter(function() {
		$(this).children("ul").css({"display": "table"});
	}).mouseleave(function() {
		$(this).children("ul").css({"display": "none"});
	});
	
	$("#formIMC").submit(function(){
		if( $("#altura").val() == "altura" || $("#peso").val() == "peso" )
		{
			alert("Por favor informe sua altura e peso corretamente");
			return false;
		}
	});
	
	$("#altura").focus(function(){
		if( $(this).val() == "altura" )
			$(this).val("");
	}).blur(function(){
		if( $(this).val() == "" )
			$(this).val("altura");
	}).keydown(function(e){
		var key = e.charCode || e.keyCode || 0;
		// permite somente backspace, tab, delete, vírgula, seta e numeros
		return ( key == 8 || key == 9 || key == 46 || key == 188 ||(key >= 37 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105) );
	});

	
	$("#peso").focus(function(){
		if( $(this).val() == "peso" )
			$(this).val("");
	}).blur(function(){
		if( $(this).val() == "" )
			$(this).val("peso");
	}).keydown(function(e){
		var key = e.charCode || e.keyCode || 0;
		// permite somente backspace, tab, delete, vírgula, seta e numeros
		return ( key == 8 || key == 9 || key == 46 || key == 188 || (key >= 37 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105) );
	});
	
	$("#divSessaoCortesia div #unidades").change(function(){
		window.location.href = "index.php?p=sessao-cortesia&u=" + $(this).val();
	});
	
	$("#olDiferenciais li").mouseenter(function() {
		$("#divGaleria").children("p").text( $(this).attr("rel") );
		$("#imgBoxDiferenciais").attr("src", "media/img/" + $(this).attr("id") + ".png");
	});
	
	$("#olApoioAoFranqueado li").mouseenter(function() {
		$("#divGaleria img").attr("src", "media/img/" + $(this).attr("id") + ".png");
		$("#divGaleria").children("p").text( $(this).attr("rel") );
	});
	
	$("#ulAtividadesProgramaFT li").mouseenter(function() {
		$("#imgAtividadesProgramaFT").attr("src", "media/img/" + $(this).attr("rel") + ".png");
	});
	
	$("#selectUnidadeProxima").change(function(){
		$("#divListaUnidades p").each(function(){
			$(this).css({ "display": "none" });
		});
		$("#divListaUnidades p[id='" + $(this).val() + "']").fadeIn();
	});
});
