/*
 * jQuery Sprite Navigation Plugin
 *
 * Copyright (c) 2009 Dennis Ridder (itengineering.nl)
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Thanks to Dave Shea (http://www.alistapart.com/d/sprites2/), whose history plugin I adapted ROUGHLY to the current version
 */
 
;(function($) {

	$.fn.spritenav = function(activeClass, hoverClass, hoverspeed, style) {
		generateMenuAnimations(this, activeClass, hoverClass, hoverspeed, style);
	}
	
	$.fn.removenav = function() {
		deleteMenuAnimations(this);
	}
	
	function deleteMenuAnimations(parent)
	{
		
	}

	function generateMenuAnimations(parent, activeClass, hoverClass, hoverspeed, style)
	{
		// overflow hidden - could help out with some style problems
		$(parent).css("overflow", "hidden");

		// children of parent
		$(parent).children("li").each(function() {
			var myClass = $(this).children("a").eq(0).attr("class");
			
			if (myClass != activeClass) {
				// turn on nav events for non-active element this loop identifies
				attachMenuEvents(this, parent, hoverClass, hoverspeed, style);
			}
		});
	}
	
	function attachMenuEvents(menuitem, parent, hoverClass, hoverSpeed, style)
	{
		var myClass = $(menuitem).children("a").eq(0).attr("class");
		
		// Actions that will be performed anyway, whatever style chosen
		$(menuitem).each(function() {
			el = $(this).children("a").eq(0);
			pos = el.offset();
			w = el.width(); // + parseInt(el.css("marginLeft"));
			h = el.height(); // + parseInt(el.css("marginTop"));
			elh = el.clone().insertAfter(el).attr("t", pos.top).attr("h", h).attr("w", w).attr("mt", el.css("marginTop")).css({ display: 'none', position: 'relative'}).addClass(hoverClass);
		});
		
		// Perform animations based on style choice
		switch (style)
		{
			case 'fadein':
				$(menuitem).hover(function() {
					var currentpos = $(this).children("a:first").offset();
					var el = $(this).children("a."+hoverClass).eq(0);

					$(this).children("a."+hoverClass).css({ 'z-index' : 84, 'position': 'absolute', 'left': currentpos.left, 'top': currentpos.top }).fadeIn(hoverSpeed);
					$(this).children("a:first").css('visibility', 'hidden');
				},
				function() {
					var el = $(this).children("a."+hoverClass);
					var toppos = parseInt(el.attr("h")) + parseInt(el.attr("t"));
					$(this).children("a."+hoverClass).fadeOut(hoverSpeed);
					$(this).children("a:first").stop().css('visibility', 'visible');
				});
				break;
			case 'slideright':
				$(menuitem).hover(function() {
					var currentpos = $(this).children("a:first").offset();
					var el = $(this).children("a."+hoverClass).eq(0);
					$(this).children("a."+hoverClass).dequeue().show().css({ 'z-index' : 84, 'position': 'absolute', 'left': currentpos.left, 'top': currentpos.top }).animate({ 'width': $(this).children("a:first").width(), 'top': currentpos.top }, hoverSpeed, "swing");
				},
				function() {
					var currentpos = $(this).children("a:first").offset();
					$(this).children("a."+hoverClass).dequeue().animate({ 'width': 0, 'top': currentpos.top }, hoverSpeed, "swing", function() { $(this).css("display", "none") });
				});
				break;
			case 'slideleft':
				$(menuitem).hover(function() {
					var currentpos = $(this).children("a:first").offset();
					var el = $(this).children("a."+hoverClass).eq(0);
					$(this).children("a."+hoverClass).dequeue().show().css({ 'width': 0, 'z-index' : 84, 'position': 'absolute', 'left': currentpos.left + $(this).children("a:first").width(), 'top': currentpos.top }).animate({ 'width': $(this).children("a:first").width(), 'left': currentpos.left }, hoverSpeed, "swing");
				},
				function() {
					var currentpos = $(this).children("a:first").offset();
					$(this).children("a."+hoverClass).dequeue().animate({ 'width': 0, 'top': currentpos.top, 'left': currentpos.left + $(this).children("a:first").width() }, hoverSpeed, "swing", function() { $(this).css("display", "none") });
				});
				break;
			case 'slidedown':
			default:
				$(menuitem).hover(function() {
					var currentpos = $(this).children("a:first").offset();
					var el = $(this).children("a."+hoverClass).eq(0);
					$(this).children("a."+hoverClass).stop().css({ 'z-index' : 84, 'position': 'absolute', 'height': 0, 'left': currentpos.left, 'top': currentpos.top + $(this).children("a:first").height() }).show().animate({ 'height': $(this).children("a:first").height(), 'top': currentpos.top }, hoverSpeed, "swing");
					$(this).children("a:first").css('visibility', 'hidden');
				},
				function() {
					var currentpos = $(this).children("a:first").offset();
					$(this).children("a."+hoverClass).animate({ 'height': 0, 'top': currentpos.top + $(this).children("a:first").height() }, hoverSpeed, "swing");
					$(this).children("a:first").stop().css('visibility', 'visible');
				});
				break;
		}
	}
})(jQuery);