(function() {
	var QueryString = function(queryString) {
		this.props = [];
		this.propLookup = [];
		if (queryString) {
			this.parse(queryString);
		}
	}

	QueryString.prototype = {
		parse: function(queryString) {
			var args = queryString.replace(/^\?/, '').split('&');
			for (var i = 0; i < args.length; i++) {
				var arg = args[i].split('=');
				this.set(arg[0], decodeURIComponent(String(arg[1]).replace(/\+/g, ' ')));
			}
		},
		set: function(prop_name, prop_value) {
			if (prop_value == null) {
				return this;
			}

			var value = { name: prop_name, value: prop_value };
			var ns_prop = this.nsProp(prop_name);
			if (this.get(prop_name) != null) { // key exists, overwrite value
				this.props[this.propLookup[ns_prop]] = value;
			} else {
				this.props.push(value);
				this.propLookup[ns_prop] = (this.props.length - 1);
			}
			return this;
		},
		isset: function(prop_name) {
			return (this.get(prop_name) == null) ? false : true;
		},
		nsProp: function(property_name) {
			return 'indeed_' + property_name;
		},
		get: function(prop_name) {
			// we add a string to the beginning of the name so it does not overwrite or conflict
			// with any existing methods/properties (like sort, length, etc.)
			var ns_prop = this.nsProp(prop_name);

			if (this.propLookup[ns_prop] != null) {
				return this.props[this.propLookup[ns_prop]].value;
			}
			return null;
		},
		toString: function() {
			for (var i = 0, return_value = []; i <  this.props.length; ++i) {
				return_value[i] = this.props[i].name + "=" + encodeURIComponent(this.props[i].value).replace(/\%20/, '+');
			}
			return "?" + return_value.join('&');
		}
	};

	// get parameters and set defaults
	var indeed_base_url = 'http://www.indeed.com/p/jobsite.php';
	var indeedJobSearchHeight = window.indeedJobSearchHeight || 2000;
	var indeedJobSearchScroll = window.indeedJobSearchScroll || 'auto';
	var indeedJobSearchStyleSheet = window.indeedJobSearchStyleSheet || null;
	var indeedJobSearchDefaultWhat = window.indeedJobSearchDefaultWhat || null;
	var indeedJobSearchDefaultWhere = window.indeedJobSearchDefaultWhere || null;
	var indeedJobSearchLimit = window.indeedJobSearchLimit || null;
	var indeedJobSearchLinkTarget = window.indeedJobSearchLinkTarget || null;
	var indeedJobSearchChannel = window.indeedJobSearchChannel || null;


    var query =  new QueryString(window.top.location.search);
    query.set('pid', window.indeed_publisher_id).set('ts', new Date().getTime()).set('baseurl', window.location.protocol + '//' + window.location.host + window.location.pathname);

    // set default action if none...
	if (!query.isset('action')) {
		query.set('action', 'search');
	}

	if (!query.isset('css')) {
		query.set('css', indeedJobSearchStyleSheet);
	}

    // Application views ( search, view, post, preview, pay )
    if (query.get('action') == 'search') {
		if (!query.isset('q')) {
			query.set('q', indeedJobSearchDefaultWhat);
		}
		if (!query.isset('l')) {
			query.set('l', indeedJobSearchDefaultWhere);
		}
    	if (!query.isset('limit')) {
    		query.set('limit', indeedJobSearchLimit);
    	}
		if (!query.isset('ijs_linktarget')) {
    		query.set('ijs_linktarget', indeedJobSearchLinkTarget);
    	}
    	if (!query.isset('ijs_channel')) {
    		query.set('ijs_channel', indeedJobSearchChannel);
    	}
	}

    document.write('<div id="jobsiteApplication" style="width:100%; padding:0px; margin:0px;"><ifr' + 'ame' +
    ' id="jobsiteFrame"' +
    ' name="indeed_jobsearch_frame"' +
    ' src="' + indeed_base_url + query.toString() +'"'+
    ' frameborder="0"' +
    ' marginwidth="0"' +
    ' marginheight="0"' +
    ' vspace="0"' +
    ' hspace="0"' +
	' scrolling="'+indeedJobSearchScroll+'"' +
    ' style="border: 0; width:100%; padding:0px; margin:0px; height: ' + indeedJobSearchHeight + 'px;"' +
    ' ></ifr' + 'ame></div>');

})();

