﻿/// <reference path="jquery-1.3.2.js" />
/// <reference path="rx.js" />
/// <reference path="rx.ExtJs.js" />
/// <reference path="rx.jQuery.js" />


jQuery.extend({

    
    actionLink : function (text, href){
        return "<a href=\"" + href + "\">" + text + "</a>";
    },

    uri: function ( action ) {
        return "/Publisher/Mail/" + action;
    },

    menu: function () {
        
        Rx.Observable.Interval(5000)        
        .Subscribe(
            function () {
                $.post($.uri("MessageCount"), {},
                  function (e) {
                      $('.inbox', '.actions').html($.inboxActionLink(e));
                  }, "json");
            },
            
            function(error) {
                alert(error);
            });                        
    },

    markAsRead : function (id){
        $.post($.uri("MarkAsRead"), { "id" : id },
               function (e) {
                    if (!e.Success)  {
                        alert("FAIL");
                    }
               }, "json");
    },
    
    inboxActionLink: function (messageCount) {
        
        var count = "(" + messageCount + ") ";
                        
//        if (messageCount >= 0) {
//            count = "(" + messageCount + ") ";
//        }        

        return this.actionLink("Inbox " + count, "/Publisher/Mail/Inbox");
    },  
});


$.fn.selectRange = function(start, end) {
        return this.each(function() {
                if(this.setSelectionRange) {                        
                        this.setSelectionRange(start, end);
                } else if(this.createTextRange) {
                        var range = this.createTextRange();
                        range.collapse(true);
                        range.moveEnd('character', end);
                        range.moveStart('character', start);
                        range.select();
                }
        });
};





