// Thanks, Dr Nic! :)

CTBFeed = new function() {
	var ctb_url;
	var ROOT = 'ctb_job_list';

	this.setOptions = function(str) {
		pairs = str.split(/[?&]/)
		while (pair = pairs.shift()) {
			bits = pair.split('=')
			this.options[bits[0]] = bits[1]
		}
	}
	
	// Determine the domain to use for further requests
	// by introspecting in the current document how this
	// script was loaded
	this.findMyDomain = function() {
		scripts = document.getElementsByTagName('script')
		for (i = 0, l = scripts.length; i < l; i++) {
			if (match = scripts[i].src.match(/(.*)\/javascripts\/jobfeed\.js\??(.*)?/)) {
				this.ctb_url = match[1]
				if (match[2]) this.setOptions(match[2])
			}
		}
	}

	// Insert a stylesheet into the current document
	this.requestStylesheet = function() {
		stylesheet = document.createElement("link");
		stylesheet.rel = "stylesheet";
		stylesheet.type = "text/css";
		stylesheet.href = this.ctb_url + "/stylesheets/jobfeed.css";
		stylesheet.media = "all";
		document.lastChild.firstChild.appendChild(stylesheet);
	}

	// Insert a call to CTBFeed.serverResponse() with data  
	// from the server into the head of the current document
	this.requestContent = function() {
		script = document.createElement('script');
		script.src = this.ctb_url + "/positions/list.js";
		document.getElementsByTagName('head')[0].appendChild(script);
	}

	this.serverResponse = function(data) {
		if (!data) return;
		div = document.getElementById(ROOT);
		dl = document.createElement('dl')
		for (var i = 0, length = data.length; i < length; i++) {
			dt = document.createElement('dt');
			dt.className = 'job_title'
			dt.appendChild(document.createTextNode(data[i].name));
			
			dd = document.createElement('dd')
			
			desc_lines = data[i].description.split(/\r\n\r\n/)
			for (var l = 0, dll = desc_lines.length; l < dll; l++) {
				p = document.createElement('p')
				break_lines = desc_lines[l].split(/\r\n/)
        if ((bl = break_lines.length) > 1) {
          for (b = 0, bl = break_lines.length; b < bl; b++) {
            p.appendChild(document.createTextNode(break_lines[b]))
            if (b < bl) {
              p.appendChild(document.createElement('br'))
            }
          }
        } else {
          p.appendChild(document.createTextNode(desc_lines[l]));
        }
				dd.appendChild(p)
			}
			dd.className = 'job_description'
			dd.appendChild(this.submissionLink(data[i].token))
			
			dl.appendChild(dt)
			
			dl.appendChild(dd)
		}
		if (data.length == 0) {
			dt = document.createElement('dt');
			dt.appendChild(document.createTextNode('There are no open positions at this time.'));
			dl.appendChild(dt);
		}
		div.appendChild(dl)
		
		a = document.createElement('a')
		a.title = "Track, share, and rate resumes with Catch the Best"
		a.href = 'http://catchthebest.com'
		a.appendChild(document.createTextNode('Catch the Best'))
		p = document.createElement('p')
		p.className = 'promo'
		p.appendChild(document.createTextNode('Job list powered by '))
		p.appendChild(a)
		div.appendChild(p)
		
		div.style.display = 'block'; // make element visible
		div.style.visibility = 'visible'; // make element visible
	}

	// Generate links for applying based on whether the submission
	// form should be filled out on-site via an iframe or at the
	// CTB site
	this.submissionLink = function(token) {
		a = document.createElement('a')
		url = this.ctb_url + '/apply/' + token

		if (this.options['source']) {
			url += '/' + this.options['source']
		}
		
		if (this.options['iframe']) {
			url += '?iframe=true'
			a.target = this.options['iframe']
		}
		a.href = url
		a.className = 'job_link'
		a.appendChild(document.createTextNode('Apply'))
		return a
	}

	this.init = function() {
		this.options = window.CTBOptions || {}
		this.findMyDomain();
		this.requestStylesheet();
		document.write("<div id='" + ROOT + "' style='display: none' class='jobfeed'></div>");
		this.requestContent();
		var no_script = document.getElementById('no_script');
		if (no_script) { no_script.style.display = 'none'; }
	}
}

CTBFeed.init();
