jQuery.fn.label_to_value = function(){
	jQuery.each(this, function(){
		var label = jQuery('label[for=' + jQuery(this).attr('id') + ']');
		
		if(!jQuery(this).val()){ jQuery(this).val(label.html()); }
		
		label.hide();
		
		jQuery(this).blur(function(){
			if(jQuery(this).val() == '' || jQuery(this).val() == label.text()){
				jQuery(this).val(label.text());
			}
		});
		
		jQuery(this).focus(function(){ 
			if(jQuery(this).val() == label.text()){
				jQuery(this).val('');
			}else{
				this.select();
			}
		});
	});
}

jQuery.fn.overlay_tooltips = function(child){
	jQuery(this).find(child).hide();
	
	jQuery(this).mouseover(function(){
		jQuery(this).find(child).show();
		jQuery(this).find(child).addClass('overlayTooltip');
	});
	
	jQuery(this).mouseout(function(){
		jQuery(this).find(child).hide();
		jQuery(this).find(child).removeClass('overlayTooltip');
	});
}

jQuery.fn.set_time_of_day = function(){
	var date = new Date;
	
	var hours = date.getHours();
	
	var new_class = 'day';
	
	if(hours >= 17 && hours <= 19){ new_class = 'dusk'; }
	
	if(hours > 19 || hours <= 6){ new_class =  'night'; }
	
	if(new_class != ''){ jQuery(this).addClass(new_class); }
}

jQuery.calculate_footer = function(){
	if(jQuery('#container').height() < jQuery(window).height()){
		jQuery('#page-content .inner').css({ height: jQuery(window).height() - 331 });
	}else{
		jQuery('#page-content .inner').height('auto');
	}
}

$(document).ready(function(){	
	//$('body').set_time_of_day();
	
	if (disableLazyLoad == false)
    {
        /*$('img').lazyload({ effect : 'fadeIn' });*/
    }
	
	$('#age').change(function(){
		if($(this).val() == 'u13'){
			$('label[for=email]').text('Parents Email Address');
		}else{
			$('label[for=email]').text('Email Address');
		}
	});
	
	$('#masthead input').label_to_value();
	
	$('#badgeTooltips li').overlay_tooltips('h3');
	
	$('#allBadgesModal li').overlay_tooltips('h3');
	
	$.calculate_footer();
	
	$(window).resize(function(){
		$.calculate_footer();
	});
	
	$('#buddies-truncated li:gt(19)').hide();
	
	if($('#buddies-truncated ul').children().size() > 20){ $('#show-buddies').html('<a href="#">Show all buddies</a>'); }
	
	$('#show-buddies').live('click', function(e){
		e.preventDefault();
		$('#buddies-truncated li').show();
		$('#footer').hide();
	});
	
	$('#badge_key_trigger').click(function(e){
		e.preventDefault();
				
		$('#profile-badges').show('fast');
	});
	
	$('#profile-badges').live('click', function(){
		$(this).hide('fast');
	});
	
	$('#competition-items li:nth-child(4)').remove();
	
	$('#fav_film, #fav_book, #fav_music, #fav_tv_show, #fav_game').attr('maxlength', 36);
	
	$('#registration_form').submit(function(e){
		if($('#password').val() != $('#password_2').val()){
			e.preventDefault();
			
			alert('You must re-type your password identically.');
		}
	});
  
  $('#container-wrapper-inner').css('cursor', 'pointer');

  $('#container-wrapper-inner').click(function(event) {
      if (event.target == event.currentTarget) window.open('http://www.homesheephome2.com/');
  });

  $('#container').hover(function() {
      $('#container').css('cursor', 'auto');
  });
  
});
