﻿$(function () {
    if ($("#blog-dropdown-cols > ul > li").size() == 0) {
        $("#blog-dropdown").remove();
    }

    $('#venues-dropdown-location div.button').hide();
    $('#venues-dropdown-location div.message').hide();

    $('#venues-dropdown-location fieldset .fields').focusout(function () {
        $('#venues-dropdown-location div.button').show();
        $('#venues-dropdown-location div.button').children().show();
        $('#venues-dropdown-location div.message').hide();
    }).keyup(function () {
        $('#venues-dropdown-location div.button').show();
        $('#venues-dropdown-location div.button').children().show();
        $('#venues-dropdown-location div.message').hide();
    }).keypress(function (evt) {
        var charCode = evt.charCode || evt.keyCode;
        if (charCode == 13) {
            return false;
        }
    });

    $('#venues-dropdown-location div.button a.cancel').click(function () {
        $(this).parent().hide();
        $('#venues-dropdown-location fieldset .fields input:first').val($('#venues-dropdown-location fieldset .fields input:first').attr('ovalue'));
        $('#venues-dropdown-location fieldset .fields select:first').val($('#venues-dropdown-location fieldset .fields select:first').attr('ovalue'));
        return false;
    });

    $('#venues-dropdown-location div.button a.change').click(function () {
        $('#venues-dropdown-location div.button a.cancel').hide();
        grayscale($(this));

        $('#venues-dropdown-location div.message').hide();
        var city = $('#venues-dropdown-location fieldset .fields input:first');
        var state = $('#venues-dropdown-location fieldset .fields select:first');

        if (city.val() != '' && city.val() != 'City' && !(state.val() === '' || state.val() === 'State')) {
            $.ajax({
                url: '/ven_srv_ddl.aspx?city=' + escape(city.val()) + '&state=' + escape(state.val()),
                dataType: "html",
                timeout: 5000,
                error: function () {
                    grayscale.reset($('#venues-dropdown-location div.button a.change'));
                    $('#venues-dropdown-location div.button').show();
                    $('#venues-dropdown-location div.message')
                            .css({ 'padding-top': '0px' })
                            .html("There was an error while processing your request.")
                            .fadeToggle();
                },
                success: function (data) {
                    grayscale.reset($('#venues-dropdown-location div.button a.change'));
                    $('#venues-dropdown-location div.button').hide();
                    city.attr('ovalue', city.val());
                    state.attr('ovalue', state.val());
                    $("#venues-dropdown-cols").html(data);
                    $('#venues-dropdown-location div.message')
                            .css({ 'padding-top': '25px' })
                            .html("<a href='#' class='disabledButton'>Location Updated</a>")
                            .fadeToggle();
                }
            });
        } else {
            grayscale.reset($('#venues-dropdown-location div.button a.change'));
            $('#venues-dropdown-location div.message')
                            .css({ 'padding-top': '0px' })
                            .html("<br /><div>Please provide a city and<br />a state.</div>")
                            .fadeToggle();
        }
        return false;
    });

    grayscale.prepare($('#venues-dropdown-location div.button a.change'));
});
