/*
 * 	Image Rotation v 2.1 - jQuery plugin (Special Build for batukota.go.id my lovely city)
 *	written by David Wijaya - Trilogicsoft Indonesia
 *
 *	Copyright (c) 2011 David Wijaya (http://www.trilogicsoft.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 * 
 */
 
(function ($) {
	$.fn.carousel = (function (options) {
		var defaults = {
			delay : 10000,
			interval : 5000,
			speed : 500,
			width : 960,
			height : 180,
			nextButton : null,
			prevButton : null,
			autoSlide : true,
			wrap: 'circular'
		};
		var options = $.extend(defaults, options);
		var $self = this,
			$child = new Array(),
			$allwidth = 0,
			$playedID = 0,
			$leftPos = 0,
			startcarousel = function(){
				if(options.wrap == 'circular') {
					var nowLeft = $leftPos - options.width;
					do_circular(nowLeft);
				} else {
					var nowLeft = $leftPos - options.width;
					if(nowLeft <= ($allwidth * -1)) nowLeft = 0;
					$self.animate({
						left: nowLeft,
					}, options.speed, function() {
						$leftPos = $self.position().left;
					});
				}
			},
			
			do_circular = function(leftX){
				$self.animate({
					left: leftX,
				}, options.speed, function() {
					$leftPos = 0;
					var tempchild = null;
					$self.find('li').each(function(i){
						if(i == 0) tempchild = $(this);
					});
					if(tempchild != null) {
						//tempchild.remove();
						$self.append(tempchild);
					}
					$self.css({'left':'0'});
					setTimeout(function(){startcarousel();}, options.interval);
				});
			}
		$self.parent().css({'width':options.width+'px', 'height':options.height+'px', 'overflow':'hidden', 'margin':'0', 'padding':'0'});
		$self.css({'position':'relative', 'float':'left', 'height':options.height+'px', 'margin':'0', 'padding':'0', 'list-style-type':'none'});
		$self.find('li').each(function(){
			$(this).css({'float':'left', 'width':options.width+'px', 'margin':'0', 'padding':'0', 'height':options.height+'px'});
			$allwidth = $allwidth + $(this).width();
		});
		
		$self.width($allwidth);
		
		if(options.autoSlide == true) {
			setTimeout(function(){startcarousel();}, options.interval);
		}
	});
})(jQuery);

$(document).ready(function(){$('#mycarousel').carousel({wrap:'circular'});});
