/*

	-- -- -- -- -- -- --
	Stolen from bright creative
	( http://www.brightcreative.com/portfolio/ ) 
	And modify by Dustin York :D 
	-- -- -- -- -- -- --
		
*/

	var animSpeed = 200;
	var panelWidth = 352;
	
	$(document).ready(function(){


		// set all the UI bits in one go
		function drawUI(position, direction) {
			setPosition(position, direction);
			setTitle(position);
			//setNav(position);
		}

		// set the item title and counter status
		function setTitle(counter) {
			var currentPos = counter;
			$("h3#portfolio_title").text("").prepend('<span class="counter">' + currentPos + '/' + (items.length - 1) + '</span>');
			// let's make sure the title attribute is set first, if not show counter only
			if ($("#item-" + currentPos).attr("title")) {
				var title = $("#item-" + currentPos).attr("title");
				$("h3#portfolio_title").prepend(title + " ");
			}
		}
		// set the nav links for the item pager
		function setNav(counter) {
			var next = counter + 1;
			if (counter < (items.length - 1)) {
				$("#pager .next a").attr("href", "#item-" + next);
			}
			var prev = counter - 1;
			if (counter > 1) {
				$("#pager .previous a").attr("href", "#item-" + prev);
			}
		}
		// set new position of items and slide them in place
		function setPosition(current, direction) {

			// current will be index 1
			// but our array is index 0

			var offsets = new Array();
			
			// first, set descending offsets for all items prior to current
			offsetValue = 0;
			// debug:
			//$("body").append(current + "/" + (items.length - 1)  + " | ");
			for (i = current; i > 0; i--) {
				offsetValue = offsetValue - panelWidth;
				offsets[i] = offsetValue;
				// debug:
				//$("body").append("a" + i + " " + offsets[i] + " | ");
			}
			// then, set ascending offsets for all items after current
			offsetValue = 0;
			for (i = current; i <= (items.length - 1); i++) {
				offsetValue = offsetValue + panelWidth;
				offsets[i] = offsetValue;
				// debug:
				//$("body").append("b" + i + " " + offsets[i] + " | ");
			}
			offsets[current] = 0;


			// the adjacent panel is the one we're animating along with the current panel
			// counter-intuitively, it's in the opposite direction that the panels are traveling
			var adjacent = false;
			if (direction == "left") {
				adjacent = current + 1;
			} else {
				adjacent = current - 1;
			}

			// debug:
			// $("body").append("p" + current + " " + adjacent + " | ");
			// $("body").append("<br />");

			for (i = 1; i <= (items.length - 1); i++) {
				// we only want to animate the visible ones
				// otherwise, rapid double-clicks lead to ugly stacking errors
				if ((i == current) || (i == adjacent)) {
					$("#portfolio_slideshow #" + items[i]).animate({left:offsets[i]}, animSpeed);
				} else {
					// if we're not going to animate them though, let's just move the offsets
					$("#portfolio_slideshow #" + items[i]).css({left:offsets[i]});
				}
			}
		}





		// start by getting a list of every "item" div on the page
		var items = new Array();
		var i = 1;
		$("#portfolio_slideshow").children().each(function(){
				items[i] = $(this).attr("id");
				i++;
		});


		// initialize starting position
		var currentPanel = 1;
		drawUI(currentPanel, "");

		
		// the meat of the slider. One set for prev, another for next
		$(".section_nav a.nexthover").click(function() {
			// let's only do this if there's more to come
			if (currentPanel < (items.length - 1)) {
				currentPanel++;
				drawUI(currentPanel, "right");
			}
			return false;
		});
		$(".section_nav a.prehover").click(function() {
			// let's only do this if there's more to come
			if (currentPanel > 1) {
				currentPanel--;
				drawUI(currentPanel, "left");
			}
			return false;
		});
		
		$(".section_nav a").hover( function() {
											$(".section_nav").addClass( $(this).attr("class") );
											},
									function() {
											$(".section_nav").removeClass(  $(this).attr("class") );
									} );


	});
