﻿function showChallengeSlow(unit){
    $(unit).removeClass("no-hover").addClass("hover");
    $(" > div.responsetext", unit).fadeIn('slow', function () {
        var $parent = $(this).parent();
        if (!$parent.hasClass("hover")) 
            hideChallenge($parent);
    });
}

function showChallenge(unit) {
    $(unit).removeClass("no-hover").addClass("hover");
    $(" > div.responsetext", unit).show();
}

function hideChallenge(unit) {
    $(unit).removeClass("hover").addClass("no-hover");
    $(" > div.responsetext", unit).hide();
}

$.fn.challenges = function (index) {
	
	var indexIsValid = (index > -1 && $(this).children().length);

    var responsetextJ = $(" div.responsetext", this);
    responsetextJ.hide();

    $(this).mouseout(function () {
        hideChallenge($(" > li", this));
		if(indexIsValid)
			showChallengeSlow($(" > li:eq(" + index + ")", this));
    });
    showChallenge($(" > li:first", this));
    return this.children().each(function () {
        $(this).mouseover(function () {
            hideChallenge($(" > li", $(this).parent()));
            showChallengeSlow(this);
        });
    });

    $(" > li:first", this).addClass("hover");
}
