$(document).ready(function() {

    setTimeout(function(){$('.register-form input#username.text').focus()}, 500);
    setTimeout(function(){$('#email').focus()}, 700);
    // for right side helpers (inline) functionality
    $('.arrow').click(function(){
        var dlg = $(this).parent().parent().next();
        if (dlg.css('display') == 'none') {
            dlg.slideDown();
        } else {
            dlg.slideUp();
        }
        return false;
    });

    $('#reset_button').click(function(){
             $("form")[0].reset();
    });

    if($('.pricing').length) { //whether we're on purchase subscription page
        $('#reset_button').click(function() {
                 $("form")[0].reset();
        });


        $(".plan_option input[type='radio']").click(function(){
            $('#subscribe-form').find('#product_id').val($(this).val());

            $(this).parent().parent().find('em').innerHtml = '$' +  $(this).attr('rel');
            $(this).parent().parent().find('em').text("$" + $(this).attr('rel'));
            $(this).parent().parent().find('em').next().text("/" + $(this).next().text());
        });

        $('.subscribe-for-selected-plan').click(function() {
			if(!parseInt($('#subscribe-form').find('#product_id').val())) {
			  alert(_('Please select subscription period'));
			  return false;
			}
            $('#subscribe-form').submit();
        });
    }

    $('.user_acc_edit').click(function(e) {
        $("#tabs").tabs();
        $.ajax({
            type: 'POST',
            url: '/account/get',
            success: function(data) {
                eval("data = " + data);

                if (data.success) {
                    $('#user-update-info form').find('[name=user_name]').val(data.user.first_name);
                    $('#user-update-info form').find('[name=user_surname]').val(data.user.last_name);
                    $('#user-update-info form').find('[name=user_company]').val(data.user.company);
                    $('#user-update-info form').find('[name=user_email]').val(data.user.email);

                    $('#tabs').tabs('destroy');
                    $('#tabs').tabs();
                    var thisDialogActions = {};
                    thisDialogActions[_('Cancel')] = function() {
                        $(this).dialog('destroy');
                    };
                    thisDialogActions[_('Save')] = function() {
                        if($('#tabs').tabs('option', 'selected') == 1) {
                            switcher = 'password';
                        } else {
                            switcher = 'info';
                        }
                        $.ajax({
                            type: 'POST',
                            url: '/account/' + switcher,
                            data: $('#form_' + switcher).serialize(),
                            success: function(data) {
                                eval("data = " + data);

                                if(data.success) {
                                    $('#upd_message_div').animate({opacity: '1.0'}, 1);
                                    $('#upd_message').text(data.message);
                                    $('#upd_message_div').animate({opacity: '1.0'}, 2500).animate({opacity: '0.0'}, 2500);

                                    window.location.reload();
                                } else {
                                    $('#upd_message_div').animate({opacity: '1.0'}, 1);
                                    $('#upd_message').text(data.message);
                                    $('#upd_message_div').animate({opacity: '1.0'}, 2500).animate({opacity: '0.0'}, 2500);
                                }
                            }
                        });
                    };
                    $("#project-notification-dialog").find('.ui-widget').remove();
                    $('#user-account-dialog').dialog({
                        width: 500,
                        modal: true,
                        buttons: thisDialogActions
                    }).dialog('open');
                }
            }
        });

        e.stopPropagation();
        return false;
    });

    /*
     * Account deletion
     */

    var step = 1;
    var is_data_exported = 0;

    $('form[name="export-data"]').submit(function(){
        window.is_data_exported  = 1;
    });



    $('#remove-account-button').click(function(){
        if (window.is_data_exported == 0) {

            var thisDialog_1 = {};
            thisDialog_1[_('No')] = function() {
                window.is_data_exported = 1;
                $(this).dialog('destroy');

                var thisDialog_2 = {};
                thisDialog_2[_('No')] = function() {
                    $(this).dialog('destroy');
                };
                thisDialog_2[_('Yes')] = function() {
                    $(this).parent('.ui-dialog').mask(_('Removing your account...'));
                    $(this).dialog('destroy');
                    $('#remove-account-button').click();
                    $('form[name="delete-account"]').submit();
                };
                $('#removing-confirmation-dialog').dialog({
                    width: 300,
                    modal: true,
                    buttons: thisDialog_2
                }).dialog('open');
            };
            thisDialog_1[_('Yes')] = function() {
                $(this).dialog('destroy');
                var buttons = $('.remove-account-dialog').siblings('.ui-dialog-buttonpane').children();
                $(buttons[2]).click();
                $('form[name="export-data"]').submit();
            };
            $('#export-data-confirmation-dialog').dialog({
                width: 300,
                modal: true,
                buttons: thisDialog_1
            }).dialog('open');
            return false;
        } else {
            var dialog_3 = {};
            dialog_3[_('No')] = function() {
                $(this).dialog('destroy');
            };
            dialog_3[_('Yes')] = function() {
                $(this).parent('.ui-dialog').mask(_('Removing your account...'));
                $(this).dialog('destroy');
                $('#remove-account-button').click();
                $('form[name="delete-account"]').submit();
            };
            $('#removing-confirmation-dialog').dialog({
                width: 300,
                modal: true,
                buttons: dialog_3
            }).dialog('open');
        }
        $(this).parents('.ui-dialog').unmask();
    });

    var dialogActions = {};

    dialogActions[_('Cancel')] = function() {
        $(this).dialog('destroy');
    };
    dialogActions[_('Next')] = function(e) {
        var buttons = $('.remove-account-dialog').siblings('.ui-dialog-buttonpane').children();
        $('.steps_form').hide();
        switch(step) {
            case 1: {
                    $('#step-2').show();
                    $(buttons[2]).show();
                    step++;
                }
                break;
            case 2: {
                    $(buttons[0]).show();
                    $(buttons[1]).hide();
                    $(buttons[2]).show();
                    $('#step-3').show();
                    step++;
                }
                break;
            case 3:{
                    $(this).dialog('close');
                }
                break;
        }
        e.stopPropagation();
        return false;
    };

    dialogActions[_('Back')] = function() {
        var buttons = $('.remove-account-dialog').siblings('.ui-dialog-buttonpane').children();
        $('.steps_form').hide();
        switch(step) {
            case 1:
                $(buttons[2]).hide();
                break;
            case 2:
                $(buttons[2]).hide();
                $('#step-1').show();
                step--;
                break;
            case 3:
                $('#step-2').show();
                $(buttons[1]).show();
                step--;
                break;
        }
    };

    $('#remove_account_button').click(function(e){
        $('.remove-account-dialog').dialog({
            width: 500,
            heght: 500,
            modal: true,
            buttons: dialogActions,
            open: function(event, ui) {
                    step = 1;
                    var buttons = $('.remove-account-dialog').siblings('.ui-dialog-buttonpane').children();
                    $(buttons[2]).hide();
                    $(buttons[1]).show();
                    $(buttons[0]).show();
                    $('.steps_form').hide();
                    $('#step-1').show();
                },
            close: function() {
                step = 1;
                var buttons = $('.remove-account-dialog').siblings('.ui-dialog-buttonpane').children();
                $(buttons[2]).hide();
                $('.steps_form').hide();
                $('#step-1').show();
            }
        }).dialog('open');

        e.stopPropagation();
        return false;
    });



});



