function NewPropertiesListHandler()
{
	this.myId;				//module id
	this.myView;			//object of view class
	this.container;			//parent DOM Object
	this.isSubView = false;			//Flag for showing sub-view on right.
	this.propertyHeading;
	this.myName;
	this.viewType;
	this.sortField='property_createddate';
	this.myView = new NewPropertiesList();
	this.init();
}
NewPropertiesListHandler.prototype.loadView=function(moduleId, viewContainer, subViewContainer)
{
	this.myId = moduleId;
	this.viewContainer = viewContainer;
	this.subViewContainer = subViewContainer;
	
	this.currentPage = 1;
	
	this.propertyHeading="New Properties";
	this.myView.showView(this.callbackHandlerFn,this.viewContainer,this.subViewContainer,this.isSubView,this,this.propertyHeading);
	
	this.myView.addSortDropDown();
	
	reapfield.G_API.getNewPropertiesListData(this.currentPage,this.rowsPerPage);

}
NewPropertiesListHandler.prototype.loadDetailData=function(infoObj)
{
}
NewPropertiesListHandler.prototype.init=function()
{
	this.currentPage = 1;
	this.rowsPerPage = 10;
	this.totalRecords;
	this.totalPages;
	//Register events, that are required for this module.
	reapfield.G_EventMgr.registerEvent('NewPropertiesListData',this,'NewPropertiesListDataListener');
}
NewPropertiesListHandler.prototype.NewPropertiesListDataListener=function(eventObj)
{
	if(reapfield.G_ViewMgr.activeModule.myName != this.myName)
		return;
	
	var responseObj = eventObj.data;
	this.currentPage = responseObj.page;
	this.totalRecords = responseObj.records;
	this.totalPages = responseObj.total;
	
	//alert(this.currentPage+":"+this.totalRecords+":"+this.totalPages);
	
	this.myView.displayMainViewData(responseObj);
	
	if(this.currentPage == 1)
	{
		this.myView.addPagerDropDown(this.getPagetData(this.totalPages),this.currentPage,this.totalPages);
	}
	else
	{
		this.myView.updatePagerDropDown(this.currentPage,this.totalPages);
	}
	
}
NewPropertiesListHandler.prototype.getPagetData=function(totalPages)
{

	var pagerList = [];
	for(var i=0; i<totalPages; i++)
	{
		var pageVal = (i+1);
		pagerList.push({id:pageVal,text:"Page "+pageVal+" of "+totalPages});
	}
	
	return pagerList;
}
NewPropertiesListHandler.prototype.callbackHandlerFn=function(action, data, owner)
{
	if(action=="moreInfoClick")
	{
		var dataObj={moduleId:'6',detailId:data};
		reapfield.G_EventMgr.fireEvent('loadModuleEvent',dataObj);
	}
	else if(action == "pageChangeEvent")
	{
		owner.currentPage = data;
		reapfield.G_API.getNewPropertiesListData(owner.currentPage,owner.rowsPerPage);
	}
	else if(action == "sortChangeEvent")
	{
		// alert(" search  ..:" +data);
		if(data ==0)
			owner.sortField='';
		else
			owner.sortField=data;
			reapfield.G_API.getNewPropertiesListData(owner.currentPage,owner.rowsPerPage);
		
	}
	else if(action == "pageNavigateEvent")
	{
		if(data == "prev")
		{
			owner.currentPage--;
		}
		else if(data == "next")
		{
			owner.currentPage++;
		}
		reapfield.G_API.getNewPropertiesListData(owner.currentPage,owner.rowsPerPage);
	}
}





function NewPropertiesList() { };

NewPropertiesList.prototype.showView=function(callbackFn,viewParent,subViewParent,isSubView,owner,heading)
{
	this.viewParentObj = viewParent || document.body;
	this.subViewParentObj = subViewParent || document.body;
	this.owner = owner;
	this.isSubView = isSubView;
	this.callbackFn = callbackFn;
	this.propertyLocationId="propertyLocation";
	this.NewPropertiesList="NewPropertiesList";
	this.bathNumberId="bathNum";
	this.bedNumberId="bedNum";
	this.headingText=heading;
	this.moreInfo="NewPropertiesListingMoreInfo";
	this.carNumberId="carNum";
	this.titleId = "listingTitle";
	this.propertyDescritptionId="propertyDescription";
	this.propertyAddressId="propertyAddress";
	this.propertyPriceId="propertyPrice";
	this.contactAddress="contactAddress";
	this.topPagerId = "pageListSpanTop";
	this.topPagerListId = "pageSelectListTop";
	this.bottomPagerId = "pageListSpanBottom";
	this.bottomPagerListId = "pageSelectListBottom";
	
	this.topSortList="topSortList";
	this.bottomSortList="bottomSortList";
	this.topSortListId="topSortListId";
	this.bottomSortListId="bottomSortListId";
	
	
	
	this.topPrevBtnId = "topPrevNavButton";
	this.topNextBtnId = "topNextNavButton";
	this.botPrevBtnId = "bottomPrevNavButton";
	this.botNextBtnId = "bottomNextNavButton";
	this.propertyImageId="reapFieldPropertyImage";
	this.propertyImageDivId="reapFieldPropertyImageDiv";
	this.reapFieldAgentPhotoImageId="reapFieldAgentPhotoImageId";
	
	this.createMainView();
	if(this.isSubView)
	{
		this.createSubMainView();
	}
	else
	{
		this.clearSubMainView();
	}
}

NewPropertiesList.prototype.createMainView=function()
{
	var viewContent='<div class="contentTitle">'+this.headingText+'</div>\
					<div class="reapPropertySearch">\
					<input type="button" id="'+this.topPrevBtnId+'" value="<<" disabled="true" />\
					<span id="'+this.topPagerId+'">\
						<select name="'+this.topPagerListId+'" id="'+this.topPagerListId+'" class="quickSearchFields">\
							<option>Loading...</option>\
						</select>\
					</span>\
					<input type="button" id="'+this.topNextBtnId+'" value=">>" disabled="true" />\
					</div>\
					<div id="'+this.NewPropertiesList+'" class="reapFieldAgentProfile">\
					</div>\
					<div class="reapPropetyLowerlist">\
						<input type="button" id="'+this.botPrevBtnId+'" value="<<" disabled="true" />\
						<span id="'+this.bottomPagerId+'">\
							<select name="'+this.bottomPagerListId+'" id="'+this.bottomPagerListId+'" class="quickSearchFields">\
								<option>Loading...</option>\
							</select>\
						</span>\
						<input type="button" id="'+this.botNextBtnId+'" value=">>" disabled="true" />\
					</div>';
	
	this.viewParentObj.innerHTML = viewContent;
	
	this.topPrevBtn = document.getElementById(this.topPrevBtnId);
	this.topNextBtn = document.getElementById(this.topNextBtnId);
	this.botPrevBtn = document.getElementById(this.botPrevBtnId);
	this.botNextBtn = document.getElementById(this.botNextBtnId);
	
	this.eventHandler(this.topPrevBtn,"click","prev",this.callbackFn,this.owner);
	this.eventHandler(this.topNextBtn,"click","next",this.callbackFn,this.owner);
	this.eventHandler(this.botPrevBtn,"click","prev",this.callbackFn,this.owner);
	this.eventHandler(this.botNextBtn,"click","next",this.callbackFn,this.owner);
	
}
NewPropertiesList.prototype.createSubMainView=function()
{
	//add sub main view
}
NewPropertiesList.prototype.clearSubMainView=function()
{
	var viewContent='';
	this.subViewParentObj.innerHTML = viewContent;
}
NewPropertiesList.prototype.displayMainViewData=function(responseData)
{
	var agentImgDivId='', propertyHTML = '', NewPropertiesList = responseData.developments;
	
	for(var propertyCount=0;propertyCount<NewPropertiesList.length;propertyCount++)
	{
	
		if(propertyCount%2)
		{
			propertyHTML+=	'<div class="reapPropertyRow">';
		}
		else
		{		
			propertyHTML += '<div class="reapPropertyRowColor">';	
		}
		var tmpDiv=document.createElement("div");
		tmpDiv.innerHTML=NewPropertiesList[propertyCount].description;
		var descTmp=tmpDiv.innerText || tmpDiv.textContent;
		var desc=descTmp.substring(0, 200);
		if(parseInt(descTmp.length)>parseInt(desc.length))
			desc+="...";
		propertyHTML+='<table border="0" cellpadding="0" cellspacing="0">\
			<tr>\
				<td style="height:135px;width:150px;" valign="top">\
					<div class="linkItem" id="projectImageDiv'+propertyCount+'"><img id="projectImage'+propertyCount+'" src="" /></div>\
				</td>\
				<td valign="top">\
					<div style="padding:10px;width:453px;">\
					<div id="'+this.propertyImageDivId+propertyCount+'" class="linkItem" style="font-weight:bold">'+NewPropertiesList[propertyCount].title+'</div><div>'+desc+'</div>\
					</div>\
				</td>\
			</tr>\
		</table></div>';
		
	}
		
	var NewPropertiesListDiv=document.getElementById(this.NewPropertiesList);
	NewPropertiesListDiv.innerHTML=propertyHTML;
	
	for(var propertyCount=0;propertyCount<NewPropertiesList.length;propertyCount++)
	{
		reapfield.ChangePhoto(NewPropertiesList[propertyCount].image,"projectImage"+propertyCount,150,135);
		this.eventHandler(document.getElementById(this.propertyImageDivId+propertyCount),"click",NewPropertiesList[propertyCount].developmentid,this.callbackFn,this.owner);
		this.eventHandler(document.getElementById("projectImageDiv"+propertyCount),"click",NewPropertiesList[propertyCount].developmentid,this.callbackFn,this.owner);
	}

	// for(var propertyCount=0;propertyCount<NewPropertiesList.length;propertyCount++)
	// {
		// var propertyData = NewPropertiesList[propertyCount];
		// this.eventHandler(document.getElementById(this.moreInfo+propertyCount),"click",propertyData.property_propertyid,this.callbackFn,this.owner);
		// this.eventHandler(document.getElementById(this.propertyImageDivId+propertyCount),"click",propertyData.property_propertyid,this.callbackFn,this.owner);
		// this.eventHandler(document.getElementById(this.titleId+propertyCount),"click",propertyData.property_propertyid,this.callbackFn,this.owner);
	// }
	reapfield.G_ViewMgr.fixFooterHeight();
	
}
NewPropertiesList.prototype.createDateFormat=function(dateStr)
{
	if(dateStr)
	{
		var dateObj = new DateTimeHandler(),
			datePart = dateStr.split(' ')[0];
			//alert(dateObj.createDate(datePart,"dMy"));
		return dateObj.createDate(datePart,"dMy");
	}
	else
		return "";
	
}
NewPropertiesList.prototype.displaySubViewData=function(responseData)
{
	
}

NewPropertiesList.prototype.selectDropDownList=function(dropDownList,selectValue)
{
	for(var i=0; i<dropDownList.options.length; i++)
	{
		if(dropDownList.options[i].value == selectValue)
		{
			dropDownList.selectedIndex = i;
		}
	}
}
NewPropertiesList.prototype.createDropDownList=function(optionsList,valueField,titleField,listName,defaultSelectValue)
{
	var dropDownList = '<select name="'+listName+'" id="'+listName+'" class="quickSearchFields">';
	for(var item=0;item<optionsList.length; item++)
	{
		var listObj = optionsList[item], optValue = listObj[valueField], optText = (listObj[titleField] || "");
		dropDownList += '<option  value="' + optValue+'"';
		if(optValue == defaultSelectValue)
			dropDownList += ' selected';
		dropDownList += '>'+ optText +'</option>';
	}
	dropDownList += '</select> ';
	return dropDownList;
	
}

NewPropertiesList.prototype.updatePagerDropDown=function(currentPage, totalPages)
{
	this.selectDropDownList(document.getElementById(this.topPagerListId),currentPage);
	this.selectDropDownList(document.getElementById(this.bottomPagerListId),currentPage);
	if(currentPage == totalPages)
		this.disableNextButtons(true);
	else
		this.disableNextButtons(false);
	this.disablePrevButtons(false);
}
NewPropertiesList.prototype.addSortDropDown=function()
{
	var sortObj =[];
	sortObj.push({"id":0,"sortname":"Sort By"});
	sortObj.push({"id":"property_saleprice","sortname":"Price"});
	sortObj.push({"id":"property_createddate","sortname":"Latest Entry"});
	sortObj.push({"id":"property_builtupareasize","sortname":"Land/builtUp Area"});
	
	// this.topSortList="topSortList";
	// this.bottomSortList="bottomSortList";
	// this.topSortListId="topSortListId";
	// this.bottomSortListId="bottomSortListId";
	

//	var topPagerSpan = document.getElementById(this.topSortList);
	//topPagerSpan.innerHTML = this.createDropDownList(sortObj,"id","sortname",this.topSortListId)
	
//	var botPagerSpan = document.getElementById(this.bottomSortList);
	//botPagerSpan.innerHTML = this.createDropDownList(sortObj,"id","sortname",this.bottomSortListId)
	
	
	
//	this.eventHandler(document.getElementById(this.topSortListId),"change","",this.callbackFn,this.owner);
//	this.eventHandler(document.getElementById(this.bottomSortListId),"change","",this.callbackFn,this.owner);
	
}
NewPropertiesList.prototype.addPagerDropDown=function(pagingList,currentPage,totalPages)
{
	var topPagerSpan = document.getElementById(this.topPagerId);
	topPagerSpan.innerHTML = this.createDropDownList(pagingList,"id","text",this.topPagerListId,currentPage)
	
	var botPagerSpan = document.getElementById(this.bottomPagerId);
	botPagerSpan.innerHTML = this.createDropDownList(pagingList,"id","text",this.bottomPagerListId,currentPage)
	
	this.eventHandler(document.getElementById(this.topPagerListId),"change","",this.callbackFn,this.owner);
	this.eventHandler(document.getElementById(this.bottomPagerListId),"change","",this.callbackFn,this.owner);
	//alert(currentPage+":"+totalPages);
	if(currentPage != totalPages)
		this.disableNextButtons(false);
	this.disablePrevButtons(true);
}

NewPropertiesList.prototype.disableNextButtons=function(status)
{
	this.topNextBtn.disabled = status;
	this.botNextBtn.disabled = status;
}

NewPropertiesList.prototype.disablePrevButtons=function(status)
{
	this.topPrevBtn.disabled = status;
	this.botPrevBtn.disabled = status;
}
NewPropertiesList.prototype.eventHandler=function(obj,event,value,callback,owner)
{
	var myObj=this;
	function handlerFn()
	{
		if(obj.id.indexOf(myObj.propertyImageDivId) >= 0 || obj.id.indexOf("projectImageDiv") >= 0 || obj.id.indexOf(myObj.moreInfo) >= 0)
		{
			callback("moreInfoClick",value,owner);
		}
		else if(obj.id == myObj.topPagerListId)
		{
			callback("pageChangeEvent",obj.value,owner);
		}
		else if(obj.id == myObj.bottomPagerListId)
		{
			callback("pageChangeEvent",obj.value,owner);
		}
		else if(obj.id == myObj.topSortListId)
		{
			callback("sortChangeEvent",obj.value,owner);
		}
		else if(obj.id == myObj.bottomSortListId)
		{
			callback("sortChangeEvent",obj.value,owner);
		}
		else if(obj.id ==  myObj.topPrevBtnId || obj.id == myObj.topNextBtnId || obj.id ==  myObj.botPrevBtnId || obj.id ==  myObj.botNextBtnId)
			callback("pageNavigateEvent",value,owner);
		
	}
	
	reapfield.AttachEvent(obj,event,handlerFn,false)
	
}
NewPropertiesList.prototype.ChangePhoto=function(newPhoto,imageId,setX,setY)
{
	if(!newPhoto || newPhoto == '')
	{
		document.getElementById(imageId).parentNode.innerHTML = '';
		return;
	}
	var myImg = new Image();//document.createElement('img');
	myImg.onload=function()
	{
		var width = myImg.width,
			height = myImg.height;
			var newx = (setX?setX:100); // set  images max width to be.
			var newy = (setY?setY:100); // set images maximum height to be.
			
			
		// trace("... "+width+":"+height);
		if ( width >= height )
		{
			var tmpy = height*newx/width;
			if ( tmpy <= newy )
				newy = tmpy;
			else
				newx = width*newy/height;
		}
		else
		{
			tmpx = width*newy/height;
			if ( tmpx <= newx )
				newx = tmpx;
			else
				newy = height*newx/width;
		}
		myImg.id = imageId;
		myImg.style.height = newy+"px";
		myImg.style.width = newx+"px";
		
		var ourImgParent = document.getElementById(imageId).parentNode;
		ourImgParent.innerHTML = '';
		ourImgParent.appendChild(myImg);
		ourImgParent.style.backgroundColor = "transparent";
		// ourImg.src = myImg.src;
		// ourImg.style.height = newy+"px";
		// ourImg.style.width = newx+"px";
		// ourImg.parentNode.style.backgroundColor = "transparent";
	}
	if(newPhoto.indexOf("http://") >= 0)
		myImg.src=newPhoto;
	else
		myImg.src=reapfield.G_API.webServiceURL + newPhoto;
	
}

