$(function(){
	TWZ_application();
	TWZ_homeHover();
	TWZ_searchbox();
	TWZ_field_examples();
	TWZ_clickable();
	TWZ_forms();
	TWZ_addCheckboxClass();
	TWZ_setupCountDown();
});

$.ajaxSetup({ 
  beforeSend: function(xhr){
    xhr.setRequestHeader("Accept", "text/javascript");
  }
});

function TWZ_application(){
	$("#tabsCntr").tabs();
	setTimeout(hide_flash, $('.flash').text().length * 150);
	$('.flash').mousemove(hide_flash);
	
	
	$('.show_on_submit').hide();
	$('form').submit(function(){
		$('.hide_on_submit').hide();
		$('.show_on_submit').show();
	});
	
	$('#limit_to_selection_form input:checkbox').click(function(){
		$('#limit_to_selection_form').submit();
	});
}

function TWZ_searchbox(){
	var $expander = $('#extended_search_expander');
	
	$expander.data('open', $expander.text());
	$expander.data('close', $expander.attr('rel'));

	if($('#extended_search_expander').is('.extended')){
		_expand_searchbox(false);
	}
	else{
		_close_searchbox(false);
	}
	
	$('#extended_search_expander').click(function(){
		if($('#extended_search').is(':visible')){
			_close_searchbox(true);
		}
		else{
			_expand_searchbox(true);
		}
		
		return false;
	});
}

function _expand_searchbox(effect){
	if(effect){
		$('#extended_search').slideDown();
	}
	else{
		$('#extended_search').show();
	}
	$('#extended_search_expander').text($('#extended_search_expander').data('close'));
}

function _close_searchbox(effect){
	if(effect){
		$('#extended_search').slideUp();
	}
	else{
		$('#extended_search').hide();
	}
	$('#extended_search_expander').text($('#extended_search_expander').data('open'));
	$('#extended_search select').val('');
}

function hide_flash(){
	$('.flash').slideUp('slow', function(){
		$('.flash').remove();
	});
}

function TWZ_homeHover(){
	$("tr.job").hover(
			function(){$(this).addClass("trHover");},
			function(){$(this).removeClass("trHover"); }
		);
};

function TWZ_field_examples(){
	$('.field_example').each(function(){
		var $this = $(this);
		var $field = $($this.attr('rel'));

		if($field.val().length == 0){
			$field.val($this.text()).addClass('preview');
			$field.bind('focus', function(){
				$field.val('').unbind('focus').removeClass('preview');
			});
		}
		
		$this.parents('form:first').submit(function(){
			if($field.is('.preview')){
				$field.val('');
			}
		});
	});
}

function TWZ_clickable(){
	$('.clickable').click(function(){
		document.location.href = $(this).attr('rel');
		return false;
	});
}

function TWZ_forms(){
	$('input#job_asap').click(_update_job_asap).each(_update_job_asap);
}

function	TWZ_addCheckboxClass(){
	$('input:checkbox').addClass('checkbox');
}

function TWZ_setupCountDown(){
	$('textarea.countdown').each(function(){
		var $this = $(this);
		
		$this.data('countdown', $('span.countdown[rel=' + this.id + ']'));
		$this.data('maxlength', parseInt($this.attr('maxlength'), 10));
	}).keyup(_limit_length).each(_limit_length);
}

function _limit_length(){
	var $this = $(this);
	
	if($this.val().length > $this.data('maxlength')){
		$this.val($this.val().slice(0, $this.data('maxlength')));
	}
	$this.data('countdown').text($this.data('maxlength') - $this.val().length);
}

function _update_job_asap(){
	if(this.checked){
		$('div#job_not_asap').hide();
	}
	else{
		$('div#job_not_asap').show();
	}
}