var $jq = jQuery.noConflict();

$jq(function() {	
	$jq(".collapse").each(function(){
		var toCollapse = this;
		$jq("<a href='javascript:;' class='expander'>Refine your search by sales type, property type or price & home features</a>")
			.bind("click", {show:$jq(".collapsible",toCollapse)}, function(event) {
				$jq(this).hide();
				event.data.show.show();
			})
			.css({display:"block",padding:"10px",backgroundColor:"#eaf1f7",margin:"1em -10px",clear:"both"})
			.insertAfter($jq(".collapsible", this));
	});

	$jq(".carousel .dl").each(function() {
		var dl = $jq(this);
//		dl.css({left:"0px"});
		var slides = $jq(".slide", this);
		var total = slides.length;
		if (total < 4) return;
		var ctrls = $jq("<div class='prevnext'></div>")
		$jq("<a href='#0' class='previous_btn' disabled='disabled'>Previous</a>")
			.bind("click", function(event) {
				var currPos = this.hash.substr(1);
				if(currPos > 0) {
					var counter = $jq(".counter", this.parentNode);
					this.href = "#" + --currPos;
					$jq(".next_btn", this.parentNode).attr("href", "#" + currPos).removeAttr("disabled");
					$jq(slides[currPos]).show();
					$jq(slides[currPos+3]).hide();
					counter.html((currPos+1) + " - " + (currPos+3) + " of " + total);
				}
				if (currPos <= 0) {
					$jq(this).attr("disabled", "disabled");
				}
				return false;
			})
			.css({float: "left"})
			.appendTo(ctrls);
		$jq("<a href='#0' class='next_btn'>Next</a>")
			.bind("click", function(event) {
				var currPos = this.hash.substr(1);
				if(currPos < total-3) {
					var counter = $jq(".counter", this.parentNode);
					this.href = "#" + ++currPos;
					$jq(".previous_btn", this.parentNode).attr("href", "#" + currPos).removeAttr("disabled");
					$jq(slides[currPos-1]).hide();
					$jq(slides[currPos+2]).show();
					counter.html((currPos+1) + " - " + (currPos+3) + " of " + total);
				}
				if (currPos >= total-3) {
					$jq(this).attr("disabled", "disabled");
				}

				return false;
			})
			.css({float: "right"})
			.appendTo(ctrls);
		$jq("<span class='counter'>1 - " + (total < 3 ? total : 3) + " of " + total + "</span>").appendTo(ctrls);
		ctrls.appendTo(this.parentNode);
		$jq("<div class='clear'></div>").appendTo(this);
		dl.css({width: (209 * total) + "px"});
	});
	
	$jq(".gallery_directions #detail_photo_gallery .thumbnails").each(function() {
			var dl = $jq(this);
			var thumbnails = $jq(".thumbnail_div", this);
			var total = thumbnails.length;
			if (total <= 4) return;
			var ctrls = $jq("<div class='prevnext'></div>")
			$jq("<a href='#0' class='previous_btn' disabled='disabled'></a>")
				.bind("click", function(event) {
						var currPos =  this.hash.substr(1);
						if(currPos > 0) {
							var counter = $jq(".counter", this.parentNode);
							this.href = "#" + --currPos;
							$jq(".next_btn", this.parentNode).attr("href", "#" + currPos).removeAttr("disabled");
							$jq(thumbnails[currPos]).show();
							$jq(thumbnails[currPos+4]).hide();
							counter.html((currPos+1) + " - " + (currPos+4) + " of " + total);
						}
						if (currPos <= 0) {
							$jq(this).attr("disabled", "disabled");
						}
						return false;
					})
					.css({float: "left"})
					.appendTo(ctrls);
			$jq("<a href='#' class='next_btn'></a>")
				.bind("click", function(event) {
						var currPos = this.hash.substr(1);
						if(currPos < total-4) {
							var counter = $jq(".counter", this.parentNode);
							this.href = "#" + ++currPos;
							$jq(".previous_btn", this.parentNode).attr("href", "#" + currPos).removeAttr("disabled");
							$jq(thumbnails[currPos-1]).hide();
							$jq(thumbnails[currPos+3]).show();
							counter.html((currPos+1) + " - " + (currPos+4) + " of " + total);
						}
						if (currPos >= total-4) {
							$jq(this).attr("disabled", "disabled");
						}
		
						return false;
					})
					.css({float: "right"})
					.appendTo(ctrls);
			$jq("<span class='counter'>1 - " + (total < 4 ? total : 4) +" of " + total + "</span>").appendTo(ctrls);
			ctrls.appendTo(this.parentNode);
			$jq("<div class='clear'></div>").appendTo(this);
			dl.css({width: 306 + "px"});
	});
});
