﻿/// <reference path="jquery-1.3.2.min.js" />

/* Twitter Proxy Wrapper */
/*
Campaign Solutions - Daniel E. Ulfe - Oct/2009
    
Requires Twitter Proxy back-end
*/

(function(jQuery) {
    jQuery.twitterCached = function(elem, options) {
        jQuery(elem).twitterCached(options);
    };

    jQuery.twitterCached.inUse = false;

    jQuery.fn.twitterCached = function(options) {
        return this.each(function() {
            // Helper
            var jQuerythis = jQuery(this);
            // Default options
            this.options = jQuery.extend({}, jQuery.fn.twitterCached.defaults, options);

            if (jQuery.twitterCached.inUse) {
                alert('Only 1 simultaneous AJAX call allowed.');
                return;
            }

            // In-Use Flag
            jQuery.twitterCached.inUse = true;

            // Ajax Call
            jQuery.postToDotNetService("/twitter/twitter.asmx/GetPublicStatuses", "{username:'" +
                this.options.username + "', ttl:" +
                this.options.cacheTTL + ", count:" +
                this.options.count + "}",
                function(data, status, error) {
                    // Flag
                    jQuery.twitterCached.inUse = false;

                    // Processing results
                    if (status = 'success') {
                        if (data.d.Code == 0) {
                            var ul = jQuery('<ul>').addClass('twitter');
                            for (var i = 0; i < data.d.Statuses.length; i++) {
                                // Converting MS JSON DateTime to JS DateTime
                                data.d.Statuses[i].CreationTime = eval(data.d.Statuses[i].CreationTime.replace(/\/Date\((\d+)\)\//gi, 'new Date($1)'));

                                if (jQuerythis[0].options.ondatarowbound == null)
                                    var li = jQuery('<li>' + data.d.Statuses[i].Message + '</li>');
                                else
                                    var li = jQuery('<li>' + jQuerythis[0].options.ondatarowbound(data.d.Statuses[i]) + '</li>');

                                if (i == 0)
                                    li.addClass('first');

                                if (i == data.d.Statuses.length - 1)
                                    li.addClass('last');

                                ul.append(li);
                            }
                            jQuerythis.html("");
                            jQuerythis.append(ul);

                            if (jQuerythis[0].options.onend != null)
                                jQuerythis[0].options.onend(jQuerythis, 'success');
                        }
                        else {
                            jQuerythis.html(jQuerythis[0].options.errorMessage_TwitterFailed);

                            if (jQuerythis[0].options.onend != null)
                                jQuerythis[0].options.onend(jQuerythis, 'twittererror', data.d.code, data.d.message);
                        }
                    }
                    else {
                        jQuerythis.html(jQuerythis[0].options.errorMessage_ProxyFailed);

                        if (jQuerythis[0].options.onend != null)
                            jQuerythis[0].options.onend(jQuerythis, 'proxyerror', error);
                    }
                });
        });
    };

    jQuery.fn.twitterCached.defaults = {
        username: '',
        count: 5,
        cacheTTL: 60,
        errorMessage_TwitterFailed: 'Twitter did not respond',
        errorMessage_ProxyFailed: 'Twitter proxy service failed',
        ondatarowbound: null,
        onend: null
    };
})(jQuery);
