﻿$.fn.searchBox = function(options) {
    var defaults = {
        initialText: "Search"
    };
    options = $.extend(defaults, options);

    return this.each(function() {
    	this.value = options.initialText;
        $(this).focus(function() {

            if (this.value == options.initialText)
                this.value = "";
            else
                this.select();
        });
        $(this).blur(function() {
            if (this.value == "")
                this.value = options.initialText;
        });
    });
};

$.fn.equalizeHeight = function(options) {
    var defaults = {};
    options = $.extend(defaults, options);
    
   	var heightContributors = new Array();
	heightContributors[0] = "padding-top";
	heightContributors[2] = "padding-bottom";
	heightContributors[3] = "border-width-top";
 	heightContributors[4] = "border-width-bottom";

    tallest = 0;
    this.each(function() {
    	var $this = $(this);
        thisHeight = $this.height();
        for(var i=0; i<heightContributors.length; i++)
    	{
    		var val = $this.css(heightContributors[i]);
    		if(typeof val == "string" && val != "")
    		{
    			thisHeight += parseInt(val);
			}
		}
        if(thisHeight > tallest)
        {
            tallest = thisHeight;
        }
    });
    return this.each(function(){
    	var $this = $(this);
    	var newHeight = tallest;
    	for(var i=0; i<heightContributors.length; i++)
    	{
    		var val = $this.css(heightContributors[i]);
    		if(typeof val == "string" && val != "")
    		{
    			newHeight -= parseInt(val);
    		}
    	}
    	$this.height(newHeight);
    });
};

$.fn.addFirstItemClass = function(options){
    var defaults = {itemSelector:'li',firstItemClassName:'first'};
    options = $.extend(defaults, options);
	return this.each(function() {
		$(this).find(options.itemSelector+':first').addClass(options.firstItemClassName);
	});
};

$.fn.getTweet = function(options) {
	var	defaults = {
		userName: null,
		loaderHtml: "Loading tweet..."
	};

	var o = $.extend(defaults, options);
	
	return this.each(function() {
		var c = $(this);

		// remove alternative content
		c.empty();
		
		var preLoaderHTML = $(o.loaderHtml);
		c.append(preLoaderHTML);
		
		$.getJSON("http://twitter.com/statuses/user_timeline/"+o.userName+".json?count=1&callback=?", function(data) {
			$(preLoaderHTML).remove();
			c.html("<span class=\"time\">" + twitterRelativeTime(data[0].created_at) + "</span> " + convertUrlsToLinks(data[0].text));
		});
	});
};

function twitterRelativeTime(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}

function convertUrlsToLinks(input)
{
	return input.replace(/(^|\s)(https?:\/\/[\w_.\/-]+)(\s|$)/, '$1<a href="$2" target="_blank">$2</a>$3');
}