﻿/*
 * Easy Retweet Button
 * http://ejohn.org/blog/retweet/
 *   by John Resig (ejohn.org)
 *
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Lot's of changes by TVGUIDE.COM, no longer same script at all.
 */
 
    // Array.slice() - Copy and return several elements
if( typeof Array.prototype.slice==='undefined' ) {
 Array.prototype.slice = function( a, c ) {
  var i, l = this.length, r = [];
  if( !c ) { c = l; }
  if( c<0 ) { c = l + c; }
  if( a<0 ) { a = l - a; }
  if( c<a ) { i = a; a = c; c = i; }
  for( i = 0; i < c - a; i++ ) { r[i] = this[a+i]; }
  return r;
 };
}

// Array.concat() - Join two arrays
if( typeof Array.prototype.concat==='undefined' ) {
 Array.prototype.concat = function( a ) {
  for( var i = 0, b = this.copy(); i<a.length; i++ ) {
   b[b.length] = a[i];
  }
  return b;
  };
}

function ParamCallBack(funcPtr,firstArg) {
    var Args=arguments;
    return function() {
        var SliceArray=Array.prototype.slice.call(arguments);
        funcPtr.apply(null,SliceArray.concat(Array.prototype.slice.call(Args,1)));
    };
}

function disableAnchor(obj, disable){
    if(disable) {
        var href = obj.getAttribute("href");
        if(href && href !== "" && href !== null) {
            obj.setAttribute('href_bak', href);
        }
        obj.removeAttribute('href');
        obj.style.color="gray";
    }
    else {
        obj.setAttribute('href', obj.attributes['href_bak'].nodeValue);
        obj.style.color="blue";
    }
}

function RetweetModule()
{
    // constants and other variables

    window.RetweetJS = {
	    // David note: moving prefix to the end, which doesn't make it a prefix, but hey, that's life
	    prefix: " by @tvguide",
	    // Actual displayed link.  This should already be on the page.
	    link_text: "<img src=\"/images/global/socialbookmark_twitter.gif\" alt=\"Tweet This!\" border=\"0\" />",
        // David T : This is location of the handler on the production cluster.
        bitservice : "/handler/bitlyhandler.ashx?longurl="
    };

    //////////////// No Need to Configure Below Here ////////////////

    this._windowNumber = 1;
    
    this.GetXmlHttpObject1=function()     {        var xmlHttp=null;        try {            xmlHttp=new XMLHttpRequest();        } catch (e) {            try {                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");            } catch (e) {                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");            }        }        return xmlHttp;    };
    
    this.AddEventToElem=function(elem,eventName,func)
    {
        if (window.attachEvent)
        {
            elem.attachEvent(eventName,func); 
        } else {
            elem.setAttribute(eventName,func);
        }
    };
    
    this.popwin=function(url,name)
	{
	    var IE6 = false;
        if (IE6) 
        {
            name += (_windowNumber++).toString();
        }
        
        var tweetwindow = window.open(url,name);
    };
    
    // Target for click handler
    // This function calls into the webwservice and gets the bit.ly url for the long url
    //
    // This supports legacy pages
    
    this.clickhandler=function()
	{
    	var indexStart=0;
    	if (arguments[indexStart].toString().indexOf("http:")==-1) indexStart++;
 
    	var longurl = arguments[indexStart];
    	var mesg = arguments[indexStart + 1];
    	var elem = arguments[indexStart + 2];
    	var that = arguments[indexStart + 3];
    	
    	var xmlHttp = that.GetXmlHttpObject1();
    	
    	if (xmlHttp)
    	{
    	    xmlHttp.open("GET",window.RetweetJS.bitservice+encodeURIComponent(longurl),false);
    	    xmlHttp.send(null);
    	    
    	    var url = xmlHttp.responseText;
    	
    	    var sUrl = "http://twitter.com/home?status=" + encodeURIComponent(mesg + " " + url + " " + window.RetweetJS.prefix);
	        that.disableAnchor(elem,false);  
	        elem.onclick="return true";
            elem.href = sUrl;
        	
    	    return true;
    	}
    	return false;
    };

    // External Entry point to allow this to retry
    this.init = function() {
        var i, elems = [], urlElem = {}, hashURL = {};

        if (document.getElementsByClassName) {
            elems = document.getElementsByClassName("retweet");
        }
        else {
            var tmp = document.getElementsByTagName("a");
            for (i = 0; i < tmp.length; i++) {
                if (/(^|\s)retweet(\s|$)/.test(tmp[i].className)) {
                    elems.push(tmp[i]);
                }
            }
        }

        for (i = 0; i < elems.length; i++) {
            var elem = elems[i];
            var origText = elem.title || elem.textContent || elem.innerText;
            if (origText) {
                if (elem.href.indexOf("bit.ly") != -1) {
                    var sUrl = "http://twitter.com/home?status=" + encodeURIComponent(origText + " " + elem.href.toString() + " " + window.RetweetJS.prefix);
                    elem.href = sUrl;
                    elem.onclick = "return true;";
                } else {
                    var regex = /(^|\s)self(\s|$)/;
                    if (regex.test(elem.className)) {
                        elem.href = window.location;
                        elem.title = document.title;
                    }
                    var href = elem.href;
                    if (elem.ohref) { href = elem.ohref; }
                    origText = origText.substr(0, 82);
                    elem.innerHTML = "<span>" + window.RetweetJS.link_text + "</span>";

                    elem.title = "";
                    AddEventToElem(elem, "onclick", ParamCallBack(this.clickhandler, href, origText, elem, this));
                    elem.onclick = ParamCallBack(this.clickhandler, href, origText, elem, this);
                    elem.style.cursor = "pointer";
                    this.disableAnchor(elem, true);
                }
            }
        }
    };
    
    this.init();	
    
    return this;
}

// Use JQUERY to initialize the component.

$(document).ready(
    function()
    {
        window.RetweetModule = RetweetModule();
    }
 );
 
