$(document).ready(function() {
	// initAboutmePage();
	
	// hidePageTitles();
	showWorks();
	showMainImages();
	hideEntries();
	hideCategory();
	
	bindSidebarHoverEvent();
	bindSidebarClickEvent();
	bindAboutmeEvents();
	
	hideLoader();
});


function showPost(id) {
	$("#post-"+id).show();
}

function hideEntries() {
	$(".entry").hide();
}
function hideCategory() {
	$("#category").hide();
}
function hideAboutmePage() {
	$("#aboutme").hide();
}
function hidePageTitles() {
	$(".page-item-2").hide();
}
function hideWorks() {
	$("#works").hide();
}
function showWorks() {
	$("#works").show();
}


function showMainImages() {
	$(".ngg-galleryoverview div.ngg-gallery-thumbnail-box:first-child").show();
}


function bindSidebarHoverEvent() {
	$(".active").hover(
		function() {
			hideCategory();
			
			var catId = $(this).attr("class").substring(4,5);
			$("#category").show();
			$("#category").attr("class","cat-" + catId);
			
			// if (catId != '8') {
				hideEntries();
				$("#post-" + this.id).show();
			// }
						
			switch(catId) {
				case '8': { $("#category").html("contact me"); break; }
				case '3': { $("#category").html("graphics"); break; }
				case '4': { $("#category").html("web"); break; }
				case '5': { $("#category").html("interaction"); break; }
				case '6': { $("#category").html("product"); break; }
				case '7': { $("#category").html("experiments"); break; }
			}
		  	Cufon.replace('#category', { fontFamily: 'neosans', fontSize: '12px' });
		},
		function() {
//			var catId = $(this).attr("class").substring(4,5);
			
//			if (catId != '8') {
//				hideCategory();
//				hideEntries();
//			}
			
		}
	);
}

function bindSidebarClickEvent() {
	$(".active").click(
		function() {
			// hideEntries();
			// $("#post-" + this.id).show();
			// unbindSidebarHoverEvent();
			// unbindSidebarClickEvent();
			
			var src = $("#post-" + this.id + " a#video").attr("href");			
			
		 	if (src) { // video
		 			// TODO: non richiamare nggallery
				$("#post-" + this.id + " > div.ngg-galleryoverview >:first-child > div.ngg-gallery-thumbnail a").trigger('click');
//		 		window.open(src,"video","status=0,toolbar=0,menubar=0,resizable=0,scrollbars=0,width=752,height=412");
		 			
		 		setTimeout(function() {
			 		if(src.indexOf("youtube") != -1) {
			 			$("#shTopImg").replaceWith('<iframe width="752" height="412" src="'+ src + '?HD=1;rel=0;showinfo=0;controls=0;autoplay=1" frameborder="0" allowfullscreen></iframe>');
			 		}
			 		else if(src.indexOf("vimeo") != -1) {
			 			$("#shTopImg").replaceWith('<iframe src="'+ src + '?title=0&byline=0&portrait=0&autoplay=1&color=ffffff&loop=0" width="752" height="412" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>');
		 			}
		 		}, 3000);
		 	}
		 	else { // gallery
				$("#post-" + this.id + " > div.ngg-galleryoverview >:first-child > div.ngg-gallery-thumbnail a").trigger('click');
		 	}
		}
	);
}


function unbindSidebarHoverEvent() {
	$(".active").unbind('mouseenter mouseleave');
	setTimeout("bindSidebarHoverEvent()", 8000);
}


function unbindSidebarClickEvent() {
	$(".active").unbind('click');
	setTimeout("bindSidebarClickEvent()", 2000);
}


function bindAboutmeEvents() {
	$(".page-item-2").click(openAboutmePage);
	$("#aboutme").click(closeAboutmePage);
}

function initAboutmePage() {
	$(".page-item-2").show();
}
function openAboutmePage() {
	$(".page-item-2").hide();
	$("#works").hide();
	$("#aboutme").show('slide', {direction: 'left'}, 1000);
}
function closeAboutmePage() {
	$("#aboutme").hide('slide', {direction: 'left', distance: '278px'}, 1000);
	setTimeout('$(".page-item-2").show()', 1000);
	setTimeout('$("#works").fadeIn()', 1100);
}

function replaceFonts() {
  	Cufon.replace('h2', { fontFamily: 'neosans', fontSize: '13px' });
  	Cufon.replace('.works_cat', { fontFamily: 'neosans', fontSize: '12px' });
  	Cufon.replace('#category', { fontFamily: 'neosans', fontSize: '12px' });
  	Cufon.replace('.entry p', { fontFamily: 'neosans-light', fontSize: '12px' });
  	Cufon.replace('.client', { fontFamily: 'neosans-light', fontSize: '12px' });
  	Cufon.replace('.year', { fontFamily: 'neosans-light', fontSize: '12px' });
  	Cufon.replace('.description', { fontFamily: 'neosans-light', fontSize: '12px' });
  	Cufon.replace('.title', { fontFamily: 'neosans-light', fontSize: '12px' });
  	Cufon.replace('#cformsform', { fontFamily: 'neosans', fontSize: '12px' });
}

function hideLoader() {
	$("#loader").hide();
}

