/* 
	Methods 
	Get, GetCaptcha, GetOne, Delete, DeleteOne, Save, Count
	
	Classes
	Object, Type, Id, Group

	Query
	Start, Count, Attributes, 
	
	Flag
	Moderated: true, false
	
*/

OH_object = {
	GetClass : function(Type, Id, Callback) {
		Obj = {
			Method: "Get",
			Class : Type,
			Query : {
				Id : Id
			}
		}
		
		Obj.Query = $.toJSON(Obj.Query);
		$.post(OH_config.jsonPath, Obj, function(json) {
			OH_object.Type = json.Results[0].Id;
			Callback(json);
		}, "json");
	},
	
	GetGroup : function(Id, Callback) {
		Obj = {
			Method: "Get",
			Class : "Group",
			Query : {
				Id : Id
			}
		}
		
		Obj.Query = $.toJSON(Obj.Query);
		$.post(OH_config.jsonPath, Obj, function(json) {
			Callback(json);
		}, "json");
	},
	
	GetTypeById : function(Id, Callback, formData) {
		Obj = {
			Method: "Get",
			Class : "Type",
			Query : {
				Id : Id
			}
		}
		
		Obj.Query = $.toJSON(Obj.Query);
		$.post(OH_config.jsonPath, Obj, function(json) {
			OH_object.Type = json.Results[0].Id;
			Callback(json, formData);
		}, "json");
		
	},
	
	GetTypeByTag: function(Tag, Callback, formData) {
		Obj = {
			Method: "Get",
			Class : "Type",
			Query : {
				Attributes : [["Tag", "=", Tag]]
			}
		}
		
		Obj.Query = $.toJSON(Obj.Query);
		$.post(OH_config.jsonPath, Obj, function(json) {
			OH_object.Type = json.Results[0].Id;
			Callback(json, formData);
		}, "json");
		
	},
	
	GetGroupByTag: function(Tag, Callback, formData) {
		Obj = {
			Method: "Get",
			Class : "Group",
			Query : {
				Attributes : [["Tag", "=", Tag]]
			}
		}
		
		Obj.Query = $.toJSON(Obj.Query);
		$.post(OH_config.jsonPath, Obj, function(json) {
			OH_object.Type = json.Results[0].Id;
			Callback(json, formData);
		}, "json");
		
	},
	
	GetObjectsFromType : function(Type, Callback) {
		Obj = {
			Method: "Get",
			Class : "Object",
			Query : {
				Type : Type
			}
		}
		
		Obj.Query = $.toJSON(Obj.Query);
		
		$.post(OH_config.jsonPath, Obj, function(json) {
			Callback(json);
		}, "json");
	},
	
	GetSomeObjectsFromType : function(Type, Callback, Start, Count) {
		Obj = {
			Method: "Get",
			Class : "Object",
			Query : {
				Type : Type
			},
			Start : Start,
			Count : Count
		}
		
		Obj.Query = $.toJSON(Obj.Query);
		
		$.post(OH_config.jsonPath, Obj, function(json) {
			Callback(json);
		}, "json")
	},
	
	GetObjectById : function(Id, Callback) {
		Obj = {
			Method: "GetOne",
			Class : "Object",
			Query : {
				Id : Id
			}
		}
		Obj.Query = $.toJSON(Obj.Query);
		
		$.post(OH_config.jsonPath, Obj, function(json) {
			//alert(dump(json));
			OH_object.GetTypeByTag(json.Results.Type, Callback, json);
		}, "json");
	},
	
	BaleteOne: function(Id) {
		Obj = {
			Method: "DeleteOne",
			Class : "Object",
			Query : {
				Id : Id
			}
		}
		
		Obj.Query = $.toJSON(Obj.Query);
		$.post(OH_config.jsonPath, Obj, function(json) {
			OH_object.removeRow(Id);
		}, "json");
	},
	
	ApproveOne : function(Id, Val) {
		Obj = {
			Method: "Save",
			Class : "Object",
			Flag : { 
				Moderated : Val
			},
			Id : Id
		}

		$.post(OH_config.jsonPath, Obj, function(json) {}, "json");
	},
	
	removeRow : function(Id) {
		$("#row"+Id).fadeOut(200);
	},

	GetCaptcha : function(callback, dom) {
		Obj = { Method: "GetCaptcha" }
		$.post(OH_config.jsonPath, Obj, function(json) {
			callback(json, dom);
		}, "json");
	},
	
	GetNumObjects : function(Obj, Callback) {
		Obj.Query = eval("("+Obj.Query+")");
		CountObj = {
			Method : "Count",
			Class : Obj.Class,
			Query : {
				Type : Obj.Query.Type
			}
			
		}
		
		if(Obj.Query.Sort != undefined)
			CountObj.Query.Sort = Obj.Query.Sort
			
		if(Obj.Query.Properties != undefined)
			CountObj.Query.Properties = Obj.Query.Properties
			
		CountObj.Query = $.toJSON(CountObj.Query);
		$.post(OH_config.jsonPath, CountObj, function(json) {
			Callback(json);
		}, "json");
		
	},
	
	adjustCount : function() {
		
		--OH_paging.config.numRecords;
		
	},
	
	toggleApprove : function(id) {
		$("#unapprove-"+id+",#approve-"+id).toggle();
	}
}
