/**
 * Tippables
 */
Tippables = {
	
	init: function( baseElement, baseForm )
	{
		var tippables = typeof( baseElement ) == 'undefined' ? $( '.tippable' ) : $( '.tippable', baseElement ),
			tippables_count = tippables.length;

		while ( tippables_count -- )
		{
			var node = tippables[ tippables_count ],
				$node = $(node);
			

			node.tip = node.tip ? node.tip : node.title;
			//node.title = '';

			if ( !node.value )
			{
				node.value = node.tip;
				$node.removeClass( 'modified' );
			}
			else
				$node.addClass( 'modified' );

			$node.bind( 'focus', Tippables.__focus );
			$node.bind( 'keydown', Tippables.__focus	);
			$node.bind( 'click', Tippables.__focus );

			$node.bind( 'blur', Tippables.__blur );
		}
		
		this.initForms( baseForm );
	},
	
	__focus: function()
	{
		$this = $(this);
		if ( $this.hasClass( 'modified' ) )
			return;
		$this.addClass( 'modified' );
		this.value = '';
	},
	
	__blur: function()
	{
		$this = $(this);
		if ( this.value )
			return;
		this.value = this.tip;
		$this.removeClass( 'modified' );
	},
	
	
	initForms: function( baseForm )
	{
		var forms = typeof( baseForm ) !== 'undefined' ? [ baseForm ] : $( 'form' ),
			forms_count = forms.length;

		while ( forms_count -- )
			$(forms[ forms_count ]).bind( 'submit', function() { Tippables.clear( this ) } );
	},
	
	clear: function( baseElement )
	{
		var tippables = typeof( baseElement ) == 'undefined' ? $( '.tippable' ) : $( '.tippable', baseElement ), 
			tippables_count = tippables.length;

		while ( tippables_count -- )
		{
			var node = tippables[ tippables_count ];

			if ( !$(node).hasClass( 'modified' ) )
			{
				node.value = '';
			}
		}
	}
}
