/*jslint nomen: false, debug: true, onevar: false */
/*global $, jQuery, _gaq, document, console, window, Smooth_Reload */

function validEmail(src) {
    var emailReg = "^[\\s]*[\\w-_.]*[\\w-_.]\\@[\\w].+[\\w]+[\\w][\\s]*$";
    var regex = new RegExp(emailReg);
    return regex.test(src);
}

function closeSigninDialog() {
    $('#dialogSignin').dialog('close');
    $('.hgm__iframe_ad').show();
}

function closeSignupDialog() {
    $('#dialogSignup').dialog('close');
    $('.hgm__iframe_ad').show();
}

function nativeSignout() {
    hgm.depends(['cookie']);
    jQuery.cookie("hgm__user", null, {
        expires: -1,
        path:'/'
    });
    Smooth_Reload.lout();
    return true;
}


hgm.processMemberResponse = function(data) {
    if(data.success) {
        closeSigninDialog();
        closeSignupDialog();
        $('#dialogSignup').remove();
        $('#dialogSignin').remove();
        if('undefined' !== typeof Smooth_Reload) {
            Smooth_Reload.lin();
        }
    } else {
        $('#dialogSignin .errorMsg').html(data.error);
        for(var error in data.errors) {
            $('#dialogSignupForm').find('#' + error).html(data.errors[error]);
        }
    }
};

hgm.initMemberForms = function () {
    hgm.form($('#dialogSigninForm'), {
        submitHandler : hgm.processMemberResponse,
        messages: {
            username: { required: 'Username is required' },
            password: { required: 'Password is required' }
        }
    });
    hgm.form($('#dialogSignupForm'), {
        submitHandler : hgm.processMemberResponse,
        rules: {
            username: {minlength: 4},
            password: {minlength: 6}
        },
        messages: {
            email: { email: 'Invalid email address!' },
            username: { minlength: 'Username is too short. It needs to be at least 4 characters long' },
            password: {
                minlength: 'Password is too short. It needs to be at least 6 characters long',
                required: 'Password is required'
            },
            password2: {
                minlength: 'Password is too short. It needs to be at least 6 characters long',
                required: 'Password is required'
            }
        }
    });

};
checkOrRequestPermissions = function () {
    closeSigninDialog();
    closeSignupDialog();
};

hgm.openMembersDialog = function (dialog) {
    var dialogName = 'dialog' + dialog.replace(/^(.)|\s(.)/g, function ($1) {
        return $1.toUpperCase();
    });
    $('.hgm__iframe_ad').hide();
    if(!hgm.memberDialogs) {
        hgm.memberDialogs = [];
    }
    if ($('#dialogSignin').length == 0) {
        $.get("/blocks/member-dialogs/", {'mode' : 'local', 'dialogs' : dialog},
            function (responseText, textStatus, req) {
                // setup our flag that we have a dialog
                hgm.memberDialogs[dialogName] = $('#' + dialogName);
                // write the dialog to the dom
                $("#dialog-container").html(responseText);
                // initialize form validation and setup error qtip and field hinting
                hgm.initMemberForms();
                $("#dialogSignin").dialog({
                    autoOpen: false,
                    draggable: false,
                    resizable: false,
                    modal: true,
                    width: 630,
                    height: 470,
                    closeText: 'x',
                    title: $('#dialogSignin .dialogTitle')
                });
                $("#dialogSignup").dialog({
                    autoOpen: false,
                    draggable: false,
                    resizable: false,
                    modal: true,
                    width: 630,
                    height: 530,
                    closeText: 'x',
                    title: $('#dialogSignup .dialogTitle')
                });
                // wrap the dialog in a scope to avoid jquery ui theme
                $("#dialogSignin").dialog().parents('.ui-dialog:eq(0)').wrap('<div class="memberDialogScope"></div>');
                $("#dialogSignup").dialog().parents('.ui-dialog:eq(0)').wrap('<div class="memberDialogScope"></div>');
                $('#' + dialogName).dialog('open');
            }
        );
    } else {
        $("#" + dialogName).dialog('open');
    }
};

hgm.openSigninDialog = function () {
    closeSignupDialog();
    hgm.openMembersDialog('signin');
};
openSigninDialog = hgm.openSigninDialog;
hgm.openSignupDialog = function () {
    closeSigninDialog();
    hgm.openMembersDialog('signup');
};
openSignupDialog = hgm.openSignupDialog;

