/**
 * ListingDetails JQuery Plugin
 *	usage: used on the listing details page
 *	dependencies: jquery, flot (with pie charting)
 */
(function() {
	var listingDetailsAjaxRequest = null;
	var listingInfoAjaxRequest = null;
	var highlightedDetailsSummaryTab = null;
	var highlightedDetailsTab = null;

	/** This function initializes the listings search page.
	  *
	  */
	$.listingDetails = function() {
		// show listing details
		$("#listingDetailsSlideshow").RPMSlideShow();
	}

	/** This function displays a slide show for a specified listing
	  *
	  * @param listNum listing to grab slideshow for
	  * @param secId section id for the listing summary
	  */
	$.displayListingDetailsSlideshow = function(listNum, secId){
		// empty the content panel
		$("#topSec_cnt").empty();

		// add the maps DOM element
		$("#topSec_cnt").html('<div align="center" style="vertical-align:middle;">Loading Slideshow...</div>');
	
		// abort any pending requests
		if(listingDetailsAjaxRequest){
			listingDetailsAjaxRequest.abort();
		}

		//Get Search Options here & construct query string
		listingDetailsAjaxRequest = $.ajax({
			type: "GET",
			url: "listings?pathway=6&listingNumber="+listNum+"&slideshow=true",
			dataType: "html",
			error: function(data, error){
				alert("Error: displayListingDetailsSlideshow(): " + error + " " + data);
			},
			success: function(data){
				$("#topSec_cnt").html(data);
			},
			complete: function (XMLHttpRequest, textStatus) {
				$("#listingDetailsSlideshow").RPMSlideShow({});
			}
		});
	}

	/** This function displays the virtual tours for a specified listing
	  *
	  * @param listNum listing to grab virtual tour for
	  */
	$.displayListingDetailsVTours = function(listNum){
		// add the maps DOM element
		$("#topSec_cnt").html('<div align="center" style="vertical-align:middle;">Loading Virtual Tour...</div>');

		// abort any pending requests
		if(listingDetailsAjaxRequest){
			listingDetailsAjaxRequest.abort();
		}

		listingDetailsAjaxRequest = $.ajax({
			type: "GET",
			url: "listings?pathway=6&type=results&listingNumber="+listNum+"&vrTours=true",
			dataType: "html",
			error: function(data, error){
				alert("Error: displayListingDetailsVTours(): " + error + " " + data);
			},
			success: function(data){
				$("#topSec_cnt").html(data);
			},
			complete: function (XMLHttpRequest, textStatus) {
				$("#listingDetailsTour").RPMSlideShow({virtualTour:true});
			}
		});
	}

	/** This function displays a map for the listing.
	  *
	  * @param lat lattitude for the map
	  * @param lng longitude for the map
	  */
	$.displayListingDetailsMap = function(lat, lng){
		// abort any pending requests
		if(listingDetailsAjaxRequest){
			listingDetailsAjaxRequest.abort();
		}

		// empty the content panel
		$("#topSec_cnt").empty();

		// add the maps DOM element
		$("#topSec_cnt").html('<div class="mapWrap"><div id="listingDetailsMap" style="width: 650px; height: 397px"></div></div>');

		// do the map magic
		var map = new GMap2($("#listingDetailsMap").get(0));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(lat, lng),15);
		var point = new GLatLng(lat, lng);
		map.addOverlay(new GMarker(point));
		
		// add map unload to document unload function
		$(document).unload(function(){
			GUnload();
		});
		return false;
	}

	/** This function displays the contact me form for a specified listing
	  *
	  * @param listNum listing to grab the e-mail form for
	  */
	$.displayListingEmail = function(listNum){
		// add the maps DOM element
		$("#topSec_cnt").html('<div align="center" style="vertical-align:middle;">Loading Email Form...</div>');

		// abort any pending requests
		if(listingDetailsAjaxRequest){
			listingDetailsAjaxRequest.abort();
		}

		listingDetailsAjaxRequest = $.ajax({
			type: "GET",
			url: "listings",
			data: "pathway=6&listingNumber="+listNum+"&listingEmailView=141&loadListingEmail=true",
			dataType: "html",
			error: function(data, error){
				alert("Error: listingDetails.displayListingEmail(): " + error + " " + data.statusText());
			},
			success: function(data){
				 $("#topSec_cnt").html(data);
			}
		});
		return false;
	}

	/** This function displays the listing media files for a specified listing
	  *
	  * @param listNum listing to grab media for
	  */
	$.displayListingMedia = function(listNum){
		// add the maps DOM element
		$("#topSec_cnt").html('<div align="center" style="vertical-align:middle;">Loading Media List...</div>');

		// abort any pending requests
		if(listingDetailsAjaxRequest){
			listingDetailsAjaxRequest.abort();
		}

		listingDetailsAjaxRequest = $.ajax({
			type: "GET",
			url: "listings",
			data: "pathway=6&type=results&listingNumber="+listNum+"&media=true",
			dataType: "html",
			error: function(data, error){
				alert("Error: listingDetails.displayListingMedia(): " + error + " " + data.statusText());
			},
			success: function(data){
				 $("#topSec_cnt").html(data);
			}
		});
		return false;
	}



	/** This function displays the summary for a listing
	  * DEPRICATED: View will hide information instead of call database for info already grabbed on load...
	  * @param listNum identifier for the listing
	  * @param pressed the button that was pressed
	  */
	$.displayListingSummary = function(listNum, pressed){
		// set the currently highlighted tab in case we need to rollback
		var self = this;
		this.highlightedDetailsTab = $("#detailsSectionTabs li.select a").get(0);
		
		// highlight this tab
		$.formatListingDetailsTabs(pressed);

		// put loading message 
		$("#botSec_cnt").html('<div align="center" style="vertical-align:middle;">Loading Listing Summary...</div>');
	
		// stop AJAX request if necessary
		if(self.listingDetailsAjaxRequest){
			self.listingDetailsAjaxRequest.abort();
		}
			
		//Get Search Options here & construct query string
		self.listingDetailsAjaxRequest = $.vIPAjax({
			type: "GET",
			url: "listings?pathway=6&summary=true&listingNumber="+listNum,
			error: function(data, error){
				alert("Error - displayListingSummary(): error = " + error + ", data = " + data);
			},
			success: function(data){
				try{
					$("#botSec_cnt").empty().html(data);
				}catch(ex){
					alert("Error: displayListingStatistics() [SUCCESS]: " + ex);
				}
			}
		}, function(){
			// rollback the operation
			$(self.highlightedDetailsTab).click();
		});

		return false;
	}
	
	//Top section tab click events
	function top_tabSelect(clickedTab){
		$('#displaySectionTabs li').removeClass("select");
		$(clickedTab).parent().addClass("select");
	}
	
	$.topSec_formatListingDetailsTabs = function(pressed){
		top_tabSelect(pressed);
	}

	//Bottom section tab click events
	
	/** This function sets a specified button to a 'selected' format.
	  *
	  * @param pressed dom object whose parent should be set as selected
	  */
	function bot_tabSelect(clickedTab){
		$('#detailsSectionTabs li').removeClass("select");
		$(clickedTab).parent().addClass("select");
	}
	
	$.botSec_formatListingDetailsTabs = function(pressed){
		bot_tabSelect(pressed);
	}
	
	$.showSummary = function(pressed){
		bot_tabSelect(pressed);
		$('#botSec_cnt_other').html("");
		$('#botSec_cnt_det').css("display", "none");
		$('#botSec_cnt_summ').css("display", "block");
		$('#botSec_cnt_qualifier').css("display","none");
		return false;
	}

	$.showDetails = function(pressed){
		bot_tabSelect(pressed);
		$('#botSec_cnt_other').html("");
		$('#botSec_cnt_summ').css("display", "none");
		$('#botSec_cnt_det').css("display", "block");
		$('#botSec_cnt_qualifier').css("display","none");
		return false;
	}
	
	/** This function displays the statistics for a listing
	  *
	  * @param listNum identifier for the listing
	  * @param pressed the button that was pressed
	  */
	$.showListingStatistics = function(listNum, pressed){
		// set the currently highlighted tab in case we need to rollback
		var self = this;
		this.highlightedDetailsTab = $("#detailsSectionTabs li.select a").get(0);
		
		// highlight this tab
		bot_tabSelect(pressed);

		// put loading message 
		$('#botSec_cnt_summ').css("display", "none");
		$('#botSec_cnt_det').css("display", "none");
		$('#botSec_cnt_qualifier').css("display","none");
		$("#botSec_cnt_other").empty().show();
		$("#botSec_cnt_other").html('<div align="center" style="vertical-align:middle;">Loading Listing Statistics...</div>');

		// stop AJAX request if necessary
		if(listingDetailsAjaxRequest){
			listingDetailsAjaxRequest.abort();
		}

		//Get Search Options here & construct query string
		listingDetailsAjaxRequest = $.vIPAjax({
			type: "GET",
			url: "listings?pathway=6&statistics=true&listingNumber="+listNum,
			error: function(data, error){
				alert("Error: displayListingStatistics(): " + error + " " + data);
			},
			success: function(data){
				$("#botSec_cnt_other").empty().html(data);
			},
			complete: function (XMLHttpRequest, textStatus) {
				//re-initialize thickbox
				tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
				imgLoader = new Image();// preload image
				imgLoader.src = tb_pathToImage;
				$.graphDaysOnMarket(daysOnMarket.MLSStatsDOMs, $('#daysOnMarket'));
				$.graphPrices(listingPrices.MLSStatsPrices, $('#prices'));
				$.graphListedVersusSold(listedVersusSold.MLSStatsListedVersusSold, $('#listedVersusSold'));
			}
		}, function(){
			// rollback the operation
			$(self.highlightedDetailsTab).click();
		});
		return false;
	}

	$.showMisc = function(pressed){
		bot_tabSelect(pressed);
		//Todo: pull misc stuff from table in database and display...
		
		$('#botSec_cnt_summ').css("display", "none");
		$('#botSec_cnt_det').css("display", "none");
		$('#botSec_cnt_qualifier').css("display","none");
	}
	
	/** This function displays the mortgage calculator  legacy, now mortgage calculator become the part (tab) of the rpm qualifier
	  *
	  * @param pressed
	  * @param listprice price of the listing
	  * @param tax annual tax on the listing
	  * @param cFees monthly condo fees
	  */
	/*
	$.showMortgageCalc = function(pressed, listPrice, tax, cFees){
		bot_tabSelect(pressed);
		
		if(listingInfoAjaxRequest){
			listingInfoAjaxRequest.abort();
		}

		listingInfoAjaxRequest = $.ajax({
			type: "GET",
			url: "listings",
			data: "pathway=6&calculator=true&listPrice=" + listPrice + "&propTax=" + tax + "&conFees=" + cFees,
			error: function(data, error){
				alert("Error: showMortgageCalc(): " + error + " " + data);
			},
			success: function(data){
				try{
					$('#botSec_cnt_summ').css("display", "none");
					$('#botSec_cnt_det').css("display", "none");
					$("#botSec_cnt_other").empty().show();
					$("#botSec_cnt_other").html(data);
				}catch(ex){
					alert("Error: runSearch() [SUCCESS]: " + ex);
				}
			}
		});
	}
	*/
	

})();// close ListingDetails