﻿//BANNERS
var img_timer = null;
var last_img = 1;
function clearTimer(){
	clearTimeout(img_timer)	
	img_timer = null
}
function changeImg(){
	clearTimer()
	var images = $('.thumb img')
	if(images.length > 0){
		if(images.length <= last_img) last_img = 0
		var img = $(images[last_img++])
		$('.thumb-selected').removeClass('thumb-selected')
		img.parent().addClass('thumb-selected')
		var src = img.attr('src').replace('pequenas/', '').replace(".gif", ".jpg")
		$('#img-principal').fadeOut('slow', function(){
			this.src = src
			//$(this).fadeIn('slow')
		})
		//
		var img_src = img.parent()[0].href
		var target = img.parent().attr('target')
	
		$('#saiba-mais').attr('href', img_src) 
		$('#saiba-mais').attr('target', target)
		
		//
		if(last_img <= 1){
			if(getFirst) { getFirst() }
		} else {
			if(last_img % 3 == 1) getNext()	
		}
	}

}
function callChange(){
	img_timer = setTimeout("changeImg()",  9000)
}
var getNext = false;
var getPrev = false;
var getFirst = false;

//VIDEOS
var last_vid = 1;
function changeVid(num){
	var videos = parent.$('.thumbs p')
	if(videos.length > 1){
		parent.$('.thumb-selected').removeClass('thumb-selected')
		if(num > videos.length) num = 1;
		if(num < 1) num = videos.length;
		var vid = parent.$(videos[num-1])
		$('#outro h4').html('<span>' + num + '/' + videos.length + '</span>' + parent.$('.thumbs p a.titulo')[num-1].innerHTML + '');
		vid.addClass('thumb-selected')

		//videosTo(num);
		stepcarousel.stepTo('mygallery', num);
		
		//console.log($('.panel object')[last_vid]);

		if($('.panel object')[last_vid-1].stopVideo){
			$('.panel object')[last_vid-1].stopVideo();
		}


		if($('.panel object')[num-1].playVideo){
			$('.panel object')[num-1].playVideo();
		}
		
		last_vid = num;
	}
}
var videosTo = false;

var noticiaProxima = false;
var noticiaAnterior = false;

function noticia_callback(carousel) {
    noticiaProxima = function() {
        carousel.next()
		return false
		carousel.startAuto(0)
    }

    noticiaAnterior = function() {
        carousel.prev()
        return false
		carousel.startAuto(0)
    }

	carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });


};

function carousel_callback(carousel) {
    getNext = function() {
        carousel.next()	
        return false
    }

    getPrev = function() {
        carousel.prev()
        return false
    }
	
	getFirst = function() {
        carousel.scroll(1)
        return false
    }
	
};

function depoimentos_callback(carousel) {
    depoimentoProximo = function() {
        carousel.next()	
        return false
    }

    depoimentoAnterior = function() {
        carousel.prev()
        return false
    }
	
	getFirst = function() {
        carousel.scroll(1)
        return false
    }
	
};

var casesTo

function cases_callback(carousel) {
    casesProximo = function() {
        carousel.next()	
        return false
    }

    casesAnterior = function() {
        carousel.prev()
        return false
    }
	
	getFirst = function() {
        carousel.scroll(1)
        return false
    }
	
	casesTo = function(num){
		carousel.scroll(num)
		return false
	}
	
};
var firstVideo = true
function video_callback(carousel) {
	videosTo = function(num){
		if(!firstVideo){
			if($('.jcarousel-item-' + carousel.last + ' object')[0].stopVideo){
				$('.jcarousel-item-' + carousel.last + ' object')[0].stopVideo()
				$('.jcarousel-item-' + num + ' object')[0].playVideo()
			}
		}
		carousel.scroll(num)
		firstVideo = false
		return false
	}
	changeVid(1);
};

var clicked = false;
var loadImg = function(){
	$(this).fadeIn(); 
	if(!clicked) callChange();	
}
$(function() {
	$('#mycarousel').jcarousel({initCallback: carousel_callback});
	/*$('#video').jcarousel({
			initCallback: video_callback,
			scroll: 1,
			wrap: 'last'
	});*/
	$('#noticia-carousel').jcarousel({
			initCallback: noticia_callback,
			auto: 5,
			scroll: 1,
			wrap: 'last'
	});
	$('#depoimentos').jcarousel({
			initCallback: depoimentos_callback,
			auto: 9,
			scroll: 1,
			wrap: 'last'
	});
	$('#cases-carousel').jcarousel({
			initCallback: cases_callback,
			scroll: 1,
			wrap: 'last'
	});
	try {
	var img = $('.thumb img:eq(0)').attr('src').replace('images/banner-destaque/pequenas/', '').replace('.gif', '.jpg')
	$('.thumb:eq(0)').addClass('thumb-selected')
	$('#pai .filho').append('<img id="img-principal" src="images/banner-destaque/' + img + '"/>')
	}catch(e){};
	$('.thumb').click(function(){
		clearTimer()
		var src = $(this).find('img').attr('src').replace('pequenas/', '').replace(".gif", ".jpg")
		$('.thumb-selected').removeClass('thumb-selected')
		$(this).addClass('thumb-selected')

		var target = $(this).attr('target')
	
		
		$('#saiba-mais').attr('href', this.href) 
		$('#saiba-mais').attr('target', target)
		clicked = true
		$('#img-principal').fadeOut(' ', function(){
			this.src = src
		})
		return false
	})
	$('.thumb').mouseover(function(){
		$('.thumb-hover').removeClass('thumb-hover')
		$(this).find('img').animate({'opacity': 0.35}, 10, function(){
				$(this).addClass('thumb-hover')
				$(this).animate({'opacity': 1}, 300)
	    })
	})
	$('.thumb').mouseout(function(){
		$(this).removeClass('thumb-hover')
		$(this).find('img').animate({'opacity': 0.35}, 10)
	})
	
	$('.efeito-brilho').mouseover(function(){
		$(this).animate({'opacity': 0.6}, 100)
		$(this).addClass('selected')
		$(this).animate({'opacity': 1}, 200)
	})
		
	$('.efeito-brilho').mouseout(function(){
		$(this).animate({'opacity': 0.6}, 100)
		$(this).removeClass('selected')
		$(this).animate({'opacity': 1}, 100)
	})
	
	$('#saiba-mais').mouseover(function(){
		$(this).animate({'opacity': 0.6}, 150)
		$(this).addClass('saiba-selected')
		$(this).animate({'opacity': 1}, 260)
	})
	
	$('#saiba-mais').mouseout(function(){
		$(this).animate({'opacity': 0.6}, 150)
		$(this).removeClass('saiba-selected')
		$(this).animate({'opacity': 1}, 260)
	})
	
	/*$('#saiba-mais').mouseout(function(){
		$(this).fadeOut('fast', function(){
			$(this).removeClass('saiba-selected').fadeIn('fast')
		})
	})*/
	callChange();
	$('#img-principal').load(loadImg)
	
	$("#cases-carousel li a").each(function(num, obj){
		if(obj.href == window.location.href){
			var index = Math.floor(num / 12) + 1			
			casesTo(index)
		}
	})
	
});
