/**
 * ListingsSearch JQuery Plugin
 *	usage: called for the listings search page
 */
(function() {
	var listingsSearchAjaxRequest = null;

	/** This function initializes the listings search page.
	  *
	  */
	$.listingsSearch = function() {
		var x = this;
		var parameters = document.location.search;
	
		// check which tab is selected; use the default if nothing selected
		//WTF IS THIS PART FOR?!
		/*
		var tabSelected = 0;
		if(parameters != ""){
			if(parameters.search("status") > 0){
				if(parameters.split("status")[1].search("1") > 0){
					if(parameters.split("listingsSection")[1].search("2") > 0){
						tabSelected = 2;
					}else{
						tabSelected = 1;
					}
				}
			}
		}
		*/
		// initialize tabs
		$('#listingsSearchActive, #listingsSearchSold, #listingsSearchBSSolds').click(function(){
			$('#listingsSearchActive, #listingsSearchSold, #listingsSearchBSSolds').parent().removeClass("select");
			$(this).parent().addClass("select");
			$('#listingsSearchActiveComponent, #listingsSearchSoldComponent, #listingsSearchBSSoldsComponent').each(function(){
				$(this).hide();
			});
			$('#'+this.id+'Component').show();
			
			x.getListingsSearchResults();
			return false;
		});

		// initialize onclick events for form elements
		$("#listingsSearchActiveForm, #listingsSearchSoldForm, #listingsSearchBSSoldsForm").find(":input").not(":hidden").each(function(){
			if($(this).attr('type') == 'radio'){
				$(this).click(function(){
					x.getListingsSearchResults();
				});
			}else{
				$(this).change(function(){
					x.getListingsSearchResults();
				});
			}
		});
	}

	/** This function refreshes the entire results section for the listings search and
	  * is called whenever there is a change in one of the input elements in the form.
	  *
	  * @param pageNumber number from whence to begin the results
	  */
	$.getListingsSearchResults = function(pageNumber){
		var x = this;
		$("#listingSearchLoadingMessage").show();
		if(listingsSearchAjaxRequest){
			listingsSearchAjaxRequest.abort();
		}
		
		// generate applicable paging variable
		var page = "";
		if(pageNumber != null)
			page = '&page='+pageNumber;

		// get URL data
		// check if we're only searching office listings (listingsView won't be present)
		var urlData;
		if($("input[name='listingsView']").size() == 0){
			urlData = "pathway=1&results=true&"+$('#listingsSearchActiveComponent:not(:hidden), #listingsSearchSoldComponent:not(:hidden), #listingsSearchBSSoldsComponent:not(:hidden)').find('form').formSerialize()+"&listingsView=1&"+page;
		}else{
		 	urlData = "pathway=1&results=true&"+$('#listingsSearchActiveComponent:not(:hidden), #listingsSearchSoldComponent:not(:hidden), #listingsSearchBSSoldsComponent:not(:hidden)').find('form').formSerialize()+page;
		}
		
		// make the AJAX call to retrieve listings
		listingsSearchAjaxRequest = $.ajax({
			type: "GET",
			url: "listings",
			data: urlData,
			dataType: "html",
			error: function(data, error){
				alert("Error: getResults(): " + error + " " + data);
			},
			success: function(data){
				$("#listingSearchLoadingMessage").hide();
				$("#listingSearchResults").html(data);
			},
			complete: function (XMLHttpRequest, textStatus) {
			//Code below not needed
			/*
				$(".paginationRight").children("a:not(:has(img))").each(function(){
					$(this).click(function(){
						alert("This Got Clicked!");
						x.getListingsSearchResults(this.id.substring(4, this.id.length));
						return false;
					});
				});
			*/
			}
		});
	}
	
	/** This function displays a map for a specific listing summary.
	  *
	  * @param secId section id for the listing summary
	  * @param lat lattitude for the map
	  * @param lng longitude for the map
	  */
	$.displayListingSummaryMap = function(secId, lat, lng){
		$("#right_view_content_"+secId).html("");
		$("#right_view_summary_"+secId+", #agentDisclaimer_"+secId).css("display", "none");
	
		// abort any pending requests
		if(listingsSearchAjaxRequest){
			listingsSearchAjaxRequest.abort();
		}
	
		$("#right_view_content_"+secId).html('<div id="map'+secId+'" style="width: 421px; height: 250px"></div>');
		$("#right_view_content_"+secId).css("display", "block");
	
		var map = new GMap2(document.getElementById("map"+secId));
		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();
		});
	}

	/** 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
	  */
	$.displayListingSummarySlideshow = function(listNum, secId){
		// abort any pending requests
		if(listingsSearchAjaxRequest){
			listingsSearchAjaxRequest.abort();
		}

		// show loading message
		$("#right_view_content_"+secId).html('<div align="center" style="vertical-align:middle;">Loading Slideshow...</div>');
		$("#right_view_summary_"+secId+", #agentDisclaimer_"+secId).css("display", "none");
		$("#right_view_content_"+secId).css("display", "block");

		//Get Search Options here & construct query string
		listingsSearchAjaxRequest = $.ajax({
			type: "GET",
			url: "listings?pathway=6&listingNumber="+listNum+"&slideshow=true&summary=true",
			dataType: "html",
			error: function(data, error){
				alert("Error: displayListingSummarySlideshow(): " + error + " " + data);
			},
			success: function(data){
				$("#right_view_content_"+secId).html(data);
			},
			complete: function (XMLHttpRequest, textStatus) {
				$("#listingSummarySlideshow_"+listNum).RPMSlideShow({});
			}
		});
	
	}

	/** This function displays the virtual tours for a specified listing
	  *
	  * @param listNum listing to grab slideshow for
	  * @param secId section id for the listing summary
	  */
	$.displayListingSummaryVTours = function(listNum, secId){
		if(listingsSearchAjaxRequest){
			listingsSearchAjaxRequest.abort();
		}

		// show loading message
		$("#right_view_content_"+secId).html('<div align="center" style="vertical-align:middle;">Loading Virtual Tour...</div>');
		$("#right_view_summary_"+secId+", #agentDisclaimer_"+secId).css("display", "none");
		$("#right_view_content_"+secId).css("display", "block");

		listingsSearchAjaxRequest = $.ajax({
			type: "GET",
			url: "listings?pathway=6&type=results&listingNumber="+listNum+"&vrTours=true&summary=true",
			dataType: "html",
			error: function(data, error){
				alert("Error: runSearch(): " + error + " " + data);
			},
			success: function(data){
				$("#right_view_content_"+secId).html(data);
			},
			complete: function (XMLHttpRequest, textStatus) {
				$("#listingSummaryVTours_"+listNum).RPMSlideShow({virtualTour:true});
			}
		});
	}
	
	$.displayListingSummaryMedia = function(listNum, secId){
		if(listingsSearchAjaxRequest){
			listingsSearchAjaxRequest.abort();
		}
		
		$("#right_view_content_"+secId).html('<div align="center" style="vertical-align:middle;">Loading...</div>');
		$("#right_view_summary_"+secId+", #agentDisclaimer_"+secId).css("display", "none");
		$("#right_view_content_"+secId).css("display", "block");
		
		listingsSearchAjaxRequest = $.ajax({
			type: "GET",
			url: "listings?pathway=6&type=results&listingNumber="+listNum+"&media=true&summary=true",
			dataType: "html",
			error: function(data, error){
				alert("Error: runSearch(): " + error + " " + data);
			},
			success: function(data){
				
				$("#right_view_content_"+secId).html(data);
			},
			complete: function (XMLHttpRequest, textStatus) {
				$("#listingSummaryVTours_"+listNum).RPMSlideShow({virtualTour:true});
			}
		});
	}



	/** This function displays the virtual tours for a specified listing
	  *
	  * @param secId section id for the listing summary
	  * @param listNum listing to grab slideshow for
	  */
	$.displayListingSummaryEmailForm = function(secId, listNum){
		// show loading message
		$("#right_view_content_"+secId).html('<div align="center" style="vertical-align:middle;">Loading Email Form...</div>');
		$("#right_view_summary_"+secId+", #agentDisclaimer_"+secId).css("display", "none");
		$("#right_view_content_"+secId).css("display", "block");

		// abort any pending requests
		if(listingsSearchAjaxRequest){
			listingsSearchAjaxRequest.abort();
		}

		listingsSearchAjaxRequest = $.ajax({
			type: "GET",
			url: "listings",
			data: "pathway=6&listingNumber="+listNum+"&listingEmailView=1&loadListingEmail=true",
			dataType: "html",
			error: function(data, error){
				alert("Error: listingDetails.displayListingEmail(): " + error + " " + data.statusText());
			},
			success: function(data){
				$("#right_view_content_"+secId).html(data);
			}
		});
		return false;
	}

	/** Shows the summary details for this listing
	  *
	  * @param secId section id for the listing summary
	  */
	$.displayListingSummaryDetails = function(secId){
		$("#right_view_content_"+secId).html("");
		$("#right_view_content_"+secId).css("display", "none");
		$("#right_view_summary_"+secId+", #agentDisclaimer_"+secId).css("display", "block");
	}

	/** This function sets a specified button to a 'selected' format.
	  *
	  * @param secId section id for the listing summary
	  * @param buttonId id of button to be changed
	  */
	$.formatListingSummaryTabs = function(secId, buttonId){
		for(var i=1;i<=7;i++){
			if(i == buttonId){
				$("#listing_btn_"+i+"_"+secId).addClass("select");
			}else{
				$("#listing_btn_"+i+"_"+secId).removeClass("select");
			}
		}
		return false;
	}
})();// close ListingsSearch