/*
http://cssglobe.com/post/2494/using-form-labels-as-text-field-values
*/
this.label2value = function(){	
	var inactive = "inactive";
	var active = "active";
	var focused = "focused";

	$("#secondary form label, #module-container form label").each(function(){		
		obj = document.getElementById($(this).attr("for"));
		if(($(obj).attr("type") == "text") || ($(obj).attr("type") == "password") || (obj.tagName.toLowerCase() == "textarea")){			
			$(obj).addClass(inactive);			
			var text = $(this).text();
			$(this).css("display","none");			
			$(obj).val(text);
			$(obj).focus(function(){	
				$(this).addClass(focused);
				$(this).removeClass(inactive);
				$(this).removeClass(active);								  
				if($(this).val() == text) $(this).val("");
			});	
			$(obj).blur(function(){	
				$(this).removeClass(focused);													 
				if($(this).val() == "") {
					$(this).val(text);
					$(this).addClass(inactive);
				} else {
					$(this).addClass(active);		
				};				
			});				
		};	
	});		
};

/* James Padolseys take on Pretty Date: http://james.padolsey.com/javascript/recursive-pretty-date/ */
var niceTime=(function(){var a={second:1,minute:60,hour:3600,day:86400,week:604800,month:2592000,year:31536000};return function(e){e=+new Date(e);var f=((+new Date())-e)/1000,c,d;for(var b in a){if(f>a[b]){d=b}}c=f/a[d];c=Math.floor(f>a.hour?(Math.round(c*100)/100):Math.round(c));c+=" "+d+(c>1?"s":"")+" ago";return c}})();

$(function() {
	
	// Convert labels to values of certain text inputs
	label2value();	
	
	// Home page stuff
	(function () {

		var $current = $('#billboard .on');
		var $default = $current;

		// helper function to switch to new nav item
		var setCurrent = function ($new) {
			$current.removeClass('on');
			$current = $new;
			$current.addClass('on');
		};

		// handle nav item mouseovers
		$('#billboard h3 a').mouseenter(function () {

			// get wrapper
			var $navItem = $(this).closest('div');

			// don't do anything if this is the current item 
			if ($navItem[0] == $current[0]) {
				return;
			}

			// change current status
			setCurrent($navItem);
		});

		// account for "default" nav item
		// if we leave the nav in general, revert to default
		$('#billboard').mouseleave(function (e) {
			setCurrent($default);
		});

	})();

	$("body#take-action #result-explanation").tabs();

	// AJAXY login features
	$("#login-dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 300,
		modal: true
	});

	$('li.login a').click(function() {
		$('#login-dialog').dialog('open');
	});
	
	// A new window/tab handler to replace target="_blank"
	$('a.pop').click(function(){
		window.open(this.href);
		return false;
	});	

});

