/*
 * Custom Fade Tabs
 *
 * Copyright (c) 2008 Alexander Kislitsyn
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 */
(function($){

    // if the Ander scope is not availalable, add it
    $.ander = $.ander || {};

	$.fn.tabs = function(options) {
		var defaults = {
			speed: "normal",
			mainPanel: "#fragment0",
			panelClass: "panelClass"
		};
		var options = $.extend(defaults, options);
	    
		return this.each(function() {
			new $.ander.tabs(this, options);
		});
	};

    $.ander.tabs = function(element, options) {
	
		this.counter = 0; // Log counter
		this.loglevel = 0; // Log level
		
		this.options = options;
		
		this.menuitemClickAction = this.defaultMenuItemClickAction;
		this.menuitemMouseoverAction = this.defaultMenuItemMouseoverAction;
		this.menuitemMouseoutAction = this.defaultMenuItemMouseoutAction;

		this.panelMouseoverAction = this.defaultPanelMouseoverAction;
		this.panelMouseoutAction = this.defaultPanelMouseoutAction;
		
		this.pendingPanel = "";
		this.currentPanel = this.options.mainPanel;
		this.isAnimationRunning = false;
		
		this.initTabs(element);
	};
	
    // instance methods
    $.extend($.ander.tabs.prototype, {
		// Log function
        log: function(text, level) {
			if (typeof level == 'undefined') level = 0;
			if (this.loglevel > level) {
				var temp = $("#console").html();
				this.counter++;
				$("#console").html(this.counter + ". " + text + "<br/>\n" + temp); 
			}
        },

		initTabs: function(element) {
		
			var self = this;
			$("#items a", element).each(function() {
				self.initAnchor(this);
			});
			
			$(element).hover(
				function() {
					return self.panelMouseoverAction(self, this);
				},
				function() {
					return self.panelMouseoutAction(self, this);
				}
			);
		},
		
		// Init function, element should be A tag
		initAnchor: function(element) {

			// Retrieve panel ID
			var panelId = $(element).attr("href");

			// Add panel class
			// $(panelId).removeClass(this.options.panelClass).addClass(this.options.panelClass);
			$(panelId).css('position', 'absolute');

			// Show/hide panels
			if (panelId == this.options.mainPanel) {
				$(panelId).show();
				this.log("show: " + $(element).attr("href"));
			} else {
				$(panelId).hide();
				this.log("hide: " + $(element).attr("href"));
			}
			
			var self = this;
			// Click event handler
			$(element).click(
				function () { 
					return self.menuitemClickAction(self, this); 
				}
			);
			// Mouseover/out event handlers
			$(element).hover(
				function() {
					return self.menuitemMouseoverAction(self, this);
				},
				function() {
					return self.menuitemMouseoutAction(self, this);
				}
			);
			
		},
		
		// Init function, element should be A tag
		fadeAnimation: function(panelToShow) {
		
			this.isAnimationRunning = true;
			
			this.log("fade: " + this.currentPanel + " -> " + panelToShow, 5);
			
			var self = this;
			$(self.currentPanel).fadeOut(self.options.speed, function() {

				$(panelToShow).fadeIn(self.options.speed, function() {
				
					self.isAnimationRunning = false;
					
					if (self.pendingPanel != "") {
						if (self.pendingPanel == self.currentPanel) {
							self.pendingPanel = "";
						} else {
							self.fadeAnimation(self.pendingPanel);
						}
					}
				});
				self.currentPanel = panelToShow;
			});
		},
		
		/*
		 * Section with default event handlers
		 */
		
		defaultMenuItemClickAction : function(self, element) {
			element.blur();
			return false;
		},

		defaultMenuItemMouseoverAction : function(self, element) {
			var panelId = $(element).attr("href");
			if (self.isAnimationRunning) {
				self.pendingPanel = panelId;
			} else {
				if (panelId != self.currentPanel) self.fadeAnimation(panelId);
			}
			return false;
		},

		defaultMenuItemMouseoutAction : function(self, element) {
			return false;
		},

		defaultPanelMouseoverAction : function(self, element) {
			return false;
		},

		defaultPanelMouseoutAction : function(self, element) {
			if (self.isAnimationRunning) {
				self.pendingPanel = self.options.mainPanel;
				this.log("pendingPanel: " + self.options.mainPanel, 5);
			} else {
				if (self.currentPanel != self.options.mainPanel) self.fadeAnimation(self.options.mainPanel);
				this.log("fadeAnimation: ", 5);
			}
			return false;
		}
		
	});
	
})(jQuery);
