/**
 * Feature Listing JQuery Plugin
 *	usage: used on any page with the feature listing widget
 *	dependencies: jQuery, WebsiteManager.js
 */
(function() {
	var featureListingAjaxRequest = null;
	var nextRun = null;

	/** This function stops and frees everything used by the featured listing widget
	  *
	  */
	$.finalizeFeatureListingWidget = function() {
		// prevent the script from running again
		if(this.nextRun != null)
			clearTimeout(this.nextRun);
		
		// clear the content (we do this becuase if we drag the feature listing out again we
		// want to run the $.initializeFeatureListingWidget that is in the content. This step
		// is only necessary for widgets that require javascript initialize functions to be run
		// after drag)
		$('#featureListingWidget').parent().children(".widgetHeader, .widgetContent").remove();
	}

	/** This function initializes the listings search page.
	  *
	  * @param identifier the SystemInfo.RPM_WIDGET_FEATURE_LISTING
	  */
	$.initializeFeatureListingWidget = function(identifier) {

		// register this widget with the widget manager (if necessary)
		if(typeof(websiteManager) != "undefined")
			websiteManager.widgetManager.removeWidgetCallbacks[identifier] = function(){$.finalizeFeatureListingWidget()};
		
		// get the feature listing
		$.getFeatureListing();
	}

	/** get Feature Listing
	  *
	  */
	$.getFeatureListing = function(){
		// get self
		var self = this;
		
		// abort any pending requests
		if(this.featureListingAjaxRequest){
			this.featureListingAjaxRequest.abort();
		}

		// grab the feature listing, display, then set timeout for 10 seconds
		this.featureListingAjaxRequest = $.ajax({
			type: "GET",
			url: "listings",
			data: "pathway=6&loadFeatureListing=true",
			dataType: "html",
			error: function(data, error){
				//alert("Error: featureListingWidget(): " + error + " " + data);
			},
			success: function(data){
				//$('.listingPhotoContainer').fadeOut("slow");
				$('#featureListingWidget').fadeOut("slow", function(){
					$(this).html(data);
					$('#featureListingWidget').fadeIn("slow");
				});
			},
			complete: function (XMLHttpRequest, textStatus) {
				self.nextRun = setTimeout("$.getFeatureListing()", 20000);
			}
		});
	}
	
})();