
$(document).ready(function(){SortHistory.loadHistoryEvent();TableViewSorter.init("sortByPicker","listView");if(typeof SearchFilter=='undefined'){SortHistory.strSortHash=window.location.hash.replace(/^#/,"");SortHistory.updateLinkAnchors();Breadcrumbs.updateAnchors(SortHistory.strSortHash);}
TableViewSorter.oSortByPicker.change(function(){TableViewSorter.sortTable($(this).val());SortHistory.updateURL();SortHistory.updateLinkAnchors();Breadcrumbs.updateAnchors(SortHistory.strSortHash);$(window).trigger("sort");return false;});});

var SortHistory={strDefaultSortOption:"hb_sort_rating",strSortOption:"hb_sort_rating",strSortLabel:"table_sort",strSortHash:"",stuSortOptions:{hb_sort_sharedprice_asc:"shared_asc",hb_sort_sharedprice_des:"shared_des",hb_sort_privateprice_asc:"private_asc",hb_sort_privateprice_des:"private_des",hb_sort_rating:"rating",hb_sort_propertyname:"name",hb_sort_propertytype_asc:"ptype_asc",hb_sort_propertytype_des:"ptype_des"},updateHash:function(sortOrder){SortHistory.strSortOption=sortOrder;if(typeof SearchFilter!="undefined"){SortHistory.strSortHash=SearchFilter.getFilterHash();}else{SortHistory.strSortHash="/"+SortHistory.strSortLabel+"="+SortHistory.stuSortOptions[SortHistory.strSortOption];}
SortHistory.strSortHash=SortHistory.strSortHash.toLowerCase();},updateURL:function(){$.address.value(SortHistory.strSortHash);},updateLinkAnchors:function(){if($("#navList a").attr("href")!="undefined"&&$("#navMap a").attr("href")){var strListViewURL=$("#navList a").attr("href");var strMapViewURL=$("#navMap a").attr("href");var arrListURLParts=strListViewURL.split("#");var arrMapURLParts=strMapViewURL.split("#");$("#navList a").attr("href",arrListURLParts[0]+"#"+SortHistory.strSortHash);$("#navMap a").attr("href",arrMapURLParts[0]+"#"+SortHistory.strSortHash);}},writeRefineCookie:function(sortOrder){CookieUtil.setCookie("HB_REFINERESULTS",SortHistory.strSortHash);},log:function(sortOrder){SortHistory.updateHash(sortOrder);SortHistory.writeRefineCookie();},loadHistoryEvent:function(){$.address.externalChange(function(event){SortHistory.externalPageload(event.value.replace(/^\//,""));});},externalPageload:function(hash){SortHistory.strSortOption=SortHistory.strDefaultSortOption;if(hash.length>0){var iNameValue,arrNameValue,sortOrder;var arrHash=hash.split("/");var intHashLength=arrHash.length;var foundSortValue=false;for(iNameValue=0;iNameValue<intHashLength;iNameValue++){arrNameValue=arrHash[iNameValue].split("=");if(arrNameValue.length!=2)continue;if(arrNameValue[0]==SortHistory.strSortLabel){sortOrder=arrNameValue[1];foundSortValue=true;break;}}
if(foundSortValue)
{for(var iParam in SortHistory.stuSortOptions){if(typeof SortHistory.stuSortOptions[iParam]=="undefined")continue;if(SortHistory.stuSortOptions[iParam]==sortOrder){SortHistory.strSortOption=iParam;break;}}}
TableViewSorter.sortTable(SortHistory.strSortOption,false);$(window).trigger("sort");}
$("#sortByPicker").val(SortHistory.strSortOption);}};

var SortOrderNedStat={strTrackingBaseURL:"",setTrackingBaseURL:function(strTrackingBaseURL){SortOrderNedStat.strTrackingBaseURL=strTrackingBaseURL;},fireTrackingCode:function(sortOrder,bDoNedStat){if(bDoNedStat){var strTrackingString=SortOrderNedStat.strTrackingBaseURL+'&amp;hb_sort_type='+sortOrder.replace("hb_sort_","")+'&amp;hb_sort_count=1';sitestat(strTrackingString);}}};

var TableViewSorter={oSortByPicker:"",oSortByTable:"",init:function(strIDSortByPicker,strIDSortByTable){TableViewSorter.oSortByPicker=$("#"+strIDSortByPicker);TableViewSorter.oSortByTable=$("#"+strIDSortByTable);return this;},sortTable:function(sortOrder,bDoNedStat){if(bDoNedStat===undefined){bDoNedStat=true;}
var View=null;if(typeof MapView!="undefined"){View=MapView;}
if(typeof ListView!="undefined"){View=ListView;}
try{var arrSortSettings=View.selectOption(sortOrder);TableViewSorter.oSortByTable.trigger("sorton",[arrSortSettings]);SortOrderNedStat.fireTrackingCode(sortOrder,bDoNedStat);SortHistory.log(sortOrder);}catch(err){TableViewSorter.oSortByPicker.val(SortHistory.strDefaultSortOption);TableViewSorter.sortTable(SortHistory.strDefaultSortOption,false);}}};

var ListView={selectOption:function(sortOrder){var intPrimarySortCol,intPrimarySortDir,intSecondarySortCol,intSecondarySortDir;switch(sortOrder){case"hb_sort_sharedprice_asc":intPrimarySortCol=4;intPrimarySortDir=0;intSecondarySortCol=3;intSecondarySortDir=1;break;case"hb_sort_sharedprice_des":intPrimarySortCol=4;intPrimarySortDir=1;intSecondarySortCol=3;intSecondarySortDir=1;break;case"hb_sort_privateprice_asc":intPrimarySortCol=5;intPrimarySortDir=0;intSecondarySortCol=3;intSecondarySortDir=1;break;case"hb_sort_privateprice_des":intPrimarySortCol=5;intPrimarySortDir=1;intSecondarySortCol=3;intSecondarySortDir=1;break;case"hb_sort_rating":intPrimarySortCol=3;intPrimarySortDir=1;intSecondarySortCol=1;intSecondarySortDir=0;break;case"hb_sort_propertyname":intPrimarySortCol=1;intPrimarySortDir=0;intSecondarySortCol=3;intSecondarySortDir=1;break;case"hb_sort_propertytype_asc":intPrimarySortCol=2;intPrimarySortDir=0;intSecondarySortCol=2;intSecondarySortDir=0;break;case"hb_sort_propertytype_des":intPrimarySortCol=2;intPrimarySortDir=1;intSecondarySortCol=2;intSecondarySortDir=1;break;}
return[[intPrimarySortCol,intPrimarySortDir],[intSecondarySortCol,intSecondarySortDir]];}};

