$(document).ready(function(){
	$('div.carousel').each(function () {
		$.carouselDiv = $(this);
		$.flipNumber = 0;
		$.totalFlips = $("div.panels",$.carouselDiv).children().length;
		$.panelsHeight = 100;
		for(i=0; i<$.totalFlips; i++){
			if($("div.panels",$.carouselDiv).children().eq(i).height() > $.panelsHeight){
				$.panelsHeight = $("div.panels",$.carouselDiv).children().eq(i).height();
			}
		}
		$("div.panels",$.carouselDiv).css("height",$.panelsHeight + "px");
		$("div.panels",$.carouselDiv).children().eq(0).fadeIn();
		$("div#counter",$.carouselDiv).html(($.flipNumber+1) + "/" + $.totalFlips);
		Cufon.refresh();
		$("a.arrow-left",$.carouselDiv).click(function flipBack(){
			$(this).unbind("click");
			$.flipScroller = $(this).parent().parent().siblings("div.panels");
			$.flipScroller.children("div.panel").eq($.flipNumber).fadeOut('fast', function() {
				if($.flipNumber > 0){
					$.flipNumber--;
				} else {
					$.flipNumber = $.totalFlips - 1;
				}
				$("div#counter",$.carouselDiv).html(($.flipNumber+1) + " / " + $.totalFlips);
				Cufon.refresh();
				$.flipScroller.children("div.panel").eq($.flipNumber).fadeIn('fast', function() {
					$("a.arrow-left",$.carouselDiv).bind("click", flipBack);
				});
			});
		});
		$("a.arrow-right",$.carouselDiv).click(function flipForward(){
			$(this).unbind("click");
			$.flipScroller = $(this).parent().parent().siblings("div.panels");
			$.flipScroller.children("div.panel").eq($.flipNumber).fadeOut('fast', function() {
				if($.flipNumber < ($.totalFlips - 1)){
					$.flipNumber++;
				} else {
					$.flipNumber = 0;
				}
				$("div#counter",$.carouselDiv).html(($.flipNumber+1) + " / " + $.totalFlips);
				Cufon.refresh();
				$.flipScroller.children("div.panel").eq($.flipNumber).fadeIn('fast', function() {
					$("a.arrow-right",$.carouselDiv).bind("click", flipForward);
				});
			});
		});
		if($.carouselDiv.attr("id") == "auto"){
			intval = setInterval( "$('a.arrow-right',$.carouselDiv).click();", 5000 );
			$.carouselDiv.hover(function(){clearInterval(intval);},function(){intval = setInterval( "$('a.arrow-right',$.carouselDiv).click();", 5000 );});
		}
	});
});
