﻿function CSTinyURLUtil()
{
}

CSTinyURLUtil.prototype.init = function()
{
    var linkList = $("a[LongToTinyURL]");
    this.InitForList(linkList);
}

CSTinyURLUtil.prototype.InitForList = function( linkList )
{
    for(var i = 0; i < linkList.length; i++)
    {
        linkList[i].TwitterClick = this.OnClick;
        linkList[i].TinyURLUtil = this;
    }
    linkList.bind('click', function(event) { return this.TwitterClick(event);});
}

// This is called in the scope of the Clicked element
CSTinyURLUtil.prototype.OnClick = function(event)
{
    var sender = event.currentTarget;
    // Check to see if the TinyURL is set
    // and get it using AJAX if it isn't
    var cURL = document.location.href;
    if (sender.attributes["href"].value == "#" 
     || sender.attributes["href"].value == cURL+"#")
    {
        // Calling back to TinyURLUtil Scope
        this.TinyURLUtil.GetTinyURL(sender);
        
        return false;
    }
    
    // The TinyURL is all set just return to open
    // up new Tweeter window
    return true;
}

CSTinyURLUtil.prototype.GetTinyURL = function(sender)
{
        var cbHandler = new CSTinyCallback();
        cbHandler.element = sender;
        cbHandler.Callback = this.OnAjaxCallback;
        
        var replacewiththeurl = sender.attributes["LongToTinyURL"].value;
        var now = new Date();
        var postURL = "/JSHelpers/TinyURLGW.aspx?lurl=" + replacewiththeurl;
                
        $.ajax({ 
                url : postURL,
                dataType: 'json',
                data: null,
                success: cbHandler.OnCallback,
                error: cbHandler.OnError,
                context: cbHandler
                });
}

/*
    This method is called to actually open the new window once the 
    TinyURL has been set.
*/
CSTinyURLUtil.prototype.OnAjaxCallback = function(sender)
{
    window.open(sender.href);
}

function CSTinyCallback()
{
}
CSTinyCallback.prototype.Callback = null;
CSTinyCallback.prototype.element = null;

/* Error handler if even the ajax request causes an error */
CSTinyCallback.prototype.OnError = function(request, textStatus, errorThrown)
{
    var lURL = this.element.attributes["LongToTinyURL"].value;
    var newHREF = this.element.attributes["TweeterHref"].value + " " + lURL + " via @communispace";
    this.element.attributes["href"].value = newHREF;
    
    this.Callback(this.element);
} 

CSTinyCallback.prototype.OnCallback = function(response)
{
    if(response.statusCode == "OK")
    {
        var shortURL = response.results[this.element.attributes["LongToTinyURL"].value]["short_url"];
        var newHREF = this.element.attributes["TweeterHref"].value + " " + shortURL + " via @communispace";
        this.element.attributes["href"].value = newHREF;
    }
    else
    {
    /* error handling if twitter returns an error */
        var lURL = this.element.attributes["LongToTinyURL"].value;
        var newHREF = this.element.attributes["TweeterHref"].value + " " + lURL + " via @communispace";
        this.element.attributes["href"].value = newHREF;
    }
    this.Callback(this.element);
}

