YAHOO.namespace('ephraim.utilities');

YAHOO.ephraim.utilities = {
    twitterCallback : function(obj) {
        var twitters = obj;
    	var statusHTML = "";
    	var username = "";
    	for (var i=0; i<twitters.length; i++){
    		username = twitters[i].user.screen_name
    		statusHTML += ('<p class="status">'+YAHOO.ephraim.utilities.linkify(twitters[i].text)+'</p> <cite>Posted '+YAHOO.ephraim.utilities.relative_time(twitters[i].created_at)+' from ' + twitters[i].source +'</cite>')
    	}
    	statusHTML += '<p class="right"><a href="http://twitter.com/ephraimYC" target="_blank">More Tweets</a></p>';
    	document.getElementById('twitter').innerHTML = statusHTML; 
    },
    relative_time : function(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';
        }
    },
    linkify : function (string, options){
        if(!options) options = {};
        if(!options.limit) options.limit = 10;
        if(!options.tagFill) options.tagFill = '';

        var regex = /((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi;

        string = string.replace(regex, function(value){
            value = value.toLowerCase();
            var m = value.match(/^([a-z]+:\/\/)/);
            var nice;
            var url;

            if(m)
            {
                nice = value.replace(m[1],'');
                url = value;
            }
            else
            {
                nice = value;
                url = 'http://' + nice;
            }

            return '<a href="' + url + '"' + (options.tagFill != '' ? (' ' + options.tagFill) : '')+ '>' + value + '</a>';
        });

        return string;
    },
    clearFocus : function(){
        if (this.value=='Enter Amount') {
            this.value = '';
            this.style.color = '#000';   
        } else if (this.value == '') {
            this.value = 'Enter Amount';
            this.style.color = '#444'
        }
    },
    init: function(){
      //set up the inputs to clear on focus
      var clears = YAHOO.util.Dom.getElementsByClassName('clearfocus', 'input');
      for (var i = clears.length - 1; i >= 0; i--){
        clears[i].onfocus = YAHOO.ephraim.utilities.clearFocus;
        clears[i].onblur = YAHOO.ephraim.utilities.clearFocus;
      };
      var carousel = new YAHOO.widget.Carousel("slider", { 
        autoPlayInterval: 3000, isCircular: true, numVisible: 1,  animation: { speed: 0.8 }
      }); 
      
      carousel.render(); // get ready for rendering the widget 
      carousel.show();   // display the widget
      carousel.startAutoPlay();
    }
}
YAHOO.ephraim.utilities.init();
//YAHOO.util.Event.onDOMReady(YAHOO.ephraim.utilities.init);