QuickHit.User = {};

$(document).ready(function() {
    QuickHit.User.init();
});

QuickHit.User.init = function() {
    $("#login-submit").click(function() {
        $("#loginForm").submit();
        return false;
    });
    $("#j_username, #j_password, #_spring_security_remember_me", "#loginForm").live("keydown", function(e) {
        var key = e.keyCode || e.which;
        if(key == 13 && String($("#j_username").val()).length && String($("#j_password").val()).length) {
            $("#loginForm").submit();
            return false;
        }
    });

    $("#change-avatar, #avatar-cancel, #avatar-upload-window .close").live("click", function() {
        $("#avatar-upload-window").toggle();
        return false;
    });

    if (typeof($.validate) != "undefined") {
        $("#edit-user-form", "#edit-user").validate({
            validClass: "valid",
            success: "valid",
            rules: {
                email: {
                    required: true,
                    email: true,
                    minlength: 5,
                    maxlength: 255,
                    remote: {
                        url: "/registration/isEmailValid",
                        dateType: "json",
                        data: {
                            email: function() {
                                return $("#email").val();
                            }
                        }
                    }
                }
            },
            messages: {
                email: {
                    remote: "Email is either in use or barred."
                }
            }
        });

        $("#edit-password-form", "#edit-user").validate({
            validClass: "valid",
            success: "valid",
            rules: {
                passwd: {
                    required: true,
                    minlength: 3,
                    maxlength: 64
                },
                repasswd: {
                    required: true,
                    minlength: 3,
                    maxlength: 64,
                    equalTo: "#passwd"
                }
            },
            messages: {
                passwd: {
                    minlength: "Must be between 3 and 64 characters.",
                    maxlength: "Must be between 3 and 64 characters."
                },
                repasswd: {
                    minlength: "Must be between 3 and 64 characters.",
                    maxlength: "Must be between 3 and 64 characters.",
                    equalTo: "Passwords must match."
                }
            }
        });
    }
};

QuickHit.User.teamLogo = function() {
	var url = "";
	if (QuickHit.CoachCard.logoPng.match(/^NFL_/)) {
		url = "http://cdn.quickhit.com/update1patch1/assets/logo-nav-bar/dark-background/" + QuickHit.CoachCard.logoPng;
		//$("#team-colors").remove();
	} else {
		url = "http://cdn.quickhit.com/update1patch1/assets/logo-nav-bar/" + QuickHit.CoachCard.primaryColorHex + "/" + QuickHit.CoachCard.secondaryColorHex + "/" + QuickHit.CoachCard.logoPng;
		$("#team-colors").css("display","block");
	}
	var divForLogo = "<div id='source-logo'></div>";
	$("#team-logo").prepend( divForLogo );
	$("#source-logo").css("background-image", 'url("' + url + '")' );
};

QuickHit.User.TeamEdit = function() {
    var onbeforeunload = function() {
        return "If you navigate away now, you will lose any pending changes you have made!";
    };

    var change = function() {
        window.onbeforeunload = onbeforeunload
    };

    var cancel = function() {
        window.onbeforeunload = null;
        window.location = "/coach/" + QuickHit.CoachCard.coachName + "/settings";
    };

    var submit = function() {
        window.onbeforeunload = null;
        window.location = "/coach/" + QuickHit.CoachCard.coachName + "/teamDetails";
    };
	
	var link = function(destination) {
		switch(destination){
			default:
				break;
				
			case "logoStore":
				window.location = "/store/logos";
				break;
		}
	};

    return {
        change: change,
        cancel: cancel,
        submit: submit,
		link: link
    }
}();

QuickHit.User.friends = (function() {
	$(".current-filter, .arrow-down").unbind("click").toggle(function() {
		$(this).parent().addClass("hover");
	}, function() {
		$(this).parent().removeClass("hover");
	});

	var getFriends = function() {
		var url = $(this).attr("href");
		$.ajax({
			url: url,
			dataType: "json",
			cache: false,
			success: function(data, status, xhr) {
				//console.log(data);
				$(".friends-page-content").empty();
				$(".friends-page-content").append(data.html);
			}
		});
		return false;
	};

	$("a.friends-filter-link").live("click",getFriends);
}());
