/*
 * jQuery Fast2 Image Player plugin
 *
 * Copyright (c) 2009 Fast2 AB (http://fast2.se)
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 */
;(function($) {

	var version = "0.2";
	
	f2 = {};
	f2.imagePlayer = {
	
		id : null,
		imageList : null,
		currentIndex : 0,
		containerRef : function () { return this.id + "Container" },
		imagePrefix : "Image-",
		imageContainer : "imageContainer", 
		
		text : {
			prev : "&#171; |",
			next : "| &#187;",
			prevtitle : "F&#246;reg&#229;nde",
			nexttitle : "N&#228;sta",
			current : "Bild {1} av {2}"
		},
		
		init : function (id, settings) {
			// Make sure everything is in orde
			if ($("#" + id).length < 0 && !$.isArray(settings.imageList)) {
				return false;
			}
		
			this.id = id;
			this.imageList = settings.imageList;
			
			// Add a wraping container
			$("#" + this.id).wrap("<div id='" + this.containerRef() + "' class='f2ImageSlider'><div class='" + this.imageContainer + "'></div></div>");
			
			// Add pagination
			$("#" + this.containerRef()).append("<div class='pagination'><span class='prev' title='" + this.text.prevtitle + "'>" + this.text.prev + "</span><span class='current'>" + this.getCurrentText() + "</span><span class='next' title='" + this.text.nexttitle + "'>" + this.text.next + "</span></div>");
			
			// Calculate current index
			for (var i = 0; i < this.imageList.length; i++) {
				if (this.imageList[i]  == $("#" + this.id).attr("src")) {
					this.currentIndex = i; 
					break;
				}
			}
			
			// Set image id
			$("#" + this.id).attr("id", this.id + this.imagePrefix + this.currentIndex);
			
			// Add event handlers
			that = this;
			$(".prev","#" + this.containerRef()).click(function () {
				that.changeImage(that.currentIndex - 1);
			});
			$(".next, .imageContainer","#" + this.containerRef()).click(function () {
				that.changeImage(that.currentIndex + 1);
			});
			
			// Preload previus and next image
			this.preload(this.currentIndex);
		},
		
		getCurrentText : function () {
			return this.text.current.replace("{1}", this.currentIndex+1).replace("{2}", this.imageList.length);
		},
		
		preload : function (index) {
			var previusIndex = (index - 1 < 0 ? this.imageList.length -1 : index - 1); 
			var nextIndex = (index + 1 > this.imageList.length - 1 ? 0 : index + 1);
			
			if ($("#" + this.id + this.imagePrefix + previusIndex).length == 0) {
				// Load previus image
				$("." + this.imageContainer ,"#" + this.containerRef()).append("<img src='" + this.imageList[previusIndex] + "' id='" + this.id + this.imagePrefix + previusIndex + "' style='display:none;' />");
			}
			if ($("#" + this.id + this.imagePrefix + nextIndex).length == 0) {
				// Load next image
				$("." + this.imageContainer ,"#" + this.containerRef() ).append("<img src='" + this.imageList[nextIndex] + "' id='" + this.id + this.imagePrefix + nextIndex + "' style='display:none;'  />");
			}
		},
		
		changeImage : function (nextIndex) {
			var indexToDisplay = (nextIndex > this.imageList.length - 1 ? 0 : (nextIndex < 0 ? this.imageList.length - 1 : nextIndex));
			// Hide current image
			$("#" + this.id + this.imagePrefix + this.currentIndex).hide();
			// Show next picture
			$("#" + this.id + this.imagePrefix + indexToDisplay).show();
			// Set the current index
			this.currentIndex = indexToDisplay;
			// Update Text
			$(".current","#" + this.containerRef() ).text(this.getCurrentText());
			// Preload images
			this.preload(indexToDisplay);
		}
	};
		
	$.fn.f2ImagePlayer = function(settings) {
		f2.imagePlayer.init(this.attr("id"), settings);
		
		return this;
	};
})(jQuery);
