deeplocal = {};

deeplocal.gumband = {};
deeplocal.gumband.categories = undefined;
deeplocal.gumband.customerId = undefined;
deeplocal.gumband.endpoint = "http://peanut.deeplocal.com:8080/gumbandbase/";

deeplocal.gumband.createPlace = function(placeName, placeDescription, placeAddress1, placeAddress2, placeCity, placeState, placeZip, placeCode, placeLatitude, placeLongitude, callback) {
	var query = "";
	query += "&place_lati=" + placeLatitude;
	query += "&place_longi=" + placeLongitude;
	query += "&place_address1=" + placeAddress1;
	if(typeof placeAddress2 != "undefined" && placeAddress2 !== null) {
		query += "&place_address2=" + placeAddress2;
	}

	query += "&place_city=" + placeCity;
	query += '&place_state=' + placeState;
	query += '&place_zip=' + placeZip;
	query += '&place_name=' + escape(placeName);
	query += '&place_description='+ escape(placeDescription);
	if(typeof placeCode != "undefined" && placeCode !== null) {
		query += '&place_code=' + placeCode;
	}
	
	jQuery.getJSON(deeplocal.gumband.endpoint + "place?action=add&customer=" + deeplocal.gumband.customerId + query + "&jsoncallback=?", callback);
};

deeplocal.gumband.updatePlace = function(placeId, placeName, placeDescription, placeAddress1, placeAddress2, placeCity, placeState, placeZip, placeCode, placeLatitude, placeLongitude, callback) {
	var query = "";
	query += "&place_id=" + placeId;
	query += "&place_lati=" + placeLatitude;
	query += "&place_longi=" + placeLongitude;
	query += "&place_address1=" + placeAddress1;
	if(typeof placeAddress2 != "undefined" && placeAddress2 !== null) {
		query += "&place_address2=" + placeAddress2;
	}

	query += "&place_city=" + placeCity;
	query += '&place_state=' + placeState;
	query += '&place_zip=' + placeZip;
	query += '&place_name=' + escape(placeName);
	query += '&place_description='+ escape(placeDescription);
	if(typeof placeCode != "undefined" && placeCode !== null) {
		query += '&place_code=' + placeCode;
	}

	jQuery.getJSON(deeplocal.gumband.endpoint + "place?action=edit&customer=" + deeplocal.gumband.customerId + query + "&jsoncallback=?", callback);
};

deeplocal.gumband.deleteEntity = function(entId, callback) {
	jQuery.getJSON(deeplocal.gumband.endpoint + "items?action=delete&customer=" + deeplocal.gumband.customerId + "&" + "entId=" + entId + "&jsoncallback=?", callback);
};

deeplocal.gumband.createEntity = function(name, description, placeId, categoryId, startDate, endDate, meta, callback, duid) {
	var paramNames = "";
	var params = "";

	for(var i in meta) {
		if(typeof meta[i] != "function") {
			paramNames += i + "|";
			params += i + "=" + meta[i] + "&";
		}
	}
	paramNames = paramNames.substr(0, paramNames.length -1);
	params = params.substr(0, params.length -1);

	var requestString = "";
	requestString += "action=store&";
	requestString += "customer=" + deeplocal.gumband.customerId + "&";
	requestString += "entity_name=" + escape(name) + "&";
	if(typeof duid != "undefined" && duid !== null) {
		requestString += "duid=" + duid + "&";
	} else {
		requestString += "duid=&";
	}

	if(typeof startDate != "undefined" && startDate !== null) {
		requestString += "start_date=" + startDate + "&";
	} else {
		requestString += "start_date=&";
	}

	if((typeof startDate != "undefined" && startDate !== null) && (typeof endDate != "undefined" && endDate !== null)) {
		requestString += "end_date=" + endDate + "&";
	} else {
		requestString += "end_date=&";
	}

	requestString += "entity_description=" + escape(description) + "&";
	requestString += "entity_category=" + categoryId + "&";
	requestString += "entity_place=" + placeId + "&";
	requestString += "param_names=" + paramNames + "&";
	requestString += params + "&";

	jQuery.getJSON(deeplocal.gumband.endpoint + "items?" + requestString + "jsoncallback=?", callback);
};

deeplocal.gumband.updateEntity = function(entityId, name, description, placeId, categoryId, startDate, endDate, meta, callback, oldPlaceId){
	var paramNames = "";
	var params = "";

	for(var i in meta){
		if(typeof meta[i] != "function"){
			paramNames += i + "|";
			params += i + "=" + meta[i] + "&";
		}
	}
	paramNames = paramNames.substr(0, paramNames.length -1);
	params = params.substr(0, params.length -1);

	var requestString = "";
	requestString += "action=update&";
	requestString += "customer=500&";
	requestString += "entity_id=" + entityId + "&";
	requestString += "entity_name=" + name + "&";
	if(typeof startDate != "undefined" && startDate !== null){
		requestString += "start_date=" + startDate + "&";
	} else {
		requestString += "start_date=&";
	}

	if(typeof endDate != "undefined" && endDate !== null){
		requestString += "end_end=" + endDate + "&";
	} else {
		requestString += "end_date=&";
	}

	requestString += "entity_description=" + description + "&";
	requestString += "entity_category=" + categoryId + "&";

	requestString += "entity_place=" + placeId + "&";
	if (typeof oldPlaceId != "undefined") {
		requestString += "entity_old_place=" + oldPlaceId + "&";
	}

	requestString += "param_names=" + paramNames + "&";
	requestString += params + "&";

	jQuery.getJSON(deeplocal.gumband.endpoint + "items?" + requestString + "jsoncallback=?", callback);
};

deeplocal.gumband.findEntites  = function(searchTerm, placeId, catId, startDate, endDate, callback){
	//Deprectaed. Use findFullEntities
	deeplocal.gumband.findFullEntities(searchTerm, placeId, catId, startDate, endDate, false, callback);
};

deeplocal.gumband.findFullEntities = function(searchTerm, placeId, catId, startDate, endDate, fullList, callback, beforeCreate, afterCreate, latitude, longitude, radius, meta, count, startIndex){
	var searchString = "customer=" + deeplocal.gumband.customerId + "&action=find";

	if(fullList){
		searchString +="&full=true";
	}else{
		searchString +="&full=false";
	}

	if(searchTerm){
		searchString += "&search=" + searchTerm;	
	}

	if(startDate){
		searchString += "&startDate=" + startDate;
	}

	if(endDate){
		searchString += "&endDate=" + endDate;
	}

	if (catId && (catId + "").indexOf('|') == -1) {
		searchString += "&catId=" + catId;
	} else if (catId && (catId + "").indexOf('|') != -1) {
		searchString += "&categoryList=" + catId;
	}

	if(placeId){
		searchString += "&placeId=" + placeId;
	}

	if(beforeCreate){
		searchString += "&beforeCreate=" + beforeCreate;
	}

	if(afterCreate){
		searchString += "&afterCreate=" + afterCreate;
	}

	if(latitude){
		searchString += "&latitude=" + latitude;
	}

	if(longitude){
		searchString += "&longitude=" + longitude;
	}

	if(radius){
		searchString += "&radius=" + radius;
	}

	var tags = [];
	var values = [];
	var i;

	for(i in meta){
		if(typeof meta[i] != "function"){
			tags[tags.length] = i;
			values[values.length] = meta[i];
		}
	}

	if (tags.length !== 0) {
		tags = tags.join('|');
		values = values.join('|');

		searchString += "&tags=" + tags;
		searchString += "&values=" + values;
	}

	if(count){
		searchString += "&getCount=" + count;
	}

	if(startIndex){
		searchString += "&startIndex=" + startIndex;
	}

	if(callback){
		searchString += "&jsoncallback=?";
	}

	jQuery.getJSON(deeplocal.gumband.endpoint + "items?" + searchString, callback);
};

deeplocal.gumband.getAllCategories = function(parentId, decendants, callback){
	var parentParam = '';
	if(parentId){
		parentParam = '&parentId=' + parentId;
	}

	var descendantClause = "";
	if(decendants){
		descendantClause = "&getChildren=1";
	}

	jQuery.getJSON(deeplocal.gumband.endpoint + "category?action=list" + descendantClause + "&customer=" + deeplocal.gumband.customerId + parentParam + "&jsoncallback=?", callback);
	return false;
};

deeplocal.gumband.createCategory = function(categoryName, categoryDescription, categoryParent, categoryIcon, callback){
	jQuery.getJSON(deeplocal.gumband.endpoint + "category?action=add&customer=" + deeplocal.gumband.customerId + "&category_name=" + categoryName + "&category_description=" + categoryDescription + "&jsoncallback=?", callback);
};

deeplocal.gumband.getEntitiesInCategory = function(catId, nearcode, latitude, longitude, start, count, callback){
	deeplocal.gumband.returnEntitiesInCategory(catId, nearcode, latitude, longitude, start, count, false, callback);
};

deeplocal.gumband.returnEntitiesInCategory = function(catId, nearcode, latitude, longitude, start, count, fullList, callback){
	var queryString = deeplocal.gumband.endpoint + "items?action=incat&customer=" + deeplocal.gumband.customerId + "&"; 
	queryString += "category=" + catId + "&";

	if(fullList){	
		queryString += "full=true&";
	}else{
		queryString +="full=false&";
	}

	if(typeof start != "undefined"){
		queryString += "startIndex=" + start + "&";
	}

	if(typeof count != "undefined"){
		queryString += "getCount=" + count + "&";
	}

	if(typeof nearcode != "undefined"){
		queryString += "nearcode=" + nearcode + "&";
	} else {
		if(typeof latitude != "undefined" && typeof longitude != "undefined"){
			queryString += "latitude=" + latitude + "&";
			queryString += "longitude=" + longitude + "&";
		}
	}

	queryString += "jsoncallback=?";

	jQuery.getJSON(queryString, callback);
};

deeplocal.gumband.getNumEntitiesInCategory = function(catId, nearcode, latitude, longitude, start, count, callback){
	var queryString = deeplocal.gumband.endpoint + "items?action=incat&customer=" + deeplocal.gumband.customerId + "&"; 
	queryString += "category=" + catId + "&";

	if(typeof start != "undefined"){
		queryString += "startIndex=" + start + "&";
	}

	if(typeof count != "undefined"){
		queryString += "getCount=" + count + "&";
	}

	if(typeof nearcode != "undefined"){
		queryString += "nearcode=" + nearcode + "&";
	} else {
		if(typeof latitude != "undefined" && typeof longitude != "undefined"){
			queryString += "latitude=" + latitude + "&";
			queryString += "longitude=" + longitude + "&";
		}
	}
	queryString += "format=count&";
	queryString += "jsoncallback=?";
	jQuery.getJSON(queryString, callback);
};

deeplocal.gumband.getPlace = function(placeId, callback){
	jQuery.getJSON(deeplocal.gumband.endpoint + "place?action=get&customer=" + deeplocal.gumband.customerId + "&" + "placeid=" + placeId + "&jsoncallback=?", callback);
};

deeplocal.gumband.getPlacesNear = function(latitude, longitude, callback){
	jQuery.getJSON(deeplocal.gumband.endpoint + "place?action=near&customer=" + deeplocal.gumband.customerId + "&latitude=" + latitude + "&longitude=" + longitude + "&jsoncallback=?", callback);
};

deeplocal.gumband.deletePlace = function(placeId, callback){
	jQuery.getJSON(deeplocal.gumband.endpoint + "place?action=delete&customer=" + deeplocal.gumband.customerId + "&place_id=" + placeId + "&jsoncallback=?", callback);
};

deeplocal.gumband.getEntity = function(entId, callback){
	jQuery.getJSON(deeplocal.gumband.endpoint + "items?action=get&customer=" + deeplocal.gumband.customerId + "&entId=" + entId + "&jsoncallback=?", callback);
};

deeplocal.gumband.findPlacesByName = function(name, callback){
	jQuery.getJSON(deeplocal.gumband.endpoint + "place?action=search&customer=" + deeplocal.gumband.customerId + "&q=" + name + "&jsoncallback=?", callback);
};

deeplocal.gumband.findPlacesByCategory = function(cat_id, callback) {
	jQuery.getJSON(deeplocal.gumband.endpoint + "place?action=category&customer=" + deeplocal.gumband.customerId + "&cat_id=" + cat_id + "&jsoncallback=?", callback);
};

deeplocal.gumband.getCategory = function(categoryId, callback){
	jQuery.getJSON(deeplocal.gumband.endpoint + "category?action=get&customer=" + deeplocal.gumband.customerId + "&categoryId=" + categoryId + "&jsoncallback=?", callback);
};

deeplocal.gumband.movePlace = function(placeId, placeLatitude, placeLongitude, callback){
	if(typeof placeLatitude == 'undefined' || typeof placeId == 'undefined' || typeof placeLongitude == 'undefined') {
		return;
	}
	
	if(placeId > 0 && placeLatitude !== '' && placeLongitude !== '') { 
		var url = deeplocal.gumband.endpoint + "place?action=move&placeId="+placeId+"&latitude=" +
		placeLatitude + "&longitude=" + placeLongitude + "&customer="+deeplocal.gumband.customerId+"&jsoncallback=?";
		jQuery.getJSON(url, callback);
	}
};

deeplocal.gumband.getNewCode = function(size, callback){
	jQuery.getJSON(deeplocal.gumband.endpoint + "place?action=getnewcode&customer=" + deeplocal.gumband.customerId + "&size=" + size + "&jsoncallback=?", callback);
};

