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

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

$(document).ready(function() {
	
	$('#webdoorthumbs-outer').hover(
		function () {
			$('#webdoorthumbs-outer').animate({
				height: '126px'
				}, {queue:false,duration:800}, function() { }
			);
			$('#webdoornav-outer').animate({
				top: '306px'
				}, {queue:false,duration:800}, function() { }
			);
		}, function () {
			$('#webdoorthumbs-outer').animate({
				height: '54px'
				}, {queue:false,duration:800}, function() { }
			).delay(800);
			$('#webdoornav-outer').animate({
				top: '378px'
				}, {queue:false,duration:800}, function() { }
			);
		}
	);
	
	if($.browser.msie && (jQuery.browser.version=='7.0' || jQuery.browser.version=='6.0'))
		$('#webdoorthumbs-outer .webdoorthumbs ul').width(($('#webdoorthumbs-outer .webdoorthumbs ul li').width()+10)*$('#webdoorthumbs-outer .webdoorthumbs ul li').length);
		
	// Backwards navigation
	$("#back").click(function() {
		stopAnimation();
		navigate("back");
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	// Forward navigation
	$("#next").click(function() {
		stopAnimation();
		navigate("next");
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	var interval;
	$("#control").toggle(function(){
		stopAnimation();
		// Change the background image to "play"
		$(this).removeClass('control_play');
		$(this).addClass('control_pause');
	}, function() {
		// Change the background image to "pause"
		$(this).removeClass('control_pause');
		$(this).addClass('control_play');
		
		// Show the next image
		navigate("next");
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	$('#webdoorthumbs-outer ul li').click(function(){
		stopAnimation();
		navigate($(this).index()+1);
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	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 if(direction == "back") { 
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		} else { //especifica uma posição
			currentImg = direction;
			if(currentImg == 0) {
				currentImg = 1;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		//Sinaliza nas thumbs qual a imagem atual em exibição
		$('#webdoorthumbs-outer .webdoorthumbs ul li').removeClass('select').eq(currentImg-1).addClass('select');
		
	};
	
	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
		$("#webdoorimg" + activeContainer).css({
			"background-image" : "url("+photoObject.url+"/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		if(photoObject.showtext && $("#webdoortxt").length) {
			// Hide the webdoor text
			$("#webdoortxt").css({"display" : "none"});
			
			// Set the new webdoor text
			$("#firstline").html(photoObject.firstline);
			$("#secondline")
				.attr("href", photoObject.url)
				.html(photoObject.secondline);
			$("#pictureduri")
				.attr("href", photoObject.url)
				.html(photoObject.title);
		}
		if(photoObject.linksaiba) 
			$("#wd_saiba_mais").attr("href", photoObject.linksaiba);
		else
			$("#wd_saiba_mais").attr("href", "");
		
		// Fade out the current container
		// and display the webdoor text when animation is complete
		$("#webdoorimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#webdoortxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		if($("#control").length){
			$(this).removeClass('control_play');
			$(this).addClass('control_pause');
		}
		
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
