$(function(){
	var projects = $('ul.projects').children().length;
	var width =  projects * 902;
	var dotwidth = (projects * 31);
	if(projects > 1){
		var html = '<span id="prevdot"></span><span id="nextdot"></span><ul id="dots">';
		for(i=1; i<=projects; i++){
			if(i==1){
				html += '<li class="current"></li>';
			}else{
				html += '<li></li>';
			}
		}
		html += '</ul>';
		$('ul.projects').parent().append(html);
		$('ul.projects').css({'width':width+'px'});
		$('ul#dots').css({'width':dotwidth+'px'});
		$('.portfolio').css({'overflow':'hidden'});
	}
	$('.projects .pics ul img').click(function(){
		var src = $(this).attr('src').replace('_thumb', '');
		$(this).parent().parent().parent().children('.mainpic').attr('src', src);
	});
	$('#dots li').hover(function(){
		if(!$(this).hasClass('current')){
			$(this).css({'background-image':'url(images/dot_hover.png)'});
		}
	},function(){
		if(!$(this).hasClass('current')){
			$(this).css({'background-image':'url(images/dot.png)'});
		}
	});
	$('#dots li').click(function(){
		var index = $(this).index();
		$('ul.projects').animate({'left':'-'+index*902}, 200);
		$('#dots li').removeClass('current');
		$(this).addClass('current');
		$(this).removeAttr('style');
	});
	$('#prevdot').hover(function(){
		$(this).css({'background-image':'url(images/prev_hover.png)'});
	},function(){
		$(this).css({'background-image':'url(images/prev.png)'});
	});
	$('#prevdot').click(function(){
		var index = $('#dots li.current').index() - 1;
		if(index < 0){
			index = projects - 1;
		}
		$('ul.projects').animate({'left':'-'+index*902}, 200);
		$('#dots li').removeClass('current');
		$('#dots li').eq(index).addClass('current');
		$('#dots li').eq(index).removeAttr('style');
	});
	$('#nextdot').hover(function(){
		$(this).css({'background-image':'url(images/next_hover.png)'});
	},function(){
		$(this).css({'background-image':'url(images/next.png)'});
	});
	$('#nextdot').click(function(){
		var index = $('#dots li.current').index() + 1;
		if(index > (projects - 1)){
			index = 0;
		}
		$('ul.projects').animate({'left':'-'+index*902}, 200);
		$('#dots li').removeClass('current');
		$('#dots li').eq(index).addClass('current');
		$('#dots li').eq(index).removeAttr('style');
	});
	
	$('#web_carosel').hover(function(){
		if (!$(this).children().children('img').hasClass('animated')) {
			$(this).children().children('img').dequeue().stop().fadeIn(200);
		}
	},function(){
		$(this).children().children('img').addClass('animated').fadeOut(200, function() {
			$(this).removeClass('animated').dequeue();
		});
	});
	
	$('#games_carosel').hover(function(){
		if (!$(this).children().children('img').hasClass('animated')) {
			$(this).children().children('img').dequeue().stop().fadeIn(200);
		}
	},function(){
		$(this).children().children('img').addClass('animated').fadeOut(200, function() {
			$(this).removeClass('animated').dequeue();
		});
	});
	
	$('#print_carosel').hover(function(){
		if (!$(this).children().children('img').hasClass('animated')) {
			$(this).children().children('img').dequeue().stop().fadeIn(200);
		}
	},function(){
		$(this).children().children('img').addClass('animated').fadeOut(200, function() {
			$(this).removeClass('animated').dequeue();
		});
	});
	
	setTimeout("nextslide('web_carosel')", 3000);
	setTimeout("nextslide('games_carosel')", 4000);
	setTimeout("nextslide('print_carosel')", 5000);
	
	
//	setTimeout("displaynext(1)", 3000);
	
	
});



	function nextslide(carosel){
		var current_slide = $('#'+carosel+' .current').index();
		var num_slides = $('#'+carosel+' li').size();
		//alert(num_slides);
		if(current_slide >= (num_slides-1)){
			var next_slide = 0;
		}else{
			var next_slide = current_slide + 1;
		}
		$('#'+carosel+' li').eq(next_slide).fadeIn(1000);
		$('#'+carosel+' li').eq(next_slide).addClass('current');
		$('#'+carosel+' li').eq(current_slide).fadeOut(1000);
		$('#'+carosel+' li').eq(current_slide).removeClass('current');
		setTimeout("nextslide('"+carosel+"')", 5000);
	}

