/**
 * Fancy easing equations
 */
jQuery.extend( jQuery.easing,
{
	pull: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	boing2: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	boing: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.2;
		if (a < Math.abs(c)) { a=c; var s=p/2; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-20*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	}
});

/**
 * @author Alexander Farkas
 * v. 1.02
 */
(function($) {
	$.extend($.fx.step, {
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem, 'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);
/* */

/**
 * Adds a count (and link) to the
 * number of delicious.com bookmarks
 * about the current page.
 */
(function(){

	var get_deliciousness = function(){
		var endpoint = 'http://feeds.delicious.com/v2/json/urlinfo/data?';
		var url = window.location.href.replace("http://10.17.1.99/cowburn/", "http://cowburn.info/");
		var params = jQuery.param({url: url});
		var url = endpoint+params+'&callback=?';
		jQuery.getJSON(url, parse_deliciousness);
	};
	
	var parse_deliciousness = function(data){
		var item  = (data && data[0]) ? data[0] : false;
		if (item === false) return false;
		jQuery(".entry-meta .delicious").after(
			jQuery('<a title="See who has bookmarked this page" class="delicious-info"/>')
				.attr("href", "http://delicious.com/url/" + item.hash)
				.text("(" + item.total_posts + ")")
		).after(" ");
	};

	jQuery(function(){
		if (jQuery("body").hasClass("single"))		
			get_deliciousness();		
	});
})();

/**
 * Adds a count of the number of
 * feed subscribers via feedburner.com (and YQL)
 */
(function(){
	var get_subscribers = function(who){
		var endpoint = 'http://query.yahooapis.com/v1/public/yql?';
		var query    = 'use "http://cowburn.info/playground/yql/feedburner/feedburner.feed.xml";'
		             + 'select * from feedburner.feed where uri="' + who + '";';
		var params   = jQuery.param({q:query, format:"json", diagnostics:false});
		var url = endpoint + params + "&callback=?";
		jQuery.getJSON(url, parse_subscribers);
	};
	
	var parse_subscribers = function(data){
		var item = (data.query.results.entry) ? data.query.results.entry : false;
		var count = (item ? item.circulation : 0);
		var date  = (item ? item.date : 'unknown');
		jQuery("#navigation .feed a").append(
			jQuery('<span/>')
				.attr("title", "Subscriber count for " + date)
				.text(" (" + count + ")")
		);
	};
	
	jQuery(function(){ get_subscribers("cowburn"); });
})();


/**
 * Bouncy header logo link
 */
jQuery(function(){
	jQuery("#logo > a")
		.hover(function(e){
			e.stopPropagation();
			jQuery(this).stop().animate({
				backgroundPosition: "(9px -280px)",
				paddingLeft: "130px",
				color: "#999999"
			}, {duration: 2000, easing: 'boing'});
		}, function(e){
			e.stopPropagation();
			jQuery(this).stop().animate({
				backgroundPosition: "(9px -180px)",
				paddingLeft: "110px",
				color: "#000000"
			}, {duration: 2000, easing: 'boing'});
		});		
});

/**
 * Asynchronously load Google Analytics
 * page tracking code. The "default" code
 * for this is horrible and locks up the 
 * page load.
 * Original concept: http://tinyurl.com/jresig-jquery-gajs
 */
 
/*
(function(tracker){
	var prefix   = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	var source   = prefix + "google-analytics.com/ga.js";
	var pageview = function(){
		try {
			_gat._getTracker(tracker)._trackPageview();
		} catch(e) { }
	};
	jQuery.getScript(source, pageview);
})("UA-1187395-2");
*/

/*
	Analytics
*/
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1187395-2']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script');
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	ga.setAttribute('async', 'true');
	document.documentElement.firstChild.appendChild(ga);
})();


/*
	Wee tracker :-)
*/
(function(params) {
	var uri = 'http://salathe.co.uk/track.gif?rand=' + Math.random();
	for (var k in params) {
		var val = params[k] !== null ? params[k] : '';
		uri += '&' + encodeURIComponent(k) + '=' + encodeURIComponent(val);
	}
	jQuery(function(){
		jQuery("body").append(jQuery('<img src="'+uri+'" />'));
	});
})({
	location: document.location,
	vertical: "cowburn",
	referer: document.referrer
});

