$(document).ready(function(){
	// $(document.body).hide();
		Cufon.now();
	setTimeout(function(){
		$(document.body).fadeIn(200);
	},150);
	
	if ($("#project-gallery")[0]) {
	 	var projectGallery = new simpleGal({
			container:"#project-gallery",
			item:".gallery-item",
			prev:".gallery-prev",
			next:".gallery-next"
		});
	}
	
	$(".thumb-gallery").each(function(i,el){
		new simpleGal({
			container:el,
			item:".gallery-item",
			prev:".gallery-prev",
			next:".gallery-next"
		});
	});
	
	if($('body').hasClass('home')) {
		setInterval(function(){
			$('.home .gallery-next').trigger('click');
		}, 4000);
	}
	

	if (!$("#projects-nav>a[rel=next]")[0]) {
		// console.log($(".next-project"));
		// $("#projects-nav>.navdash").hide();
	}
	
	// watchScroll();
});

$(window).load(function(){
	var hash = window.location.hash;
	if (hash) {
		hash = "#"+hash.replace("#","");
		$(window).scrollTo(hash);
	}
})

$(window).unload(function(){
	$(document.body).fadeOut();
	return false;
})

function watchScroll() {
	var st = document.body.scrollTop;
	var sw = document.body.scrollHeight - document.body.offsetHeight;
	if (st==sw) {
		$("#show-more").trigger("click");
	}
	
	setTimeout(watchScroll,250);
}

function simpleGal(selectors) {
	selectors.currentCap = ".gallery-currentcap";
	selectors.container = $(selectors.container);
	
	this.selectors = selectors;
	this.container = $(selectors.container)[0];
	this.count = $(selectors.item,this.container).length;
	// this.itemWidth = $(selectors.item,this.container).eq(0).width();
	this.currentIndex = 0;
	var ref = this;
	
	$(selectors.item,this.container).hide();
	
	$(selectors.next,this.container).click(function(e){
		// if ($(this).hasClass("disabled")) return false;
		if ((ref.currentIndex+1)<ref.count) {
			ref.gotoIndex(ref.currentIndex+1);	
		}else ref.gotoIndex(0);	 
		return false;
	});
	
	$(selectors.prev,this.container).click(function(e){
		if ((ref.currentIndex-1)>=0) {
			ref.gotoIndex(ref.currentIndex-1);	
		}else ref.gotoIndex(ref.count-1); 
		return false;	
	});
		
	this.gotoIndex = function(index) {
		$(selectors.item,this.container).eq(this.currentIndex).fadeOut();
		$(selectors.item,this.container).eq(index).fadeIn();
				
		$(this.selectors.currentCap,this.container).text(index+1);

		this.currentIndex = index;
	}
	
	this.gotoIndex(0);
	
	return {
		gotoIndex: ref.gotoIndex
	}
}

// Read more
function readMore(postId) {
	$(document).ready(function(){
		if (!$('#show-more')[0]) return;
			
		moreIndex = $('#more-'+postId).parent('p').index();
		moreHide = $('#more-'+postId).parent('p').index() - 1;
		hideEmpty = $('#more-'+postId).parent('p').index() + 1;
		$('.one-col p:nth-child('+hideEmpty+')').hide();
		$('.one-col p:gt('+moreHide+')').addClass('read-more');
		$('.read-more').wrapAll('<div id="more-text"></div>');
		
		$('#more-text').after('<a href="#" id="show-more">More</a>');
		
		$('#show-more').click(function(){
			if ($('.content-wrapper').css("display")=="none") {
				$(this).addClass("active");
				$('.content-wrapper').slideDown();
			}else{
				$(this).removeClass("active");
				$('.content-wrapper').slideUp();
			 }
			return false;
		});
	});
}
