swfobject.embedSWF(skinUrl + 'flash/home.swf', 'banner', '950', '195', '9.0.0', skinUrl + 'flash/expressInstall.swf');

// onload
$(function() {
	$('#caseStudies').carousel();	
});

(function ($) {
	$.fn.carousel = function (options) {
		var defaults = {
			speed: 300
		};

		// Extend our default options with those provided.
		var opts = $.extend(defaults, options);

		var $carousel = $(this);
		var $wrapper = $(this).find('.caseStudiesWrapper');		

		$carousel.data('busy', false);

		// set container width, height and overflow. ascertain height from contained images
		var img = $carousel.find('.caseStudy:first img');

		var width = img.css('width');

		var unitlessWidth = parseInt(width.substr(0, width.length - 2));

		// assign incremental number to each item
		var count = 1;
		$('.caseStudy').each( function() {
			$(this).data('count', count);
			count++;
		});
		
		// store ref to number of items
		$carousel.data('max', count-1);

		// set width of wrapper to number of items
		$wrapper.css('width', (unitlessWidth * (count-1)) +'px');
		
		// store ref to currently shown item
		var $current = $wrapper.find('div.on');
				
		$carousel.data('current', $current.data('count'));

		// make nav links visible
		$('#caseStudies ul').show();
		
		// attach events to next/prev links
		$('.left a').click( function() {
			current = $carousel.data('current');
			max = $carousel.data('max');
			
			if(current == 1) {
				// at beginning
				return false;
			}
			
			var prev = parseInt(current) - 1;
			
			if(prev >= 1) {
				// scroll to selected image
				var offset = (current-1) * unitlessWidth;
				offset = offset - unitlessWidth;
				$wrapper.animate({
						'left': (0 - offset)
					},
					opts.speed,
					function() {
						// update current ref
						$carousel.data('current', prev);
						$carousel.data('busy', false);
					}
				);				
			}
			
			return false;
		});
		
		$('.right a').click( function() {
			current = $carousel.data('current');
			max = $carousel.data('max');
			if(current == max) {
				// at end
				return false;
			}

			var next = parseInt(current) + 1;
			
			if(next <= max) {
				// scroll to selected image
				var offset = (next -1) * unitlessWidth;

				$wrapper.animate({
						'left': (0 - offset)
					},
					opts.speed,
					function() {
						// update current ref
						$carousel.data('current', next);
						$carousel.data('busy', false);
					}
				);
			}
			
			return false;
		});		
		
  };
})(jQuery);



