﻿//*******************************************************************
// --  used to automate twitter links for blogs/website pages --
//
// COPYRIGHT: 2008 Spiderweb Logic Web Design
// http://www.SpiderwebLogic.com
// 
// LICENSE/RESTRICTIONS
//
// All copyright info, credits, and license terms are to be kept in place.
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//
//  
//*******************************************************************
/*
  SAMPLE: (for copy/paste)
<script language='javascript' src='http://www.spiderweblogic.com/js/twitterthis.js' type='text/javascript'></script>
<a href='#' onclick='return openTwitter();'><img border='0' height='16px' src='http://www.spiderweblogic.com/images/twitterthis.png' style='margin:1px' width='13px'/></a>

// var timestamp = &#39;<data:post.timestamp/>&#39;;

*/

var twitterthis_url = location.href; //default
function setTwitterUrl(newUrl) {
  var twitterthis_url=newUrl;
}
var twitterthis_title = document.title; //default
function setTwitterTitle(newTitle) {
  var twitterthis_title=newTitle;
}
function getTwitterUrl() {
   var twitter_link_140= "http://twitter.com/home?status=I%20just%20read%20" 
         + twitterthis_url + "%20~%20" + urlencodeTwit(twitterthis_title);
   return twitter_link_140;
}

function openTwitter() {
   window.open(getTwitterUrl(),"_blank"); 
   return false;
}

// GENERIC URLENCODE by Philip Peterson
function urlencodeTwit(str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                                     
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}