/* Autofusion JWS API v0.3.1 by Josh Lizarraga */
/* Copyright 2009 Autofusion.com */

/* AF Library */

if(typeof(YAHOO.AUTOFUSION) == "undefined"){
	YAHOO.namespace("AUTOFUSION");
}
if(typeof(YAHOO.AUTOFUSION.items) == "undefined"){
	YAHOO.AUTOFUSION.items = new Object();
}

/* Firebug Console Helper */
(function(){
	if(!window.console || !console.firebug){
		var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
		window.console = {};
		for (var i = 0; i < names.length; ++i)
			window.console[names[i]] = function() {}
	}
})();

/* JWSAPI */

function JWSAPI(confID){
	var that = this;
	this.JSON = new Object();
	this.requests = new Array();
	this.asyncSuccess = function (pObj){
		if(pObj.responseText != ""){
			this.JSON = YAHOO.lang.JSON.parse(pObj.responseText);
		} else {
			this.JSON = {};
			console.warn("JSON response was empty.");
		}
		if(typeof(pObj.argument["callback"]) != "undefined"){
			if(typeof(pObj.argument["args"]) != "undefined"){
				pObj.argument["callback"](pObj.argument["args"]);
			} else {
				pObj.argument["callback"]();
			}
		}
	};
	this.asyncFailure = function (pObj){
		console.error("Asynchronous connection failed: " + pObj.statusText);
	};
	this.asyncJWS = function (pData, pCallback, pArgs){
		var oConfID = (confID) ? confID : "jws_api";
		try {
			var oJSON = YAHOO.lang.JSON.stringify(pData);
		}
		catch (e) {
			console.error("Failed to Stringify: " + e);
		}
		var oCallback = {
			success:  this.asyncSuccess,
			failure: this.asyncFailure,
			scope: this,
			argument: new Object()
		};
		if(pCallback){
			oCallback.argument["callback"] = pCallback;
		}
		if(pArgs){
			oCallback.argument["args"] = pArgs;
		}
		var oParamString = "conf_id=" + oConfID + "&json=" + oJSON;
		var oRequest = YAHOO.util.Connect.asyncRequest("POST", "/AF2/JWS_Matchmaker.php", oCallback, oParamString);
		that.requests.push(oRequest);
	};
	this.setPref = function (pPref, pCallback, pArgs){
		console.info("JWSAPI: setPref()");
		this.asyncJWS({"action":"setPref","pref":pPref}, pCallback, pArgs);
	};
	this.getPref = function (pCallback, pArgs){
		console.info("JWSAPI: getPref()");
		this.asyncJWS({"action":"getPref"}, pCallback, pArgs);
	};
	this.setSinglePref = function (pPref, pValue, pCallback, pArgs){
		console.info("JWSAPI: setSinglePref(" + pPref + ")");
		this.asyncJWS({"action":"setSinglePref","option":pPref,"value":pValue}, pCallback, pArgs);
	};
	this.setParam = function (pParam, pCallback, pArgs){
		console.info("JWSAPI: setParam()");
		this.asyncJWS({"action":"setParam","param":pParam}, pCallback, pArgs);
	};
	this.getParam = function (pCallback, pArgs){
		console.info("JWSAPI: getParam()");
		this.asyncJWS({"action":"getParam"}, pCallback, pArgs);
	};
	this.addSingleParam = function (pParam, pValue, pCallback, pArgs){
		console.info("JWSAPI: addSingleParam(" + pParam + ")");
		this.asyncJWS({"action":"addSingleParam","filter":pParam,"value":pValue}, pCallback, pArgs);
	};
	this.delSingleParam = function (pParam, pValue, pCallback, pArgs){
		console.info("JWSAPI: delSingleParam(" + pParam + ")");
		this.asyncJWS({"action":"delSingleParam","filter":pParam,"value":pValue}, pCallback, pArgs);
	};
	this.clearSingleParam = function (pParam, pCallback, pArgs){
		console.info("JWSAPI: clearSingleParam(" + pParam + ")");
		this.asyncJWS({"action":"clearSingleParam","filter":pParam}, pCallback, pArgs);
	};
	this.getFilter = function (pFilter, pCallback, pArgs){
		console.info("JWSAPI: getFilter(" + pFilter + ")");
		this.asyncJWS({"action":"getFilter","filter":pFilter}, pCallback, pArgs);
	};
	this.getMatchCount = function (pCallback, pArgs){
		console.info("JWSAPI: getMatchCount()");
		this.asyncJWS({"action":"getMatchCount"}, pCallback, pArgs);
	};
	this.getMatch = function (pPage, pCallback, pArgs){
		console.info("JWSAPI: getMatch(" + pPage + ")");
		this.asyncJWS({"action":"getMatch","page":pPage}, pCallback, pArgs);
	};
	this.getVehicle = function (pVIN, bInv, pCallback, pArgs){
		if(bInv == true){
			console.info("JWSAPI: getVehicle(VIN)");
			this.asyncJWS({"action":"getVehicle","vin":pVIN}, pCallback, pArgs);
		} else {
			console.info("JWSAPI: getVehicle(car_id)");
			this.asyncJWS({"action":"getVehicle","car_id":pVIN}, pCallback, pArgs);
		}
	};
	this.getSavelist = function (pCallback, pArgs){
		console.info("JWSAPI: getSavelist()");
		this.asyncJWS({"action":"getSavelist"}, pCallback, pArgs);
	};
	this.setSave = function (pName, pCallback, pArgs){
		console.info("JWSAPI: setSave(" + pName + ")");
		this.asyncJWS({"action":"setSave","name":pName}, pCallback, pArgs);
	};
	this.getSave = function (pName, pCallback, pArgs){
		console.info("JWSAPI: getSave(" + pName + ")");
		this.asyncJWS({"action":"getSave","name":pName}, pCallback, pArgs);
	};
	this.delSave = function (pName, pCallback, pArgs){
		console.info("JWSAPI: delSave(" + pName + ")");
		this.asyncJWS({"action":"delSave","name":pName}, pCallback, pArgs);
	};
	this.cancel = function (pCallback){
		console.warn("JWSAPI: cancel()");
		if(that.requests.length > 0){
			if(YAHOO.util.Connect.isCallInProgress(that.requests[that.requests.length - 1])){
				YAHOO.util.Connect.abort(that.request, pCallback);
			} else {
				console.warn("JWSAPI: Most recent call has already completed.");
			}
		} else {
			console.error("JWSAPI: No calls have been made!");	
		}
	};
};