$j(function() {
	$j(".carousel .dl").each(function() {
		var dl = $j(this);
		var slides = $j(".slide", this);
		slides.each(function() {
			$j('.head', this).insertAfter($j('.photo', this));
			var lnkVal = $j('.stext .more a:first', this).attr('href');
			if(lnkVal) {
				$j('.photo img', this).wrap('<a href="'+ lnkVal +'"></a>');
				$j('.stext .more', this).remove();
			}
		});
		var total = slides.length;
		if (total < 4) return;
		var ctrls = $j("<div class='prevnext'></div>")
		$j("<a href='#0' class='previous_btn' disabled='disabled'><span>Previous</span></a>")
			.bind("click", function(event) {
				var currPos = this.hash.substr(1);
				if(currPos > 0) {
					var counter = $j(".counter", this.parentNode);
					this.href = "#" + --currPos;
					$j(".next_btn", this.parentNode).attr("href", "#" + currPos).removeAttr("disabled");
					$j(slides[currPos]).show();
					$j(slides[currPos+3]).hide();
					counter.html((currPos+1) + " - " + (currPos+3) + " of " + total);
				}
				if (currPos <= 0) {
					$j(this).attr("disabled", "disabled");
				}
				return false;
			})
			.css({float: "left"})
			.appendTo(ctrls);
		$j("<a href='#0' class='next_btn'><span>Next</span></a>")
			.bind("click", function(event) {
				var currPos = this.hash.substr(1);
				if(currPos < total-3) {
					var counter = $j(".counter", this.parentNode);
					this.href = "#" + ++currPos;
					$j(".previous_btn", this.parentNode).attr("href", "#" + currPos).removeAttr("disabled");
					$j(slides[currPos-1]).hide();
					$j(slides[currPos+2]).show();
					counter.html((currPos+1) + " - " + (currPos+3) + " of " + total);
				}
				if (currPos >= total-3) {
					$j(this).attr("disabled", "disabled");
				}
	
				return false;
			})
			.css({float: "right"})
			.appendTo(ctrls);
		
		$j("<span class='counter'>1 - " + (total < 3 ? total : 3) + " of " + total + "</span>").appendTo(ctrls);
		ctrls.prependTo($j('.carousel .dl'))
		$j("<div class='clear'></div>").appendTo(this);
		dl.css({width: (209 * total) + "px"});
	});
});