$(function(){
	
	var username;
		
	$("ul.sf-menu").superfish({ 
		delay: 1200
	}); 

	$.get("/cgi/list.fcgi?rm=username&time=" + new Date().getTime(),
		function(data){
		if (data)
		{
			username = data;
			$('.save_job').fadeIn('slow');
			$("#logoutBox").html('<a href="/cgi/list.fcgi?rm=advance">SEARCH JOBS</a>&nbsp; | &nbsp;<a href="/cgi/list.fcgi?rm=account">MY ACCOUNT</a>&nbsp; | &nbsp;<a href="/cgi/list.fcgi?rm=logout">LOGOUT</a>');
		}
		else
		{
			$("#membersLoginBox").show();
		}
	});

	$('input[type="radio"],input[type="checkbox"]').css({'width':'20px', 'border':'none'});
	
	$('#country').load('/cgi/list.fcgi?rm=search&get_location=1');
    $('#category').load('/cgi/list.fcgi?rm=search&get_category=1');
	
	// if (! $('#user_table_form_custom_global_location_id').val()) {
	// 	$('#user_table_form_location').html('<option selected="selected"> -- Any Location -- </option>').attr('disabled', 'disabled');
	// 	$('#user_table_form_area,#user_table_form_custom_global_location_id').html('<option selected="selected"> -- Any Area -- </option>').attr('disabled', 'disabled');
	// }
	
	// if (! $('#user_table_form_custom_global_location_id').val()) {
	// 	$('#user_table_form_sub_category,#user_table_form_custom_global_classification_id').html('<option selected="selected"> -- Any Sub Category -- </option>').attr('disabled', 'disabled');
	// }	
	
	$('#user_table_form_country').change(function(){
		if ($('#user_table_form_country').val() != ''){
			$('#user_table_form_location').html('<option selected="selected"> -- Any Location -- </option>').attr('disabled', 'disabled').load('/cgi/list.fcgi', {'rm':'search', 'get_location':1, 'country': $('#user_table_form_country').val()}, function()
            {
                $('#user_table_form_location').removeAttr('disabled');
            });
		}
		else {
			$('#user_table_form_location').html('<option selected="selected"> -- Any Location -- </option>').attr('disabled', 'disabled');
			$('#user_table_form_area').html('<option selected="selected"> -- Any Area -- </option>').attr('disabled', 'disabled');
		}
	});
	
	$('.hotJob').remove();
	
	$.ajax({
	  url: '/cgi/list.fcgi?rm=hot',
  		success: function(d) {
			$('h2:contains("HOT Jobs")').after(d);
 		}
	});

	$('#user_table_form_location').change(function(){
		if ($('#user_table_form_location').val()  != '' && $('#user_table_form_country').val()  != ''){
			
			if ($('#user_table_form_area').attr('name'))
			{
				$('#user_table_form_area').html('<option selected="selected"> -- Any Area -- </option>').attr('disabled', 'disabled').load('/cgi/list.fcgi', {'rm':'search', 'get_location':1, 'country': $('#user_table_form_country').val(), 'location': $('#user_table_form_location').val()}, function() {
					$('#user_table_form_area').removeAttr('disabled');
	            });
			}
			else
			{
				$('#user_table_form_custom_global_location_id').html('<option selected="selected"> -- Any Area -- </option>').attr('disabled', 'disabled').load('/cgi/list.fcgi', {'rm':'search', 'as_id':1, 'get_location':1, 'country': $('#user_table_form_country').val(), 'location': $('#user_table_form_location').val()}, function() {
					$('#user_table_form_custom_global_location_id').removeAttr('disabled');
	            });
			}			
		}
		else {
			$('#user_table_form_area,#user_table_form_custom_global_location_id').html('<option selected="selected"> -- Any Area -- </option>').attr('disabled', 'disabled');
		}
	});
	
	$('.cover_letter_type').click(function(){		
		if ($(this).val() == 'text')
		{
			$('#user_table_form_cover_letter_container').html('<textarea cols="55" id="user_table_form_cover_letter" maxlength="255" name="user_table_form_cover_letter" rows="10"></textarea>');
		}
		else
		{
			$('#user_table_form_cover_letter_container').html('<input id = "user_table_form_cover_letter" name ="user_table_form_cover_letter" maxlength="255" type = "file">');
		}		
	});
	
	
	$('.save_job').click(function(){
		var that = $(this);
		var ori_text = that.text();
		that.text('Saving...');
		$.ajax({
			url: '/cgi/list.fcgi',
		 	data: {'rm':'save', 'object': that.attr('rel')},
	  		success: function(d) {
				that.text('Saved');
				// setTimeout(that.text(ori_text), 1500);
	 		}
		});
		return false;
	});
	
	$('#user_table_form_category').change(function(){
		if ($('#user_table_form_category').val() != ''){
			if ($('#user_table_form_sub_category').attr('name')) {
				$('#user_table_form_sub_category').html('<option selected="selected"> -- Any Sub Category -- </option>').attr('disabled', 'disabled').load('/cgi/list.fcgi', {'rm':'search', 'get_category':1, 'category': $('#user_table_form_category').val()}, function(){
					$('#user_table_form_sub_category').removeAttr('disabled');
				});
			}
			else {
				$('#user_table_form_custom_global_classification_id').attr('disabled', 'disabled').load('/cgi/list.fcgi', {'rm':'search', 'as_id':1, 'get_category':1, 'category': $('#user_table_form_category').val()}, function(){
					$('#user_table_form_custom_global_classification_id').removeAttr('disabled');
				});
			}
		}
		else {
			$('#user_table_form_sub_category,#user_table_form_custom_global_classification_id').html('<option selected="selected"> -- Any Sub Category -- </option>').attr('disabled', 'disabled');
		}
	});
	
	$(function() {
		$('textarea.tinymce').tinymce({
			script_url : '/persistent/js/tiny_mce/tiny_mce.js',
			theme : "advanced",
			plugins : "safari,pagebreak,iespell,paste,nonbreaking",
			theme_advanced_buttons1 : "undo,redo,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,link,unlink,|,pastetext,pasteword",
			theme_advanced_buttons2 :"",
			theme_advanced_buttons3 : "",
			paste_create_paragraphs : false,
			paste_create_linebreaks : true,
			paste_use_dialog : false,
			paste_auto_cleanup_on_paste : true,
			convert_urls : false,
			valid_elements : "p,br,-ol[type|compact],-ul[type|compact],-li,strong/b,em/i,strike,u"			
		});
	});
	
	$('.colorbox').colorbox({scrolling:false});	
	$('.report').colorbox({scrolling:false, iframe:true, width:'50%', height:'80%'});
	
	$('.sort_by').click(function(){
		var href;
		if (window.location.href.match(/sort_by/)) {
			href = window.location.href.replace(/&?sort_by=.*?(&|$)/, '');
		}
		else
		{
			href = window.location.href;
			if (! href.match(/\?/))
			{
				href = href + '?';
			}
		}
		
		if ($(this).val() == 'timestamp') {
			window.location = href + '&sort_by=timestamp%20desc';
		}
		else {
			window.location = href + '&sort_by=' + $(this).val();
		}
		
		$('.sort_by').attr('disabled', 'disabled');
	});
	
});


function get_locations()
{
  if ($('#country').val())
  {
    $('#location').html('<option selected="selected"> -- Any Location -- </option>').attr('disabled', 'disabled').load('/cgi/list.fcgi', {'rm': 'search', 'get_location' : 1, 'country': $('#country').val()}, function(){
		$('#location').removeAttr('disabled');
	});
  }
}


function check_keywords()
{
  var kw = document.jobSearch.q.value;
  if (kw == 'Enter Keyword(s)')
  {
    document.jobSearch.q.value = '';
  }
  return true;
}

