/*
Team CrossFit
Created By: Mitch Gohman
Date: 2010-04-04
With much love to JQuery - the javaScript browser equalizer
This ditty takes the first set of posts in Wordpress, and creates a horiz. sliding navigation
*/
var SLDPST = {}
SLDPST.width = 960;
SLDPST.height = 350;
SLDPST.slideSpeed = 1000;
SLDPST.postLimit = 7; //number of posts to round up and display in sliding navaigation

SLDPST.slide = function(event) {
	var thisItem = this;
	
	var destPosition = thisItem.rel;
	$("#slider").animate({"left":"-" + destPosition + "px"},1000);//HERE I AM
	
	$("#sliderNav a").removeClass('selected');
	
	$(thisItem).addClass('selected');
	
	event.preventDefault();
};

SLDPST.set = function() {
	//the container for our slding posts
	$('<div id="slidingPosts"><div id="slider"></div></div><div id="sliderNav"></div>').insertAfter("#masthead");
	
	//set width of slider to match total width of sliding posts
	var totalWidth = SLDPST.postLimit * SLDPST.width;
	$("#slider").css("width",totalWidth + "px");
	
	//build slider navigation 
	for (var i=0; i < SLDPST.postLimit; i++)
		{
		//which left position to slide to
		var thePos = i * SLDPST.width;
		$('<a href="#" rel="' + thePos + '">' + (i+1) + '</a>').appendTo("#sliderNav");
		}
	
	//make the first item selected
	$("#sliderNav a:first").addClass('selected');
	
	//bind sliding function to each link
	$("#sliderNav a").bind("click", SLDPST.slide);
	
	//get all posts
	var thePosts = $("#articles .post");
	
	//only manage the postLimit
	for (var i=0; i < SLDPST.postLimit; i++)
		{
		//alert($(thePosts[i]).html());
		
		var postLink = $("h3 a",thePosts[i]).attr("href");
		//alert(postLink);
		var currPost = $(thePosts[i]).html();
		$('<div id="slidePost' + i + '" class="slidePost">' + currPost + "<a href=\"" + postLink + "\" class=\"readmore\">read more</a></div>").appendTo("#slider");
		$(thePosts[i]).hide();
		}
	
	//set up initial position of each slide horizontally
	var newPosts = $("#slidingPosts .slidePost");
	var currLeftPos = 0;
	for (var i=0; i < newPosts.length; i++)
		{
		//var _thisObj = this;
		$(newPosts[i]).css("left",currLeftPos + "px");
		currLeftPos = currLeftPos + SLDPST.width;
		
		var postImages = $("img", newPosts[i]); 
		
		for (var j = 0; j < postImages.length; j++)
			{
			if (j == 0)
				{
				var imgDimH = postImages[j].height;
				var imgDimW = postImages[j].width;
				
				var percentChng = SLDPST.height/imgDimH;
				
				postImages[j].height = SLDPST.height;
				postImages[j].width = Math.round(imgDimW * percentChng);
				
				//if width exceeds desired limit, 450
				if (postImages[j].width > 475)
					{
					percentChng = imgDimW/475;
					postImages[j].width = 475;
					postImages[j].height = Math.round(imgDimH * percentChng);
					}
				
				}
				else
				{
				//if there is more than 1 image, hide it.
				$(postImages[j]).hide();
				}
			}
		}
};
//$("document").ready(SLDPST.set);
