$(document).ready(function() {

    var clicktocall = {};
    clicktocall.msgs = {
        'waiting':'Click on \'call\' button to start dialing...',
        'dialing':'Dialing...',
        'unable_to_connect':'Unable to connect',
        'phone_not_valid':'Phone number is not valid'
    }
    clicktocall.npa = $('#npa');
    clicktocall.nnx = $('#nnx');
    clicktocall.line = $('#line');
    clicktocall.company_name = $('#company-call-name');
    clicktocall.notification_area = $('#call-result');
    clicktocall.notification_area.setMessage = function(msg) {
        clicktocall.notification_area.text(msg);
    }
    clicktocall.notification_area.clearMessage = function(msg) {
        clicktocall.notification_area.text('');
    }

    //vars
    clicktocall.companyId = 0;
    clicktocall.companyName = '';

    //validator
    clicktocall.validatePhone = function() {
        //issue a query
        var npa = clicktocall.npa.val();
        var nnx = clicktocall.nnx.val();
        var line = clicktocall.line.val();

        var phone = npa . concat(nnx) . concat(line);

        if (phone.length == 10) {
            return phone;
        }

        return false;
    }



    //set autotab
    clicktocall.npa.autotab({ target: 'nnx', format: 'numeric' });
    clicktocall.nnx.autotab({ target: 'line', format: 'numeric', previous: 'npa' });
    clicktocall.line.autotab({ previous: 'nnx', format: 'numeric' });


    $('#click-to-call-window').dialog({
        autoOpen: false,
        modal: true,
        title: 'Click to call',
        width: 420,
        height: 275,
        open: function(event, ui) {

            //set initial message
            clicktocall.notification_area.setMessage(
                clicktocall.msgs['waiting']
            );

            //set company helper
            clicktocall.company_name.text(clicktocall.companyName);

            //set window title
            $('#click-to-call-window').dialog( "option", "title", 'Call to ' +  clicktocall.companyName);

            var btnDelete = $('.ui-dialog-buttonpane').find('button:contains("Close")');
            //btnDelete.prepend('<span style="float:left; margin-top: 5px;" class="ui-icon ui-icon-trash"></span>');
            //btnDelete.height(btnDelete.height() - 10);
            btnDelete.css({
                'background':'#d2e8c4',
                'color':'#000'
            });

            //ga
            try {
                _gaq.push(['_trackEvent', 'phone2call', 'call_window_opened']);
            } catch(Exception) {}
        },
        buttons: {
            Call: function() {

                //validate phone
                var phone = clicktocall.validatePhone();
                if(!phone) {
                    clicktocall.notification_area.setMessage(
                        clicktocall.msgs['phone_not_valid']
                    );
                    return false;
                } else {
                    clicktocall.notification_area.setMessage(
                        clicktocall.msgs['dialing']
                    );
                }

                //initiate request if everything is ok
                $.post(free_quotes_url,
                {
                    company: clicktocall.companyId,
                    phone_to_call: phone
                },
                function(data){
                    if(data) {
                        clicktocall.notification_area.setMessage(data['status']);
                        var status = 'call_dialing';
                        var log_data = data['status'];
                    } else {
                        clicktocall.notification_area.setMessage(
                            clicktocall.msgs['unable_to_connect']
                        );
                        status = 'call_failure';
                        log_data = 'failure';                    }

                    //ga tracking
                    try {
                        _gaq.push(['_trackEvent', 'phone2call', status, log_data]);
                    } catch(Exception) {}
                },
                "json");
            },
            Close: function() {
                $(this).dialog('close');
            }
        }
    });

    window.clickToCall = function(cid, companyName){
        clicktocall.companyId = cid;
        clicktocall.companyName = companyName;
        $('#click-to-call-window').dialog('open');
        return false;
    }

    $('.green-button').button();
    /*
    .addClass('ui-widget ui-state-default ui-corner-all')
    .hover(
        function () {
            $(this).switchClass('ui-state-default', 'ui-state-hover', 1);
        },
        function () {
        $(this).switchClass('ui-state-hover', 'ui-state-default', 1);
    })
    .focus(function () {
        $(this).addClass("ui-state-focus");
    })
    .blur(function () {
        $(this).removeClass("ui-state-focus");
    });
    */

    $('#email-dialog-window').dialog({
        autoOpen: false,
        modal: true,
        title: 'Send Email To Company',
        width: 620,
        height: 550,
        open:
            function(event, ui) {
    },
    buttons: {
        Submit:
            function() {
                //checking values
                var companySlug = $("#companySlug").val();
                var name = $("#edName").val();
                var phone = $("#edPhone").val();
                var email = $("#edEmail").val();
                var message = $("#edMessage").val();
                if(name == "" || phone == "" || email == "" || message == "") {
                    $('#email-onsubmit-dialog_window').html("Please feel out all the required fields.");
                    $('#email-onsubmit-dialog_window').dialog('open');
                } else {
                    //resizing window to show progress
                    $(this).dialog({height: 600});
                    var dialogWindow = this;
                    $("#sendingEmailProgress").show();
                    //sending form
                    $.get("/email-to-company/" + companySlug,
                            {name: name,
                            phone: phone,
                            email: email,
                            message: message}, function(data) {
                                $('#email-onsubmit-dialog_window').html("Thank you for your message. The company manager will contact you soon.");
                                $('#email-onsubmit-dialog_window').dialog('open');
                                $(dialogWindow).dialog('close');
                                //invoking Google Analytics event tracking
                                try {
                                    pageTracker._trackEvent("Email To Company",
                                            "Email from " + name);
                                } catch(err) {}
                    }, "html");
                }
            },
        Close:
            function() {
                $(this).dialog('close');
            }
        }
    });

    $('#email-onsubmit-dialog_window').dialog({
        autoOpen: false,
        modal: true,
        title: 'Send Email To Company',
        width: 300,
        height: 200,
        open:
            function(event, ui) {
            },
        buttons: {
            Close:
                function() {
                    $(this).dialog('close');
                }
        }
    });
});

function showEmailDialog() {
    $('#email-dialog-window').dialog('open');
}

