var totalphotos = 0;
var currentphoto = 0;

var sliding = false;
var loop = true;
var shorttimewait = 4000;
var longtimewait = 8000;
var timeoutid;
var busy = false;
var showtitle = true;
$(document).ready(function(){
	if($("table.Slideshow").length>0 || $("table.VideoGallery").length>0)
		PrepareSlideshow();
})

function PrepareSlideshow()
{

	var groot = $("table.Slideshow").hasClass("groot");
	$("table.Slideshow tr, table.VideoGallery tr").css("display", "none");
	$("table.Slideshow").wrap("<div class='slideshow "+ (groot ? "groot":"klein") +"'></div>");
	$("div.slideshow").append("<div class=\"image\"></div><div class=\"imagetitle\"></div><div class=\"control\"><div class=\"controls\"></div></div>");
	//$("div.slideshow").html($("div.slideshow").html() + "<div class=\"image\"></div><div class=\"imagetitle\"></div><div class=\"control\"><div class=\"controls\"></div></div>");
	
	/*
	if($("table.Slideshow tr div.imagetext").length>0)
	{
		var htmlo = $("table.Slideshow tr div.imagetext");
		//alert(htmlo);
		$("div.slideshow").before($("table.Slideshow tr div.imagetext"));
		$("table.Slideshow tr:first").remove();
		
	}
	*/
	
	totalphotos = $("table.Slideshow tr").length;
	currentphoto = 1;
	
	var html = new Array("<div class=\"photopaging\"></div><ul class=\"photopaging\">");
	//html.push();
	for(var i=1;i<=totalphotos;i++)
	{
		html.push("<li class=\""+(i==1?"first":"")+ " " + (i==currentphoto?"active":"") + "\"><a href=\"#\" class=\"changephoto\">" + i + "</a></li>");
	}
	html.push("</ul>");
	$("div.slideshow div.controls").html(html.join(''));
	
	$("div.slideshow div.control").append("<br style=\"clear: both\"/>");
	
	UpdateSlideshow(true, shorttimewait);

	$("a.changephoto").click(function(){
		if(busy)
		{
			changephoto(this)
		}else{
			currentphoto = parseInt($(this).text());
			UpdateSlideshow(true, longtimewait);
		}
		
		return false; 
	});
	
}

function changephoto(o){
	if(currentphoto!=$(o).text())
	{
		//clearTimeout();
		clearTimeout(timeoutid);
		//alert(timeoutid);
		
		currentphoto = parseInt($(o).text());
		UpdateSlideshow(true, longtimewait);
		//UpdateSlideshow2();
	}
	return false;
}

function UpdateSlideshow2()
{
	var html_new = $("table.Slideshow tr:eq("+(currentphoto-1)+") td:eq(0)").html();
	if(currentphoto>0)
	{
		
	}else{
	
	}
	alert(html_new);
}

function UpdateSlideshow(left, timetowait){
	busy = true;
	var html_new = $("table.Slideshow tr:eq("+(currentphoto-1)+") td:eq(0)").html();

	$("div.slideshow ul.photopaging li.active").removeClass("active");
	$("div.slideshow ul.photopaging li:eq("+(currentphoto-1)+")").addClass("active");
	
	if(sliding){
		var html_old = $("div.slideshow div.image div.img_left").html();
	
		$("div.slideshow div.image").html("<div class=\"scroller\"><div class=\"img_left\"></div><div class=\"img_middle\"></div><div class=\"img_right\"></div></div>");	
		if(html_old){
			var leftMargin = "-980px";
			if(left)
			{
				leftMargin = "0px";
			}
			
			$("div.slideshow div.image div.scroller div.img_middle").html(html_old);
			$("div.slideshow div.image div.scroller div.img_left").html(html_new);
			$("div.slideshow div.image div.scroller div.img_right").html(html_new);
			$("div.slideshow div.image div.scroller div.img_middle").animate({ 
				opacity: 0
			}, 1500 );
			
			$("div.slideshow div.image div.scroller").animate({ 
				marginLeft: leftMargin
			}, 1500 );
			
		}
		else{
			$("div.slideshow div.image div.img_middle").html(html_new);
			$("div.slideshow div.image div.img_left").html(html_new);
			$("div.slideshow div.image div.img_right").html(html_new);
			
		}
	}else{
		$("div.slideshow div.image").html(html_new);

	}
	
	$("div.slideshow div.imagetitle").html($("table.Slideshow tr:eq("+(currentphoto-1)+") td:eq(1)").html());
	currentphoto = ((currentphoto+1)>totalphotos?1:currentphoto+1);
	
	
	if(loop)
	{
		clearTimeout(timeoutid);
		timeoutid = setTimeout("UpdateSlideshow('true','"+timetowait+"');", timetowait);
	}
	busy = false;
}

function TimedSlideshow()
{
	if($("a#ss_start").hasClass("started"))
	{
		setTimeout ("TimedSlideshow();", shorttimewait);
		SS_Next();
	}
}


