var timers = [];
var zIndex = 1;

$(document).ready(function(){
	$('.projectList a.name').map(function(index){
		$(this).bind('click', {index: index, autoHide: false}, showProject);
		$(this).bind('mouseover', {index: index, autoHide: true}, showProject);
		$(this).bind('mouseout', {index: index, autoHide: true}, hideProject);
	});
	$('.project .close a').map(function(index){
		$(this).bind('click', {index: index}, function(e){
			$('.project').eq(e.data.index).hide();
		});				
		$(this).bind('mouseover', {index: index, autoHide: true}, showProject);
		$(this).bind('mouseout', {index: index, autoHide: true}, hideProject);
	});				
	$('.project').draggable();
	$('.project').map(function(index){
		$(this).bind('mouseover', function(){
			clearTimeout(timers[index]);
		});
		$(this).bind('mouseout', {index: index}, hideProject);
		$(this).bind('mousedown', function(){
			zIndex++;
			$(this).css('z-index', zIndex);
		});					
	});
});

function showProject(e) {			
	
	zIndex++;
	
	var project = $('.project').eq(e.data.index);

	project.css('z-index', zIndex);
	
	if (e.data.autoHide == true) {
		project.addClass('autoHide');
	} else {
		project.removeClass('autoHide');
	}
	if (project.css('display') == 'none') {
		project.css('top', (e.data.index * 10) + 'px');
		project.css('left', (300 + (e.data.index)) + 'px');
	}
	project.show();
	return false;
}

function hideProject(e) {
	var project = $('.project').eq(e.data.index);
	if (project.hasClass('autoHide')) {
		timers[e.data.index] = setTimeout(function(){
			project.hide();
		}, 250);
	}
	return false;
}
