/**
 * 
 * JavaScript API for the Iowa Sex Offender Registry (c) 2009 - State of
 * Iowa - Department of Criminal Investigation (c) 2009 - State of Iowa -
 * Department of Administrative Services
 *  
 * @author DAS/ITE JC
 * @authored 2009
 * 
 * This is an example API for use by anyone to access to the Iowa SOR.
 * This code is BETA, and will likely change over time.
 *
*/

// stub for iowa object 
if (typeof IOWA == 'undefined') {
	 window['IOWA'] = {};
}

// iowa sor object
IOWA.SOR = function() {
	
	this.version = '1.0.0';
	this.criteria = null;
	this.callback = null;
	this.getVersion = function () {
		return this.version;
	};
	
	// removes random from criteria, replaces with lat and lon
	this.updateCriteria = function() {
		if (this.criteria.get('lat') == 'random') {
			this.criteria.set('lat',IOWA.SOR.RESPONSE.searched_lat);
			this.criteria.set('lon',IOWA.SOR.RESPONSE.searched_lon);
			this.criteria.set('range',IOWA.SOR.RESPONSE.searched_range);
		}
	};
	
	// gets the first page of a search
	this.getFirstPage = function() {
		
		if (IOWA.SOR.RESPONSE.page>1) {
			this.updateCriteria();
			this.criteria.page(1);
			this.execute(this.url,this.criteria, this.callback);
		return true;
		} else {
			return false;
		}
	};
	
	// gets the next page of a search
	this.getNextPage = function() {
	
		if (IOWA.SOR.RESPONSE.page<IOWA.SOR.RESPONSE.pages) {
			this.updateCriteria();
			this.criteria.page(parseInt(IOWA.SOR.RESPONSE.page,10)+1);
			this.execute(this.url,this.criteria, this.callback);
			return true;
		} else {
			return false;
		}
		
	};
	
	// gets the last page of a search
	this.getLastPage = function() {
		
		if (IOWA.SOR.RESPONSE.page<IOWA.SOR.RESPONSE.pages) {
			this.updateCriteria();
			this.criteria.page(parseInt(IOWA.SOR.RESPONSE.pages,10));
			this.execute(this.url,this.criteria, this.callback);
			return true;
		} else {
			return false;
		}
	};
	
	// gets the previous page of a search
	this.getPreviousPage = function() {
		if (IOWA.SOR.RESPONSE.page>1) {
			this.updateCriteria();
			this.criteria.page(parseInt(IOWA.SOR.RESPONSE.page,10)-1);
			this.execute(this.url,this.criteria, this.callback);
			return true;
		} else {
			return false;
		}
	};
	
	// executes a search using criteria created by the user
	this.execute = function(url, criteria, callback) {
	
		this.url = url;
		this.criteria = criteria;
		this.callback = callback;
		IOWA.SOR.CALLBACK = callback;
		var script = document.createElement('script');
		script.src = url + '?' + criteria;
		document.getElementsByTagName('head')[0].appendChild(script); 
		
	};
	
	// gets the list of recently updated registrants
	this.getRecentUpdates = function(url, callback) {
	
		var criteria = new IOWA.SOR.CRITERIA();
		criteria.set('updated','-1 week');
		criteria.limit(20);
		this.url = url;
		this.criteria = criteria;
		this.callback = callback;
		IOWA.SOR.CALLBACK = callback;
		var script = document.createElement('script');
		script.src = url + '?' + criteria;
		document.getElementsByTagName('head')[0].appendChild(script); 
		
	};	
	
};

// used to make cross domain calls work with the sor callback api
IOWA.SOR.RESPONSE = null;
IOWA.SOR.CALLBACK = null;
IOWA.SOR.CALLBACKPROXY = function (response) {

	// call the user defined callback and pass data
	eval('IOWA.SOR.RESPONSE = ' + response + ';');	
	IOWA.SOR.CALLBACK(IOWA.SOR.RESPONSE);
	
}

// sor criteria object
IOWA.SOR.CRITERIA = function () {
		
		this.isor  = null;
		this.lastname  = null;
		this.firstname  = null;
		this.middlename  = null;
		this.gender  = null;
		this.city  = null;
		this.surroundingcity  = null;
		this.state    = null;
		this.county    = null;
		this.postalcode    = null;
		this.surroundingpostal    = null;
		this.height   = null;
		this.weight   = null;
		this.useraddress   = null;
		this.usercity   = null;
		this.userstate   = null;
		this.userpostal   = null;
		this.lat   = null;
		this.lon   = null;
		this.range   = null;
		this.updated   = null;
		this["Paging->Limit"] = null;
		this["Paging->Page"] = null;
		this["Paging->SortBy"] = null;
		this["Paging->SortOrder"] = null;
		this.callback = 'IOWA.SOR.CALLBACKPROXY';
		
		// limit clause
		this.limit = function(value) {
			if (!isNaN(value)) {
				this["Paging->Limit"] = value;
			}
		}
		
		// page setting
		this.page = function(value) {
			if (!isNaN(value)) {
				this["Paging->Page"] = value;
			}
		}
		
		// sort and order setting
		this.sort = function(by,order) {
			if (typeof this[by] !='undefined') {
				this["Paging->SortBy"] = by;
				this["Paging->SortOrder"] = order;
			}
		}
		
		// setter for criteria values
		this.set = function(item,val,additional) {
			if (typeof this[item] == 'undefined') {
				alert('IOWA.SOR.CRITERIA: Invalid Critera: ' + item);
				throw "InvalidCriteria";
			}
			this[item] = val;
			if (typeof additional != 'undefined' && typeof this['surrounding' + item] != 'undefined' && additional==IOWA.SOR.CRITERIA.SURROUNDING) {
				this['surrounding' + item] = additional;
			} else {
				if (additional==IOWA.SOR.CRITERIA.NOTEQUAL) {
					this[item] = '!' + this[item];
				}
			}
		}
		
		// getter for criteria value
		this.get = function(item) { 
			return this[item] ;
		}
		
		// clear a criteria item
		this.clear = function(item) { 
			this[item] = null;
		}
		
		// toString criteria to restful conversion (automatic, you don't have to call this)
		this.toString = function () {
			
			var request = '';
			for(var param in this) {
				if (typeof this[param]!='undefined' && (typeof this[param] == 'string' || typeof this[param] == 'number')) {
					if (request.length>0) {
						request+='&';
					}
					request += param + '=' + escape(this[param]);
				}
			}
			return request;			
		}
};

// constants
IOWA.SOR.CRITERIA.EQUAL = null;
IOWA.SOR.CRITERIA.NOTEQUAL = 0;
IOWA.SOR.CRITERIA.SURROUNDING = 1;
