var hList   = null;
var hScroll = null;
var hSel    = null;
var hItems  = null;
var hPager  = null;
var hPrev   = null;
var hPos    = null;
var hNext   = null;
var hFicha  = null;
var nCur    = 0;
var nMax    = -1;

$(document).ready(function () {

	hList   = $("#hList");
	hScroll = $("#hList #hScroll");
	hSel    = $("#hList #sel");
	hItems  = $("#hList .item");
	hPager  = $("#hPager");
	hPrev   = hPager.find("#prev");
	hPos    = hPager.find("#pos");
	hNext   = hPager.find("#next");
	hFicha  = $("#hFicha");
	
	hItems.each(function (iter) {
		var jthis = $(this);
		jthis.attr("id", jthis.attr("class").substr(7));
		jthis.css({
			position: "absolute",
			left: 250*iter
		}).attr("pos", iter).click(function () {
			nCur = parseInt($(this).attr("pos"));
			f_refresh();
			return false;
		});
		nMax++;
	});
	hPrev.css("cursor", "pointer").click( function() {
		nCur--; f_refresh(); return false;
	});
	hNext.css("cursor", "pointer").click( function() {
		nCur++; f_refresh(); return false;
	});

	f_refresh();
});

function f_refresh() {
	var nDes = 0;
	if(nCur==0)
		hSel.animate({left:  10}, "slow"); 
	else if(nCur==nMax) 
		hSel.animate({left: 510}, "slow"); 
	else 
		hSel.animate({left: 260}, "slow");
	if(nCur==0)
		nDes = 0;
	else if(nCur==nMax) 
		nDes = +2;
	else
		nDes = +1;
	hPrev.css("display", (nCur==0    ? "none" : "inline"));
	hNext.css("display", (nCur==nMax ? "none" : "inline"));
	hPos.html((nCur+1)+"/"+(nMax+1));
	hScroll.animate( { left: 0-(245*(nCur-nDes)) } );
	var id = hList.find(".item:eq("+nCur+")").attr("id");
	hFicha.load(URLBase+"ajax-ficha-vivienda.aspx?idVivienda="+id, function () {
		msListeners();
		lbListeners();
	});
}