/*
 * Requires: jquery, profile.js, section.js, tags.js
 */
function Series(id, name, edit, plants, plantIds) {
	this.name = name;
	this.id = id;
	this.edit = edit;
}
Series.prototype = {
	tags: null,
	
	info: function() {
		var self = this;
		if (this.edit) {
			if (Profiles[this.getId()] && Profiles[this.getId()].setUpdate) {}
			else Profiles[this.getId()] = this;
			
			$(document).ready(function(){
				self.date = self.getElt('closingdate').datepicker();
			});
		}
		else Profiles[this.getId()] = this;
	},
	
	setPlants: function(plants, plantIds){
		var self = this;
		if ($('#plants' + this.getId()).length > 0) {
			this.tags = new Tags('plants' + this.getId(), this.name + '[' + this.id + ']' + '[plant_id][]');
			this.tags.tagsOnClick(function(id, name) {
				new Profiles(id, 'Plant').get();
			});
			
			this.tags.addQuickSearch(function(name) {
				// second argument gets called after the ajax callback to gain access to the current profile object
				new Profiles('new', 'Plant').get(name, function(id, profile, tab){
				
					//Function called by Profile.update
					Profiles['Plant' + id].setUpdate(function(data){
					
						// update tags and its quicksearch
						self.tags.getQuickSearch().addOption(data.plant_id, data.name, QuickSearchTemplate(data.name));
						self.tags.addTag(data.plant_id, data.name);
						tab.remove();
						self.selectTab();
						
						//Update mainSearch
						MAINSEARCH.get().addOption(data.plant_id, data.name, QuickSearchTemplate(data.name, 'Plant'), 'Plant');
					});
				});
			});

			var search = this.tags.getQuickSearch();
			jQuery.each(plants, function(){
				search.addOption(this.plant_id, this.name, QuickSearchTemplate(this.name));
				if (plantIds[this.plant_id])
					self.tags.addTag(this.plant_id, this.name);
			});
		}
	}
};
Series.prototype = jQuery.extend(Series.prototype, Profile.prototype);