

var ConditionalTool = Class.create(ToolTip, {
	open : function($super){
		if (this.link.up('tr.active')) return;
		$super();
	}
});

var JobPage = Class.create({
	initialize : function(container, linksel){
		this.container = container;
		this.links = container.select(linksel);
		this.setup();
		this.overlay = $('overlay_resume');
	},
	setup : function(){
		this.links.each(function(el){
			el.observe('click', this.call.bind(this));
			var p = el.up('td');
			new ConditionalTool(p, p.down('.tooltip'));
		}.bind(this));
		this.container.select('.btn_close').invoke('observe','click', this.close.bind(this));
		this.applyform = $('applyform');
		//new FormLink($$('.view'), this.applyform);
	},
	call : function(ev){
		ev.stop();
		if (this.active) return;
		this.active = true;
		var link = ev.element();
		var row = link.up('tr');
		if (row.hasClassName('active')) {
			this.close(ev);
			return;
		}
		new Ajax.Updater(row, link.readAttribute('href').split('#')[0], {parameters:{x:new Date().getTime()},insertion : Insertion.After, onComplete:function(r){this.load(row)}.bind(this)});
	},
	load : function(el){
		var pos = el.addClassName('active').next('tr');
		pos.down('.listing_detail').blindDown({duration:1, afterFinish:function(){this.active = false}.bind(this)});
		pos.select('.submitresume').each(function(e){new FormPop(e, this.overlay);}.bind(this));
	},
	close : function(ev){
		ev.stop();
		var row = ev.element().up('tr');
		row.next('tr').down('.listing_detail').blindUp({afterFinish:function(){row.removeClassName('active'); row.next('tr').remove(); this.active = false;}.bind(this)});
	}
});

document.observe('dom:loaded', function(){
	if ($('position_listing'))
		new JobPage($('position_listing'), 'tr.position .title a');
});