var listings = [];
var listingFltrd = [];
//listingFltrd[0] = new listing();

var activeCount;
var itemsPerPage = 20;
var activeSort = "sortByAmountAcs";
var orgSort = "sortByAmountAcs";
var listingsCont = document.getElementById("contListings")
var pagingCont = document.getElementById("contPaging")
var resultsCountCont = document.getElementById("resultsCountCont")
var pagingCont2 = document.getElementById("contPaging2")
var resultsCountCont2 = document.getElementById("resultsCountCont2")

function listing (listingId, imageID, amount, amountDisp, title, subTitle, details, rooms, bathrooms, locationId, location, type, rented) {
	this.listingId = listingId;
	if (imageID){
	    this.imageID = imageID;
	    }else{
	    this.imageID = "0";
	}
	this.amount = amount;
	this.amountDisp = amountDisp;
	this.title = title + " in " + location;
	this.subTitle = subTitle;
	this.details = details;
	this.rooms = rooms;
	this.bathrooms = bathrooms
	this.locationId = locationId;
	this.location = location;
	this.type = type;
	this.rented = rented;
}
function filterRange (from, to) {
	this.from = from;
	this.to = to;
}

function loadListings(vTypes,vLocations,a1,a2,r1,r2) {
	propTypes = vTypes + ",";
	locations = vLocations + ",";
	amount = new filterRange(a1, a2);
	rooms = new filterRange(r1, r2);
	applyAllFilters();
	loadHTML(1, true);
}

function loadHTML(curPage, paging) {
	listingsCont.style.height = (activeCount < itemsPerPage ? (activeCount * 126) : (itemsPerPage * 126)) +"px";
	var startIndex = ((curPage-1)*itemsPerPage);
	var endIndex = ((startIndex+itemsPerPage) > activeCount ? activeCount : startIndex+itemsPerPage);
	var iHtml = ""
	if (startIndex == endIndex) {
		document.getElementById("dvNoData").style.display = "";		
	} 
	else {
		for (var i=startIndex; i<endIndex; i++) {
			var ls = listingFltrd[i];
			var imageExt = ((/MSIE 6/i.test(navigator.userAgent)) ? 'gif' : 'png');
			var rentedOverlay = ( ls.rented == 'True' ? '<img class="rentedOverlay" src="images/rented.'+imageExt+'" />' : '');
			iHtml += '\
			<div class="itemCont" onclick="goToList(\'\','+ ls.listingId +');">\
				<div class="itemInfo">\
					<div class="itemHeader">\
						<div class="itemTitle">' + ls.title +'</div>\
						<div class="itemPrice">' + ls.amountDisp +  '</div>\
						<div class="clear"></div>\
					</div>\
					<div class="itemSubTitle">' + ls.subTitle + '</div>\
					<div class="itemDetails">' + ls.details + '</div>\
					<div class="itemButtons">\
						<a href="javascript:goToList(\'text\','+ ls.listingId +');"><img class="actionButton" src="images/textBtn.jpg" alt="Text Listing" /></a>\
						<a href="javascript:goToList(\'email\','+ ls.listingId +');"><img class="actionButton" src="images/emailBtn.jpg" alt="Email Listing" /></a>\
					</div>\
				</div>\
				<div class="itemPic" style="background-image:url(slides/' + ls.imageID + 'f.jpg)">\
					'+rentedOverlay+'\
					</div>\
				<div class="clear"></div>\
			 </div>';
		}	
	}
	listingsCont.innerHTML = iHtml;
	if (paging) {
		iHtml = ""
		var lastPage = parseInt(activeCount / itemsPerPage) +1;
		if (activeCount == ((lastPage - 1) * itemsPerPage)){
			lastPage = parseInt(activeCount / itemsPerPage);
		}
		var curClass = "";
		for (var i=1; i<lastPage+1; i++) {
			curClass = (curPage == i ? ' class= "currentPage" ': ' ')
			iHtml += "<a" + curClass + "href='#' onclick='loadHTML("+i+", true); return false;'>"+i+"</a>"+" "
		}
		if (curPage > 1){
			iHtml = "<a class='prevNext' href='#' onclick='loadHTML("+(curPage-1)+", true); return false;'><span class='arrow'>&#171;</span> previous </a>" + iHtml
		}else{
			iHtml = "<span class='prevNext pagingDisabled'><span class='arrow'>&#171;</span> previous </span>" + iHtml
		}
		if (curPage < lastPage){
			iHtml += "<a class='prevNext' href='#' onclick='loadHTML("+(curPage+1)+", true); return false;'>next <span class='arrow'>&#187;</span> </a>"
		}else{
			iHtml += "<span class='prevNext pagingDisabled'>next <span class='arrow'>&#187;</span> </span>"
		}
		pagingCont.innerHTML = "<span class='pageLbl'>Page: </span>" + iHtml;
		pagingCont2.innerHTML = "<span class='pageLbl'>Page: </span>" + iHtml;		
	}
	if (paging) {
		var endCount = itemsPerPage * curPage;
		if (endCount > activeCount){
			endCount = activeCount;
		}
		var startCount = (itemsPerPage * curPage) - itemsPerPage + 1;
		if (activeCount < startCount){
			startCount = startCount-1;
		}
		iHtml = "Results " + startCount + " - " + endCount + " of " + activeCount;
		resultsCountCont.innerHTML = iHtml;	
		resultsCountCont2.innerHTML = iHtml;		
	}
	document.getElementById("dvWait").style.display = "none";		
}

function startFilterSort() {
	document.getElementById("dvNoData").style.display = "none";
	document.getElementById("dvWait").style.display = "";
}

function applyAllFilters() {
	startFilterSort();
	listingFltrd = [];
	var ll = listings.length;
	var j = 0;
	for (var i = 0; i<ll; i++) {
		var ls = listings[i];
		if (ls.amount >= amount.from && ls.amount <= amount.to && ls.rooms >= rooms.from && ls.rooms <= rooms.to && locations.indexOf(ls.locationId)>-1 && propTypes.indexOf(ls.type)>-1) {
			listingFltrd[j] = ls;
			j++ 
		}
	}
	activeCount=listingFltrd.length;
}
function sortListing(sortFunction) {
	startFilterSort();
	listingFltrd.sort(eval(sortFunction));
	activeSort = sortFunction;
	loadHTML(1,false);
}

function sortByLocationAcs(a,b) {
    var x = a.location;
    var y = b.location;	
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortByLocationDesc(a,b) {
    var x = a.location;
    var y = b.location;	
	return ((x > y) ? -1 : ((x < y) ? 1 : 0));
}

function sortByAmountAcs(a,b) {
	return a.amount - b.amount;
}

function sortByAmountDesc(a,b) {
	return b.amount - a.amount;
}

function sliderHook() {
	amount.from = parseInt(document.getElementById("txt_LsldRent").value.replace(/[\$,]/g,""));
	amount.to = parseInt(document.getElementById("txt_RsldRent").value.replace(/[\$,]/g,""));
	rooms.from = document.getElementById("txt_LsldRoom").value;
	rooms.to = document.getElementById("txt_RsldRoom").value;
	applyAllFilters();
	if (activeSort != orgSort) {
		listingFltrd.sort(eval(activeSort));
	}
	loadHTML(1, true);	
}

function toFirstPage() {

}

function toLastPage() {

}

function getReturnURL() {
	return escape("/listings.aspx?typ=" + propTypes.substring(0,propTypes.length - 1) + "&loc=" + locations.substring(0,locations.length - 1) + "&a1=" + amount.from + "&a2=" + amount.to + "&r1=" + rooms.from + "&r2=" + rooms.to);  
}
function goToList(action, id){
    location.href='/listdetails.aspx?id='+id+'&searched=true&action='+action+'&from=' + getReturnURL();
}

//Start selectex variation javascript should be included in object

var selectExObjs = [];

function selectExObj (sxId, hdnVal, bits, fldName) {
	this.bits = bits;
	this.fldName = fldName;
	this.drpdDown = false;
	this.bitCount = bits.split(",").length;
	this.hdnVal = document.getElementById(hdnVal);
	this.cont = document.getElementById("sxContainer" + sxId);
	this.counter = document.getElementById(sxId + "Count").getElementsByTagName("SPAN");
	//alert(this.counter.offsetLeft + "-" + this.counter.offsetTop);
	var sfx = (this.bitCount==1 ? "" : "s");
	this.counter[0].innerHTML = this.bitCount;
	this.counter[1].innerHTML = this.fldName + sfx + " selected";
}
function sx_cancelRowEvent(e) {
    var e = e || window.event;
	var elm = e.target || e.srcElement; // Get the activated slider element.
    if (elm.nodeName == "INPUT") {
		sx_calcBits(selectExObjs[elm.parentNode.parentNode.id.substring(11)]);
    }
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}
function sx_checkAll(check, e, sxId) {
	var sxObj = selectExObjs[sxId];
	var flgs = sxObj.cont.getElementsByTagName("INPUT")
	for (i=0; i<flgs.length; i++) {
		flgs[i].checked = check;
	}
	sx_calcBits(sxObj);
	//sx_cancelRowEvent(e);		
}
function sx_calcBits(sxObj) {
	var flgs = sxObj.cont.getElementsByTagName("INPUT")
	var bitCount = 0;
	var bits = "";
	for (i=0; i<flgs.length; i++) {
		if (flgs[i].checked) {
			bits += "," + parseInt(flgs[i].value);
			bitCount++;
		}
	}
	var sfx = (flgs.length==1 ? "" : "s");
	sxObj.counter[0].innerHTML = bitCount;
	sxObj.counter[1].innerHTML = sxObj.fldName + sfx + " selected";
	sxObj.hdnVal.value = bits.substring(1);
	locations = document.getElementById("ctl00_mainContPh_slxLocations_sxValue").value + ",";
	propTypes = document.getElementById("ctl00_mainContPh_slxTypes_sxValue").value + ",";
	applyAllFilters();
	if (activeSort != orgSort) {
		listingFltrd.sort(eval(activeSort));
	}
	loadHTML(1, true);	
}
function sx_select(e, optn) {
	optn.getElementsByTagName("INPUT")[0].checked = !optn.getElementsByTagName("INPUT")[0].checked;
	sx_calcBits(selectExObjs[optn.parentNode.id.substring(11)]);
	sx_cancelRowEvent(e);	
}

