OH_classifieds = {
	
	Count : 0,
	currentPage : 1,
	
	create : function(Tag) {
		this.Headers = new Array();
		this.Body = new Array();
		OH_hash.set("Page", 1)
		Q = {Method:"Get",Class:"Type",Query:{Attributes : [["Tag", "=", Tag]]}}
		Q.Query = $.toJSON(Q.Query);
		$.post(OH_config.jsonPath, Q, function(json) {OH_classifieds.display(json)}, "json");
		
	},
	
	display : function(json) {
		j = json.Results[0].Properties;
		this.Current = json.Results[0].Id;
		this.Name = json.Results[0].Name;
		this.Tag = json.Results[0].Tag;
		$("#classified-tab li a.active").removeClass("active");
		$("#classified-tab li#list-"+this.Tag+" a").addClass("active");
		OH_hash.set("Type", this.Tag);
		
		//Get paging stuff
		Q = {Method:"Count",Class:"Object",Query:{Type:this.Current}}
		Q.Query.Properties = [["created", ">=", 0]];
		Q.Query = $.toJSON(Q.Query);
		$.post(OH_config.jsonPath, Q, function(json) {
			OH_paging.config.numRecords=json.Results;
			OH_paging.set(json);
		}, "json");
		
		for(key in j) { 
			
			if(j[key].Flags == "Header") {
				OH_classifieds.Headers.push({Name: j[key].Name, Tag : j[key].Tag});
			} else {
				OH_classifieds.Body.push({Name: j[key].Name, Tag : j[key].Tag});
			}
		
		}
		
		//Display Header
		OH_classifieds.displayHeader(OH_classifieds.currentPage);
		
	},
	
	displayHeader : function() {
		t = "";
		H = OH_classifieds.Headers;
			t += "<tr><th>Type</th>";
		for(i=0;i<H.length;i++) {
			t += "<th>" + H[i].Name + "</th>";
		}
			t += "<th>Details</th></tr>";
		$(OH_classifieds.Selector +" thead").html(t);
		
		if(OH_hash.hash.Page == undefined)
			OH_hash.hash.Page = 1
			
		OH_classifieds.displayBody(OH_hash.hash.Page);
	},
	
	displayBody : function(pgNum) {
		OH_paging.setActive(pgNum);
		Start = (pgNum - 1) * OH_paging.config.numPerPage;
		Q = {Method:"Get",Class:"Object",Query:{Type:OH_classifieds.Current,Start:Start,Count:OH_paging.config.numPerPage,Sort:[["created","DESC"]]}}
		//Q.Query.Properties = [["created", ">=", OH_classifieds.Expiry]];
		Q.Query = $.toJSON(Q.Query);
		$.post(OH_config.jsonPath, Q, function(json) {
			
			if(json.Results[0] == undefined) {
				t = "<tr><td colspan=\"10\">No listings found</td></tr>";
				$(OH_classifieds.Selector +" tbody").html(t);
				return false;
			}
			
			H = OH_classifieds.Headers;
			B = OH_classifieds.Body;
			t = "";
			odd = 0;
			for(ID in json.Results) {
				if(json.Results[ID].Moderated != "false") {

				if(odd!=0) {
					OddEven = "odd";
					odd = 0;
				} else {
					OddEven = "";
					odd++;
				}	
				/*
				today = 0;
				expires = 0;
					
				if(json.Results[ID].Type == "for_rent") {
					property = json.Results[ID].Properties.date_available;
					var today = new Date();
					today = today.getTime()/1000;
					//Unix time stamp of its available date
					var date = (new Date(eval(property.Year), eval(property.Month), eval(property.Day)).getTime()/1000);
					//30 days after its posted
					var expires = date + (86400 * 30);
					//alert(expires)
					if(today >= expires){
						//alert(dump(json.Results[ID]));
					}
				}
				*/
					t+="<tr class=\""+OddEven+"\">";
					t+="<td class=\""+OH_classifieds.Tag+"\">"+OH_classifieds.Name+"</td>";
					for(i=0;i<H.length;i++) {
						if(typeof(json.Results[ID].Properties[H[i].Tag]) != "object") {
							t+="<td>"+json.Results[ID].Properties[H[i].Tag]+"</td>";
						} else {
							O = json.Results[ID].Properties[H[i].Tag];
							t+="<td>";
							if(O.Day != undefined && O.Month != undefined && O.Year != undefined) {
								O.Day = eval(O.Day);
								O.Month = eval(O.Month);
								O.Year = eval(O.Year);
								t += monthName[O.Month] + " " + O.Day + ", " + O.Year;
							} else {
								for(key in O)
									t+= O[key];
							}
							t+="</td>";
						}			
					}
					t+="<td><a class=\"showMore\" onclick=\"OH_classifieds.showMore("+json.Results[ID].Id+");return false;\">More Info</a><!--<br /><a onclick=\"OH_classifieds.getSingle("+json.Results[ID].Id+");return false;\">Permalink</a>--></td>";
					t+="</tr>";
					
					t+="<tr id=\"more-"+json.Results[ID].Id+"\" class=\"moreinfo "+OddEven+"\">";
					t+="<td colspan=\"1\"></td>";
					t+="<td colspan=\"100%\"><ul>";
					for(i=0;i<B.length;i++) {
						if(typeof(json.Results[ID].Properties[B[i].Tag]) != "object") {
							if(B[i].Tag != "created")
								t+="<li><strong>"+B[i].Name+": </strong>"+json.Results[ID].Properties[B[i].Tag]+"</li>";
						} else {
							for(key in json.Results[ID].Properties[B[i].Tag])
								t+= "<li><strong>"+B[i].Name+": </strong>"+json.Results[ID].Properties[B[i].Tag][key]+"</li>";
						}
									
					}
					t+="</ul></td>";
					t+="</tr>";		
						
				}
			}
			
			
			$(OH_classifieds.Selector +" tbody").html(t);
		}, "json");

	},

	getSingle : function(id) {
		alert(id);
	},
	
	showMore : function(id) {
		$("#more-"+id).toggle();
	}
}
