/*
 * Author:      Marco Kuiper (http://www.marcofolio.net/)
*/

// Speed of the automatic slideshow
var slideshowSpeed = 7200;

var language = getLanguage();
// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
		"title" : "Garantia de Valor Agregado ao mercado de TI",
		"image" : "acao-img1.jpg",
		"url" : "empresa",
		"firstline" : "Distribuindo Solu&ccedil;&otilde;es,",
		"secondline" : "Expandindo Rela&ccedil;&otilde;es",
		"thumbnail"  : "thumb/img1.jpg",
		"title_en" : "Value Guarantee the IT market",
		"firstline_en" : "Distributing Solutions,",
		"secondline_en" : "Expanding Relationships"
	}, {
		"title" : "Melhor Distribuidor de Solu&ccedil;&otilde;es no Brasil e Am&eacute;rica Latina",
		"image" : "acao-img2.jpg",
		"url" : "reconhecimentos",
		"firstline" : "Colhendo Resultados",
		"secondline" : "Distribuidor N&deg;1",
		"thumbnail"  : "thumb/img2.jpg",
		"title_en" : "Best Solutions Distributor in Brazil and Latin America",
		"firstline_en" : "Reaping Results",
		"secondline_en" : "No. 1 Distributor"
	}, {
		"title" : "Encontre Solu&ccedil;&otilde;es para as necessidades dos seus neg&oacute;cios",
		"image" : "acao-img3.jpg",
		"url" : "guia-de-solucoes",
		"firstline" : "Procurando por Solu&ccedil;&otilde;es?",
		"secondline" : "Guia de Solu&ccedil;&otilde;es da A&Ccedil;&Atilde;O",
		"thumbnail"  : "thumb/img3.jpg",
		"title_en" : "Find Solutions for needs of your business",
		"firstline_en" : "Looking for Solutions?",
		"secondline_en" : "Solutions Guide A&Ccedil;&Atilde;O"
	}, {
		"title" : "Melhor Canal de Distribui&ccedil;&atilde;o de Grande Porte",
		"image" : "acao-img4.jpg",
		"url" : "noticias/-/blogs/informatica-hoje-aponta-acao-informatica-como-%E2%80%9Co-melhor-canal-de-comercializacao-de-grande-porte%E2%80%9D",
		"firstline" : "Destaques de 2011",
		"secondline" : "Novas Conquistas",
		"thumbnail"  : "thumb/img4.jpg",
		"title_en" : "Distribution Channel Best Large",
		"firstline_en" : "Highlights of 2011",
		"secondline_en" : "New Achievements"
	}, {
		"title" : "Projeto de Expans&atilde;o visa aprimorar as log&iacute;sticas e aumentar a capacidade de armazenagem",
		"image" : "acao-img5.jpg",
		"url" : "noticias/-/blogs/acao-investe-em-novo-centro-de-distribuicao",
		"firstline" : "Novo Centro de Distribui&ccedil;&atilde;o",
		"secondline" : "Investimento",
		"thumbnail"  : "thumb/img5.jpg",
		"title_en" : "Expansion Project aims to improve logistics and increase the storage capacity",
		"firstline_en" : "New Distribution Center",
		"secondline_en" : "Investment"
	}
];



$(document).ready(function() {
		
		 

	var countImages = 1;

	//$.each(photos, function(key, value) { 
		
		//$("#thumb").append('<li class="thumb" id="thumb" ><img src="/Banner-Home-Slider2-portlet/html/bannerhomeslider2/images/'+photos[countThumb-1].image+'" height="60px" width="100px" /></li>');
		
		//countThumb++;
		  
	//});
	
	$("#thumbDiv").hide();
	
	$("#fotoNumber").append('<li id="back" > << </li>');
	
	$.each(photos, function(key, value) { 
		
		$("#fotoNumber").append('<li class="fotoBanner" id="itemBanner'+countImages+'" >'+countImages+'</li>');
		
		  countImages++;
		  
		});
	
	$("#fotoNumber").append('<li id="next"> >> </li>');
	
	
	// Backwards navigation
	$("#back").click(function() {

		navigate("back");
		
	});
	
	// Forward navigation
	$("#next").click(function() {

		navigate("next");
		
	});
	
	var interval;
/*	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
		// Change the background image to "pause"
		$(this).css({ "background-image" : "url(/Banner-Home-Slider2-portlet/html/bannerhomeslider2/images/btn_pause.png)" });
		
		// Show the next image
		navigate("next");
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});*/
	
	var currentContainer=null;
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		$('li[class $="fotoBanner"]').css({background:'#ccc', border: '1px solid #999'});
		$('li[id $="itemBanner'+currentImg+'"]').css({border:'1px solid #999',background: '#888'});
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(/Banner-Home-Slider2-portlet/html/bannerhomeslider2/images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#headertxt").css({"display" : "none"});
		
		// Set the new header text

		if(language=="en_US"){
	
		$("#firstline").html(photoObject.firstline_en);
		$("#secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline_en);
		$("#pictureduri")
			.attr("href", photoObject.url)
			.html(photoObject.title_en);
	
		}else{
			$("#firstline").html(photoObject.firstline);
			$("#secondline")
				.attr("href", photoObject.url)
				.html(photoObject.secondline);
			$("#pictureduri")
				.attr("href", photoObject.url)
				.html(photoObject.title);
			
		}
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		$("#control").css({ "background-image" : "url(/Banner-Home-Slider2-portlet/html/bannerhomeslider2/images/btn_play.png)" });
		
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
	$('li[class $="fotoBanner"]').click(function(index) {
		
		stopAnimation();
		var numberFoto = $(this).text();
		$('li[class $="fotoBanner"]').css({background:'#ccc', border: '1px solid #999'});
		$('li[id $="itemBanner'+numberFoto+'"]').css({border:'1px solid #999',background: '#888'});
		showImage(photos[numberFoto-1],currentContainer, activeContainer);
		currentImg = numberFoto;
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
		
	});
	
	$('li[class $="fotoBanner"]').mouseover(function(index) {
		stopAnimation();
		var numberFoto = $(this).text();
		if($(this).text()!="<<" || $(this).text()!=">>"){
			$("#thumbDiv").show();
			$("#imageThumb").attr({src: '/Banner-Home-Slider2-portlet/html/bannerhomeslider2/images/'+photos[numberFoto-1].thumbnail });
			$("#headernav-outer").css({top:'274px'});
			$("#headertxt").css({ top:'-2px' });
			$("#firstlineThumb").html(photos[numberFoto-1].firstline);
			//$('p[class $="pictured"]').css({ padding:'15px 16px 9px 16px' });
			
		}
		
		
	});
	
	$('li[class $="fotoBanner"]').mouseout(function(index) {
		stopAnimation();
		if($(this).text()!="<<" || $(this).text()!=">>"){
			$("#thumbDiv").hide();
		}
		$("#headernav-outer").css({top:'350px'});
		$("#headertxt").css({ top:'74px' });
		
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
		
	});
	$('li[class $="fotoBanner"]').mouseleave(function(index) {
		if($(this).text()!="<<" || $(this).text()!=">>"){
			$("#thumbDiv").hide();
		}

	});
	
});
