function jsonify(ob) {
    result = {}
    $.each(ob, function (name, value) {
        result[name] = JSON.stringify(value);
    });
    
    return result;
}

function getToday() {
    var now = new Date();
    var milliseconds = now.getTime();

    var date = new Date(milliseconds);
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var year = date.getFullYear();
    
    return month + "/" + day;
}

function getYesterday() {
    var now = new Date();
    var milliseconds = now.getTime();

    var date = new Date(milliseconds);
    var month = date.getMonth() + 1;
    var day = date.getDate() - 1;
    var year = date.getFullYear();
    
    return month + "/" + day;
}

function secondsToDate(seconds) {
    var milliseconds = seconds * 1000;
    var date = new Date(milliseconds);
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var year = date.getFullYear();
    
    return month + "/" + day;
}

function secondsToTime(seconds) {
    var milliseconds = seconds * 1000;
    var date = new Date(milliseconds);
    var hours = date.getHours();
    var minutes = date.getMinutes();

    if (minutes < 10) { minutes = "0" + minutes; }
    
    if (hours > 11) { 
        if (hours > 13) { hours = hours - 12; }
        var time = hours + ":" + minutes + " PM"; 
    } else {
        var time = hours + ":" + minutes + " AM";
    }
    
    return time;
}

jQuery.fn.noLabel = function(label) {
    return this.each(function() {
        if ($(this).val() == "") { $(this).val(label); }
        $(this).bind('keydown', function () { if ($(this).val() == label) { $(this).val(''); } })
               .bind('blur', function () { if ($(this).val() == '') { $(this).val(label); } });
    });    
};

jQuery.fn.tip = function (text, direction) {
    var el = $(this);
    el.unbind('mouseenter').bind('mouseenter', function () {
        // should use direction for north, west, etc. tooltips
        var tip = $('<div class="tip"><div class="tip-peak"></div><div class="tip-bubble"><span class="tip-content"></span></div></div>');
        var bubble = tip.find('.tip-bubble');
        var content = tip.find('.tip-content');

        el.data('tip', tip);

        // Check which element to append the tooltip to
        if (el.parents('#content').length) {
            el.parent().append(tip);
            tip.css({ top: el.position().top + el.outerHeight(true) + 3,
                          left: el.position().left + parseInt(el.css('margin-left')) + 2 });
        } else {
            $('body').append(tip);
            tip.css({ top: el.offset().top + el.outerHeight(true) + 3,
                      left: el.offset().left + 2 });
        }

        // Set content and measure width and height
        content.html(text);
        bubble.css({width: Math.min(bubble.width(), content.outerWidth(true) + 2),
                    height: content.outerHeight(true)});
    
        $('.tip').show();
    }).unbind('mouseleave').bind('mouseleave', function () {
        el.removeData('tip');
        $('.tip').remove();
    });
};

jQuery.fn.limitMaxlength = function(options){
	var settings = jQuery.extend({
		attribute: "maxlength",
		onLimit: function(){},
		onEdit: function(){}
	}, options);

	// Event handler to limit the textarea
	var onEdit = function(){
		var textarea = jQuery(this);
		var maxlength = parseInt(textarea.attr(settings.attribute));

		if(textarea.val().length > maxlength){
			textarea.val(textarea.val().substr(0, maxlength));

			// Call the onlimit handler within the scope of the textarea
			jQuery.proxy(settings.onLimit, this)();
		}

		// Call the onEdit handler within the scope of the textarea
		jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length);
	}

	this.each(onEdit);

	return this.keyup(onEdit)
				.keydown(onEdit)
				.focus(onEdit)
				.live('input paste', onEdit);
}

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 9/11/2008
 * @author Ariel Flesler
 * @version 1.4
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);
