//-------------------------------------------------------------------
// START: VARIABLES
//-------------------------------------------------------------------	

	// global settings/vars
	var bLoadComplete 	= false;	
	var bIsPlaying		= false;
	var iBoxMasterWidth	= 0; // sets the width of the div containing the logos
	var setTimerPlay;
	var setTimerSlider_Mouseout;
	var setTimerItem_Mouseout;
	var bTimerPlayIsSet = false;
	var iThisSpotlighted = -1;
	var bMouseTrack_Status = 0; // 0 = no change, 1 = over, 2 = out
	
	// load settings
	var iLoadPosition 	= 0;
	var iBoxPosition 	= iBoxParentStopPosLeft; // where each box is positioned
	
	// mouse tracking settings
	var bMouseIsOver_Container		= false;
	var bMouseIsOver_Item			= false;
	var bMousePosX					= 0;
	var bMousePosY					= 0;
	
	// testing controls
	var bTrace	= false;
	
	// sliding/playing variables
	var setIntervalPlay;
	var iPlayDirection 	= 0; // 0 = left, 1 = right
	var iPlayPosition 	= 0;
	var iPlayRest		= false;
	
	var iHoverDistance = 0;
	var iHoverPercent = 0;
	var bHoverHasSpotlight = false; // the first time a spotlight is called this is set to fall
	var iHoverSliderStartPosition = 0;
	
	var iPlaySlideSpeedNow = 100;
	iPlaySlidePixelsPerSec = iPlaySlidePixelsPerSec/1000; // resets pixels/second to pixels/millisecond
	
	
//-------------------------------------------------------------------
// START: FUNCTIONS
//-------------------------------------------------------------------

// JQUERY - START READY FUNCTIONS
$(document).ready(function(){
	
	// last mouse positiong / tracking
	$().mousemove(function(e){
		bMousePosX = e.pageX;
		bMousePosY = e.pageY;
	}); 

	
	//-------------------------------------------------------------------
	// JQUERY - Page Set Up	
	
	// writes the images into the page -----------------------------------------------------------
	jQuery.fn.fncPageSetUp = function() {
		
		// set load positions
		iLoadPosition = iLoadPosition - (iBoxParentWidthTotal*(iMax+1));
		
		// set parent width 
		iBoxMasterWidth = (iBoxParentWidthTotal*(iMax+2));
		$("#companiesbar").css({width: iBoxMasterWidth +"px"});
		
		// start loading the background
		jQuery.fn.fncPreload();

	}; //fncPageSetUp
	
	// preload -----------------------------------------------------------
	jQuery.fn.fncPreload = function() {		
		// load the images in reverse order
		for (x=0;x<=iMax;x++) {
			var img = new Image();
	  		$(img).load(function () {
				// when completed
			}).attr('src', aCompanyImage[x]);
		} // for
		//iIntroLogoLoadBreak
		jQuery.fn.fncIntroLoad();
	}; //fncPreload
	
	//-------------------------------------------------------------------
	// JQUERY - Intro Bar
	
	// loads the intro bar -----------------------------------------------------------
	jQuery.fn.fncIntroLoad = function() {
		
		// make sure the bar is off to the left and not shown
		$("#companies_slider").css({left: iLoadPosition+'px'});
	
		for (i=0;i<=iMax;i++) {
			// add box
			jQuery.fn.fncBoxAdd(i);	
		} // for
				
		// start slide
			jQuery.fn.fncIntroPlay();
	
	} // fncIntroLoad
	
	// play the intro slide -----------------------------------------------------------
	jQuery.fn.fncIntroPlay = function() {
		
		// set right position
		iBoxParentStopPosRight = iBoxParentStopPosRight-iBoxPosition;
		
		$("#companies_slider").animate({left:0}, iIntroParentSlideSpeed, iIntroParentSlideType, function() {
			$("#companies_slider a").animate({opacity:iPlayOpacity},iPlayOpacitySpeed, function() {});
			
			bLoadComplete = true;
			
			// add mouse events
			jQuery.fn.fncAddEvents();
			
			// set position
			iPlayPosition = 0;
			
			// spotlights
			setTimerPlay=setTimeout(jQuery.fn.fncPlay,iPlaySlideRest);
		});
		
	} // fncIntroPlay
	
	//-------------------------------------------------------------------
	// JQUERY - Default Play
	
	// writes the images into the page -----------------------------------------------------------
	jQuery.fn.fncPlay = function() {
		bIsPlaying = true;
		iPlayRest = false;
		
		// should it play?
		if (!bMouseIsOver_Container) {
			
			clearTimeout(setTimerPlay);
			
			// shift everyone
			if (iPlayDirection==0) { // go left
			
				// how fast should it move now?
					// get distance to travel (d), then time (in milliseconds) = d/s
					
				iPlaySlideSpeedNow = (($("#companiesbar_image_"+iMax).position().left + iBoxParentWidthTotal - 950) + $("#companies_slider").position().left) / iPlaySlidePixelsPerSec
				
				if (bTrace) { $("#stage").append("speed left: " + iPlaySlideSpeedNow + " "); } // end trace
				
				$("#companies_slider").animate({left:iBoxParentStopPosRight}, iPlaySlideSpeedNow, sPlaySlideType, function() {
					setTimerPlay=setTimeout(jQuery.fn.fncPlay,iPlaySlideRest);
					iPlayDirection = 1;
				});
			} else { // go right
			
				// how fast should it move back?
				
				iPlaySlideSpeedNow = ($("#companies_slider").position().left*(-1)) / iPlaySlidePixelsPerSec
				
				if (bTrace) { $("#stage").append("speed right: " + iPlaySlideSpeedNow + " "); } // end trace
				
				$("#companies_slider").animate({left:0}, iPlaySlideSpeedNow, sPlaySlideType, function() {
					setTimerPlay=setTimeout(jQuery.fn.fncPlay,iPlaySlideRest);
					iPlayDirection = 0;
				});
			}		
			//if (bTrace) { $("#stage").append("dir: " + iPlayDirection + " "); } // end trace
		} // !bMouseIsOver_Container
		
	} // fncPlay
	
	// add box -----------------------------------------------------------
	jQuery.fn.fncBoxAdd = function(iThisBox) {		
		// build it
		var sHTML = "<a style='left:"+(iBoxPosition)+"px;opacity:"+iPlayOpacity+";' id=\"companiesbar_image_"+iThisBox+"\" href=\""+aCompanyLink[iThisBox]+"\" title=\"view details for "+aCompanyName[iThisBox]+"\"><img src=\""+aCompanyImage[iThisBox]+"\" /></a>";  // alt=\""+aCompanyName[iThisBox]+"\"
		
		// append it
		$("#companies_slider").append(sHTML);	
		
		// set opacity
		$("#companiesbar_image_"+iThisBox).animate({opacity:iBoxLoadOpacity},0);
		
		// set next position
		iBoxPosition = iBoxPosition+iBoxParentWidthTotal;
		
	} // fncBoxAdd	
	
	//-------------------------------------------------------------------
	// JQUERY - Mouse Events and Motion
	
	// add events -----------------------------------------------------------
	jQuery.fn.fncAddEvents = function() {
		
		// on hover of container, turn off auto play
		$('#stage').mouseover(function()	{
			jQuery.fn.fncSlider_Mouseover();
		});
		
		// on mouse out of container, turn auto play back on
		$('#stage').mouseout(function()	{											 
			// call function to test if user is really off
			setTimerSlider_Mouseout=setTimeout(jQuery.fn.fncSlider_Mouseout,iHoverMouseoutDelay);
		});
			
			
	} // fncAddEvents

	// mouse over the bar -----------------------------------------------------------
	jQuery.fn.fncSlider_Mouseover = function() {
		
		// stop playing
		$("#companies_slider").stop();
		clearTimeout(setTimerPlay);
		bTimerPlayIsSet = false;
		
		// turn off mouseout
		clearTimeout(setTimerSlider_Mouseout);
					
		if (!bMouseIsOver_Container) {
			
			bMouseIsOver_Container = true;
			
			// set opacity up
			//$("#companies_slider a").animate({opacity:iHoverOpacity},iHoverOpacitySpeed, function() {});
			$("#companies_slider a").css({opacity:iHoverOpacity});
			
			// make sure no bars still live
			$("#slide_container").remove();
			
			// add drag bars
			$("#stage").append('<div id="slide_container"><div id="slide_handle"></div></div>');
						
			// fade in drag bars
			$("#slide_container").css({opacity:0});
			$("#slide_container").animate({opacity:1},iHoverDragFadeInSpeed, function() {});
			
			// set positions, widths, etc. for the slider bar			
			iSliderBarRightMax = $("#companiesbar_image_"+iMax).position().left + iBoxParentWidthTotal - 950;
			
			// get distance it has moved as a percentage
			iHoverSliderStartPosition = ($("#companies_slider").position().left / (iSliderBarRightMax*(-1))) * 100;
			//if (bTrace) { $("#stage").append("start: " + iHoverSliderStartPosition + " ")} // end trace
					
			// add functionality to bars
			$('#slide_container').slider({ 
				handle: '#slide_handle',
				minValue: 0,
				startValue: iHoverSliderStartPosition,
				maxValue: $('#slide_container').width(),
				slide: function (ev, ui) {
					jQuery.fn.fncSlider_Slide(ui.value);
				}, 
				start: function (ev, ui) {},
				stop: function (ev, ui) {}
			}); // slider			
			
			// set link actions
			$('#companies_slider a').mouseover(function()	{
				jQuery.fn.fncSpotlightActive_Mouseover($(this).attr('id'));
			});
			
			// on mouse out of container, turn auto play back on
			$('#companies_slider a').mouseout(function()	{
				setTimerItem_Mouseout=setTimeout("jQuery.fn.fncSpotlightActive_Mouseout('"+$(this).attr('id')+"')",iHoverMouseoutDelay);
			});
			
		} // mouse over
			
	} // fncSlider_Mouseout

	// mouse off the bar -----------------------------------------------------------
	jQuery.fn.fncSlider_Mouseout = function() {
			bMouseIsOver_Container = false;
			
			bHoverHasSpotlight = false;
			iThisSpotlighted = -1;
			
			// reset opacity
			$("#companies_slider a").css({opacity:iPlayOpacity});
			
			// remove mouseover/out
			$('#companies_slider a').unbind('mouseover');
			$('#companies_slider a').unbind('mouseout');
			
			// fade down scroll bar
			$("#slide_container").animate({opacity:0},iHoverDragFadeInSpeed, function() {});
			$("#slide_container").remove();
			
			jQuery.fn.fncPlay();
			
	} // fncSlider_Mouseout
		
	// spotlight one company -----------------------------------------------------------
	jQuery.fn.fncSpotlightActive_Mouseover = function(iThisSpotlight) {
		
		bMouseIsOver_Item = true;
		
		clearTimeout(setTimerItem_Mouseout);
		
		iThisSpotlight = iThisSpotlight.replace("companiesbar_image_","");
		
		if (!bHoverHasSpotlight) { // not yet hovered
			bHoverHasSpotlight = true;
			
			for (x=0;x<=iMax;x++) {
				if (x!=iThisSpotlight) {
					// fade back others		
					$("#companiesbar_image_"+x).css({opacity:iBoxLoadOpacity});
					//$("#companiesbar_image_"+x).animate({opacity:iBoxLoadOpacity},iHoverOpacitySpeed,function() {});
				} // not this one
			} // for
		}		
		
		//if (bTrace) { $("#stage").append("this: " + iThisSpotlight + " "); } // end trace
		
		// fade in this
		$("#companiesbar_image_"+iThisSpotlight).css({opacity:iHoverOpacity});
		
		if (iThisSpotlighted<0) {
			iThisSpotlighted = iThisSpotlight;
		} else if (iThisSpotlighted!=iThisSpotlight && iThisSpotlighted>=0) {
			$("#companiesbar_image_"+iThisSpotlighted).css({opacity:iBoxLoadOpacity});
			iThisSpotlighted = iThisSpotlight;
		}
	
		
	} // fncSpotlightActive_Mouseover
	
	// reset spotlight -----------------------------------------------------------
	jQuery.fn.fncSpotlightActive_Mouseout = function(iThisSpotlight) {		
		iThisSpotlight = iThisSpotlight.replace("companiesbar_image_","");
		
		// fade back to normal
		$("#companiesbar_image_"+iThisSpotlight).css({opacity:iBoxLoadOpacity});
		
		bMouseIsOver_Item=false;
		iThisSpotlighted = -1;
		
		//if (bTrace) { $("#stage").append("off: " + iThisSpotlight + " "); } // end trace
	} // fncSpotlightActive_Mouseover
	
	// actions to take during slide -----------------------------------------------------------
	jQuery.fn.fncSlider_Slide = function(iHoverPositionThis) {
		iHoverPercent = iHoverPositionThis/100;
		iHoverDistance = (iSliderBarRightMax) * (iHoverPercent);
		$('#companies_slider').css({left:'-' + (iHoverDistance) + 'px'});
	}; // fncSlider_Slide
	
}); // end jQuery load
