/**
 * Pager for photo albums 
 * @param {Object} el DOMElement
 * @requires jQuery
 * @constructor
 */
NetR.PhotoPager = function(el){
	this.base_el = jQuery(el);
	this.sets    = [];
	this.nav     = {
		next: jQuery(".next", this.base_el),
		prev: jQuery(".prev", this.base_el)
	};

	this.current_set = parseInt(jQuery(".photos",this.base_el).attr("class").match(/set-([0-9]+)/)[1]);
	this.max         = parseInt(this.base_el.attr("class").match(/max-([0-9]+)/)[1]);
	
	this.photo_wrapper = jQuery(".photo-wrapper", this.base_el);
	this.photo_wrapper.width(this.photo_wrapper.width() * this.max + 10);

	this.sets[this.current_set] = jQuery(".photos", this.photo_wrapper);
	if(this.current_set > 1) {
		for (var i=1; i<this.current_set; i++) {
			this.sets[i] = jQuery("<div></div>").addClass("photos clearfix").addClass("set-"+(i));
			this.sets[this.current_set].before(this.sets[i]);
		}
		this.photo_wrapper.css("left", "-" + (this.base_el.width() * (this.current_set - 1)) + "px");
	}

	this._set_observers();
	this.update_navigation();
};
NetR.PhotoPager.prototype = {
	/**
	 * Set event observers
	 * @private
	 */
	_set_observers: function(){
		var self = this;
		this.nav.next.click(function(event){
			event.preventDefault();
			self.next();
		});
		this.nav.prev.click(function(event){
			event.preventDefault();
			self.prev();
		});
	},
	/**
	 * Get the next set of images
	 */
	next: function(){
		var self = this;
		var set  = this.current_set + 1;
		// Function to run after the set is fetched
		function after(init_tooltips){
			// Init tooltips for images
			if (init_tooltips) {
				NetRTooltips.init(self.sets[self.current_set + 1]);
			}
			// Animate to display next images
			self.photo_wrapper.animate(
				{left: "-" + (self.base_el.width() * self.current_set) + "px"},
				{
					duration:2000,
					complete: function(){
						self.current_set++;
						self.update_navigation();
					}
				});
		}
		if (this.current_set < this.max) {
			// Create the set if it's not already created
			if(!this.sets[set]){
				this.sets[set] = jQuery("<div></div>").addClass("photos clearfix").addClass("set-"+(set));
				this.sets[this.current_set].after(this.sets[set]);
				this.sets[set].load(this.nav.next.attr("rel") + "&set=" + (set), null, function(){ after(true); });
			} else {
				after();
			}
		}
	},
	/** 
	 * Get the previous set of images
	 */
	prev: function(){
		var self = this;
		var set  = this.current_set - 1;
		// Function to run after the set is fetched
		function after(init_tooltips){
			// Init tooltips for images
			if (init_tooltips) {
				NetRTooltips.init(self.sets[self.current_set - 1]);
			}
			// Animate to display previous images
			self.photo_wrapper.animate(
				{left: "-" + (self.base_el.width() * (self.current_set - 2)) + "px"},
				{
					duration:2000,
					complete: function(){
						self.current_set--;
						self.update_navigation();
					}
				});
		}
		if (this.current_set > 1) {
			// Create the set if it's not already created
			if(!this.sets[set]){
				this.sets[set] = jQuery("<div></div>").addClass("photos clearfix").addClass("set-"+(set));
				this.sets[this.current_set].before(this.sets[set]);
			}
			// Fetch content for set if it's not already fetched
			if (this.sets[set].children().length == 0){
				this.sets[set].load(this.nav.next.attr("rel") + "&set=" + (set), null, function(){ after(true); });
			} else {
				after();
			}
		}
	},
	/**
	 * Disable/enable navigation links
	 */
	update_navigation: function(){
		if (this.current_set == this.max) {
			this.nav.next.addClass("disabled");
		} else {
			this.nav.next.removeClass("disabled");
		}
		if (this.current_set == 1) {
			this.nav.prev.addClass("disabled");
		} else {
			this.nav.prev.removeClass("disabled");
		}
	}
};

jQuery(document).ready(function() {
	pp = new NetR.PhotoPager(jQuery(".photo-pager"))
});
