// jacks custom menu shizzle by stuart
// this function will be know as hoverDescription() 
// call it like this: 
// $(object).hoverDescription(arguments);

// first we open the jQuery selector class
(function($) { 
	// set 'self' to nothing (we'll need it later)
	var self = null;
	// now we declare the function
	$.fn.hoverDescription = function() {
		var args = arguments[0] || {}; 	
		return this.each(function() {
			new $.hoverDescription(this, args);
		});
	};
	
	// now set the default arguments
	$.hoverDescription = function (e, args) {
		
		// self now references hoverDescription just incase we go into a $(object-one).each(function(){everything in here refers to object-one, if we want to reference hoverDescription then we say self;});
		var self = this;
		
		// linkObject will be the thing we are looking at (in this instance it will be the anchor tag)
		// it's the bit that you reference: $(THIS BIT).hoverDescription();
		this.linkObject = $(e);
		
		// what about transitions? (default it to false - no transitions) if they are then what are they?
		this.transitionType = args.transitionType ? args.transitionType : false;
		// if we're using a transition then we need a speed! (default to 500ms)
		this.transitionSpeed = args.transitionSpeed ? args.transitionSpeed : 500;

		// now we set up the default arguments so that you can call the hoverDescription without any arguments :)
		//then we tell it to find all the a tags within the object we're referencing (remember linkObject?). Otherwise these can be referenced by the option aTags:'.classname', in the options.
		this.aTags = args.aTags ? $(args.aTags) : $("a",this.linkObject);
		
		// now we need to initiate the function and all the cache stuffs
		this.init();
	};

	// this sactions hold all stuffs and arguments for mini functions
	$.hoverDescription.prototype = {
		
		// declare and run that initiate function!
		init: function(){
			// self now references hoverDescription just incase we go into a $(object-one).each(function(){everything in here refers to object-one, if we want to reference hoverDescription then we say self;});
			var self = this;
			
			// ok lets do this!
			
			// first we create an array holder of all the links and their rel tags
			this.linkRelArray=[];
			// now we cycle through the aTags and pop the rel attribute in the link array.
			//alert(this.aTags);
			$(this.aTags).each(function(){
				self.linkRelArray.push($(this).attr('rel'));
				//alert($(this).attr('rel'));
				$(this).hover(
					// now put a function to listen to all the a tags for a hover moment
				  function () {
					self.displayObject(this,"show");
				  }, 
					// and one for when you hover off it...
				  function () {
					self.displayObject(this,"hide");
				  }
				 );
			});
			// that's the init done now lets move on!
		},
		
		// declare the displayObject function!
		displayObject: function(object,method){
			// ok so ready for the fancy 'if' object?
			switch(method){
				case "hide":
					// this is if you want to hide what you were just looking at.
					// ok this is pretty easy - just get the object's rel tag and display:none; (.hide();)
					var objectRel = $(object).attr("rel");
					
					// call the hide method with the possible transition!
					this.transitionObject('#'+objectRel,"hide");
					
					// unset objectRel
					objectRel = '';
					
					// done :)
				break;
				
				default:
					// this is basically anything else (just so there are no errors) but basically picks up on "show"
					// thing is it might lag so is best to hide EVERYTHING first then show the one we're looking at
					for (i=0; i<this.linkRelArray.length; i++){
						$('#'+this.linkRelArray[i]).hide();
					}
					
					// ok, now we show the one we want.
					var objectRel = $(object).attr("rel");
					
					// call the show method with the possible transition!
					this.transitionObject('#'+objectRel,"show");
					
					// unset objectRel
					objectRel = '';
					
					// done :)
				break;
				// ok now finally we need the transition hide / show function!
			}
		},
		
		transitionObject: function(objectRel,method){
			// ok, straight into the if statement! this time we're looking at this.transitionEnabled	
			switch(this.transitionType){
				// ok so first one we'll do fade - nice and easy
				case "fade":
					// ok are we showing or hiding? (default = hide)
					switch(method){
						case "show":
							// show with speed set in the argument list!
							$(objectRel).show(this.transitionSpeed);
						break;
						default:
							// hide with speed set in the argument list!
							$(objectRel).hide(this.transitionSpeed);
						break;
						// simples!
					}
				break;
				
				// ok so first one we'll do fade - nice and easy
				case "fade":
					// ok are we showing or hiding? (default = hide)
					switch(method){
						case "show":
							// show with speed set in the argument list!
							$(objectRel).show(this.transitionSpeed);
						break;
						default:
							// hide with speed set in the argument list!
							$(objectRel).hide(this.transitionSpeed);
						break;
						// simples!
					}
				break;
				
				// lets slide that bad boy!
				case "slide":
					// ok are we showing or hiding? (default = hide)
					switch(method){
						case "show":
							// show with speed set in the argument list!
							$(objectRel).show("slide", {direction: "down"},this.transitionSpeed);
						break;
						default:
							// hide with speed set in the argument list!
							$(objectRel).hide("slide", {direction: "up"},this.transitionSpeed);
						break;
						// simples!
					}
				break;
				
				default:
					// this will simply hide / show object if this.transitionEnabled is set to false / no transition is set!
					switch(method){
						case "show":
							$(objectRel).show();
						break;
						default:
							$(objectRel).hide();
						break;
						// simples!
					}
				break;
			}
		}
	}

	// unset self (just in case)
	var self = null;

// close it all! :) 
})(jQuery);
