WidgetJobboard = new function() {
	var DomainName;
	var QueryStringOptions;
	var DivWidget;	// not used
	var MainUrl;
	var TotalJobs;
	var JobsPerPage;
	var CurrentPageIndex;

	this.setOptions = function(str) {
		pairs = str.split(/[?&]/)
		while (pair = pairs.shift()) {
			bits = pair.split('=')
			this.QueryStringOptions[bits[0]] = bits[1]
		}
	}

	this.ParseParameters = function()
	{
		this.QueryStringOptions = {} || {};
		scripts = document.getElementsByTagName('script')
		for (i = 0, l = scripts.length; i < l; i++) {
			if (match = scripts[i].src.match(/(.*)\/javascripts\/widget_job_board\.js\??(.*)?/)) {
				this.MainUrl = match[1]
				this.setOptions(match[2])
			}
		}

		this.DomainName = this.QueryStringOptions['domain'];
		this.DivWidget = typeof( window[ 'widgetname' ] ) != "undefined" ? widgetname : 'simplicant_widget'; //not used
		this.JobsPerPage = -1;
		this.CurrentPageIndex = -1;
	}

	/*Includes company javascript file to populate jobs listing*/
	this.createCompanyJs = function()
	{
		script = document.createElement('script');
		script.src = this.MainUrl + "/widget/javascripts/" + this.DomainName + ".js";
		document.getElementsByTagName('head')[0].appendChild(script);
	}

	/*Links company css file for widget*/
	this.linkCompanyCss = function()
	{
		stylesheet = document.createElement("link");
		stylesheet.rel = "stylesheet";
		stylesheet.type = "text/css";
		stylesheet.href = this.MainUrl + "/widget/stylesheets/" + this.DomainName + ".css";
		stylesheet.media = "all";
		document.lastChild.firstChild.appendChild(stylesheet);
	}

	this.createJobLink = function(div_obj, JobId, email, text, job_title, apply_as) {
		a = document.createElement('a');
                if(apply_as == 2)
                {
                    url = this.MainUrl + '/job/apply/' + JobId;
                    a.href = url;
                    a.appendChild(document.createTextNode('Apply for this job opening'));
                    //div_obj.appendChild(document.createTextNode('To apply for this job, '));
                    div_obj.appendChild(a);
                }
                else if(email == '' && text == '')
                {
                    url = this.MainUrl + '/job/detail/' + JobId;
                    a.href = url;
                    a.className='job_title';
                    a.appendChild(document.createTextNode(job_title));
                    div_obj.appendChild(a);
                }
                else if(email == '')
                {
                    url = this.MainUrl + '/job/apply/' + JobId;
                    a.href = url;
                    a.appendChild(document.createTextNode('Apply for this job opening'));
                    //div_obj.appendChild(document.createTextNode('To apply for this job, '));
                    div_obj.appendChild(a);
                }
                else
                {
                    url = 'mailto:' + email + '?subject=Job application for ' + job_title;
                    a.href = url;
                    a.appendChild(document.createTextNode(email));
                    div_obj.appendChild(document.createTextNode('To apply for this job, send in your resume to '));
                    div_obj.appendChild(a);
                }

	}

	this.setNavigation = function(div_obj)
	{
		previousLink = document.createElement('a');
		previousLink.href = 'javascript:;';
		previousLink.className = '';
		previousLink.setAttribute('onclick', 'getJobsList(' + (this.CurrentPageIndex - 1) + ')');
		previousLink.appendChild(document.createTextNode('< Previous'));

		nextLink = document.createElement('a');
		nextLink.href = 'javascript:;';
		nextLink.className = '';
		nextLink.setAttribute('onclick', 'getJobsList(' + (this.CurrentPageIndex + 1) + ')');
		nextLink.appendChild(document.createTextNode('Next >'));

		navigationLinks = document.createElement('p');
		navigationLinks.className = 'job_navigation';
		navigationLinks.appendChild(previousLink);
		navigationLinks.appendChild(nextLink);

		div_obj.appendChild(navigationLinks);

	}

	this.addPostedOn = function(div_obj, value)
	{
		posted_on = document.createElement('div');
		posted_on.className = 'job_posted_on';
                posted_on_label = document.createElement('strong');
                posted_on_label.innerHTML = 'Posted on: '
		posted_on.appendChild(posted_on_label);
		posted_on.appendChild(document.createTextNode(value));
		div_obj.appendChild(posted_on);
	}

	this.addLocation = function(div_obj, value)
	{
             if(value == '')
             {
                 return;
             }
		loc = document.createElement('div');
		loc.className = 'job_location';
                loc_label = document.createElement('strong');
                loc_label.innerHTML = 'Location: ';
		loc.appendChild(loc_label);
		loc.appendChild(document.createTextNode(value));
		div_obj.appendChild(loc);
	}

	this.addApplyNow = function(div_obj, id, email, job_title, apply_as)
	{
		apply_now = document.createElement('div');
		apply_now.className = 'job_apply_now';
                this.createJobLink(apply_now, id,email,"Apply", job_title, apply_as)
		div_obj.appendChild(apply_now);
	}

	this.addDescription = function(div_obj,value)
	{
		desc = document.createElement('div');
		desc.className = 'job_description';
                desc.innerHTML = value;
		div_obj.appendChild(desc);
	}

	this.addJobTitle = function(div_obj,value,id, mode)
	{
             if(mode == 1)
             {
		title = this.createJobLink(div_obj, id,'','',value, 1);
             }
             else
             {
                title = document.createElement('h2');
                title.innerHTML = value;
		title.className='job_title';
                div_obj.appendChild(title);
             }
	}
         
        this.addSimplicantLink = function(div_obj)
        {
            if(this.DomainName != 247){
                simplicant_logo_div = document.createElement('div');
                simplicant_link = document.createElement('a');
                simplicant_link.href = 'http://www.simplicant.com/';
                link_image = document.createElement('img');
                link_image.src = 'http://www.simplicant.com/images/poweredby.gif';
                link_image.width = 106;
                link_image.height = 29;
                link_image.alt = 'powered by simplicant';
                link_image.style.border='none';
                //link_image.style.display='block !important'; //Note: this property not works in Internet Explorer
                simplicant_link.appendChild(link_image);
                simplicant_logo_div.appendChild(simplicant_link);
                simplicant_logo_div.className = 'simplicant_logo';
                div_obj.appendChild(simplicant_logo_div);
            }
        }
        
        this.addHeading = function(div_obj, text)
        {
            heading = document.createElement('h1');
            heading.innerHTML = text;
            heading.className= 'heading';
            div_obj.appendChild(heading);
        }

	this.setLayout = function()
	{
		document.write("<div id='simplicant_widget' class='simplicant_widget'></div>");
		widget_div = document.getElementById('simplicant_widget');
	}

	this.populateDIV = function(mode, apply_as, show_location, show_posted_on, widget_heading, data)
	{
		if (!data) return;

		main_div = document.getElementById('simplicant_widget');
                 
                if(widget_heading != '')
                {
                    this.addHeading(main_div, widget_heading);
                }


		for (var i = 0, length = data.length; i < length; i++) {

			job_div = document.createElement('div');
                        job_div.className = 'job_div';
                        if(mode == 1)
                        {
                            this.addJobTitle(job_div,data[i].JobTitle,data[i].Id, mode)
                        }
                        else if(mode == 2)
                        {
                            this.addJobTitle(job_div,data[i].JobTitle,data[i].Id, mode)
                            if(show_location == true)
                            {
                                this.addLocation(job_div, data[i].Location);
                            }
                            if(show_posted_on == true)
                            {
                                this.addPostedOn(job_div, data[i].PostedOn);
                            }
                            this.addDescription(job_div,data[i].Description);
                            this.addApplyNow(job_div, data[i].Id, data[i].Email, data[i].JobTitle, apply_as);
                        }

			main_div.appendChild(job_div);
		}
                this.addSimplicantLink(main_div);
	}


	this.init = function() {
		/*note: calling order is important. */
		this.ParseParameters();	//Initialize all available parameters.

		this.setLayout();

		this.linkCompanyCss();	// links CSS file for DIV display layout

		this.createCompanyJs();	// creates javascript file to call jobs populate in a DIV
	}
}

WidgetJobboard.init();
