/**
 * RPM Slide Show Plugin
 *	overview: This plugin provides all functions associated with a slideshow for a given element (slideshow container)
 *
 *	usage: used for Personal Listings, Listing Details, Saved Listings
 *	dependencies: jQuery, thickbox
 *
 *	DEPRECATED
 */
(function() {
	
	/** This function binds to a slideshow container and implements the slideshow
	  *
	  * @param options for the slideshow
	  */
	$.fn.RPMSlideShow = function( options ) {
		return this.each( function(){
			var self = this;
			this.ajaxLoading = null;

			// initialize
			this.config = jQuery.extend({
				virtualTour: false
			}, options || {});
			
			// bind to the thumbnails
			if(self.config.virtualTour){
				$(this).find("ul").find("a").each(function(){
					// replace the hyperlink in anchor link
					var x = this;
					x.hyperlink = $(this).attr("href");
					$(this).attr("href", "#");

					$(this).click(function(){
						$(self).find(".tourFrame").attr("src", x.hyperlink);
						
						return false;
					});
				});
			}else{
				$(this).find("ul").find("a").click(function(){
					// cancel any pending requests
					if(self.ajaxLoading){
						self.ajaxLoading.abort();
					}

					// clear any applets if applicable
					$(self).find("#tour").clearApplet();
					
					// add appropriate highlights
					$(self).find("ul").find("img").removeClass("selected");
					$(this).find("img").addClass("selected");
	
					// show the image and description
					$(self).find('.photo').find("img").attr("src", $(this).find("img").attr("src"));
					$(self).find('.description').html($(this).find("img").attr("alt"));
	
					return false;
				});
			}

			// bind to the prev, next buttons
			$(this).find(".photoNavPrev, .photoNavNext").click(function(){
				self.prevNextType = $(this).attr("class");
				self.count = 0;
				$(self).find("li").each(function(){
					if($(this).find("img").attr("src") == $(self).find(".photo").children("img").attr("src") && self.count >= 0){
						if(self.prevNextType == "photoNavPrev"){
							if(self.count == 0)
								self.count = $(self).find("li").size() - 1;
							else
								self.count--;
						}else{
							if(self.count == ($(self).find("ul").children().size() - 1))
								self.count = 0;
							else
								self.count++;
						}
						
						// click the appropriate thumbnail, scroll the thumbnail container
						$(self).find("li").eq(self.count).find("a").click();
						$(self).find(".thumbnailColumnWrap").get(0).scrollTop = self.count*68 - 68;
						
						// set this to below 0 so that we don't go through the list and replace again
						self.count = -1;
					}
					if(self.count >= 0)
						self.count++;
				});
				return false;
			});

			// bind to the image description icon
			$(this).find(".photoOptionsDescription").click(function(){
				$(self).find('.description').slideToggle();
				return false;
			});

			// bind to the image enlarge icon
			$(this).find(".photoOptionsEnlarge").click(function(){
				tb_show($(self).find('.description').html(), $(self).find('.photo').find("img").attr("src"), false);
				return false;
			});		
		});
	}
})(jQuery);
