/* 
	custom.js
	===============================================================
 All custom front end scripts, manually comped together 
	to save HTTP requests
	
	This file is automatically compressed by The Munger
	To sort, search for '@ '
	===============================================================
*/

/* 
	@ inline_image_hovers
	---------------------------------------------------------------	
	Automatically sets up hoverstate for images
	- whose filename includes '-off.'
	- who are wrapped in link elements
	
	.hover() not used as it doesn't support the keyboard
	_______________________________________________________________
*/		

	function inline_image_hovers()
	{ 
		$.each( $('img[src*="-off."]'),
			function(i,item)
			{
				var imgoffsrc = $(item).attr('src');
				var imgoversrc = imgoffsrc.replace( '-off.', '-over.' );	
				
				// preload over state
				$('<img />').attr({ src : imgoversrc });
				
				// store states with link parent
				$(item).parents('a:first')
				.data( 'imgoffsrc', imgoffsrc ).data( 'imgoversrc', imgoversrc )
				.bind( 'mouseenter focus',
					function () 
					{
						$(this).find('img:first').attr({ src : $(this).data('imgoversrc') });
					}
				)
				.bind( 'mouseleave blur',
					function () 
					{
						$(this).find('img:first').attr({ src : $(this).data('imgoffsrc') });
					}
				);
			} // end function
		); // end each
	}
	
/* 
	@ inline_input_hovers
	---------------------------------------------------------------	
	Automatically sets up hoverstate for inputs
	- whose src image filename includes '-off.'
	
	.hover() not used as it doesn't support the keyboard	
	_______________________________________________________________
*/		

	function inline_input_hovers()
	{ 
		$.each( $('input[src*="-off."]'),
			function(i,item)
			{
				var imgoffsrc = $(item).attr('src');
				var imgoversrc = imgoffsrc.replace( '-off.', '-over.' );	
				
				// preload over state
				$('<img />').attr({ src : imgoversrc });
				
				// store states with link parent
				$(item)
				.data( 'imgoffsrc', imgoffsrc ).data( 'imgoversrc', imgoversrc )
				.bind( 'mouseenter focus',
					function () 
					{
						$(this).attr({ src : $(this).data('imgoversrc') });
					}
				)
				.bind( 'mouseleave blur',				
					function () 
					{
						$(this).attr({ src : $(this).data('imgoffsrc') });
					}
				);
			} // end function
		); // end each
	}	
	
/* 
	@ hintify
	---------------------------------------------------------------	
	Add a default value to an input.
	On focus/blur, toggle this text, unless the user has entered
	their own input.
	
	v 0.1 | DS 26.07.2010		
	
	Examples:	
	$(input).hintify()
	_______________________________________________________________
*/		

	(function($) {
		$.fn.hintify = function() 
		{
			return this.each(function() {
																
				// IF ALREADY ENHANCED, EXIT
				if ( $(this).data('linkified') )
				{
					return;
				}			
				
				var id = $(this).attr('id');
				var default_text = $('label[for="' + id + '"]').text();
				
				// 29.07.2010 added check for existence of label
				if (!default_text)
				{
					return;
				}
		
				$(this).data( 'default_text', default_text );
				
				// ON LOAD
				// if the input is empty, add the default text		
				if ( $(this).val() == '' )		
				{
					$(this).val( $(this).data('default_text') );	
				}			
				
				// ADD CSS HOOK
				// if the input did or now does contain the default text, apply a class to lighten the colour of this
				if ( $(this).val() == default_text )
				{
					$(this).addClass('form-text-default');
				}
				
				// ADD LISTENERS TO INPUT
				// on focus/blur
				$(this)
				.bind( 'focus', 
					function()
					{
						// if the input contains the default text, remove it
						if ( $(this).val() == $(this).data('default_text') )
						{
							$(this)
							.removeClass('form-text-default')
							.val( '' );
						}				
					}
				)
				.bind( 'blur',
					function()
					{
						// if the input is empty, add the default text
						if ( $(this).val() == '' )
						{
							$(this)
							.addClass('form-text-default')
							.val( $(this).data('default_text') );
						}				
					}
				);	
				
				// ADD SUBMIT LISTENER TO PARENT FORM			
				$(this).parents('form:first')
				.data( 'input', $(this) )
				.data( 'default_text', default_text )
				.bind( 'submit', 
					function() 
					{				
						// remove the default text from the input before submitting the form
					
						var input2 = $(this).data('input');
						var default_text2 = $(this).data('default_text');
						
						if ( $(input2).val() == default_text2 )
						{
							$(input2).val( '' );
						}
						
						return true;
					}
				);			
				
				// MARK AS DONE					
				$(this).data('hintified', true);		
				
			});			
		};
	})(jQuery);	

/*
 @@ arrowify()
 ---------------------------------------------------------------
 Inject an arrow
	
 This can be done with CSS2.1 :after
 but content inserted that way is not 'inline' 
 and thus not restyled by Cufon

 Usage:
 jQuery('.more a[href]').arrowify();       

*/

  (function(jQuery) 
  {                       
    jQuery.fn.arrowify = function() 
    {
      // IF ALREADY ENHANCED, EXIT
      if ( jQuery(this).data('arrowified') )
      {
       return;
      }
						var html_str = '<span class="arrow">&nbsp;&rsaquo;</span>';
      // ADD THE ARROW
      jQuery(this).append( jQuery(html_str)      );

      // MARK AS DONE                                                        
      jQuery(this).data('arrowified', true);                                 
    };
  }
  )(jQuery);


/* 
	@ $(document).ready
	---------------------------------------------------------------
	Run all functions
	_______________________________________________________________
*/			
	
	// on dom load
	$(document).ready(function() 
	{
		if ( $('#header form').length > 0 )
		{
			$('#header form input:text').hintify();		
		}
		
		inline_input_hovers();
		
	 // wrap div around all #content horizontal rule's
  $('#content hr').wrap('<div class="hr" />');
		
  // IE 7 and lower
		if($.browser.msie && parseFloat($.browser.version) <= 7){
					// arrowify
		   jQuery('.more').arrowify();
					
					// attribute selector 'name'
     $('a[name]').addClass('attr-name')
		}
		
		Cufon.replace( $('#content-header h1'), { fontFamily: 'knockout' } );
		Cufon.replace( $('#t-home .col h2'), { fontFamily: 'knockout' } );
	
	});		
	
