$(document).ready(function(){ 
	var $body = $('body');

   	//load dropdown menu
    $("ul.sf-menu").superfish({
        dropShadows: false,
        autoArrows: false
    }); 

	// scrolltop	
	$("#footer a#top").click(function () { 
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
    });
	
	// homepage twitter feed
	if ($body.hasClass('home')) {
	
		// grab latest tweet
  	    $.getJSON('https://api.twitter.com/1/statuses/user_timeline.json?screen_name=Drummonds_uk&count=1&callback=?', function(data)      {
	          
	        // result returned
	        var tweet = data[0].text;
	      	var created = prettyDate(data[0].created_at);
	      
	        // process links and reply
	        tweet = ify.clean(tweet);
	        	      
	        // output the result
	        $("div#tweet").html(tweet).append('<br/>' + created);
	    });
	    
	    // function for formatting twitter links 		
		// http://www.dustindiaz.com/basement/ify.html
		var ify = function() {
		  return {
		    "link": function(t) {
		      return t.replace(/(^|\s+)(https*\:\/\/\S+[^\.\s+])/g, function(m, m1, link) {
		        return m1 + '<a href=' + link + '>' + ((link.length > 25) ? link.substr(0, 24) + '...' : link) + '</a>';
		      });
		    },
		    "at": function(t) {
		      return t.replace(/(^|\s+)\@([a-zA-Z0-9_]{1,15})/g, function(m, m1, m2) {
		        return m1 + '@<a href="http://twitter.com/' + m2 + '">' + m2 + '</a>';
		      });
		    },
		    "hash": function(t) {
		      return t.replace(/(^|\s+)\#([a-zA-Z0-9_]+)/g, function(m, m1, m2) {
		        return m1 + '#<a href="http://search.twitter.com/search?q=%23' + m2 + '">' + m2 + '</a>';
		      });
		    },
		    "clean": function(tweet) {
		      return this.hash(this.at(this.link(tweet)));
		    }
		  };
		}();	
	}
	
	// balance columns
//	$('ul.category-products li:nth-child(3n+1)').addClass('clear');
		
}); 
 
/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */
// converts ISO time to casual time
function prettyDate(time){
    var date = new Date((time || "").replace(/-/g,"/").replace(/TZ/g," ")),
    	diff = (((new Date()).getTime() - date.getTime()) / 1000),
    	day_diff = Math.floor(diff / 86400);
    		
    if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
    	return;
    var v = day_diff == 0 && (
    		diff < 60 && "just now" ||
    		diff < 120 && "1 minute ago" ||
    		diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
    		diff < 7200 && "1 hour ago" ||
    		diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
    	day_diff == 1 && "Yesterday" ||
    	day_diff < 7 && day_diff + " days ago" ||
    	day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
    if (!v)
    	window.console && console.log(time);
    return v ? v : '';
}
