jQuery(function(){
	
	var username;
		
	jQuery("ul.sf-menu").superfish({ 
		delay: 1200
	});	
	
	jQuery.ajax({
	  url: '/cgi/auth/list.fcgi?rm=username',
	  cache: false,
	  complete: function(jqXHR, textStatus) {
		if (textStatus === 'error') {
			jQuery("#membersLoginBox").show();
			jQuery('.applyNowButton').show();
		}
	  },
	  success: function(data){
		if (data) {			
			jQuery("#logoutBox").html('<a href="/cgi/list.fcgi?rm=advance">SEARCH JOBS</a>&nbsp; | &nbsp;<a href="/cgi/auth/list.fcgi?rm=account">MY ACCOUNT</a>&nbsp; | &nbsp;<a href="/cgi/list.fcgi?rm=logout">LOGOUT</a>');			
			
			jQuery('.save_job').each(function(){
				var job_id = jQuery(this).attr('rel');
				var that = jQuery(this);
				jQuery.ajax({
					url: '/cgi/auth/list.fcgi?rm=job_saved&job_id=' + job_id,
					cache: false,
					success: function(saved){
						if (! saved) {
							that.fadeIn('slow');
						}
					}
				});
			});
			
			jQuery('.applyNowButton').each(function(){
				var job_id = jQuery(this).attr('rel');
				var that = jQuery(this);
				
				jQuery.ajax({
					url: '/cgi/auth/list.fcgi?rm=job_applied&job_id=' + job_id,
					cache: false,
					success: function(applied){
						if (! applied) {
							that.fadeIn();						
							if (that.hasClass('internal')) {
								that.attr('href', '/cgi/auth/list.fcgi?rm=apply&action=Apply&object=' + job_id);
							}
						}
					}
				});
			});
		}
		else
		{
			jQuery("#membersLoginBox").show();
		}
	  }
	});

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

	jQuery('#user_table_form_location').change(function(){
		if (jQuery('#user_table_form_location').val()  != '' && jQuery('#user_table_form_country').val()  != ''){
			
			if (jQuery('#user_table_form_area').attr('name'))
			{
				jQuery('#user_table_form_area').html('<option selected="selected"> -- Any Area -- </option>').attr('disabled', 'disabled').load('/cgi/list.fcgi', {'rm':'search', 'get_location':1, 'country': jQuery('#user_table_form_country').val(), 'location': jQuery('#user_table_form_location').val()}, function() {
					jQuery('#user_table_form_area').removeAttr('disabled');
	            });
			}
			else
			{
				jQuery('#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': jQuery('#user_table_form_country').val(), 'location': jQuery('#user_table_form_location').val()}, function() {
					jQuery('#user_table_form_custom_global_location_id').removeAttr('disabled');
	            });
			}			
		}
		else {
			jQuery('#user_table_form_area,#user_table_form_custom_global_location_id').html('<option selected="selected"> -- Any Area -- </option>').attr('disabled', 'disabled');
		}
	});
	
	jQuery('.cover_letter_type').click(function(){		
		if (jQuery(this).val() == 'text')
		{
			jQuery('#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
		{
			jQuery('#user_table_form_cover_letter_container').html('<input id = "user_table_form_cover_letter" name ="user_table_form_cover_letter" maxlength="255" type = "file">');
		}		
	});
	
	
	jQuery('.save_job').click(function(){
		var that = jQuery(this);
		var ori_text = that.text();
		that.text('Saving...');
		jQuery.ajax({
			url: '/cgi/auth/list.fcgi',
		 	data: {'rm':'save', 'object': that.attr('rel')},
	  		success: function(d) {
				that.text('Saved');
				// setTimeout(that.text(ori_text), 1500);
	 		}
		});
		return false;
	});
	
	jQuery('#user_table_form_category').change(function(){
		if (jQuery('#user_table_form_category').val() != ''){
			if (jQuery('#user_table_form_sub_category').attr('name')) {
				jQuery('#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': jQuery('#user_table_form_category').val()}, function(){
					jQuery('#user_table_form_sub_category').removeAttr('disabled');
				});
			}
			else {
				jQuery('#user_table_form_custom_global_classification_id').attr('disabled', 'disabled').load('/cgi/list.fcgi', {'rm':'search', 'as_id':1, 'get_category':1, 'category': jQuery('#user_table_form_category').val()}, function(){
					jQuery('#user_table_form_custom_global_classification_id').removeAttr('disabled');
				});
			}
		}
		else {
			jQuery('#user_table_form_sub_category,#user_table_form_custom_global_classification_id').html('<option selected="selected"> -- Any Sub Category -- </option>').attr('disabled', 'disabled');
		}
	});
	
		
	if (typeof(jQuery.fn.tinymce) === 'function') {
		jQuery('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,|,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[class|style|mce_style],a[href|title],s,span[class|style|mce_style]"
		});
	}
	
	// jQuery('.colorbox').colorbox({scrolling:false});
	if (jQuery.fn.colorbox) {
		jQuery('.report').colorbox({scrolling:false, iframe:true, width:'50%', height:'80%'});
	}
	
	jQuery('.sort_by').click(function(){
		var href;
		if (window.location.href.match(/sort_by/)) {
			href = window.location.href.replace(/&?sort_by=.*?(&|jQuery)/, '');
		}
		else
		{
			href = window.location.href;
			if (! href.match(/\?/))
			{
				href = href + '?';
			}
		}
		
		if (jQuery(this).val() == 'timestamp') {
			window.location = href + '&sort_by=timestamp%20desc';
		}
		else {
			window.location = href + '&sort_by=' + jQuery(this).val();
		}
		
		jQuery('.sort_by').attr('disabled', 'disabled');
	});
	
});


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


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


