/*
	Widget Directions = Default is down and left.  To change, add these classes to the button:
	vertical-top
	horizontal-right
*/

QuickHit.UserActionWidget = QuickHit.UserActionWidget || {};
QuickHit.UserActionWidget.cardOverTimeout = null;
QuickHit.UserActionWidget.cardOutTimeout = null;
QuickHit.UserActionWidget.current = null;

QuickHit.UserActionWidget.init = function() {
	$(".bigusername").each(function(){
		if ( $(this).text() != $("#menu-username").text() ) {
			$(this).closest("tr").find("div.uaw-button").show();
		};
	});
	$(".uaw-trigger").live("mouseenter", function() {
		var teamId = $(this).parent().find(".presence").text();
		var coachName = $("#menu-username").text();
		$("body .uaw-button").each(function() {
			if (!$(this).hasClass("always-on")) {
				$(this).hide();
			};
		});
		var buttons = $("body .uaw-button:visible");
		if ( $(this).find(".coach-link").text() != $("#menu-username").text() && buttons.length < 1 ) {
			var button = $(this).find(".uaw-button");
			button.show();
		};
	}).live("mouseleave", function() {
		if ( !$(this).find(".uaw-button").hasClass("on") && !$(this).find(".uaw-button").hasClass("always-on") ) {
			$("body .uaw-button").hide();
		};
	});

	$(".uaw-button").live("click", function() {
		if (!$(".uawWrapper").length) {
			var target = $(this).parent();
			$(this).addClass("on").show();
			var vertical = "down";
			var horizontal = "left";
			if ($(this).hasClass("vertical-top")) {
				vertical = "up";
			}
			if ( $(this).hasClass("horizontal-right") ) {
				horizontal = "right";
			}
			QuickHit.UserActionWidget.cardOverTimeout = setTimeout(function() { QuickHit.UserActionWidget.createUaw(target, vertical, horizontal); }, 0);
		} else {
			clearTimeout(QuickHit.UserActionWidget.cardOutTimeout);
			clearTimeout(QuickHit.UserActionWidget.cardOverTimeout);
			QuickHit.UserActionWidget.cardOutTimeout = setTimeout(function() { QuickHit.UserActionWidget.removeUaw(); }, 0);
		}

	});

	$(".uawWrapper").live("mouseleave", function() {
		clearTimeout(QuickHit.UserActionWidget.cardOutTimeout);
		clearTimeout(QuickHit.UserActionWidget.cardOverTimeout);
		$("body .uaw-button").each(function() {
			if ( $(this).hasClass("always-on") ) {
				$(this).removeClass("on");
			} else {
				$(this).hide();
			}
		});
		QuickHit.UserActionWidget.cardOutTimeout = setTimeout(function() { QuickHit.UserActionWidget.removeUaw(); }, 0);
	});

	$("#header a").click(function() {
        clearTimeout(QuickHit.UserActionWidget.cardOutTimeout);
		clearTimeout(QuickHit.UserActionWidget.cardOverTimeout);
		QuickHit.UserActionWidget.cardOutTimeout = setTimeout(function() { QuickHit.UserActionWidget.removeUaw(); }, 0);
	});

    $("a", ".uawWrapper").live("mouseenter", function() {
        var menuItem = $(this);
        if (menuItem.hasClass("parent")) {
            menuItem.siblings("ul.child").show();
        }
        menuItem.parent().siblings().find("ul.child").hide();
    });
    
    $(".uaw-item-play", ".uawWrapper").live("click", function() {
        setTimeout(function() { QuickHit.UserActionWidget.removeUaw(); }, 250);
    });

//	$(".game-invite", ".uawWrapper").live("click", function() {
//        QuickHit.MessageCenter.outgoingInvite(QuickHit.UserActionWidget.current.find(".photo").text());
//        QuickHit.UserActionWidget.removeUaw();
//        return false;
//    });
    $(".chat", ".uawWrapper").live("click", function() {
		QuickHit.Chat.updateIcon(QuickHit.UserActionWidget.current.find(".photo").text(), "show");
        QuickHit.MessageCenter.outgoingUAWChat(QuickHit.UserActionWidget.current.find(".photo").text());
        QuickHit.UserActionWidget.removeUaw();
        return false;
    });
    $(".add-friend", ".uawWrapper").live("click", function() {
        QuickHit.MessageCenter.outgoingFriendInvite(QuickHit.UserActionWidget.current.find(".photo").text());
        QuickHit.UserActionWidget.removeUaw();
        return false;
    });
	$(".remove-friend", ".uawWrapper").live("click", function() {
        var coachName = QuickHit.UserActionWidget.current.find(".photo").text();
        if(confirm("Are you sure you want to remove " + coachName + " as a friend?")) {
			QuickHit.Friends.removeFriend(coachName);
        }
        return false;
    });
};

QuickHit.UserActionWidget.createUaw = function(source, vert, horiz) {
	clearTimeout(QuickHit.UserActionWidget.cardOverTimeout);
    clearTimeout(QuickHit.UserActionWidget.cardOutTimeout);
	var parentLine = source.find(".uaw-button");
    var teamId = source.find(".presence").text();
    if (teamId && teamId != QuickHit.CoachCard.teamId) {
        $(".uawWrapper").remove();
        $.ajax({
            url: "/team/uaw/" + teamId,
            dataType: 'json',
            cache: false,
            success: function(data, status) {
                var uaw = $(data.html).hide();
                $("body").append(uaw);
				var topAlign, leftAlign = 0;
				if (vert == "down") {
					topAlign = parentLine.offset().top + parentLine.height();
					uaw.css({ top: topAlign});
				} else if (vert == "up") {
					topAlign = parentLine.offset().top - uaw.outerHeight();
					uaw.css({ top: topAlign});
				}
				if (horiz == "left") {
					leftAlign = parentLine.offset().left;
					uaw.css({ left: leftAlign});
				} else if (horiz == "right") {
					rightAlign = (parentLine.offset().left - uaw.outerWidth(true) + parentLine.outerWidth(true) - 1);
					uaw.css({ left: rightAlign});
				}
				uaw.find(".uaw-item:last").addClass("last");
                uaw.show();
                QuickHit.UserActionWidget.current = uaw;
            }
        });
    }
};

QuickHit.UserActionWidget.removeUaw = function(){
    $(".uawWrapper").remove();
    QuickHit.UserActionWidget.current = null;
    clearTimeout(QuickHit.UserActionWidget.cardOverTimeout);
    clearTimeout(QuickHit.UserActionWidget.cardOutTimeout);
};
