﻿
var BookMarkingHelper = function(config) {

    var _config = config;
    var _setup = function() {
        $.ajaxSetup({ cache: false });
        $("ul[id='drop_down'] div ul").hide();

        $('#share').hover(
		    function() { $("ul[id='drop_down'] div ul").show('slow'); }
		    , function() { $("ul[id='drop_down'] div ul").hide('slow'); }
		);

        $("ul[id='drop_down'] div ul li.last.left.bold a").hover(
            function() { $('#bookMarkingHelp').show(); }
            , function() { $('#bookMarkingHelp').hide(); }
        );

        //string shortcode, string senderName, string recipientName, string recipientEmail, string message, string permalink
        $('#btnSend').click(function() {
            if (_validateInputs())
                $.post(
                _config.SendUrl
                , { senderName: $('#senderName').val()
                        , recipientName: $('#recipientName').val()
                        , recipientEmail: $('#recipientEmail').val()
                        , message: $('#message').val()
                        , permalink: $('#permalink').val()
                }
                , sendCallback, 'json');
            else
                alert(Resources.Bookmarking.Invalid);
        });
    };

    var sendCallback = function(response) {
        if (response.success == true) {
            alert(Resources.Bookmarking.Sent);
            _emptyFields();
        }
        else
            alert(Resources.Bookmarking.NotSent);
    };

    var _isValidEmail = function(email) {
        var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
        var regex = new RegExp(emailReg);

        return (regex.test(email));
    };

    var _emptyFields = function() {
        $('#senderName').val('');
        $('#recipientName').val('');
        $('#recipientEmail').val('');
        $('#message').val('');
    };

    var _validateInputs = function() {
        var isValid = true;
        var email = $('#recipientEmail').val();
        var name = $('#senderName').val();

        var isValidEmail = _isValidEmail(email);

        if (!isValidEmail)
            isValid = false;
        if (name.length == 0)
            isValid = false;

        return isValid;
    };


    return {
        Setup: function() { _setup(); }
        , ValidateInputs: function() { return _validateInputs(); }
    };
};