/*
 * jQuery infinitecarousel plugin
 * @author admin@catchmyfame.com - http://www.catchmyfame.com
 * @version 1.2.2
 * @date August 31, 2009
 * @category jQuery plugin
 * @copyright (c) 2009 admin@catchmyfame.com (www.catchmyfame.com)
 * @license CC Attribution-Share Alike 3.0 - http://creativecommons.org/licenses/by-sa/3.0/
 */
(function($) {
 $.fn.extend({
	 infiniteCarousel: function(options) {
	 var defaults =
	 {
		 transitionSpeed: 500,
		 displayTime: 4000,
		// textholderHeight: 80,
		 thumbnailWidth: '100px',
		 thumbnailHeight: '100px'
	 };
	 var options = $.extend(defaults, options);

	 return this.each(function() {
	 var o = options;
	 var obj = $(this);
	 var curr = 1;
	
	 var numImages = $('img', obj).length; // Number of images
	 var imgHeight = 300;
	 var imgWidth = 300;
	 var autopilot = 0;
	
	 $(obj).width(imgWidth).height(imgHeight);
	 $(obj).css({ 'overflow': 'hidden', 'position': 'relative' });

	 if (numImages > 1) {
		 $('li:last', obj).prependTo($('ul', obj));
		 $('ul', obj).css('left', -imgWidth + 'px');
		 $('ul', obj).width(9999);
	
		 html = '<div id="btn_rt"><a href="javascript:void(0);"><img src="/templates/images/overlay-arrow-right.png" alt="" /></a></div>';
		 html += '<div id="btn_lt"><a href="javascript:void(0);"><img src="/templates/images/overlay-arrow-left.png" alt="" /></a></div>';
		 $(obj).append(html);
	
		 $(obj).after('<div id="thumbWrap"><div id="thumbs"></div></div>');
		 for (i = 0; i <= numImages - 1; i++) {
			 thumb = $('img:eq(' + (i + 1) + ')', obj).attr('src');
			 $('#thumbs').append('<div class="thumb" id="thumb_' + (i + 1) + '" style="background:url(' + thumb + ') center center no-repeat;"></div>');
			 if (i == 0) $('#thumb_1').css({ 'border': '1px solid #d9cfc2', 'border-top': '1px solid #fff' });
		 }
		 thumb = $('img:first', obj).attr('src');
		 $('#thumb_' + numImages).css({ 'background-image': 'url(' + thumb + ')' });
		 $('#thumbs div.thumb:not(:first)').css({ 'opacity': '.5' }); // makes all thumbs opaque except the first one
		 $('#thumbs div.thumb').hover(function() { $(this).animate({ 'opacity': 1 }, 150) }, function() { if (curr != this.id.split('_')[1]) $(this).animate({ 'opacity': .5 }, 250) });
		 $('#thumbs div').bind('click', thumbclick); 
		 
		 $('#btn_rt').click(function() {
			 autopilot = 0;
			 anim('next');
		 });
		 $('#btn_lt').click(function() {
			 autopilot = 0;
			 anim('prev');
		 });
	 }
	 else
	 {
		 autopilot = 0;
	 }

	 function thumbclick(event) {
		 target_num = this.id.split('_'); // we want target_num[1]
		 if (curr != target_num[1]) {
			 $('#thumb_' + curr).css({ 'border': '1px solid #fff', 'border-top': '1px solid #d9cfc2' });
			 clearTimeout(clearInt);
			 $('#thumbs div').css({ 'cursor': 'default' }).unbind('click'); // Unbind the thumbnail click event until the transition has ended
			 autopilot = 0;
	 	 }
		 if (target_num[1] > curr) {
			 diff = target_num[1] - curr;
			 anim('next', diff);
		 }
		 if (target_num[1] < curr) {
			 diff = curr - target_num[1];
			 anim('prev', diff);
		 }
	 }
	 function borderpatrol(elem) {
		 $('#thumbs div').css({ 'border': '1px solid #fff', 'border-top': '1px solid #d9cfc2' }).animate({ opacity: 0.65 }, 250);
		 setTimeout(function() { elem.css({ 'border': '1px solid #d9cfc2', 'border-top': '1px solid #fff' }).animate({ 'opacity': 1 }, 500); }, o.transitionSpeed);
	 }
	 function anim(direction, dist) {
		 $('#btn_rt').hide();
		 $('#btn_lt').hide();
	
		 if (direction == "next") {
			 if (curr == numImages) curr = 0;
			 if (dist > 1) {
				 borderpatrol($('#thumb_' + (curr + dist)));
				 $('li:lt(2)', obj).clone().insertAfter($('li:last', obj));
				 $('ul', obj).animate({ left: -imgWidth * (dist + 1) }, o.transitionSpeed, function() {
				 $('li:lt(2)', obj).remove();
				 for (j = 1; j <= dist - 2; j++) {
				 $('li:first', obj).clone().insertAfter($('li:last', obj));
				 $('li:first', obj).remove();
			 }
			 $('#btn_rt').show();
			 $('#btn_lt').show();
			 $(this).css({ 'left': -imgWidth });
			 curr = curr + dist;
			 $('#thumbs div').bind('click', thumbclick).css({ 'cursor': 'pointer' });
			 });
		 }
		 else
		 {
			 borderpatrol($('#thumb_' + (curr + 1)));
			 $('#thumbs div').css({ 'cursor': 'default' }).unbind('click'); // Unbind the thumbnail click event until the transition has ended
			 $('li:first', obj).clone().insertAfter($('li:last', obj));
			 $('ul', obj)
				 .animate({ left: -imgWidth * 2 }, o.transitionSpeed, function() {
					 $('li:first', obj).remove();
					 $('ul', obj).css('left', -imgWidth + 'px');
 					 $('#btn_rt').show();
			 		 $('#btn_lt').show();
					 curr = curr + 1;
					 $('#thumbs div').bind('click', thumbclick).css({ 'cursor': 'pointer' });
					 });
			 }
		 }
		 if (direction == "prev") {
			 if (dist > 1) {
			 borderpatrol($('#thumb_' + (curr - dist)));
			 $('li:gt(' + (numImages - (dist + 1)) + ')', obj).clone().insertBefore($('li:first', obj));
			 $('ul', obj).css({ 'left': (-imgWidth * (dist + 1)) }).animate({ left: -imgWidth }, o.transitionSpeed, function() {
				 $('li:gt(' + (numImages - 1) + ')', obj).remove();
 				 $('#btn_rt').show();
				 $('#btn_lt').show();
				 curr = curr - dist;
				 $('#thumbs div').bind('click', thumbclick).css({ 'cursor': 'pointer' });
			 });
		 }
	 	 else {
			 borderpatrol($('#thumb_' + (curr - 1)));
			 $('#thumbs div').css({ 'cursor': 'default' }).unbind('click'); // Unbind the thumbnail click event until the transition has ended
			 $('li:last', obj).clone().insertBefore($('li:first', obj));
			 $('ul', obj)
			 .css('left', -imgWidth * 2 + 'px')
			 .animate({ left: -imgWidth }, o.transitionSpeed, function() {
				 $('li:last', obj).remove();
				 $('#btn_rt').show();
				 $('#btn_lt').show();
				 curr = curr - 1;
				 if (curr == 0) curr = numImages;
				 $('#thumbs div').bind('click', thumbclick).css({ 'cursor': 'pointer' });
			 });
			 }
		 }
	 }
	 if (false) {
	 	var clearInt = setInterval(function() { anim('next'); }, o.displayTime + o.transitionSpeed);
	 }
	 });
	 }
	 });
})(jQuery); 
