// portfolioPages
// this function sets up pages in new ajax-retrieved content
$.fn.portfolioPages = function() {
	
	var count = $("#portfolio-images .folio-image").length;
	
	// click action for page dot
	$(this).bind("click",function(e) {
		
		// prevent default click action
		e.preventDefault();
		
		// animate to current index multiplied by width of image div
		$("#portfolio-images").animate({left:-($(this).index()*950)},600);
		
		// set all dots to inactive
		$("#portfolio-pages img").attr("src","i/circle-gray.png");
		
		// set this dot to active
		$(this).children("img").attr("src","i/circle-red.png");
		
		// set global flag for pic index
		curProjectPic = $(this).index();
		
		// check for navs
		if (curProjectPic > 0) $("#portfolio-nav-left").animate({opacity:1});
		else $("#portfolio-nav-left").animate({opacity:0.25});
		if (curProjectPic < count - 1) $("#portfolio-nav-right").animate({opacity:1});
		else $("#portfolio-nav-right").animate({opacity:0.25});
		
		// related
		if (curProjectPic == count - 1) showRelated();
		
	});
	
}

// setProject
// sets the current projects pagination, pics, and info pane
function setProject() {

	// first folio reset
	firstFolio = false;
	
	// hide featured
	$("#portfolio-features").fadeOut(300,function() {
		$("#portfolio-features").remove();
	});
	
	// hide background
	$("#interior-bg").fadeOut();
	
	// hide pages
	$("#pagination").fadeOut();

	// clear info pane
	$("#portfolio-info-pane div").html("");
	
	// clear images and reset position
	$("#portfolio-images").html("").css("left","0px");
	
	// clear pagination
	$("#portfolio-pages").html("");
	
	// reset project picture
	curProjectPic = 0;
	
	// reset navs
	$("#portfolio-nav-right").animate({opacity:1});
	$("#portfolio-nav-left").animate({opacity:0.25});
	
	// retrieve info
	$.get("portfolio-info.php?i=" + $("#client").val(),function(data) {
		
		// populate info pane
		$("#project-info-pane div").html(data);
		
	});
	
	// fade in holder, if need be
	$("#portfolio-holder").fadeIn(500);
	
	// retrieve pics
	$.get("portfolio-pics.php?i=" + $("#client").val(),function(data) {
		
		// populate images		
		$("#portfolio-images").html(data);
		
		// fade out image holder (redundant—gives us an extra half second finish loading images), then fade in
		$("#portfolio-fader").fadeOut(500).fadeIn(1000);
		
		// fade in info pane
		// $("#project-info").fadeIn();
		
		// get image count
		var count = $("#portfolio-images .folio-image").length;
		
		// loop through image count
		for (i=0; i<count; i++) {
			
			// first page is active
			if (i==0) $("#portfolio-pages").append('<a href="#portfolio" class="selected"><img src="i/circle-red.png" /></a>');
			
			// remaining pages are not
			else $("#portfolio-pages").append('<a href="#portfolio"><img src="i/circle-gray.png" /></a>');
			
		}

		// bind click actions to all page indicators
		$("#portfolio-pages a").portfolioPages();
		
		// featured projects
		$("a.feature").unbind();
		$("a.feature").bind("click",function(e) {

			// prevent default
			e.preventDefault();

			// trigger a change on the segment dropdown
			$("#segment").val(0).trigger("change");

			// get my id
			var myID = $(this).attr("href").split("|")[1];
			// if (myID.indexOf("/") > -1) myID = myID.substring(myID.lastIndexOf("/")+1,myID.length);

			// after client is set...
			onClientSet = function() {
				
				// set the dropdown
				$("#client").val(myID);

				// slide up info pane
				$("#portfolio-info-pane").slideUp();

				// hide info button
				// $("#project-info").fadeOut();

				// clear info pane and pics, then set project
				$("#portfolio-fader").fadeOut(300,setProject);

				// reset onClientSet
				onClientSet = null;
				
			}		

		});
		
		// featured projects close
		$("#related-close").bind("click",function(e) {
			
			// prevent default
			e.preventDefault();
			$("#portfolio-related").fadeOut(300,function() {
				$("#portfolio-related").remove();
			});
			
		});

		

	});
		
}




// portfolio
// this function sets up controls for the portfolio
$.fn.portfolioSetup = function() {

	// reset portfolio nav
	$("#segment").val(0);
	$("#client").val(0);
	
	// show portfolio nav
	$("#portfolio-nav").fadeIn(500);

	// set first
	firstFolio = true;

	// change action for segment drop-down
	$("#segment").unbind();
	$("#segment").bind("change",function(e) {

		// did we have images yet?
		if ($("#portfolio-images").html() != "") {
	
			// slide up info pane
			$("#portfolio-info-pane").slideUp();
		
			// hide clear clients
			$("#client").html("");
	
			// retrieve client list for this segment
			$.get("clients.php?i=" + $(this).val(),function(data) {
				$("#client").html(data);
				if (onClientSet != null) onClientSet();
			});
		
		} else {
		
			// retrieve client list for this segment and fade in
			$.get("clients.php?i=" + $(this).val(),function(data) {
				$("#client").html(data);
				if (onClientSet != null) onClientSet();
			});
		
		}
	});

	// change action for client drop-down
	$("#client").unbind();
	$("#client").bind("change",function(e) {
		
		// if this hasn't been changed to nothing...
		if ($(this).val() != 0) {

			// did we have images yet?
			if ($("#portfolio-images").html() != "") {
		
				// slide up info pane
				$("#portfolio-info-pane").slideUp();
			
				// hide info button
				// $("#project-info").fadeOut();
		
				// clear info pane and pics, then set project
				$("#portfolio-fader").fadeOut(300,setProject);
			
			} else {
			
				// set project
				setProject();
			
			}
		}
	});

	// click action for info button
	$("#project-info").unbind();
	$("#project-info").bind("click",function(e) {
		
		// prevent default action
		e.preventDefault();
		
		// toggle info visibility
		$("#project-info-pane").slideToggle();
		
	});

	// click action for portfolio left arrow
	$("#portfolio-nav-left").unbind();
	$("#portfolio-nav-left").bind("click",function(e) {
		
		// prevent default action
		e.preventDefault();
		
		// get image count
		var count = $("#portfolio-images .folio-image").length;
	
		if (curProjectPic > 0) {
		
			// decrement pic index
			curProjectPic--;
			
			// hide related
			$("#portfolio-related").fadeOut();
		
			// animate to newly selected pic index multipled by image div width
			$("#portfolio-images").animate({left:-(curProjectPic*950)},600);
		
			// set all page dots to inactive
			$("#portfolio-pages img").attr("src","i/circle-gray.png");
		
			// set current index's dot to active
			$("#portfolio-pages img:eq(" + curProjectPic + ")").attr("src","i/circle-red.png");
			
			// check for self deactivation
			if (curProjectPic == 0) $("#portfolio-nav-left").animate({opacity:0.25});
			
			// check for right nav activation
			if (curProjectPic < count - 1) $("#portfolio-nav-right").animate({opacity:1});
			
		}
		
	}).animate({opacity:0.25});

	// click action for portfolio right arrow
	$("#portfolio-nav-right").unbind();
	$("#portfolio-nav-right").bind("click",function(e) {
		
		// prevent default action
		e.preventDefault();
		
		// get image count
		var count = $("#portfolio-images .folio-image").length;
	
		if (curProjectPic < count - 1) { 
		
			// increment pic inde
		 	curProjectPic++;
		
			// animate to newly selected pic index multipled by image div width
			$("#portfolio-images").animate({left:-(curProjectPic*950)},600);
		
			// set all page dots to inactive
			$("#portfolio-pages img").attr("src","i/circle-gray.png");
		
			// set current index's dot to active
			$("#portfolio-pages img:eq(" + curProjectPic + ")").attr("src","i/circle-red.png");
			
			// check for left nav activation
			if (curProjectPic > 0) $("#portfolio-nav-left").animate({opacity:1});

			// check for last item
			if (curProjectPic == count - 1) {
				
				// deactivate right button
				$("#portfolio-nav-right").animate({opacity:0.25});
				
				// delayed showing of related projects
				showRelated();
				
			}
			
		}
	});
	
	// featured projects
	$("#feature-hold a").unbind();
	$("#feature-hold a").bind("click",function(e) {
		
		// prevent default
		e.preventDefault();
		
		// trigger a change on the segment dropdown
		$("#segment").val(0).trigger("change");
		
		// get my id
		var myID = $(this).attr("href").split("|")[1];
		// if (myID.indexOf("/") > -1) myID = myID.substring(myID.lastIndexOf("/")+1,myID.length);
		
		// after client is set...
		onClientSet = function() {
			
			// set the dropdown
			$("#client").val(myID);
			setProject();
			onClientSet = null;
			
		}		
		
	});



}

function showRelated() {
	$("#portfolio-related").delay(3000).fadeIn();
}
