// Quak Bak 0.3 | http://quakbak.com
//
// Copyright (c) 2009 Jeremy Hilton
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

$(document).ready(function() {
    var url = document.location.href;

    $.getJSON(url_to_shorten_lib + "?url=" + url, function(data) {

        var urls = data.urls;
        urls.push(url);

        $.getJSON('http://search.twitter.com/search.json?rpp=100&q=&ors=' + urls.join("+") + '&callback=?',

            function(twitter_data) {
                var t_count = 0,
                    tweetback_buffer = [];

                $.each(twitter_data.results, function(key, item) {
                    tweetback_buffer.push(tmpl("tweet_back_tmpl", item));
                    t_count++;
                });
                $("#tweetbacks").html(tweetback_buffer.join(""));

                $("#tweetbackcount").html(tmpl("tweet_back_count_tmpl", {tb_count:t_count}));
            }
        );
    });
});


// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
	var cache = {};

	this.tmpl = function tmpl(str, data){
    	// Figure out if we're getting a template, or if we need to
    	// load the template - and be sure to cache the result.
    	var fn = !/\W/.test(str) ? cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) :

      	// Generate a reusable function that will serve as a template
      	// generator (and which will be cached).
      	new Function("obj",
        	"var p=[],print=function(){p.push.apply(p,arguments);};" +

        	// Introduce the data as local variables using with(){}
        	"with(obj){p.push('" +

        	// Convert the template into pure JavaScript
        	str
          	.replace(/[\r\t\n]/g, " ")
          	.split("<%").join("\t")
          	.replace(/((^|%>)[^\t]*)'/g, "$1\r")
          	.replace(/\t=(.*?)%>/g, "',$1,'")
          	.split("\t").join("');")
          	.split("%>").join("p.push('")
          	.split("\r").join("\\'")
      		+ "');}return p.join('');"
		);

    	// Provide some basic currying to the user
    	return data ? fn( data ) : fn;
  	};
})();

