// JavaScript Document

/* This Slider is by Patrick Murray.  It's sloppy, but you love it.  idsquid@gmail.com */

var thumbWidth0 = $('.thumb').outerWidth();
 var thumbWidth = $('.thumb').outerWidth(true);//alert(thumbWidth);
 var sampleWidth0 = $('.sample').outerWidth();
 var sampleWidth = $('.sample').outerWidth(true);
 var items = $('#thumbWrapper div').length;
 //the width of the samples
 var sampleStart = 494
 var sampleEnd = -494
 
//Thumbnail wrapper definitions
 var wrap = thumbWidth * items ; 
 var wrapLimit0 = 0 ;
 var wrapHome = -wrap ;
 var wrapLimit2 = -wrap*2
 var wrapPos = -wrap ;
 
//where do the Thumbs Begin?
var thumbViewPos = $('#thumbViewer').offset() ; //alert(thumbViewPos.left);
var firstThumbfake = $('#thumbWrapper div:nth-child(1)').offset() ;
var firstThumbPos = firstThumbfake + thumbWidth0 ;
//where do the Thumbs go? NOTE - gotta add the width of the element
var lastThumbIndex = "#thumbWrapper div:nth-child(" + items + ")";
var fakePos = $(lastThumbIndex).offset() ;
var lastThumbPos = fakePos.left + thumbWidth0;
//ACTUALLY ignore that shyte.  It's more flexible in an array, like so :
var thumbPosArray = new Array();
for (var i = 0; i < items+1; i++) {
   thumbPosArray[i] = i*thumbWidth - thumbWidth; 
 } ;


//where do the Samples go?
sampleLeft = {}
sampleLeft["left"] = "-=" + sampleWidth +"px" ;
sampleRight = {}
sampleRight["left"] = "+=" + sampleWidth + "px";


//Our Indexes
var targetSample = 0 ;
var currentSample = 1 ;
var currentThumb = 1 ;
var firstThumb = 1;
var lastThumb = $('#thumbWrapper div').length;
var index = new Array();
for (var i = 0; i < items; i++) {
   index[i] = i 
 } 

//now Clone the Thumbnails twice to make the thumbPress action smooth
$('#thumbWrapper').contents().clone().appendTo('#thumbWrapper');
$('#thumbWrapper').contents().clone().appendTo('#thumbWrapper');
//and move the wrapper to the middle
$('#thumbWrapper').css("left", wrapHome) ;
			
 // Button Pressed
      $('#nav a').click(function(){
	  // get href attribute, minus the #
        var button = $(this),
           direction = button.attr('href').slice(1); 
	  // get the position and call appropriate function
		   if (direction == "left") {
		   		var dir = "-";
				moveButtonAction(dir);
		   }
		   else if (direction == "right") {
		   		var dir = "+";
				moveButtonAction(dir);
		   }
		});

 //ThumbNail Pressed
  		$('#thumbWrapper a').click(function(){
			// get href attribute, minus the #
			   var thumb = $(this),
			   thumbPressed = thumb.attr('sample').slice(6); //alert(thumbPressed);
			   if (thumbPressed == currentThumb ) {return false;} // Kill if current
			   updateContent(thumbPressed); //Update the Content - DUH.
	   // Distance to move is difference between thumb position and starting position (constant)
			    distance = thumbPosArray[thumbPressed] ; 
				wrapPos = wrapPos - distance ;
				moveThumbAction(thumbPressed, distance) ;
			//reset Thumb Position Array
				for (var i = 0; i < items+1; i++) {
				   thumbPosArray[i] = thumbPosArray[i]-distance;
				   if (thumbPosArray[i] <= 0) {
				   		thumbPosArray[i]=thumbPosArray[i] + wrap;   
				   }
 				} ;
				
				return false;});
				
//Button Action
	function moveButtonAction(dir) { 
		//FIRST MOVE THE THUMBNAILS
		//USING THE ARROWS
				if (dir == "-") { //left alert('on');
		  		   	//set the wrapper counter
					wrapPos = wrapPos - thumbWidth ;
 				//move the wrapper
				move = {}
				move["left"] = dir + "=" + thumbWidth;
				$("#thumbWrapper").animate(move, "fast", function() {resetCheck();}); 
				//call the sample movement
				var distance = thumbWidth ; //constant for buttons
				var thumbPressed = 0 ; //no thumbnail pressed
				moveSample(dir, thumbPressed) ;
					//reset the counter			
					if (currentThumb != items) {	
						newThumb = parseInt(currentThumb)+1 ;
					}
					else {newThumb = 1 ;
					} ;
					//reset Thumb Position Array
				for (var i = 0; i < items+1; i++) {
				   thumbPosArray[i] = thumbPosArray[i]-distance;
				   if (thumbPosArray[i] <= 0) {
				   		thumbPosArray[i]=thumbPosArray[i] + wrap;   
				   }
 				} ;
			} ;
			if (dir == "+") { //left 
		   		//set the wrapper counter
				wrapPos = wrapPos + thumbWidth ;	
				//move the wrapper
				move = {}
				move["left"] = dir + "=" + thumbWidth;
				$("#thumbWrapper").animate(move, "fast", function() {resetCheck();}); 
				//call the sample movement
				var distance = thumbWidth ; //constant for buttons
				var thumbPressed = 0 ; //constant - no thumbnail pressed
				moveSample (dir, thumbPressed) ;
					//reset the counter for the Kill switch		
					if (currentThumb != 1) {	
						newThumb = parseInt(currentThumb)-1 ;
					}
					else {newThumb = items ;
					} ;
					//reset Thumb Position Array
				for (var i = 0; i < items+1; i++) {
				   thumbPosArray[i] = thumbPosArray[i]+distance;
				   if (thumbPosArray[i] >= wrap) {
				   		thumbPosArray[i]=thumbPosArray[i] - wrap;   
				   }
 				} ;
			} ;
			updateContent(newThumb); //Update the Content - DUH.
		return false; } ; //end function
		
//ThumbNail Action
	function moveThumbAction(thumbPressed, distance) {
		//MOVE THE THUMBNAIL WRAPPER
		move2 = {}
		move2["left"] = "-=" + distance ;
		$('#thumbWrapper').animate(move2, "normal", function() {resetCheck();}) ;
		//call the sample movement
			var dir = "-" ; //constant for thumb pressed
			thumbPressed = thumbPressed ;
			currentThumb = thumbPressed ;
			moveSample(dir, thumbPressed) ;
	} ; //end function
			
//Move the Samples
	function moveSample(dir, thumbPressed) {
	//return false;
		//MOVE THE SAMPLE
		//THREE Conditions, based on Direction and if Thumb Pressed
		if (dir=="+") { //definitely right button
			if (currentSample == 1) {
				targetSample = items ;
			}
			else {
				targetSample = currentSample - 1 ;
			}
			//Move the Samples Right to Left
				s0 = "#sample" + targetSample ; //alert(s0);
				s1 = "#sample" + currentSample ;
				$(s0).css("left", "-494px") ; //494 is the sample width
		 	$(s1).animate({"left": "494px"}, "normal");
			$(s0).animate({"left" : "0px"}, "normal");
			//set Index
			currentSample = targetSample ;
			return false;
		}
		else if (thumbPressed == 0) { //Left button
			if (currentSample == items) {
				targetSample = 1 ;
			}
			else {
				targetSample = 1 + parseInt(currentSample) ; 
			}
		}
		else {targetSample = thumbPressed;}; //alert(targetSample);
		//Move the Samples Right to Left
				s0 = "#sample" + targetSample ; //alert(s0);
				s1 = "#sample" + currentSample ;
				$(s0).css("left", "494px") ; //494 is the sample width
				$(s1).css("left", "0px") ; //494 is the sample width
		 	$(s1).animate({"left": "-494px"}, "normal");
			$(s0).animate({"left" : "0px"}, "normal");
			//set Index
			currentSample = targetSample ; 
			return false;
			
	} //end function
		
	
//Check for Wrapper reset
	function resetCheck() { //alert(wrapPos);
		//Condition 1 - only happens through Right Button pressed 
		if (wrapPos == 0) {
			$('#thumbWrapper').css("left", wrapHome) ;
			wrapPos = wrapHome;
		}
		//Condition 2 : 
		if (wrapPos < wrapLimit2) {
			newPos = wrapPos + wrap ;
			$('#thumbWrapper').css("left", newPos) ;
			wrapPos = newPos;
		}
	
	return false;}
		
//Update the Content 
	function updateContent(newContent) { //alert("on");
		var oldTitle = "#title" + currentThumb ;
		var newTitle = "#title" + newContent ;
		var oldImage = "#tImage" + currentThumb ;
		var newImage = "#tImage" + newContent ;
		var oldText = "#content" + currentThumb ;
		var newText = "#content" + newContent ;
		
		$(oldTitle).removeClass("selected-title") ;
		$(newTitle).addClass("selected-title") ;
		$(oldImage).removeClass("selected-title-image") ;
		$(newImage).addClass("selected-title-image") ;
		$(oldText).removeClass("selected-content") ;
		$(newText).addClass("selected-content") ;
		
		currentThumb = newContent //reset counter
		
		
	return false ; }
	
// LINKS
	//$(document).ready(function() {$('.website-link').fadeOut('fast')} );
	$('.website-link').mouseleave(function(){$(this).fadeOut(200)})
	$('.webStill').mouseenter(function(){$(this).prev().fadeIn(200)})


