$(document).ready(function() {

	var speed = 2000;
	var pause = 8000;
	var which = 1;
	var next = which;
/*
	if (which == 1 ){
		pause = 50000;
	}
	else{
		pause = 1000;
	}
*/
   	intervalFadeOut = setInterval(animate, pause);

	function animate() {

		next = which + 1;
		if (next > 5) { next = 1; }

		// so what we're going to do is fade out the "current" image
		// which will reveal the image beneath it


		//if (which ==2) { var timer = setTimeout(waitme(), 5000); clearTimeout(timer );}

		if (which < 5) {
			$('.brandbox #image-buffer-' + which.toString()).animate({opacity: 0}, speed);

			// now swap the image on the left side
			// first, turn this one off
			brandboxLeftOff(which);
			// now turn on the next one
			brandboxLeftOn(next);

		} else {
			// last one, back to starting positions
			$('.brandbox .image-buffer').animate({opacity: 1}, speed);

			// swap the image on the left side
			brandboxLeftOff(which);
			brandboxLeftOn(1);
		}

		which = next;

	} // end function animate


   	function brandboxLeftOn(which) {
		var imgsrc = $('.brandbox .brandbox-list #brandbox-list-' + which.toString()).children('a').children('img').attr('src');
		$('.brandbox .brandbox-list #brandbox-list-' + which.toString()).children('a').children('img').attr('src', imgsrc.replace(/_off/, '_on'));
   	}
   	function brandboxLeftOff(which) {
		var imgsrc = $('.brandbox .brandbox-list #brandbox-list-' + which.toString()).children('a').children('img').attr('src');
		$('.brandbox .brandbox-list #brandbox-list-' + which.toString()).children('a').children('img').attr('src', imgsrc.replace(/_on/, '_off'));
   	}
   	function brandboxLeftOffAll() {
   		$('.brandbox .brandbox-list li').each(function() {
			var imgsrc = $(this).children('a').children('img').attr('src');
			$(this).children('a').children('img').attr('src', imgsrc.replace(/_on/, '_off'));
   		});
   	}


   	/*
   		now, if you hover over something on the left side
   		the animation stops
   		the arrow goes to whatever you've moused over
   		and the appropriate slide is shown
   	*/
   	$('.brandbox .brandbox-list ul li').each(function() {
   		$(this).hover(
	   		// over
	   		function() {
	   			// stop the animation
	   			intervalFadeOut = clearInterval(intervalFadeOut);

	   			// turn off all the hover states
	   			brandboxLeftOff(which);

	   			// find out which li we're over
	   			var this_li = $(this).attr('id');
				this_li = this_li.split('-');
				var new_which = this_li[this_li.length - 1];

				// highlight that left side
				brandboxLeftOn(new_which);

				// show that slide
				if (which < new_which) {
					for (var j = which; j < new_which; j++) {
						$('.brandbox #image-buffer-' + j.toString()).animate({opacity: 0}, 0);
					}
				} else if (which > new_which) {
					for (var j = which; j >= new_which; j--) {
						$('.brandbox #image-buffer-' + j.toString()).animate({opacity: 1}, 0);
					}
				}


				which = new_which;


	   		},
	   		// out
	   		function() {
	   			// resume animation
	   			intervalFadeOut = setInterval(animate, pause);
	   		}
		); // end hover
	});


});


function waitme(){
  return;
}

