/* cufon text replacement statments */
Cufon.replace('.profile h2',{fontFamily: 'Gill Sans MT'});
Cufon.replace('h2.helv',{fontFamily: 'HelveticaNeue LT 97 BlackCn'});
Cufon.replace('#header p', {fontFamily: 'Gill Sans MT Light'});
Cufon.replace('#footer address, #footer li', {fontFamily: 'Gill Sans MT Light'});




$(document).ready(function(){
	
	// setup JS overlay functionality
	setupOverlays();

	// setup people profiles (popups)
	setupPopupProfiles();
});
	
	
	
	
/* popup profiles for thumbnails page */
function setupPopupProfiles() {
	$(".popupProfiles li .profile").wrapInner('<div class="inner"></div>').append('<div class="bottom"></div>');
	
	$(".popupProfiles li").mouseenter(function(){
		$(this).children(".profile").stop().addClass("show").animate({opacity: 1});
	}).mouseleave(function(){
		$(this).children(".profile").stop().animate({opacity: 0}, {complete: removeClassShow});
	});

}


/* hide stuff */
function removeClassShow() {
	$(this).removeClass("show");
}


var caseID;
var pageID;
var numberOfPages;


/* work pages overlays */
function setupOverlays() {
	// add a close button to each overlay
	$("#container").append('<div class="overlay"><div class="inner"><div class="innercontent"></div><div class="close"></div></div></div>');
	$(".overlay .inner").append('<div class="paginate"></div>').append('<div class="pages prev"></div>').append('<div class="pages next"></div>').append('<div class="logo"><img/></div>');
	
	// setup close function
	$(".close").click(function() {
		$(".overlay .innercontent").empty();
		$(this).parents(".overlay").fadeOut();
	});
	
	// setup overlay popup function
	$("a.overlay").click(function() {
		caseID = $(this).attr("id").replace("overlay", "");
		pageID = 1;
		
		var logoScr = $(this).children("img").attr("src");
		logoScr = logoScr.replace("dynamicImages/","dynamicImages/small_");
		$(".logo img").attr('src', logoScr);
				
		$("div.overlay").fadeIn();
		
		$.ajax({
			type: "POST",
			url: "php/getContent.php",
			data: "caseID="+caseID+"&pageID="+1,
			success: function(msg){
				$("div.overlay .innercontent").css("visibility", "hidden").html(msg);
				processLayout();				
				$("div.overlay .innercontent").css("visibility", "visible");
			}
		});
		
		$.ajax({
			type: "POST",
			url: "php/getPages.php",
			data: "caseID="+caseID,
			success: function(msg){
				$("div.overlay .paginate").html(getPaging(msg));
				Cufon.replace('.paginate li',{fontFamily: 'HelveticaNeue LT 97 BlackCn', hover: true});
				
			}
		});

		return false;
	});
	
	// if a pagination button is clicked it jumps to the right page
	$(".paginate li").live("click", function(){
		pageID = Number($(this).text());
		getContentFromPagination()
	});
	
	// next button functionaslity
	$(".next").live("click", function(){
		if (pageID < numberOfPages) {
			pageID = pageID + 1;
			getContentFromPagination();
		}
	});
	
	// previous button functionaslity
	$(".prev").live("click", function(){
		if (pageID > 1) {
			pageID = pageID - 1;		
			getContentFromPagination();
		}
	});

}



// vertically center text in the case studies window
function processLayout() {
	var height = $(".innercontent").height();
	var innerheight = $(".vertCentered").height();
	
	if (height && innerheight) {
		var paddingTop = (height - innerheight) / 2;
		$(".vertCentered").css("paddingTop", paddingTop)
	}	
}



// load new content
function getContentFromPagination() {
	
	$(".paginate li").removeClass("on");
	$(".paginate li.paginate"+pageID).addClass("on");
	$.ajax({
		type: "POST",
		url: "php/getContent.php",
		data: "caseID="+caseID+"&pageID="+pageID,
		success: function(msg){
			$("div.overlay .innercontent").css("visibility", "hidden").html(msg);
			processLayout();				
			$("div.overlay .innercontent").css("visibility", "visible");
		}
	});
}



// load the pagination bar for the case studies
function getPaging(num) {
	numberOfPages = num;
	
	html = "<ul>";
	for (var i=1; i<=num; i++) {
		var live = i==1 ? "on " : "";
		html += '<li class="'+live+'paginate'+i+'">'+i+'</li>';
	}
	html += "</ul>";
	
	return html;
}


