
//Datasource url setup: DataSetURL
var DataSetURL=window.location+"?EnsureStringDataType";DataSetURL=DataSetURL.split("?");
if(DataSetURL.length > 0){
	DataSetURL=DataSetURL[1];
	DataSetURL="/ClarenProject/R-RMLS/Resources/AJAX_ListingIndex.cfm?"+DataSetURL;
} else {
	DataSetURL="/ClarenProject/R-RMLS/Resources/AJAX_ListingIndex.cfm?ln=0";//this can only happen if there is an error
}

// Create a DataSet: GetListings
var GetListings = new Spry.Data.HTMLDataSet(DataSetURL, "GetListings");

// Pagenate our DataSet: pvGetListings
var pvGetListings = new Spry.Data.PagedView(GetListings,{pageSize:20});
pvGetListings.setColumnType("BATHS", "number");
pvGetListings.setColumnType("BEDROOMS", "number");
pvGetListings.setColumnType("LOT_SIZE", "number");
pvGetListings.setColumnType("LP", "number");

// Setup PageInfo for page number display: pvGetListingsPagedInfo
var pvGetListingsPagedInfo = pvGetListings.getPagingInfo();

// Serup onPostUpdate event to engage reload of favorites for current page
Spry.Data.Region.addObserver("ListingIndexRegion",{onPostUpdate:refreshFavorites});

// cookie added result conformation: not used
function MySuccessCallback(req){
  // Throw an alert with the message that was
  // passed to us via the userData.

  //alert("Callback: " + req.url);
  //alert("SUCCESS: " + req.userData.msg);
}

// request a page that uses coldfusion to manage the cookie for favorites
function favoriteUpdate(otarget,ln){
	var myObj = {"msg":"1" };
	if(otarget.title.toLowerCase()=='remove'){
		Spry.Utils.removeClassName(otarget.id,"Favorite-True");
		Spry.Utils.addClassName(otarget.id,"Favorite-False");
		var req=Spry.Utils.loadURL("GET","/ClarenProject/Resources/AJAX_Favorites_Updater.cfm?action=delete&ln="+ln,true,MySuccessCallback,{userData:myObj});
		otarget.title="Add";
	} else {
		Spry.Utils.removeClassName(otarget.id,"Favorite-False");
		Spry.Utils.addClassName(otarget.id,"Favorite-True");
		var req=Spry.Utils.loadURL("GET","/ClarenProject/Resources/AJAX_Favorites_Updater.cfm?action=add&ln="+ln,true,MySuccessCallback,{userData:myObj});
		otarget.title="Remove";
	}
}

// obviously
function getCookie(name){
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

// upon dataset being writen to doc, set favorite buttons to correct state
function refreshFavorites(notifier,data){
	var favs=getCookie("FAVORITES");
	var favs_container=null;
	favs=favs.split(",");
	for (i in favs){
		if(favs[i]!=null && favs[i]!=""){
			favs_container=document.getElementById("Favorite_"+favs[i]);
			if(favs_container!=null){
				Spry.Utils.removeClassName(favs_container.id,"Favorite-False");
				Spry.Utils.addClassName(favs_container.id,"Favorite-True");
				favs_container.title="Remove";
			}
		}
	}
}