/*
 * Om.ly Retweet Button
 * Inspired by 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
 */

function IncludeJavaScript(jsFile)
{
  document.write('<script type="text/javascript" src="'
    + jsFile + '"></scr' + 'ipt>'); 
}


IncludeJavaScript('http://cdn.jquerytools.org/1.2.3/jquery.tools.min.js');

(function(){
if (typeof(OmlyCB) == 'undefined')
    var OmlyCB = {}; // global namespace for your callback methods. Allows you to define callabacks from within other method calls.


window.RetweetJS = {
	// Your om.ly API Key
	key: "your-key-here",

	// The text to replace the links with
	link_text: (/windows/i.test( navigator.userAgent) ? "&#9658;" : "&#9851;") +
		"&nbsp;Retweet",

	// What # to show (Use "clicks" for # of clicks or "none" for nothing)
	count_type: "clicks",


	// Tweet Prefix text
	// "RT @jeresig " would result in: "RT @jeresig Link Title http://bit.ly/asdf"
	prefix: "RT ",

	// Style information
	styling: "a.retweet { font: 12px Helvetica,Arial; color: #000; text-decoration: none; border: 0px; }" +
		"a.retweet span { color: #FFF; background: #94CC3D; margin-left: 2px; border: 1px solid #43A52A; -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; padding: 3px; }" +
		"a.vert { display: block; text-align: center; font-size: 14px; float: left; margin: 4px; }" +
		"a.retweet strong.vert { display: block; margin-bottom: 4px; background: #F5F5F5; border: 1px solid #EEE; -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; padding: 3px; }" +
		"a.retweet span.vert { display: block; font-size: 12px; margin-left: 0px; }"
};

/// Client libraryy
//
var OmlyClient = { 
};

OmlyClient.shorten = function(longUrl, callback) {
	shortURL = 'http://app.objectivemarketer.com/api/shorten?' + 'apiKey=' + RetweetJS.key +  "&text=" + encodeURIComponent(longUrl) + '&callback=?';
	$j.getJSON(shortURL,function(responseJSON){ 
		if (responseJSON.status == 200){
			callback(responseJSON);
		}
	  });
};

OmlyClient.stats = function(suffix, callback) {
	statsURL= 'http://app.objectivemarketer.com/api/url_stats?' + 'apiKey=' + RetweetJS.key +  "&id=" + suffix + '&callback=?';
	$j.getJSON(statsURL,function(responseJSON){ 
	  {
			if (responseJSON.status == 200)
				callback(responseJSON);
			}
	  });

};

//////////////// No Need to Configure Below Here ////////////////

var loadCount = 1;

if ( document.addEventListener ) {
	document.addEventListener("DOMContentLoaded", loaded, false);

} else if ( window.attachEvent ) {
	window.attachEvent("onload", loaded);
}

function loaded(){
	// Need to wait for doc ready and js ready
	if ( ++loadCount < 2 ) {
		return;
	}
	$j = jQuery.noConflict();

	var elems = [], urlElem = {};

	OmlyCB.shortenResponse = function(data) {
		var hash = data.shorturl;
		var url = data.originalURL;

		var elems = urlElem[ url ];

		for ( var i = 0; i < elems.length; i++ ) {
			elems[i].href += ' ' + hash;
		}

		if ( RetweetJS.count_type === "clicks" ) {
			if (data.urlId.length > 0) {
				OmlyClient.stats(data.urlId[0].suffix, OmlyCB.statsResponse);
			}
		}
	};

	OmlyCB.statsResponse = function(data) {
		var clicks = data.clicks;
		var url = data.originalURL, elems = urlElem[ url ];

		if ( clicks > 0 ) {
			for ( var i = 0; i < elems.length; i++ ) {
				var strong = document.createElement("strong");
				strong.appendChild( document.createTextNode( clicks + " " ) );
				elems[i].insertBefore(strong, elems[i].firstChild);

				if ( /(^|\s)vert(\s|$)/.test( elems[i].className ) ) {
					elems[i].firstChild.className = elems[i].lastChild.className = "vert";
				}
			}
		}

		urlElem[ url ] = null;
	};

	if ( document.getElementsByClassName ) {
		elems = document.getElementsByClassName("retweet");
	} else {
		var tmp = document.getElementsByTagName("a");
		for ( var i = 0; i < tmp.length; i++ ) {
			if ( /(^|\s)retweet(\s|$)/.test( tmp[i].className ) ) {
				elems.push( tmp[i] );
			}
		}
	}

	if ( elems.length && RetweetJS.styling ) {
		var style = document.createElement("style");
		style.type = "text/css";

		try {
			style.appendChild( document.createTextNode( RetweetJS.styling ) );
		} catch (e) {
			if ( style.styleSheet ) {
				style.styleSheet.cssText = RetweetJS.styling;
			}
		}

		document.body.appendChild( style );
	}

	for ( var i = 0; i < elems.length; i++ ) {
		var elem = elems[i];

		if ( /(^|\s)self(\s|$)/.test( elem.className ) ) {
			elem.href = window.location;
			elem.title = document.title;
		}

		var origText = elem.title || elem.textContent || elem.innerText,
			href = elem.href;

		elem.innerHTML = "<span>" + RetweetJS.link_text + "</span>";
		elem.title = "";
		elem.href = "http://twitter.com/home?status=" +
			encodeURIComponent(RetweetJS.prefix + origText);

		if ( urlElem[ href ] ) {
			urlElem[ href ].push( elem );
		} else {
			urlElem[ href ] = [ elem ];
			OmlyClient.shorten(href, OmlyCB.shortenResponse);
		}
	}

}

})();

