var activeImageId = 0;
var activeGarden = 'default';
var placeHolder = '#slideshow';
var slideWidth = 862;
var activeSlides;

var isSliding = false;

$(document).ready(function(){
	
	qSlideshow.parseMenu();
	qSlideshow.openGarden(activeGarden);
	
	// Bind actions to the text
	$('#closeContent').bind('click', function(){
		$("#otherText:hidden").fadeIn("slow");
		$("#mainText:visible").slideUp("slow");
	});
	  
	$('#openContent').bind('click', function(){
		$("#mainText:hidden").slideDown("slow");
		$("#otherText:visible").fadeOut("slow");
	});
	
});

function qSlideshow() {};

qSlideshow.openGarden = function( ids )
{
	if (!isSliding)
	{
		isSliding = true;
		
		// Startup the preloader
		if ( ids != 'default' )
		{
			$('#preloaderImage').fadeIn(500);
		}
		
		// Close the text
		if ( ids != activeGarden )
		{
			$("#otherText:hidden").fadeIn();
			$("#mainText:visible").slideUp();
		}
		
		qSlideshow.highlightMenuItem( ids );
		qSlideshow.setMenuItemVisited( activeGarden );
		
		activeGarden = ids;
		activeImageId = 0;
		
		$('#arrowLeft').animate({opacity: "0"}, 500);
		$('#arrowRight').animate({opacity: "0"}, 500);
		$(placeHolder).animate({opacity: "0"}, 500, null, function(){
			
			// Empty the preloader bucket
			$('#preloaderBucket img').remove();
			$(placeHolder + ' img').remove();
			
			// Preload the new images
			qSlideshow.preloadImages( slidesData[activeGarden]['images'] );
			
			// Set the slides
			qSlideshow.addSlide(-1);
			qSlideshow.addSlide(0);
			qSlideshow.addSlide(1);
			
			qSlideshow.updateActiveSlides();
			
			// Add navigation arrows
			qSlideshow.addNavigation();
			
			// When slideshow is loaded
			$(placeHolder + ' img:last-child').bind("load", function(){
			   $(placeHolder).animate({opacity: "1"}, 1000);
			   
			   $('#arrowLeft').animate({opacity: "1"}, 1000);
			   $('#arrowRight').animate({opacity: "1"}, 1000);
			   
			   if (ids != 'default')
				   $('#preloaderImage').fadeOut(500);
			   
			   isSliding = false;
			});
		});
		pageTracker._trackPageview(ids);
	}
}

qSlideshow.addNavigation = function ()
{
	var left = $('<img>').attr('src', 'gfx/arrowLeft.gif');
	left.attr('id', 'arrowLeft');
	left.css('cursor', 'pointer');
	left.animate({opacity: "0"}, 1);
	left.click(function(){
		qSlideshow.slideToRight();
	});
	
	var right = $('<img>').attr('src', 'gfx/arrowRight.gif');
	right.attr('id', 'arrowRight');
	right.css('cursor', 'pointer');
	right.animate({opacity: "0"}, 1);
	right.click(function(){
		qSlideshow.slideToLeft();
	});
	
	$('#slideshow').parent().append( $(left) );
	$('#slideshow').parent().append( $(right) );
	
}

qSlideshow.addSlide = function( increment )
{
	// Get image from preloader if exists
	if ( $('#preloaderBucket #' + qSlideshow.getNewActive(increment)) ) 
	{
		var img = $('#preloaderBucket #' + qSlideshow.getNewActive(increment));
	}
	else // Else make new image
	{
		var img = $('<img>');
		$(img).attr("src", slidesData[activeGarden]['images'][qSlideshow.getNewActive(increment)] );
	}
	
	$(img).attr("class", "slide" );
	$(img).attr("id", qSlideshow.getNewActive(increment) );
	$(img).attr("style", 'margin-left:' + qSlideshow.calculateMargin(increment) + 'px' );
	
	$(placeHolder).append($(img));
	
	// Add click action
	$('#'+qSlideshow.getNewActive(increment)).bind('click', function(){
		qSlideshow.slide( this );
	});
}

qSlideshow.slide = function( clicked )
{
	if ( activeImageId == 0 && $(clicked).attr('id') == (slidesData[activeGarden]['images'].length-1))
	{
		qSlideshow.slideToRight();
	}
	else if ( activeImageId == (slidesData[activeGarden]['images'].length-1) && $(clicked).attr('id') == 0)
	{
		qSlideshow.slideToLeft();
	}
	else if ( $(clicked).attr('id') < activeImageId )
	{
		qSlideshow.slideToRight();
	}
	else if ( $(clicked).attr('id') >= activeImageId )
	{
		qSlideshow.slideToLeft();
	}
}

qSlideshow.slideToLeft = function()
{
	if (!isSliding)
	{
		isSliding = true;
		
		$('#arrowRight').animate({opacity: "0"}, 100);
		$('#arrowLeft').animate({opacity: "0"}, 100, null, function(){

			qSlideshow.addSlide(2);
			qSlideshow.updateActiveSlides();
			
			jQuery.each(activeSlides, function(eachIndex, slide) {
				
				$(slide).animate({
					'marginLeft' : parseInt($(slide).css("margin-left").replace("px", "")) - slideWidth + "px" 
				},
				1000, // Timing
				null, // Easing
				function () { 
					// Remove left element
					if ($('#' + qSlideshow.getNewActive(-2) ))
					{
						$('#preloaderBucket').append($('#' + qSlideshow.getNewActive(-2) ));
					}
					$('#arrowLeft:not(:animated)').animate({opacity: "1"}, 500);
					$('#arrowRight:not(:animated)').animate({opacity: "1"}, 500);
					
					isSliding = false;
				});
			});
			
			
			activeImageId = qSlideshow.getNewActive(1);
			
			//And slide the maintext if visible
			$("#otherText:hidden").fadeIn("slow");
			$("#mainText:visible").slideUp("slow");
		});
	}
}

qSlideshow.slideToRight = function()
{
	if (!isSliding)
	{
		isSliding = true;
		
		$('#arrowRight').animate({opacity: "0"}, 100);
		$('#arrowLeft').animate({opacity: "0"}, 100, null, function(){
			
			qSlideshow.addSlide(-2);
			qSlideshow.updateActiveSlides();
			
			jQuery.each(activeSlides, function(eachIndex, slide) {
				
				$(slide).animate({
					'marginLeft' : parseInt($(slide).css("margin-left").replace("px", "")) + slideWidth + "px" 
				},
				1000, // Timing
				null, // Easing
				function () { 
					
					// Remove left element
					if ($('#' + qSlideshow.getNewActive(2) ))
					{
						$('#preloaderBucket').append($('#' + qSlideshow.getNewActive(2) ));
					}
					$('#arrowLeft:not(:animated)').animate({opacity: "1"}, 500);
					$('#arrowRight:not(:animated)').animate({opacity: "1"}, 500);
					
					isSliding = false;
				});
			});
			
			
			activeImageId = qSlideshow.getNewActive(-1);
			
			//And slide the maintext if visible
			$("#otherText:hidden").fadeIn("slow");
			$("#mainText:visible").slideUp("slow");
		});
	}
}

qSlideshow.parseMenu = function()
{
	for ( gardenIds in slidesData )
	{
		// If there is a thumbnail
		if ( typeof( slidesData[gardenIds]['thumb'] ) != "undefined" )
		{
			var menuItem = $('<li>');
			menuItem.attr("id", gardenIds);
			
			var link = $('<a>').attr('href', '#');
			$(link).click( function(){
					qSlideshow.openGarden( $(this).parent().attr('id') );
				}
			);
			
			var img = $('<img>').attr('src', slidesData[gardenIds]['thumb']);
			img.attr("class", "color");
			var img_zw = $('<img>').attr('src', slidesData[gardenIds]['thumb_zw']);
			img_zw.attr("class", "zw");
			img_zw.animate({opacity: "0"}, 1); 
			
			$(link).append($(img));
			$(link).append($(img_zw));
			$(menuItem).append($(link));
			$('.menu').append($(menuItem));
		}
	}
	
    $('.menu li').bind("mouseenter",function(){
    	if ($(this).attr('id') != activeGarden )
    		qSlideshow.highlightMenuItem( $(this).attr('id') );
    }).bind("mouseleave",function(){
    	if ($(this).attr('id') != activeGarden )
    		qSlideshow.resetMenuItem( $(this).attr('id') );
    });
}

qSlideshow.setMenuItemVisited = function( itemIds )
{
	$('.menu #' + itemIds + ' a img').animate({opacity: "0"}, 500); 
	$('.menu #' + itemIds).css('backgroundColor', '#000'); 
}

qSlideshow.resetMenuItem = function( itemIds )
{
	$('.menu #' + itemIds + ' img.zw').animate({opacity: '0'}, 500); 
}

qSlideshow.highlightMenuItem = function( itemIds )
{
	$('.menu #' + itemIds + ' img.zw').animate({opacity: "1"}, 500); 
}

qSlideshow.calculateMargin = function( elementIndex )
{
	return (slideWidth/2) + (elementIndex-1) * slideWidth;
}

qSlideshow.getNewActive = function( increment )
{
	if ( activeImageId + increment > (slidesData[activeGarden]['images'].length-1) )
	{
		return (activeImageId+increment) - (slidesData[activeGarden]['images'].length);
	}
	else if ( activeImageId + increment < 0 )
	{
		return (slidesData[activeGarden]['images'].length) + (activeImageId+increment);
	}
	else
	{
		return activeImageId + increment;
	}
}

qSlideshow.preloadImages = function( image )
{
	for ( key in image)
	{
		if ( is_array( key ) )
			qSlideshow.preloadImages( key );
		else
		{
			var img = $("<img>").attr("src", image[key]);
			$(img).attr("id", key);
			$('#preloaderBucket').append($(img));
		}
	}
}

qSlideshow.updateActiveSlides = function()
{
	activeSlides = $('.slide');
}

function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
}
