/*

	-- -- -- -- -- -- --
	css sprites 2
	nav behaviour

	http://www.alistapart.com/articles/sprites2
	-- -- -- -- -- -- --
	
*/

function generateSprites(parent, selectedPrefix, setActive, hoverSpeed, style) {
	// throw the parent object's class into a variable
	var parentClass = $(parent).attr("class");

	// start a loop that cycles through each of the li elements inside the parent element
	$(parent).children("li").each(function() {
		// create a few variables that we'll need during this function:
		// myClass = the class of the object we're currently inspecting
		// current = what the selected class should look like for the parent of the object we're currently inspecting
		var myClass = ($(this).attr("class"))
		var current = parent.substring(1) + " current-" + ($(this).attr("class"));

		// turn on nav events for element this loop identifies
		attachNavEvents(parent, myClass, setActive, hoverSpeed, style);
	
		// let's hide the CSS-defined background image, but only if this isn't the currently-selected item
		if (parentClass != current) {
			$(this).children("a").css({backgroundImage:"none"});
		}

	});
}

function AniMate(obj, type, speed, direction) {
    var w = $(obj).width();
    var h = $(obj).height();
    var o = $(obj).offset();
    var l = o.left;
    var t = o.top;
    var f = null;

    if (direction == 1) {
        $(obj).css({ display: 'none' });
    } else {
        $(obj).css({ display: '' });
        f = function() { $(this).remove(); };
    }

    if (type == "SlideU")
        $(obj).toggle('slide', { direction: "up" }, speed, f);

    if (type == "SlideD")
        $(obj).toggle('slide', { direction: "down" }, speed, f);

    if (type == "SlideL")
        $(obj).toggle('slide', { direction: "left" }, speed, f);

    if (type == "SlideR")
        $(obj).toggle('slide', { direction: "right" }, speed, f);

    if (type == "DropL")
        $(obj).toggle('drop', { direction: "left" }, speed, f);

    if (type == "DropR")
        $(obj).toggle('drop', { direction: "right" }, speed, f);

    if (type == "DropU")
        $(obj).toggle('drop', { direction: "up" }, speed, f);

    if (type == "DropD")
        $(obj).toggle('drop', { direction: "down" }, speed, f);

    if (type == "ClipV")
        $(obj).toggle('clip', { direction: "vertical" }, speed, f);

    if (type == "ClipH")
        $(obj).toggle('clip', { direction: "horizontal" }, speed, f);

    if (type == "BlindV")
        $(obj).toggle('blind', { direction: "vertical" }, speed, f);

    if (type == "BlindH") {
        $(obj).toggle('blind', { direction: "horizontal" }, speed, f);
    }

}


function attachNavEvents(parent, myClass, setActive, hoverSpeed, style) {
    $(parent + " ." + myClass).mouseover(function() {
        // create pseudo-link
        $(this).append('<div class="nav-' + myClass + '"></div>');
        // either slide or fade, depending on the style value
        if (style == "slide") {
            // slide down the pseudo-link
            $("div.nav-" + myClass).css({ display: "none" }).slideDown(hoverSpeed);
        } else if (style == "fade") {
            // fade in the pseudo-link
            $("div.nav-" + myClass).css({ display: "none" }).fadeIn(hoverSpeed);
        } else {
            AniMate($("div.nav-" + myClass), style, hoverSpeed, 1);
        }
    }).mouseout(function() {
        // either slide or fade, depending on the style value
        if (style == "slide") {
            // slide up & destroy pseudo-link
            $("div.nav-" + myClass).slideUp(hoverSpeed, function() {
                $(this).remove();
            });
        } else if (style == "fade") {
            // fade out & destroy pseudo-link
            $("div.nav-" + myClass).fadeOut(hoverSpeed, function() {
                $(this).remove();
            });
        } else {
            AniMate($("div.nav-" + myClass), style, hoverSpeed, 0);
        }
    });


	// we only want to check the mousedown/up events if the CSS exists for :active states
	// if so, let's apply our selective filtering to undo the events above
	if (setActive) {
		$(parent + " ." + myClass).mousedown(function() {
			$("div.nav-" + myClass).attr("class", "nav-" + myClass + "-click");
		}).mouseup(function() {
			$("div.nav-" + myClass + "-click").attr("class", "nav-" + myClass);
		});
	}
}

