$(function()
    {
        $('pane_1').toggleClass('hidden_pane');

        var new_user = {};
        var xhr = undefined;

        $('#move_date').datepicker();

        $('#signup_panes').children('.pane').each(function(index, elem) {
            var id = $(elem).attr('id');

            //$('#progress_bar').append("<div id='node_" + id + "' class='progress_node'><a href='javascript:void(0);' class='progress_link' name='" + id + "'>" + $(elem).attr('name') + "</a></div>");

            $('#progress_bar').append("<div id='node_" + id + "' class='progress_node'><a href='javascript:void(0);' class='progress_link' name='" + id + "'>" + $(elem).attr('name') + "</a><div class='node_image'>&nbsp;</div></div>");

        /*
    // Make the progress node the same width
    var progress_bar_width = $('#progress_bar').width();
    var node_width = progress_bar_width / $('#progress_bar').children('.progress_node').size();
    $('#progress_bar').children('.progress_node').width(node_width - 10);
    */
        });

        $('.back_btn').click(function() {
            prev_pane.call($(this));
        });

        $('#progress_bar').children().first().addClass('in_progress');

        // Allow the user to return to previously completed panes
        $('.progress_link').click(function() {
            if ($(this).parent().hasClass('complete'))
            {
                // if the clicked pane was completed
                jump_back_to_pane($(this).attr('name'));
            }
            return false;
        });

        function next_pane()
        {
            $("#signup_record").show();
            var current_pane = $(this).parents('.pane');
            $(current_pane).addClass('hidden_pane');
            $(current_pane).next().removeClass('hidden_pane');

            var pane_id = $(current_pane).attr('id');
            $('#node_' + pane_id).addClass('complete');
            $('#node_' + pane_id).removeClass('in_progress');

            var next_pane_id = $(current_pane).next().attr('id');
            $('#node_' + next_pane_id).addClass('in_progress');
        }

        function prev_pane()
        {
            var current_pane = $(this).parents('.pane');
            $(current_pane).addClass('hidden_pane');
            $(current_pane).prev().removeClass('hidden_pane');

            var pane_id = $(current_pane).attr('id');
            $('#node_' + pane_id).removeClass('in_progress');

            var prev_pane_id = $(current_pane).prev().attr('id');
            $('#node_' + prev_pane_id).addClass('in_progress');
            $('#node_' + prev_pane_id).removeClass('complete');
            if(prev_pane_id == 'pane_1') {
                $("#signup_record").hide();
            }
        }

        function jump_back_to_pane(pane_id)
        {
            var new_pane = $('#' + pane_id);

            // Hide all sibling panes
            $(new_pane).siblings('.pane').each(function(index, pane) {
                $(pane).addClass('hidden_pane');
            });

            // modify the progress bar nodes of all of the panes after the current one
            $('#' + pane_id + " ~ div.pane").each(function(index, pane) {

                $('#node_' + $(pane).attr('id')).removeClass('in_progress');
                $('#node_' + $(pane).attr('id')).removeClass('complete');
            });
            $(new_pane).removeClass('hidden_pane');

            $('#node_' + pane_id).addClass('in_progress');
            $('#node_' + pane_id).removeClass('complete');
        }

        function get_location_preview()
        {
            $.ajax({
                url: get_location_preview_url + $('#dest_country').val() + '/' + $('#dest_state').val(),
                dataType: 'json',
                type: 'GET',
                success: function(data){
                    if(data){
                        if(data.resources != '0') {
                            $('#pane_4').prepend('<div id="resource_count" class="preview_count">' + data.resources + ' Resources found for <b>' + $('#destination_search').val() + '</b></div>');
                        }
                        if(data.vendors != '0') {
                            $('#pane_4').prepend('<div id="vendor_count" class="preview_count">' + data.vendors + ' Service Providers in <b>' + $('#destination_search').val() + '</b></div>');
                        }
                    }
                }
            });
        }


        $('#find_destination').click(function() {
            $('#destination_results').html("");
            if($('#destination_search').val() == "" || $('#destination_search').val() == "Where Are You Going?") {
                $('#destination_results').html("Please Enter A Destination");
            } else {
                if (0 != $('#dest_country').val().length)
                {
               
                    var find_dest = $(this);
                    get_location_preview();
                    $("#topboxnew").fadeOut(300, function(){
                        $("#errorMsg").html("");
                        next_pane.call(find_dest);
                        $(this).css("background-image","url(/mpt/img/frontpage_02.jpg)");
                        $('#destination').html("You're traveling to <b>" + $('#destination_search').val() + "</b>");
                        $(this).fadeIn(300);
                    });
               
                }
                else
                {
                    search_location.call($('#destination_search'), $(this));
                //$('#destination_results').html("Sorry we're not familair with that location");
                }
            }
        });


        $('#find_departure').click(function() {
            $("#departure_results").html("");
            if($("#departure_search").val() == "" || $("#departure_search").val() == "What city are you traveling from?") {
                $("#departure_results").html("Please Enter The City You Are Traveling From.")
            } else {
                if (0 != $('#depart_country').val().length)
                {
                    var find_dep = $(this);
                    $("#topboxnew").fadeOut(300, function(){
                        $("#errorMsg").html("");
                        next_pane.call(find_dep);
                        $(this).css("background-image","url(/mpt/img/frontpage_03.jpg)");
                        $('#departure').html(" from <b>" + $('#departure_search').val() + "</b>");
                        $(this).fadeIn(300);
                    });
                }
                else
                {
                    search_location.call($('#departure_search'), $(this));
                //$('#departure_results').html("Sorry we're not familair with that location");
                }
            }
        });

        $('#pet_type_01').change(function() {
            var type_id = $(this).val();
            $('#pet_breed_01').children('option').hide();
            $('#pet_breed_01').children('option').attr('disabled', 'disabled');
            $('#pet_breed_01').children('option[type="' + type_id + '"]').show();
            $('#pet_breed_01').children('option[type="' + type_id + '"]').removeAttr('disabled');
            var default_val = $('#pet_breed_01').children('option[type="' + type_id + '"]').first().val();
            $('#pet_breed_01').val(default_val);
        });

        $('#pet_type_01').change();

        $('#pet_type_02').change(function() {
            var type_id = $(this).val();
            $('#pet_breed_02').children('option').hide();
            $('#pet_breed_02').children('option').attr('disabled', 'disabled');
            $('#pet_breed_02').children('option[type="' + type_id + '"]').show();
            $('#pet_breed_02').children('option[type="' + type_id + '"]').removeAttr('disabled');
            var default_val = $('#pet_breed_02').children('option[type="' + type_id + '"]').first().val();
            $('#pet_breed_02').val(default_val);
        });

        $('#pet_type_02').change();

        $('#pet_type_03').change(function() {
            var type_id = $(this).val();
            $('#pet_breed_03').children('option').hide();
            $('#pet_breed_03').children('option').attr('disabled', 'disabled');
            $('#pet_breed_03').children('option[type="' + type_id + '"]').show();
            $('#pet_breed_03').children('option[type="' + type_id + '"]').removeAttr('disabled');
            var default_val = $('#pet_breed_01').children('option[type="' + type_id + '"]').first().val();
            $('#pet_breed_03').val(default_val);
        });

        $('#pet_type_03').change();

        $('#add_pet').click(function() {
            var nextpet = $('div.pet_info:hidden:first');
            if(nextpet.attr('id') == 'pet-3') {
                $('#add_pet').val('3 pets max. You can add more later.');
            }
            nextpet.fadeIn(400);
        });


        $('#register_pane').click(function() {
            next_pane.call(this);
            if($("#pet_name_01").val() == "Pet's Name") {
                $("#pet_name_01").val('');
            }
            if($("#pet_weight_01").val() == "Weight (lbs)") {
                $("#pet_weight_01").val('');
            }
            if($("#pet_name_02").val() == "Pet's Name") {
                $("#pet_name_02").val('');
            }
            if($("#pet_weight_02").val() == "Weight (lbs)") {
                $("#pet_weight_02").val('');
            }
            if($("#pet_name_03").val() == "Pet's Name") {
                $("#pet_name_03").val('');
            }
            if($("#pet_weight_03").val() == "Weight (lbs)") {
                $("#pet_weight_03").val('');
            }
            if($('#pet_name_01').val() != "") {
                var pet_html = " with <b>" + $('#pet_name_01').val() + "</b>";
            }
            if($('#pet_name_02').val() != "") {
                pet_html = pet_html + " and <b>" + $('#pet_name_02').val() + "</b>";
                if($('#pet_name_03').val() != "") {
                    pet_html = pet_html + " and <b>" + $('#pet_name_03').val() + "</b>";
                }
            }
            $('#pets').html(pet_html);
        });

        $("#sending_info").dialog({
            autoOpen:false
        });

        
        function ajax_signup()
        {
            $(this).attr('disabled', 'disabled');
            $(loading_img).insertAfter($(this).children('input[type="submit"]'));
            $(loading_img).show();
            $("#sending_info").dialog("open");

            var data = {};
            data.first_name = $('#first_name').val();
            data.last_name = $('#last_name').val();
            data.email = $('#email').val();
            data.password = $('#password').val();

            data.depart_city_id = $('#depart_city').val();
            data.depart_state_id = $('#depart_state').val();
            data.depart_country_id = $('#depart_country').val();

            data.dest_city_id = $('#dest_city').val();
            data.dest_state_id = $('#dest_state').val();
            data.dest_country_id = $('#dest_country').val();

            data.move_date = $('#move_date').val();

            var pets = Array();
            if (0 != $("#pet_name_01").val().length)
            {
                pets[0] = {};
                pets[0].name = $("#pet_name_01").val();
                pets[0].type = $("#pet_type_01").val();
                pets[0].breed = $("#pet_breed_01").val();
                pets[0].weight = $("#pet_weight_01").val();
            }
            if (0 != $("#pet_name_02").val().length)
            {
                pets[1] = {};
                pets[1].name = $("#pet_name_02").val();
                pets[1].type = $("#pet_type_02").val();
                pets[1].breed = $("#pet_breed_02").val();
                pets[1].weight = $("#pet_weight_02").val();
            }

            if (0 != $("#pet_name_03").val().length)
            {
                pets[2] = {};
                pets[2].name = $("#pet_name_03").val();
                pets[2].type = $("#pet_type_03").val();
                pets[2].breed = $("#pet_breed_03").val();
                pets[2].weight = $("#pet_weight_03").val();
            }

            data.pets = pets;

            xhr = $.ajax({
                url: signup_url
                ,
                data: data
                ,
                type: 'POST'
                ,
                dataType: 'json'
                ,
                success: function(results) {
                    if (results.success)
                    {
                        // redirect to new user's profile page
                        window.location.href = profile_url;
                    }
                    else
                    {
                        $(loading_img).hide();
                        alert(results.error);
                    }
                }
                ,
                complete: function(jqXHR, textStatus)
                {
                    
                },
                error: function(jqXHR, textStatus, errorThrown)
                {
                    if ('abort' != textStatus)
                    {
                        $(loading_img).hide();
                        alert(errorThrown);
                    }
                }
            });

            return false;
        }

        $('#signup').click(function() {
            //console.log('submit');
            });

        $('#fp_register').validate(
        {
            submitHandler: function(form) {
                ajax_signup.call(form);
                return false;
            },
            errorLabelContainer: "#errorMsg",
            wrapper: "span",
            messages: {
                dest_country: "You must provide the country you are traveling to",
                depart_country: "You must provide the country you are traveling from",
                move_date: "You must enter the date of your trip",
                first_name: "Please enter your First Name",
                last_name: "Please enter your Last Name",
                email: "Please enter a valid email address",
                password: {
                    required: "Please provide a password",
                    rangelength: jQuery.format("Enter at least {0} characters")
                },
                confirm_password: {
                    required: "Please confirm your password",
                    minlength: jQuery.format("Enter at least {0} characters"),
                    equalTo: "Invalid password confirmation"
                }
            }
        });
    });
