$(window).load(function() {
	/* ---------------------------------------------------------------------- */
	/* Scrolling gallery of companies
	/* ---------------------------------------------------------------------- */
	
	// toggle debugging
	//$('.companies_container').data('log', 'true');
	
	// prepend left and right translucent pngs
	$('<div class="left_cover"></div>').prependTo('.footer .companies_container');
	$('<div class="right_cover"></div>').appendTo('.footer .companies_container');
	
	// fix pngs in IE6
	if ($.browser.msie && $.browser.version == '6.0') {
		DD_belatedPNG.fix('.footer .companies_container .left_cover, .footer .companies_container .right_cover, .footer .companies_container img');
	}
	
	// set up css for gallery
	$('.footer .companies_container').css({
		display: 'block',
		height: 72
	})
	$('.footer .companies_container .companies').css({
		position: 'absolute',
		width: 10000,
		left: 0,
		zIndex: 42
	})
	
	// determine what width of all the companies together is and find starting point
	$('.companies').data('totalSingleWidth', 0);
	$('.companies li').each(function() {
		$('.companies').data('totalSingleWidth', parseInt(parseInt($('.companies').data('totalSingleWidth')) + $(this).outerWidth()));
	})
	$('.companies').data('startingPoint', - parseInt($('.companies').data('totalSingleWidth') + $('.footer .companies_container').outerWidth() + 160));
	log('totalSingleWidth ' + $('.companies').data('totalSingleWidth'))
	log('startingPoint ' + $('.companies').data('startingPoint'))
	
	// add a second set of the companies and append
	$('.companies li').clone().appendTo('.companies');

	// and then set starting point for gallery
	$('.companies').css('left', $('.companies').data('startingPoint'));
	log('starting left ' + $('.companies').css('left'))
	
	// add the current market sector and arrow below the gallery and hide
	var marketSector = $('<div class="showing_market_sector"><div class="content"></div><span class="right_bg">&nbsp;</span></div>');
	var arrow = $('<div class="arrow">&nbsp;</div>');
	$('.companies_container').prepend(arrow).prepend(marketSector);
	
	marketSector.hide();
	arrow.hide();
	
	// When your mouse enters the gallery, reset
	$('.companies_container').mouseenter(clear);
	
	// When the mouse leaves the gallery or the page loads, default to moving right
	moveRight();
	$('.companies_container').mouseleave(clear);
	$('.companies_container').mouseleave(moveRight);
	
	// When the mouse moves within the container, determine direction
	// to move gallery in
	$('.companies_container').mousemove(function(e) {
		// current x mouse position
		var x = e.pageX - $(this).offset().left;
		
		// if in the left third of the gallery, move left
		if (x <= 143 && $(this).data('current_direction') != 'left') {
			$(this).data('current_direction', 'left');
			
			moveLeft();
		}
		
		// if in the center third of the gallery, do nothing
		if (x > 143 && x < 286) {
			$(this).data('current_direction', 'center');
			
			clear();
		}
		
		// if in the right third of the gallery, move right
		if (x >= 286 && $(this).data('current_direction') != 'right') {
			$(this).data('current_direction', 'right');
			
			moveRight();
		}
		
		// periodically change the market sector tooltip position
		updateMarketSectorArrowPosition(e);
	})
	
	// show or hide the tooltip when hovering over a company
	$('.companies_container .companies a')
		// on mouse enter show the tooltip
		.mouseenter(function(e) {
			// append new data for tooltip
			$(this).parents('.companies_container').find('.content').html($(this).siblings('.market_sector').html());

			// if there is content for the tooltip, show it
			if ($(this).parents('.companies_container').find('.content').html().replace(/^\s+|\s+$/g,"") != '') {
				$(this).parents('.companies_container').find('.showing_market_sector, .arrow').show();
			}
		
			updateMarketSectorArrowPosition(e);
		})
		
		// On mouse move, update the tooltip's position
		.mousemove(function(e) {
			// update the tooltip position
			updateMarketSectorArrowPosition(e);
		})

		// On mouse leave, hide the tooltip
		.mouseleave(function() {
			// hide the tooltip
			$(this).parents('.companies_container').find('.showing_market_sector, .arrow').hide();
		});
	
	
	/**
	 * Stop all animations on the gallery
	 *
	 * @return mixed
	 **/
	function clear() {
		$('.companies').stop();
	}
	
	
	/**
	 * Move the gallery left
	 *
	 * @return mixed
	 **/
	function moveLeft() {
		// if already to the furthest left, stop
		if (parseInt($('.companies').css('left')) >= 0) {
			$('.companies').css('left', $('.companies').data('startingPoint'));
		}
		
		// continue looping indefinitely
		$('.companies').animate({"left": '+=1px'}, 20, moveLeft);
	}
	
	
	/**
	 * Move the gallery right
	 *
	 * @return mixed
	 **/
	function moveRight() {
		// if already to the furthest right, move over one set so it appears
		// that you're starting again from the beginning
		if (Math.abs(parseInt($('.companies').css('left'))) >= parseInt(($('.companies').data('totalSingleWidth') * 2))) {
			$('.companies').css('left', $('.companies').data('startingPoint'));
		}
		
		// continue looping indefinitely
		$('.companies').animate({"left": '-=1px'}, 20, moveRight);
	}
	
	
	/**
	 * Update the position of the market sector tooltip
	 *
	 * @param  object  e  Event object from hover instantiation
	 * @return null
	 **/
	function updateMarketSectorArrowPosition(e) {
		var container = $('.companies_container');
		
		// x mouse position offset by left cover
		var left_cover_width = 30;
		var x = (e.pageX - container.offset().left) - left_cover_width;
		
		// if the mouse position is within the gallery
		if (x < 370) {
			// determine how far left the tooltip should be
			var total_container_width             = container.width() - (2 * left_cover_width);
			var market_sector_width               = container.find('.showing_market_sector .content').width() + 44 /* width of two sides bg */;
			var area_for_market_sector            = total_container_width - market_sector_width + left_cover_width;
			var area_for_arrow                    = total_container_width - container.find('.arrow').outerWidth() + left_cover_width - 44 /* width of two sides bg */;
			var market_sector_left_from_container = ((x * area_for_market_sector) / total_container_width) + left_cover_width;
			var arrow_left_from_container         = ((x * area_for_arrow) / total_container_width) + left_cover_width + 22 /* width of one side bg */;
			
			// and position accordingly
			container.find('.showing_market_sector').css('left', market_sector_left_from_container);
			container.find('.arrow').css('left', arrow_left_from_container);
		}
	}
})


/**
 * Helper fn for console logging
 * Set $.fn.progressBar.debug to true to enable debug logging
 *
 * @return void
 **/
function log() {
	if ($('.companies_container').data('log') == 'true' && window.console && window.console.log) {
		window.console.log(arguments[0]);
	}
};

