// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
Effect.SlideRightOutOfView = function(element) {
  element = $(element);
  element.makeClipping();
  return new Effect.Scale(element, 0,
    Object.extend({ scaleContent: false, 
      scaleY: false, 
      restoreAfterFinish: true,
      afterFinishInternal: function(effect) {
        effect.element.remove().undoClipping();
      } 
    }, arguments[1] || {})
  );

}

SlidingBanner = Class.create();
Object.extend(SlidingBanner.prototype, {
  initialize: function(interval, pagesize, pagecount, containerelement, newpageurl, newpageparam, startpage){
	this.interval = interval;
	this.page_size = pagesize;
    this.page_count = pagecount; 
	this.container_element = $(containerelement); 
	this.new_page_url = newpageurl; 
	this.new_page_param = newpageparam;
	this.current_page = startpage;
  },
  play: function(){
	new PeriodicalExecuter(function(pe) {
	  childs = this.container_element.getElementsByClassName('slidingElement')
	  if (childs.size()<(2*this.page_size)) {
		if (this.current_page==this.page_count) {
			this.current_page=0
		}
		this.current_page=this.current_page+1;
		new Ajax.Request(this.new_page_url+"?"+this.new_page_param+"="+this.current_page, {
		  method: 'get',
		  onSuccess: function(transport) {
			new Insertion.Bottom(this.container_element, transport.responseText);
		  }.bind(this)
		});
	  }
	  var el = childs.find(function(e){
			return !e.hasClassName('fixed_banner');
	   });
	  new Effect.SlideRightOutOfView(el);
	}.bind(this), this.interval);
  }
});

function do_search(base_url) {
  t = $F('terms');
  a = $F('search_area');
  r = $F('search_region');
  
  url = base_url
  if (a!="-1") {
	url += "/area_name-"+a;
  }
  if (r!="-1") {
	url += "/offer_location_region-"+r;
  }
  if (t) {
	url += "?terms="+t
  }
  if (url == base_url) {
	alert("Seleziona almeno un criterio di ricerca.");
  } else {
	document.location = url
/*	$('cercaOfferte').action = url;
	$F('terms').value = '';
	$F('search_area').value = -1;
	$F('search_region').value = -1;
	$('cercaOfferte').submit();
*/  }
  return false;
}
function fill_skills_combo_for_category(category, profile_skill_id) {
	if (!profile_skill_id) {
		profile_skill_id = 'new';	
	}
	container_id = 'profile_skill_'+profile_skill_id+'_skills_container';
	if (category) {
	    new Ajax.Updater(container_id, 'competenze;skills_for_category', 
	        {parameters: { category_id: category, profile_skill_id: profile_skill_id }, asynchronous:true, method:'get' }
	        );		
	}
	
}
function fill_regions_combo_for_country(country, profile_location_id) {
	if (!profile_location_id) {
		profile_location_id = 'new';	
	}
	container_id = 'profile_location_'+profile_location_id+'_regions_container';
	if (country) {
	    new Ajax.Updater(container_id, 'luoghi;regions_for_country', 
	        {parameters: { country_id: country, profile_location_id: profile_location_id }, asynchronous:true, method:'get' }
	        );		
	}
	
}
function fill_provinces_combo_for_region(region, profile_location_id) {
	if (!profile_location_id) {
		profile_location_id = 'new';	
	}
	container_id = 'profile_location_'+profile_location_id+'_provinces_container';
	if (region) {
	    new Ajax.Updater(container_id, 'luoghi;provinces_for_region', 
	        {parameters: { region_id: region, profile_location_id: profile_location_id }, asynchronous:true, method:'get' }
	        );		
	}
	
}

function fill_study_subtypes_combo_for_type(type, profile_study_id) {
	if (!profile_study_id) {
		profile_study_id = 'new';	
	}
	container_id = 'profile_study_'+profile_study_id+'_subtypes_container';
	if (type) {
	    new Ajax.Updater(container_id, 'studi;subtypes_for_type', 
	        {parameters: { type_id: type, profile_study_id: profile_study_id }, asynchronous:true, method:'get' }
	        );		
	}
	
}

function fill_studies_combo_for_subtype(subtype, profile_study_id) {
	if (!profile_study_id) {
		profile_study_id = 'new';	
	}
	container_id = 'profile_study_'+profile_study_id+'_studies_container';
	if (subtype) {
	    new Ajax.Updater(container_id, 'studi;studies_for_subtype', 
	        {parameters: { subtype_id: subtype, profile_study_id: profile_study_id }, asynchronous:true, method:'get' }
	        );		
	}
	
}

function fill_profile_branches_for_sector(sector_id, profile_id) {
   if (sector_id&&sector_id!='' && sector_id!=undefined ) {
	   if (profile_id&&profile_id!='' && profile_id!=undefined ) {
		   new Ajax.Updater('branches_container', profile_id+'/comparti;branches_for_sector', 
		       {parameters: { sector_id: sector_id }, asynchronous:true, method:'get' }
		       );		
	   }
   }
}
function fill_branches_for_sector(sector_id) {
   if (sector_id&&sector_id!='' && sector_id!=undefined ) {
	   new Ajax.Updater('branches_container', 'esperienze;branches_for_sector', 
	       {parameters: { sector_id: sector_id }, asynchronous:true, method:'get' }
	       );		
   }
}
function fill_areas_for_branch(branch_id) {
   if (branch_id&&branch_id!='' && branch_id!=undefined ) {
	   new Ajax.Updater('areas_container', 'esperienze;areas_for_branch', 
	       {parameters: { branch_id: branch_id }, asynchronous:true, method:'get' }
	       );		
   }
}
function fill_aliases() {
   new Ajax.Request('esperienze;job_title_aliases', 
       {parameters: Form.serialize('experience_form'), asynchronous:true, method:'get' }
       );		
}
function fill_experience_size_type_description(selected_title){
   if (selected_title.value&&selected_title.value!='' && selected_title.value!=undefined ) {
	   new Ajax.Request('esperienze;size_type_description', 
	       {parameters: Form.serialize('experience_form'), asynchronous:true, method:'get' }
	       );	
		$('experience_job_definition').value = selected_title.text;	
		Element.update('description_container', job_descriptions_from_ajax[selected_title.value]);	
   } else {
		$('experience_job_definition').value = '';	
		Element.update('description_container','');		
   }
}

var job_descriptions_from_ajax;

function show_loading(container) {
	$(container).innerHTML = '<img src="/images/ajax_small.gif" style="border:transparent 1px;" />'
}
