var nextProjectId;
var nextProjectTitle;
var nextProjectText;
var currentProjectId;
var targetProjectId = false;

var slideDelay = 20000;
var slideSpeed = 1200;
var quickSpeed = 400;
var useSpeed = slideSpeed;
var isPaused = false;
var inTransition = false;


$(document).ready(function(){
	$('#mainmenu > ul > li').each(function(){
			$(this).bind('click',function(){document.location = $(this).children('a')[0].href});
		});
	$('#focus_image,#playwin,#playpane,#playcontrol,#playslider,#playcursor').hover(function(){
		$('#playpane').stop().animate({opacity:1,marginTop:8},400);
		$('#playcontrol').data('visible',true);
	},function(){
		if(!isPaused){
			$('#playpane').stop().animate({marginTop:48},400);
			$('#playcontrol').data('visible',false);
		}else{
			$('#playpane').stop().animate({opacity:0.5},400);
		}
	});
	$('#pausebutton > img,#playbutton > img,#nextbutton > img').hover(function(){
		this.src = this.src.split(".gif").join("_mo.gif");
	},function(){
		this.src = this.src.split("_mo.gif").join(".gif");
	});
	$('#pausebutton').bind('click',function(){
		isPaused = true;
		$(this).hide();
		$('#playbutton').show();
		$('#playslider').stop().animate({opacity:0},200)
	});
	$('#playbutton').bind('click',function(){
		isPaused = false;
		$(this).hide();
		$('#pausebutton').show();
		startSlides();
	});
	$('#nextbutton').bind('click',function(){
		$('#playslider').stop().animate({opacity:0},200)
		nextProject();
	});
	startSlides();
});

function startSlides(){
//	$('#projects').data('nextSlideTimeOutID',setTimeout('nextProject()',slideDelay));
	$('#playslider').stop().animate({opacity:1},300);
	$('#playslider').css({width:10}).animate({width:677}, slideDelay, "linear", nextProject);
	$('#playbutton').hide();
}

function goToProjectWithId(id){
	targetProjectId = id;
	useSpeed = quickSpeed;
	nextProject();
}

function nextProject(){
	if(!inTransition){
		inTransition = true;
		nextProjectId = Number($('#projects > div > ul > li')[0].id.split('_')[1]);
		var url = $('base')[0].href + 'assets/ajax'
		$.post(url
			,{output:'pagetitle',docid:nextProjectId}
			, nextProjectImage);
		$('#JT').remove();
	}
}

function nextProjectImage(data){
	lastProjectTitle = nextProjectTitle;
	nextProjectTitle = data;
	$('#attention > div').fadeOut(Math.floor(useSpeed/3), replaceAttentionText);
	$('#projects > div > ul').animate({ marginTop: -90 }, useSpeed);
	var i = $('#projects > div > ul > li:first > img')[0].src;
	$('#focus_image').append('<div><img src="'+i+'"- alt="" border="0" /></div>');
	$($('#focus_image > div')[0]).css({marginTop:0}).animate({marginTop:-362}, useSpeed, replaceFirstProjectImage);
	if($('#playcontrol').data('visible')!=true)$('#playslider').stop().animate({opacity:0},300)
}
function replaceAttentionText(){
	$('#attention > div').html("<h2>"+nextProjectTitle+"</h2>").fadeIn(Math.floor(useSpeed/2));
	var url = $('base')[0].href + 'assets/ajax'
	$.post(url
		,{output:'content',docid:nextProjectId}
		, addAttentionText);
}
function addAttentionText(data){
	$('#attention > div').append(data).fadeIn(Math.floor(useSpeed/2));
}
function replaceFirstProjectImage(){
	var i = $('#focus_image > div > img')[0].src;
	$($('#focus_image > div')[0]).remove();
	$($('#focus_image > div')[0]).css({marginTop:0});
	
	$('#projects > div > ul > li:first').remove();
	$('#projects > div > ul').css({marginTop:0})
	$('#projects > div > ul').append('<li style="display:none;" id="itemId_'+currentProjectId+'" onclick="goToProjectWithId('+currentProjectId+')" tip="'+lastProjectTitle+'"><img src="'+i+'" alt="" border="0" width="150" height="80" /></li>');
	$('#projects > div > ul > li:last').fadeIn();
	JT_init(); 
	currentProjectId = nextProjectId;
	if(!isPaused) startSlides();
	inTransition = false;
	
	if(targetProjectId!=false && currentProjectId!=targetProjectId){
		setTimeout(nextProject,1);
	}else{
		targetProjectId = false;
		useSpeed = slideSpeed;
	}
	//$('#nextbutton').animate({opacity:1},300);
}
