﻿$(document).ready(function () {
    $("#ScheduleLink").click(function () {
        $("#schedule").show();
        $("#schedulefloat").hide();
        $("#SendMessage").val("Send");
        _gaq.push(['_trackEvent', 'Send_Ajax', 'Show Schedule']);
        return false;
    });

    $("#schedulecancel").click(function () {
        $("#schedule").hide();
        $("#schedulefloat").show();
        $("#SendMessage").val("Send Now");
        $("#Date").val("");
        $("#Time").val("");
        _gaq.push(['_trackEvent', 'Send_Ajax', 'Hide Schedule']);
        return false;
    });

    $("#usetemplate").colorbox({ inline: true, href: "#picktemplate", width: 500, height: 350, onOpen: function () { _gaq.push(['_trackEvent', 'Send_Ajax', 'Open Templates']); } }, function () {
        $.ajax({
            url: "Send.aspx/ListTemplates",
            success: function (data) {
                $("#picktemplate ul").empty();
                if (data.d == "") {
                    $("#picktemplate").append("<p>Use templates for your common messages. <a href=\"/msp/settings/templates-add.aspx\">Click here to configure</a></p>");
                    return false;
                }
                $.each(data.d, function () {
                    $("#picktemplate ul").append("<li><a href=\"#\" title=\"" + this[1].replace("\"", "&quot;") + "\">" + this[0] + "<span>" + this[1].substring(0, 70) + ((this[1].length > 70) ? "..." : "") + "</span></a></li>");
                });
                $("#picktemplate a").click(function () {
                    $("#MessageText").val(unescape($(this).attr("title")));
                    $.fn.colorbox.close();
                    CountAndClean("#MessageText", "#charcount", "#smscount");
                    _gaq.push(['_trackEvent', 'Send_Ajax', 'Pick Template']);
                    return false;
                });
            },
            error: function () {
                _gaq.push(['_trackEvent', 'Send_Ajax', 'Ajax Error', 'Templates']);
            },
            cache: false
        });
    });

    $("#mergefields>li").click(function () {
        switch ($.trim($(this).text())) {
            case "First name":
                insertAtCursor("MessageText", "#FNAME#");
                break;
            case "Surname":
                insertAtCursor("MessageText", "#SNAME#");
                break;
            case "Custom":
                insertAtCursor("MessageText", "#CUST#");
                break;
        }
        CountAndClean("#MessageText", "#charcount", "#smscount");
        _gaq.push(['_trackEvent', 'Send_Ajax', 'Merge', $.trim($(this).text())]);
        return false;
    });

    $("#showbranding").click(function () {
        $.ajax({
            url: "Send.aspx/ListBranding",
            success: function (data) {
                $("#brandinglist ul").empty();
                if (data.d == '') {
                    $("#brandinglist").html("<p>Add your company's branding to the end of your message. <a href=\"/msp/settings/branding-add.aspx\">Click here to configure</a></p>");
                    return false;
                }
                $.each(data.d, function () {
                    $("#brandinglist ul").append("<li>" + this + "</li>");
                });
                $("#tags>li").click(function () {
                    $("#MessageText").val($("#MessageText").val() + " " + $(this).html().replace(/(.*)<span>.*/i, "$1"));
                    $("#brandinglist").hide();
                    $("#showbranding").show();
                    CountAndClean("#MessageText", "#charcount", "#smscount");
                    _gaq.push(['_trackEvent', 'Send_Ajax', 'Pick Branding']);
                });
            },
            error: function () {
                _gaq.push(['_trackEvent', 'Send_Ajax', 'Ajax Error', 'Branding']);
            },
            cache: false
        });
        $("#brandinglist").show();
        $("#showbranding").hide();
        _gaq.push(['_trackEvent', 'Send_Ajax', 'Show Branding']);
        return false;
    });

    $("#hidebranding").click(function () {
        $("#brandinglist").hide();
        $("#branding>a").show();
        _gaq.push(['_trackEvent', 'Send_Ajax', 'Hide Branding']);
    });

    var toStr = $("#To").val();
    $("#To").val("");
    var toBox = new TextboxList("#To", {
        unique: true,
        plugins: {
            livesearch: {
                url: "Send.aspx/SearchContacts",
                minLength: 2,
                highlight: false,
                placeholder: "Type the name of a contact, group or phone number"
            }
        }
    });
    if (toStr != "") {
        $.ajax({
            url: "Send.aspx/PopulateContacts",
            data: "{ idString: \"" + toStr + "\" }",
            success: function (data) {
                $.each(data.d, function () {
                    toBox.add(this[1], this[0], this[2]); // plain,id,html
                });
            },
            error: function () {
                _gaq.push(['_trackEvent', 'Send_Ajax', 'Ajax Error', 'Populate Contacts']);
            },
            cache: false
        });
    }
    $("#addgroup").colorbox({ inline: true, href: "#pickgroup", width: 545, height: 300, onOpen: function () { _gaq.push(['_trackEvent', 'Send_Ajax', 'Open Groups']); } }, function () {
        $.ajax({
            url: "Send.aspx/ListGroups",
            success: function (data) {
                $("#pickgroup ul").empty();
                $("#pickgroup ul").append("<li><a href=\"#\" grp=\"all\">All Contacts<span>All your contacts</span></a></li>");
                $.each(data.d, function () {
                    $("#pickgroup ul").append("<li><a href=\"#\" grp=\"g:" + this[0] + "\">" + this[1] + "<span>" + this[2] + " people</span></a></li>");
                });
                $("#pickgroup a").click(function () {
                    toBox.add($(this).text(), $(this).attr("grp"), "<span class=\"group\">" + $(this).html().replace(/(.*)<span>.*/i, "$1") + "</span>");
                    $.fn.colorbox.close();
                    _gaq.push(['_trackEvent', 'Send_Ajax', 'Pick Group']);
                    return false;
                });
            },
            error: function () {
                _gaq.push(['_trackEvent', 'Send_Ajax', 'Ajax Error', 'Groups']);
            },
            cache: false
        });
    });

    $("#addsub").colorbox({ inline: true, href: "#picksub", width: 570, height: 350, onOpen: function () { _gaq.push(['_trackEvent', 'Send_Ajax', 'Open Subs']); } }, function () {
        $.ajax({
            url: "Send.aspx/ListSubs",
            success: function (data) {
                $("#picksub ul").empty();
                $.each(data.d, function () {
                    $("#picksub ul").append("<li><a href=\"#\" data-sub=\"s:" + this.Id + "\" data-dest=\"" + this.DestAddr + "\">" + this.Name + "<span>" + this.Count + " people</span></a></li>");
                });
                $("#picksub a").click(function () {
                    toBox.add($(this).text(), $(this).attr("data-sub"), "<span class=\"sub\">" + $(this).html().replace(/(.*)<span>.*/i, "$1") + "</span>");
                    var dest = $(this).attr("data-dest");
                    if (dest != undefined && dest != '') {
                        $('#FromList').val(dest);
                    }
                    InitCountAndClean("#MessageText", "#charcount", "#smscount", 433);
                    $.fn.colorbox.close();
                    _gaq.push(['_trackEvent', 'Send_Ajax', 'Pick Sub']);
                    return false;
                });
            },
            error: function () {
                _gaq.push(['_trackEvent', 'Send_Ajax', 'Ajax Error', 'Subs']);
            },
            cache: false
        });
    }); InitCountAndClean("#MessageText", "#charcount", "#smscount");

    $("#Date").datepicker({
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        minDate: -0
    });

    $("#Time").timepicker({});

    $("#SendMessage").click(function () {
        $(".textboxlist-bit-editable-input").each(function () {
            if ($(this).val().length > 0) {
                var str = $(this).val() + "," + $("#To").val();
                $("#To").val(str);
            }
        });
    });

    $('div.textboxlist').focusin(function () {
        $(this).addClass('hasfocus');
    }).focusout(function () {
        $(this).removeClass('hasfocus');
    });
});
        
