$(document).ready(function() {
    $("#nav-one li").hover(
    function() {
        $("ul", this).show();
        $("img", this).attr("src", '/images/nav_arrow_over.png');
    },
    function() {
        if ($(this, this).attr('class') != "active") {

            $("img", this).attr("src", '/images/nav_arrow.png');
        }
    }
    );

    jQuery.preloadImages = function()
    {
        for (var i = 0; i < arguments.length; i++)
        {
            jQuery("<img>").attr("src", arguments[i]);
        }
    }

    $.preloadImages("/images/nav_arrow_over.png", "/images/nav_arrow.png");

    if ($('#loginsubmit').length > 0) {
        var clicked = false;
        if (!clicked) {
            $("#username").click(function() {
                $("#username").val('');
                clicked = true;
            });
        }
        $("#loginsubmit").click(function() {
            $("#loginform").submit();
        });

    }

    if ($('#searchbox').length > 0) {
        var clicked = false;
        if (!clicked) {
            $("#searchbox").click(function() {
                $("#searchbox").val('');
                clicked = true;
            });
        }
        $("#searchbutton").click(function() {
            $("#searchform").submit();
        });

    }



    // profile page
    if ($('#profileform').length > 0) {
        // // select all desired input fields and attach tooltips to them
        // $("#profileform :input").tooltip({
        //
        //     // place tooltip on the right edge
        //     position: ['center', 'right'],
        //
        //     // a little tweaking of the position
        //     offset: [-2, 10],
        //
        //     // use a simple show/hide effect
        //     effect: 'toggle',
        //
        //     // custom opacity setting
        //     opacity: 0.7
        // });	
        $("#submit_saveprofile").click(function() {
            $("#profileform").submit();
        });

        if ($('#agegroup').length > 0) {
            $('#agegroup').selectbox();
        }

    }

    // challenge page
    if ($('#friend_username').length > 0) {
        var challclicked = false;
        if (!challclicked) {
            $("#friend_username").click(function() {
                $("#friend_username").val('');
				
                $("#cby_friend").attr('checked', true);
                challclicked = true;
				if(emailclicked) {
	                $("#cby_email_msg").toggle('fast');
		              emailclicked = false;
				}
            });

        }
        var emailclicked = false;
        if (!emailclicked) {
            $("#friend_email").click(function() {
                $("#cby_email").attr('checked', true);

                $("#friend_email").val('');
                $("#friend_email").focus();
			
				if(!emailclicked) {
			        $("#cby_email_msg").toggle('fast');
				}
				$("#cby_email_msg_txt").click(function() {
					$("#cby_email_msg_txt").val('');
				});
                emailclicked = true;
            });
        }

        var randomclicked = false;
        if (!randomclicked) {
            $("#cby_random").click(function() {
                randomclicked = true;
				if(emailclicked) {

	                $("#cby_email_msg").toggle('fast');
	                emailclicked = false;
				}
            });

        }

        $("#friend_username").blur(function() {
            if ($("#friend_username").val() == '') {
                $("#friend_username").val('enter a friends username...');
            }
        });


        $("#friend_email").blur(function() {
            if ($("#friend_email").val() == '') {
                $("#friend_email").val('enter a friends Email...');

            }
        });

        $("#challengesubmit").click(function() {
            $("#friendchallenge").submit();
        });


        $('.challenge_by').click(function() {
            $('input[type=radio]:eq(0)', this).attr({
                checked: true
            })
        });

        $('#difficulty').selectbox();
        $('#categoryId').selectbox();

        $(function() {
            $("#friend_username").autocomplete("/challenge/suggest",
            {
                minChars: 2,
                matchSubset: 1,
                matchContains: 1,
                cacheLength: 10,
                onItemSelect: selectItem,
                selectOnly: 1,
                elementID: 'friend_username'
            }
            );

        });
    }

    if ($('#category').length > 0) {
        $('#category').selectbox();
    }


    $(function() {
        $('.choice').hover(function() {
            $(this).addClass('choice-hover');
        },
        function() {
            $(this).removeClass('choice-hover');
        });
    });

    $(function() {
        $('.choice').click(function() {
            $('input[type=radio]:eq(0)', this).attr({
                checked: true
            })
        });
    });


});

$.fn.hoverClass = function(c) {
    return this.each(function() {
        $(this).hover(
        function() {
            $(this).addClass(c);
        },
        function() {
            $(this).removeClass(c);
        }
        );
    });
};


function selectItem(li, elementID) {
    $("#" + elementID).val(0);
    var setVal = (li.extra) ? li.extra[0] : 0;
    $("#" + elementID).val(setVal);
}

