/***
	Generic JavaScript functions.
	(Comments to luckyluke@luckyluke.eu)
***/
currentProject = 0;

function swapProjects(){
	if(window.currentProject){
//		$('.logo').html(currentProject);
		$('.projects').hide();
		$('.project'+currentProject).fadeIn(500);
		if(currentProject==1) {
			$('.prevButton').hide();
		}else{
			$('.prevButton').show();
		}
		if(currentProject==projectCount) {
			$('.nextButton').hide();
		}else{
			$('.nextButton').show();
		}
		if(currentProject!=1 && currentProject!=projectCount){
			$('.curIndicator').show();
		} else {
			$('.curIndicator').hide();
		}
	//	alert($('#myCarousel'+currentProject).css("width"))
//		curLi = currentProject;
		LICount = ($('#myCarousel'+currentProject+" LI").length)
		ULWidth = LICount*91;
		$('#myCarousel'+currentProject).css("width",ULWidth+"px");
	//	$('#myCarousel'+currentProject).css("width","1001px");
	//	$('.curIndicator').html(currentProject+"/"+projectCount);
//		alert(currentProject+" a "+LICount);
//		LICount = window.currentProject ? $("#myCarousel"+currentProject+" LI").length : $(".carousel LI").length;
		curLi = 0;
		showlist();
	}
}
var curLi = 0
var LiCount = 0;
function showlist(){
//	$("#canvas").append("curLi= "+curLi+" a LiCount= "+LiCount+"<br />");
	if(curLi <= LiCount){
		if(window.currentProject){
			$("#myCarousel"+currentProject+" LI").eq(curLi).fadeIn(50, function(){ curLi++; showlist(); });
		} else {
			$(".carousel LI").eq(curLi).fadeIn(50, function(){curLi++; showlist(); });
		}
	} 
	if (curLi == LiCount){
//		$("#canvas").append("kurva<br />");
//		alert("currentProject = "+currentProject);
		$('#myCarousel'+currentProject).jcarousel({scroll:1}); //,wrap:"circular"
//		$('.jcarousel-next').css({"border":"1px solid red"});
//		$("#canvas").append("disabled = "+$('.jcarousel-next').attr("disabled")+"<br />");
		
	}
}
$(document).ready(function(){
	LiCount = window.currentProject ? $("#myCarousel"+currentProject+" LI").length : $(".carousel LI").length;
//	for(i=1;$('#myCarousel'+i).length>0;i++){
		$('#myCarousel'+currentProject).jcarousel({scroll:1}); //,wrap:"circular"
//	}
	$(".carousel LI").hide();
	$('.projectNav').show();
//	alert($('#mycarousel1').length);
    $('.leaveComment').toggle(function(){
		$(this).parents(".postItem").children(".bespokeComment").show();
		return false;
	}, function(){
		$(this).parents(".postItem").children(".bespokeComment").hide();
		return false;
	})
	$('.bigImage1').show();
	$(".heading").fadeIn(500)
	$(".content").fadeIn(500);
	$('.prevButton').click(function(){
		if(currentProject>1) currentProject--;
		swapProjects();
		return false;	
	});
	$('.nextButton').click(function(){
		if(currentProject<projectCount) currentProject++;
		swapProjects();
		return false;	
	});
	if(window.currentProject) swapProjects();
	else showlist();
//	$('.curIndicator').html(currentProject+"/"+projectCount);
	/* PROJECT HUB */	
	$('.page-id-180 .carousel LI A, .page-id-182 .carousel LI A, .page-id-185 .carousel LI A, .page-id-187 .carousel LI A, .page-id-189 .carousel LI A, .page-id-191 .carousel LI A').hover(function(){
		projectNumber = $(this).attr("id");
		projectNumber = projectNumber.substring(6,projectNumber.length);
		$('.bigImages').hide();
		$('.bigImage'+projectNumber).fadeIn(500);
	},function(){
		$('.bigImage'+projectNumber).fadeOut(1000);
	});
//	$('.bigImage1').fadeOut(1000);
	/* PROJECT PAGE */	
	$('.JSSwap LI A').click(function(){
		projectNumber = $(this).parents("UL").attr("id");
		projectNumber = projectNumber.substring(10,projectNumber.length);
//		alert(projectNumber+", "+$(this).attr("rel"))
		$('.bigImage'+projectNumber).hide();
//		$('.bigImage'+projectNumber).attr("src",$(this).attr("rel"));
//		$('.bigImage'+projectNumber).fadeIn(500);
//		$('.bigImage'+projectNumber).attr("src",$(this).attr("rel")).fadeIn(500);
		$('.imgCol3').addClass("loading");
		$('.bigImage'+projectNumber).attr("src",$(this).attr("rel"));
		$('.bigImage'+projectNumber).load(function(){
			$('.imgCol3').removeClass("loading");
			//if(${'.bigImage'+projectNumber} != '') alert('IFRAME FINISHED LOADING');
			$('.bigImage'+projectNumber).fadeIn(500);
		}); 
		return false;
	});
	/* PRESS PAGE */	
	$('.page-template-press-php .carousel LI A').click(function(){
		projectNumber = $(this).attr("id");
		projectNumber = projectNumber.substring(6,projectNumber.length);
		currentProject = projectNumber;
//		alert(projectNumber);
		$('.projects').hide();
		$('.project'+currentProject).fadeIn(500);
		return false;
	});
    

})



