$(function(){
	$('#search').submit(function(){
		var $this = $(this);
		
		var $sex = $this.find('#search_sex').val() == 'male' ? 'man' : 'vrouw';
		
		var $seeking = $this.find('#search_seeking').val() == 'male' ? 'man' : 'vrouw';
		
		$this.attr('action', 'resultaten/' + $seeking + '-zoekt-' + $sex);
	});
	
	$('a.delete').click(function(){
	return confirm('Weet je zeker dat je dit bericht wil verwijderen?');
	});
	
	$('#search #search_age_from').change(function(){
		var $this = $(this);

		if($this.val() == 18){
			$('#search #search_age_to option:first').attr('selected', 'selected');
		}else{
			$('#search #search_age_to option[value=' + $this.val() + ']').next().attr('selected', 'selected');
		}
	});
	
	$('.pagination a').live('click', function(){
		var $this = $(this);
		
		$('html, body').animate({scrollTop:0}, 'slow');
		
		var $values = {};
		
		if($('#search_sex [selected]').size()){
			$values = $('#search').serialize();
		}
		
		$.post($this.attr('href'), $values, function(data){
			$('#center').html(data);
		})
		
		return false;
	});
	
	$('#other_images img').click(function(){
		
		$this = $(this);
		
		if($this.attr('class')){
			$('#main_img img').attr('src', $this.attr('class'));

			$('#profile_back').show('normal');
		}
		
		return false;
	});
	
	$('#profile_back').click(function(){
		
		$('#main_img img').attr('src', $('#main_img img').attr('class'));
		
		$('#profile_back').hide('normal');
	})
	
	$('.switch_male, .switch_female, .switch_new').click(function(){
		var $this = $(this);
		
		var $class = $(this).attr('class');
		
		$('.box_switch_male, .box_switch_female, .box_switch_new').hide();
		
		$('.box_' + $class).show();
	});
	
	$('textarea#text').attr('value', $('textarea#text').attr('class'));
	
	$('textarea#text').focus(function(){
		if($(this).attr('value') == $(this).attr('class')){
			$(this).attr('value', '');
		}
	});
	
	$('textarea#text').blur(function(){
		if(!$(this).attr('value')){
			$(this).attr('value', $(this).attr('class'));
		}
	});
	
	$('.message_send form').submit(function(){
		if($('#text').val() == $('#text').attr('class')){
			
			alert('Het bericht is leeg!');
			
			return false;
		}
	})
	
	$('textarea#text').keyup(function(){
		
		var $total = 250;
		
		if(this.value.length >= $total) {
			//handle the over the limit part here
			this.value = this.value.substring(0, $total);
		}
		
		$('#countdown').text($total-this.value.length + '/' + $total);
	});
	
	// $('div').pngFix();
});
