﻿$(document).ready(function() {

    var myMap = document.getElementById('myMap');
    var HomeMap = document.getElementById('HomeMap');

    if (null != myMap || null != HomeMap) {
        GetMap();
    }

    var txtCourseCity = $("#txtCourseCity");
    var dThinking = $("#dThinking");
    var ContactResponse = $("#ContactResponse");

    if (null != ContactResponse) {
        ContactResponse.hide();
    }

    if (null != dThinking) {
        dThinking.hide();
    }

    $('#dReviewPosted').hide();
    //    $("Exception").hide();

    $("input.PhoneMask").mask("(999) 999-9999");
    $("input.ZipCodeMask").mask("99999");
    $('.error').hide();


    $("#txtCourseCity").autocomplete($('#frmCourseSearch').attr('rooturl') + '/CityAutoComplete.ashx', {
        extraParams: {
            state: function() { return $("#ddlState").val(); }
        }
    });


    $("#txtCourseName").autocomplete($('#frmCourseSearch').attr('rooturl') + '/CourseAutoComplete.ashx');

    $("#frmCourseSearch").validate();
    $("#ContactForm").validate({ submitHandler: function(form) {


        var value = $('#txtFirstName').val();

        $(form).ajaxForm({
            url: "ContactHandler.ashx",
            success: function(result) {
                dThinking.hide();
                ContactResponse.show();
                alert(result);
            }
        });
        return false;
    }
    });

    if (null == $('#txtFirstName')[0]) {
        $('#txtCourseName').focus();
    } else {
        $('#txtFirstName').focus();
    }

    //http://greatwebguy.com/programming/dom/default-html-button-submit-on-enter-with-jquery/
    $("form input").keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('button[type=submit] .default').click();
            return false;
        } else {
            return true;
        }
    });

    if (null != $("#stars-wrapper1")) {

        $("#stars-wrapper1").stars({
            cancelShow: false,
            oneVoteOnly: true,
            captionEl: $("#stars-cap"),
            callback: function(ui, type, value) {
                $.getJSON("RateCourse.ashx", { rate: value, BookId: $('#CourseId').val() }, function(json) {
                    $("#fake-stars-on").width(Math.round($("#fake-stars-off").width() / ui.options.items * parseFloat(json.avg)));
                    $("#fake-stars-cap").text(json.avg + " (" + json.votes + ")");
                });
            }
        });
    }

    $('#fReview').submit(function() {
        $("#dMakeReviewForm").hide();
        $("#dThinking").show();
        // submit the form
        $(this).ajaxSubmit({
            url: $('#fReview').attr('action'),
            success: function(result) {
                $("#dThinking").hide();
                $("#dReviewPosted").innerHTML = result;
                $("#dReviewPosted").show();
            }
        });

        // return false to prevent normal browser submit and page navigation 
        return false;
    });

    $('#usernameLoading').hide();
    $('#txtUsername').blur(function() {
    $('#usernameLoading').show();
        $.post("checkusername.ashx", {
            username: $('#txtUsername').val()
        }, function(response) {
        $('#usernameResult').fadeOut();
            setTimeout("finishAjax('usernameResult', '" + escape(response) + "')", 400);
        });
        return false;
    });

    $("#dLoginError").hide();

    $("#LoginForm").validate({ submitHandler: function(form) {

        $("#AnonymousView").hide();
        $("#dThinking").show();
        $("#dLoginError").hide();

        $(form).ajaxSubmit({ type: "POST",
            url: "AuthenticateUser.ashx",
            success: function(result) {
                $("#dThinking").hide();

                if (result == 'Success') {
                    window.location = "Profile.aspx";
                } else {
                    $("#dLoginError").show();
                    $("#AnonymousView").show();
                }
            }
        });
        return false;
    }
    });


    $("#dRegisterError").hide();

    $("#RegisterForm").validate({ submitHandler: function(form) {

        $("#AnonymousView").hide();
        $("#dThinking").show();
        $("#dRegisterError").hide();

        $(form).ajaxSubmit({ type: "POST",
            url: "CreateUser.ashx",
            success: function(result) {
                $("#dThinking").hide();

                if (result == 'Success') {
                    window.location = "Profile.aspx";
                } else {
                    $("#dRegisterError").show();
                    $("#AnonymousView").show();
                }
            }
        });
        return false;
    }
    });


});


//function to swap backgrounds
function bgpos2(val, which) {
    var dwhich = $(which)
    if (dwhich.attr("src") == "star.gif") {
        dwhich.attr("src", "star_blue.gif");
        dwhich.prevAll().attr("src", "star_blue.gif");
    }
    else {
        dwhich.attr("src", "star.gif");
        dwhich.nextAll().attr("src", "star.gif");
    }
}


function finishAjax(id, response) {
    $('#usernameLoading').hide();
    $('#' + id).html(unescape(response));
    $('#' + id).fadeIn();
} //finishAjax
