/*
 *  TODO REMOVE THIS ..here for debug 
 * 
<cfscript>
	pagination = structNew();
	pagination.startrow = startrow;
	pagination.totalrows = attributes.qSearchResult.recordcount;

	if (Val(pagination.startrow) lte 0) pagination.startrow=1;
	if (Val(pagination.startrow) gt pagination.totalrows) pagination.startrow=1;

	session.startrow = pagination.startrow;

	pagination.maxrows = 2;

	pagination.prevstart = pagination.startrow - pagination.maxrows;
	pagination.nextstart = pagination.startrow + pagination.maxrows;

	pagination.pagetotal= Ceiling(pagination.totalRows / pagination.maxrows);
	pagination.pageno	= Ceiling(pagination.startrow / pagination.maxrows);
</cfscript>
*/

/*
 * 
 * Available modes for the result list are thumb,list and map where thumb is the default 
 * 
 */
var propertyListing_currentType='sale';
var propertyListing_currentMode='thumb';
var propertyListing_currentNode=0;
var propertyListing_currentPage=0;
var propertyListing_currentRecords=0;
var propertyListing_currentMaxRows=0;		
var propertyListing_changeEnabled=true; //used to avoid multiple calls without affecting asynchronous calls	
var propertyListing_customType='';
var propertyListing_order='sort_by_price';
var propertyListing_orderDirection='asc';
var propertyListing_contactSearchId = 0;
var propertyListing_currentSavedSearch = '';


var propertyListing_toPage=0;		//the page requested to be viewed
var propertyListing_toStartrow=0;

var propertyListing_LIST_MODE ='list';
var propertyListing_THUMB_MODE ='thumb';
var propertyListing_MAP_MODE ='map';

var propertyListing_viewMode = {};
	 	propertyListing_viewMode.list	='/site/master.cfm?fuseaction=site-ajax.displayPropertyResultList';
		propertyListing_viewMode.thumb	='/site/master.cfm?fuseaction=site-ajax.displayPropertyResultThumb';
		propertyListing_viewMode.map	='/site/master.cfm?fuseaction=site-ajax.displayPropertyResultMap';

var propertyListing_options = {
								dataType: 'html',
								url: propertyListing_viewMode.thumb,
								type: 'GET',
								data: "startrow=1",
								async:true, 
								cache:false,
								queue:false,
								/*timeout:30,    */								
								success: function(responseHtml, textStatus){
				   					 //show the content
				   					
				   					 $("#col2_content_wrapper").animate({ opacity: 'hide'}, 
				   					 							 { queue:"false", 
				   					 							   duration:1000, 
				   					 							   easing:"linear", 
				   					 							   complete:function(){
				   					 							     $("#col2_content_wrapper").html(responseHtml);	
																	 $("#col2_content_wrapper").animate({ opacity: 'show'}, { queue:"true", duration:1000, easing:"linear" });	
																   	 propertyListing_changeEnabled=true; 
																  		
																   } 
									});


				   								   					 				   					 		   
				  				},
				  				error: function(XMLHttpRequest, textStatus, errorThrown){
				  					//TODO change all alerts in all javascripts to modal windows....
				  					alert("An error occured, please try again\n"+ textStatus + ' ' + errorThrown);
				  					propertyListing_changeEnabled=true; 
				  				} 	
				  											    
							  };

/* function switchToMode
 * 
 * switch the result list into different modes
 * 
 */		
function propertyListing_switchToMode(mode,full){
	

	var fullListing=0;
	
	if (full != undefined){		
		//we are requesting a full listing
		fullListing=1;
				
		$(".pagination .showAll").hide();
		$(".pagination .showPages").show();
		
	}
	else {
		$(".pagination .showAll").show();
		$(".pagination .showPages").hide();
		
	}
	

	
	if ( propertyListing_changeEnabled ){ 			
		
		 //hide content
		 $("#col2_content_wrapper").animate({ opacity: 'hide'}, { queue:"false", duration:100, easing:"linear" });	
		 
		 //display loading design
		 $("#col2_content_wrapper").html('<div class="preloader_wrapper_col3"><div id ="loader" class="preloader_light_32_alt"/><span>Loading</span></div>');    
		 $("#col2_content_wrapper").animate({ opacity: 'show'}, { queue:"false", duration:100, easing:"linear" });	
		 
		
		propertyListing_options.data= "startrow=" + propertyListing_toStartrow + "&searchType=" + propertyListing_currentType + "&relocate=false&pageid=" + propertyListing_currentNode + "&customType=" + propertyListing_customType + "&order=" + propertyListing_order  + "&orderDirection=" + propertyListing_orderDirection + "&contactSearchId=" + propertyListing_contactSearchId + "&currentSavedSearch=" + propertyListing_currentSavedSearch + "&fullListing=" + fullListing; 
		propertyListing_options.url = propertyListing_viewMode[mode];
		
		//remove all active states
		$("#list_mode, #thumbnail_mode, #map_mode").removeClass('active');
		
		//apply the new active based on mode selected
		propertyListing_setCurrentMode(mode);

		//disable other queries
		propertyListing_changeEnabled=false;
		 
		 //make the request
		 $.ajax(propertyListing_options); 
				
	}
				
	return false;
		
}

/* function switchToPage
 * 
 * switch the result list to a different page but in the same current mode
 * 
 */		
function propertyListing_switchToPage(pageType){



	var startrow=0;

	if (propertyListing_changeEnabled){ 
		
		if (pageType == 'next') propertyListing_toPage = propertyListing_currentPage +1;
		else if(pageType == 'prev') propertyListing_toPage = propertyListing_currentPage -1;
		else propertyListing_toPage = 1;
		
		startrow=((propertyListing_toPage-1)*propertyListing_currentMaxRows)+1;
	
		propertyListing_toStartrow=startrow;
		propertyListing_switchToMode(propertyListing_currentMode);
		
	}

	
}

/* function switchToFullList
 * 
 * switch the result list to a different page but in the same current mode
 * 
 */		
function propertyListing_switchToFullList(pageType){



	var startrow=0;

	if (propertyListing_changeEnabled){ 
		
		$(".pagination .showAll").hide();
		$(".pagination .showPages").show();
		
		propertyListing_toPage = 1;
		startrow=1;
	
		propertyListing_toStartrow=startrow;
		
		propertyListing_currentMaxRows=propertyListing_currentRecords;

		
		
		propertyListing_switchToMode(propertyListing_currentMode,"full");
		
	}

	
}


/* function switchToPageList
 * 
 * switch the result list to a different page but in the same current mode
 * 
 */		
function propertyListing_switchToPageList(pageType){



	var startrow=0;

	if (propertyListing_changeEnabled){ 
		
		
		$(".pagination  .showAll").show();
		$(".pagination  .showPages").hide();
		
		propertyListing_toPage = 1;
		startrow=1;
	
		propertyListing_toStartrow=startrow;
		
		propertyListing_currentMaxRows=11;
		
		propertyListing_switchToMode(propertyListing_currentMode);
		
	}

	
}


/* function setCurrentPage
 * 
 * sets the currentPage variable and does the necessary html changes around the interface
 * 
 */		
function propertyListing_setCurrentPage(page){
	//only apply at a change of page
	
	if (propertyListing_currentPage != page){
		propertyListing_currentPage=page;
		
		//propertyListing_refreshInterface();
		
	}

}

/* function setStartrow
 * 
 * sets the currentPage variable and does the necessary html changes around the interface
 * 
 */		
function propertyListing_setStartrow(row) {
	//only apply at a change of page
	
	if (propertyListing_toStartrow != row){
		propertyListing_toStartrow=row;
		
		//propertyListing_refreshInterface();
		
	}

}



/* function setCurrentRecords
 * 
 * sets the currentRecords variable
 * this function will be called every time a page or piece of page gets loaded
 */	
function propertyListing_setCurrentRecords(value){
	propertyListing_currentRecords=value;	
}

/* function setCurrentMaxRows
 * 
 * sets the currentMaxRows variable
 * this function will be called every time a page or piece of page gets loaded
 */	
function propertyListing_setCurrentMaxRows(value){
	propertyListing_currentMaxRows=value;		
}

/* function setCurrentNode
 * 
 * sets the currentNode variable
 * this function will be called every time a page or piece of page gets loaded
 */	
function propertyListing_setCurrentNode(value){
	propertyListing_currentNode=value;		
}

/* function setCurrentMode
 * 
 * sets the currentMode
 * this function will be called every time a page or piece of page gets loaded
 */	
function propertyListing_setCurrentMode(mode){
	propertyListing_currentMode=mode;		
	
	switch (mode) {
		case propertyListing_LIST_MODE:
			$("#list_mode").addClass('active');
			//propertyListing_refreshInterface();
			break;
		case propertyListing_THUMB_MODE:
			$("#thumbnail_mode").addClass('active');
			//propertyListing_refreshInterface();
			break;
		case propertyListing_MAP_MODE:
			$("#map_mode").addClass('active');
			
			//map mode need to re-check the container height when opened
			recalculateNavHeight();
			//propertyListing_refreshInterface();
			
			
			
			break;
		
	}
	
}

/* function setCurrentType
 * 
 * sets the currentType variable which will be used to search again in the same area
 * 
 */		
function propertyListing_setCurrentType(searchType){
	//only apply at a change of page
	
	if (propertyListing_currentType != searchType){
		propertyListing_currentType=searchType;	
		
	}

}

/* function setCustomType
 * 
 * sets the customType variable
 * 
 */		
function propertyListing_setCustomType(customType){
	
	if (propertyListing_customType != customType){
		propertyListing_customType=customType;			
	}

}


/* function setContactSearchId
 * 
 * sets the contactSearchId variable
 * 
 */		
function propertyListing_setContactSearchId(contactSearchId){
	if (propertyListing_contactSearchId != contactSearchId){
		propertyListing_contactSearchId=contactSearchId;			
	}

}

/* function setCurrentSavedSearch
 * 
 * sets the currentSavedSearch variable
 * 
 */		
function propertyListing_setCurrentSavedSearch(currentSavedSearch){
	if (propertyListing_currentSavedSearch != currentSavedSearch){
		propertyListing_currentSavedSearch=currentSavedSearch;			
	}

}



/* function refreshInterface
 * 
 * does html changes based on current pagination variables
 */	
function propertyListing_refreshInterface(){
	
	//recalculate pages
	var pageTotal = Math.ceil(propertyListing_currentRecords / propertyListing_currentMaxRows);
	

	
	if(pageTotal != 0) {
		$('#pagination_label').html('Page '+propertyListing_currentPage+'/'+pageTotal+' of '+propertyListing_currentRecords+' matches');
		$('.icons_views li#iconPrint,.icons_views li#iconThumbView,.icons_views li#iconListView,.icons_views li#iconMapView,.icons_views li.iconDivider').css('display','block');
		$('.property_search_sort_by').css('display','block');
	} else {
		$('#pagination_label').html('');
		$('.icons_views li#iconPrint,.icons_views li#iconThumbView,.icons_views li#iconListView,.icons_views li#iconMapView,.icons_views li.iconDivider').css('display','none');
		$('.property_search_sort_by').css('display','none');
	}
		
	if (pageTotal <= propertyListing_currentPage) 
		$('.nextPage').css('display','none');
	else
		$('.nextPage').css('display','inline');
	

	if (propertyListing_currentPage <= 1) {
		$('.prevPage').css('display','none');
	}
	else {
		$('.prevPage').css('display','inline');
	}
	
	if(pageTotal <= 1)
		$('.footer_pagination, .footer_pagination_outer').css('display','none');
	else 
		$('.footer_pagination, .footer_pagination_outer').css('display','block');	
		

	
	if(propertyListing_currentMode==propertyListing_MAP_MODE) {
		//all modes have pagination with the exception of the map mode which will display all the results			
			$('#pagination_label').html('');
			$('.property_search_sort_by').css('display','none');
	}
	
	
	//dynamically change heading based on which search page we're on, saved or normal
	if(propertyListing_currentSavedSearch != "") {
		$('h2#searchResults_heading').html(propertyListing_currentSavedSearch);
	}
	
	if(propertyListing_customType == "" && propertyListing_currentSavedSearch == "") {
		$('.icons_views li#iconSaveSearch').css('display','block');	
	} else {		
		$('.icons_views li#iconSaveSearch').css('display','none');	
	}
	
	if(propertyListing_customType=="favouriteProperties") {
		$('.icons_views li#iconSendEmail,.icons_views li#iconSendMMS').css('display','block');
		$('.icons_views li#iconSaveSearch').css('display','none');	
	}	

	if(propertyListing_order=="sort_by_date_leased") {
		$('.property_search_sort_by #search_sort').css('display','none');
		$('.property_search_sort_by #sort_by_date_sold').css('display','none');
		$('.property_search_sort_by #sort_by_date_leased').css('display','block');
		$('.property_search_sort_by #sort_by_most_viewed').css('display','none');
		$('.property_search_sort_by #sort_by_date_listed').css('display','none');	
	} else if(propertyListing_customType=="recentSales" || propertyListing_order=="sort_by_date_sold") {			
		$('.property_search_sort_by #search_sort').css('display','none');
		$('.property_search_sort_by #sort_by_date_sold').css('display','block');
		$('.property_search_sort_by #sort_by_date_leased').css('display','none');
		$('.property_search_sort_by #sort_by_most_viewed').css('display','none');
		$('.property_search_sort_by #sort_by_date_listed').css('display','none');		
		
	} else if(propertyListing_customType=="mostViewed" || propertyListing_order=="sort_by_most_viewed") {
		$('.property_search_sort_by #search_sort').css('display','none');
		$('.property_search_sort_by #sort_by_date_sold').css('display','none');
		$('.property_search_sort_by #sort_by_date_leased').css('display','none');
		$('.property_search_sort_by #sort_by_most_viewed').css('display','block');
		$('.property_search_sort_by #sort_by_date_listed').css('display','none');
	} else if(propertyListing_order=="sort_by_date_listed") {
		$('.property_search_sort_by #search_sort').css('display','none');
		$('.property_search_sort_by #sort_by_date_sold').css('display','none');		
		$('.property_search_sort_by #sort_by_date_leased').css('display','none');
		$('.property_search_sort_by #sort_by_most_viewed').css('display','none');
		$('.property_search_sort_by #sort_by_date_listed').css('display','block');
	}
	else {
		$('.property_search_sort_by #search_sort').css('display','block');
		$('.property_search_sort_by #sort_by_date_sold').css('display','none');
		$('.property_search_sort_by #sort_by_date_leased').css('display','none');
		$('.property_search_sort_by #sort_by_most_viewed').css('display','none');
		$('.property_search_sort_by #sort_by_date_listed').css('display','none');
	}
	
	
	
}



/* function propertyListing_setOrder
 * 
 * sets the propertyListing_order variable
 * 
 */		
function propertyListing_setOrder(order){
	if (propertyListing_order != order){
		propertyListing_order=order;			
	}

}


/* function propertyListing_setOrderDirection
 * 
 * sets the propertyListing_orderDirection variable
 * 
 */		
function propertyListing_setOrderDirection(orderDirection){
	
	if (propertyListing_orderDirection != orderDirection){
		propertyListing_orderDirection=orderDirection;			
	}

}


/* function propertyListing_toggleSortStyle
 * 
 * sets the appropriate styles for Sort By navigation based on active sort type and order
 * 
 */	
function propertyListing_toggleSortStyle(sortType, sortOrder) { 
	
	
	$(".property_search_sort_by ul a").each(
		function() {
				$(this).removeClass('sort_active');
				$(this).removeClass('ascending');
				$(this).removeClass('descending');
		
		}
	
	);

	$('.property_search_sort_by ul a#' + sortType).addClass('sort_active');
	
	if(sortOrder == 'desc') // used for initial sort set
		$('.property_search_sort_by ul a#' + sortType).addClass('descending');
	else
		$('.property_search_sort_by ul a#' + sortType).addClass('ascending');	
}


/* function propertyListing_setSort
 * 
 * sets the sort order and orderDirection vars and runs an ajax call to refresh search
 * 
 */	
function propertyListing_setSort(sortType, sortOrder) { 
		
	propertyListing_setOrder( sortType );
	propertyListing_setOrderDirection ( sortOrder );
	propertyListing_switchToMode(propertyListing_currentMode);	
		

}



/* function openingListing_sendEmail
 *  
 * display a thickbox for the user to send favourite properties to an email address
 *  
 * 
 * Note:requires thickbox scripts to be already loaded
 */ 
 
function propertyListing_sendEmail(pagePath,nType,ntoKey,nfromKey,nReferer){
	

	var options = {
			dataType: 'json',
			url: '/site/master.cfm?fuseaction=site-ajax.validateAuth',
			type: 'GET',	 
			async:false, 
			cache:false,       					
			timeout:30, //ms

			success: function(response) { 	
				    	
		    	if (response.retcode == "OK")	{
		    		
		    		//display saved search window, it wil not redirect on completion, it will just close
		    		subnav_show('site-ajax.displayPopupEmailFavourites',0);
		    		
		    		//Track hit		    		
					trackme(nType,ntoKey,nfromKey,nReferer);
		    		
		    	
		    	} 					    		
		    	else{	//user is not authenticated		    		
		    	
		    		//display login window with redirection to the search on success
		    		subnav_show('site-ajax.displayLogin',1,'component','site-ajax.displayPopupEmailFavourites%26thisPagePath='+pagePath);
		    		
		    	}				  	    						 										 
		    },
		    
		    error: function(XMLHttpRequest, textStatus, errorThrown){
				alert("An error occured, please try again\n"+ textStatus + ' ' + errorThrown);
			} 	
			    
	  };		    

	//ajax call for the popup					
	$.ajax(options);

	
	return false;
	
	
}



/* function propertyListing_sendFavouritesMMS
 *  
 * display a thickbox for the user to send  properties to mobile via mms
 *  
 * 
 * Note:requires thickbox scripts to be already loaded
 */ 
function propertyListing_sendFavouritesMMS(pagePath,nType,ntoKey,nfromKey,nReferer) {
		
	
 
	var options = {
			dataType: 'json',
			url: '/site/master.cfm?fuseaction=site-ajax.validateAuth',
			type: 'GET',	 
			async:false, 
			cache:false,       					
			timeout:30, //ms
			
			success: function(response) { 	
				    	
		    	if (response.retcode == "OK")	{
		    		
		    		//display saved search window, it wil not redirect on completion, it will just close
		    		subnav_show('site-ajax.displayPopupFavouriteMMS',0);
		    		
		    		//Track hit		    		
					trackme(nType,ntoKey,nfromKey,nReferer);	    		
		    	
		    	} 					    		
		    	else{	//user is not authenticated		    		
		    	
		    		//display login window with redirection to the search on success
		    		subnav_show('site-ajax.displayLogin',1,'component','site-ajax.displayPopupFavouriteMMS%26thisPagePath='+pagePath);
		    		
		    	}				  	    						 										 
		    },
		    
		    error: function(XMLHttpRequest, textStatus, errorThrown){
				alert("An error occured, please try again\n"+ textStatus + ' ' + errorThrown);
			} 	
		    
	  };	
				
			
	//ajax call for the popup					
	$.ajax(options);

	
	return false;
	
	
}










function propertyListing_setTooltip() {
	
$('#col2_content_wrapper .tooltip_icons a, #col2_content_wrapper .tooltip_icons span, #col2_content_wrapper .tooltip_icons li').tooltip({
				track: true,
				delay: 0,
				fade: 0,
				top: -45,
				left: -10,
				showURL: false,
				fixPNG: true
			});	
			
return false;
}



$(document).ready(function(){
	
	$('#col2_navigation .tooltip_icons a, #col2_navigation .tooltip_icons span').tooltip({
		track: true,
		delay: 0,
		fade: 0,
		top: -45,
		left: -10,
		showURL: false,
		fixPNG: true
	});
	

	
	// Sort search results and toggle sort by navigation
	$(".property_search_sort_by ul a#sort_by_suburb, .property_search_sort_by ul a#sort_by_price").click(function() {
		

		$(this).toggleClass("descending");
		$(this).toggleClass("ascending");
		
		propertyListing_setCurrentPage(1);
		propertyListing_setStartrow(1);
		
		
		if( $(this).hasClass("sort_active") ) {
		
			if( $(this).hasClass("descending") )
				propertyListing_setSort(this.id, 'desc');
			else
				propertyListing_setSort(this.id, 'asc');
		
		} else {
			propertyListing_setSort(this.id, 'asc');
		}
		
		
		propertyListing_toggleSortStyle(this.id, propertyListing_orderDirection);
		
    });
	
	
	
	
});





