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

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

var language = getLanguage();
// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
	
		"image" : "grupoacao.jpg",
		"url" : "",
		"thumbnail"  : "thumb/img-principal.jpg",
		//"title" : "Garantia de Valor Agregado ao mercado de TI",
		//"firstline" : "Grupo A&Ccedil;&Atilde;O",
		//"secondline" : "Distribuindo Solu&ccedil;&otilde;es",
				
		"image_en" : "grupoacao-en.jpg",
		//"title_en" : "Garantia de Valor Agregado ao mercado de TI",
		//"firstline_en" : "Grupo A&Ccedil;&Atilde;O",
		//"secondline_en" : "Distributing Solutions",
			
		"image_es" : "grupoacao-es.jpg",
		//"title_es" : "Garantizar el valor de mercado de TI",
		//"firstline_es" : "Grupo A&Ccedil;&Atilde;O",
		//"secondline_es" : "Distribuindo Soluciones",
			
	}, {
		"image" : "brasil.jpg",
		"url" : "http://www.acao.com.br",
		"thumbnail"  : "thumb/brasil.jpg",
		//"title" : "Distribuidor de Solu&ccedil;&otilde;es",
		//"firstline" : "A&Ccedil;&Atilde;O Inform&aacute;tica",
		//"secondline" : " Brasil",
				
		"image_en" : "brazil-en.jpg",
		//"title_en" : "Solutions Distributor",
		//"firstline_en" : "A&Ccedil;&Atilde;O Inform&aacute;tica",
		//"secondline_en" : "Brazil",
			
		"image_es" : "brasil.jpg",
		//"title_es" : "Soluciones Distribuidor",
		//"firstline_es" : "A&Ccedil;&Atilde;O Inform&aacute;tica",
		//"secondline_es" : "Brasil",
		
	}, {
		"image" : "argentina.jpg",
		"url" : "http://www.aktio.com/argentina/",
		"thumbnail"  : "thumb/argentina.jpg",
		//"title" : "Distribuidor de Valor Agregado",
		//"firstline" : "AKTIO",
		//"secondline" : "Argentina",
				
		"image_en" : "argentina.jpg",
		//"title_en" : "Value Added Distributor",
		//"firstline_en" : "AKTIO",
		//"secondline_en" : "Argentina",
			
		"image_es" : "argentina.jpg",
		//"title_es" : "Distribuidor de Valor Agregado",
		//"firstline_es" : "AKTIO",
		//"secondline_es" : "Argentina",
		
	}, {
		"image" : "chile.jpg",
		"url" : "http://www.aktio.com/chile/",
		"thumbnail"  : "thumb/chile.jpg",
		//"title" : "Distribuidor de Valor Agregado",
		//"firstline" : "AKTIO",
		//"secondline" : "Chile",
				
		"image_en" : "chile.jpg",
		//"title_en" : "Value Added Distributor",
		//"firstline_en" : "AKTIO",
		//"secondline_en" : "Chile",
		
		"image_es" : "chile.jpg",
		//"title_es" : "Distribuidor de Valor Agregado",
		//"firstline_es" : "AKTIO",
		//"secondline_es" : "Chile",
		
	}, {
		"image" : "colombia-br.jpg",
		"url" : "/web/aktio/colombia",
		"thumbnail"  : "thumb/colombia.jpg",
		//"title" : "Distribuidor de Valor Agregado",
		//"firstline" : "AKTIO",
		//"secondline" : "Col&ocirc;mbia",
				
		"image_en" : "colombia.jpg",
		//"title_en" : "Value Added Distributor",
		//"firstline_en" : "AKTIO",
		//"secondline_en" : "Colombia",
		
		"image_es" : "colombia.jpg",
		//"title_es" : "Distribuidor de Valor Agregado",
		//"firstline_es" : "AKTIO",
		//"secondline_es" : "Colombia",
			
	}, {
		"image" : "equador.jpg",
		"url" : "/web/aktio/colombia",
		"thumbnail"  : "thumb/ecuador.jpg",
		//"title" : "",
		//"firstline" : "AKTIO Ecuador",
		//"secondline" : "Investimento",
				
		"image_en" : "ecuador.jpg",
		//"title_en" : "Garantia de Valor Agregado ao mercado de TI",
		//"firstline_en" : "AKTIO Ecuador",
		//"secondline_en" : "Expandindo Rela&ccedil;&otilde;es",
		
		"image_es" : "ecuador.jpg",
		//"title_es" : "",
		//"firstline_es" : "",
		//"secondline_es" : "",
		
	},{
		"image" : "uruguai.jpg",
		"url" : "http://www.aktio.com/uruguay/",
		"thumbnail"  : "thumb/uruguay.jpg",
		//"title" : "",		
		//"firstline" : "",
		//"secondline" : "",
		
		
		"image_en" : "uruguay.jpg",
		//"title_en" : "",
		//"firstline_en" : "",
		//"secondline_en" : "",
			
		"image_es" : "uruguay.jpg",
		//"title_es" : "Distribuidor de Valor Agregado",
		//"firstline_es" : "AKTIO",
		//"secondline_es" : "Uruguay",
	},	
];



$(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--;
		

		
		$("#linkImage").attr("href",photoObject.url);
		
		// Hide the header text
		$("#headertxt").css({"display" : "none"});
		
		// Set the new header text

		if(language=="en_US"){
			// Set the background image of the new active container
			$("#headerimg" + activeContainer).css({
				"background-image" : "url(/GRUPO-ACAO-Banner-Home-portlet/html/grupoacaobannerhome/images/" + photoObject.image_en + ")",
				"display" : "block",
				"z-index" : currentZindex
			});
			
			
		$("#firstline").html(photoObject.firstline_en);
		$("#secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline_en);
		$("#pictureduri")
			.attr("href", photoObject.url)
			.html(photoObject.title_en);
	
		}
		if(language=="es_ES"){
			// Set the background image of the new active container
			$("#headerimg" + activeContainer).css({
				"background-image" : "url(/GRUPO-ACAO-Banner-Home-portlet/html/grupoacaobannerhome/images/" + photoObject.image_es + ")",
				"display" : "block",
				"z-index" : currentZindex
			});
			
			$("#firstline").html(photoObject.firstline_es);
			$("#secondline")
				.attr("href", photoObject.url)
				.html(photoObject.secondline_es);
			$("#pictureduri")
				.attr("href", photoObject.url)
				.html(photoObject.title_es);
				
		}
	if(language=="pt_BR"){
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(/GRUPO-ACAO-Banner-Home-portlet/html/grupoacaobannerhome/images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		
		$("#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(/GRUPO-ACAO-Banner-Home-portlet/html/grupoacaobannerhome/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: '/GRUPO-ACAO-Banner-Home-portlet/html/grupoacaobannerhome/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();
		}

	});
	
});
