Section = {
	getAll : function(){
		q = {Method : "Section.getAll"}
		API.query(q,Locker.display);
	},
	get : function(id, callback){
		q = {Method : "Section.get", Building: id}
		
		if(callback != undefined)
			API.query(q, callback);
		else
			API.query(q,Section.display);
	},
	set : function(dom, id){
		Section.clear();
		Section.Current = Section.all.Sections[id];
		
		Section.Current.Count.Total = parseInt(Section.Current.Count.Full[1]) + parseInt(Section.Current.Count.Half[1]);
		//Section.Current.Count.Total = Section.Current.Count.Total + parseInt(Section.Current.Count.Accessible[1]);

		Section.Current.Count.Free = parseInt(Section.Current.Count.Full[0]) + parseInt(Section.Current.Count.Half[0]);
		//Section.Current.Count.Free = Section.Current.Count.Free + parseInt(Section.Current.Count.Accessible[0]);
		
		$(dom).addClass("active");
		txt = "Total: "+Section.Current.Count.Free+" of "+Section.Current.Count.Total+" lockers available.";
		txt += " ("+Section.Current.Count.Accessible[0]+" accessible)";
		$("#sectionAVAIL").html(txt);
		//Bind events to select_full, _half, any
		Section.selectTypeBind(Section.Current.Count);
	},
	display : function(json){
		Section.all = json;
		txt = "";
		for(i=0;i<json.Sections.length;i++){
			S = json.Sections[i];
			S.Count.Total = parseInt(S.Count.Full[1]) + parseInt(S.Count.Half[1]) + parseInt(S.Count.Accessible[1]);
			S.Count.Free = parseInt(S.Count.Full[0]) + parseInt(S.Count.Half[0]) + parseInt(S.Count.Accessible[0]);
			
			if(S.Count.Free <= 0)
				c = "class=\"inactive\"";
			else
				c = "";
				
			txt += "<li id=\"section"+S.Id+"\"><a "+c+" value=\""+i+"\"><span style=\"display:none;\">"+S.Name+" ("+S.Count.Free+")"+"</span></a></li>";
		}
		
		$(".building").html(txt);
		$(".refresh").show();
		
		Section.Current = undefined;
			
		Section.bind();
	},
	clear : function(){
		$(".building li a.active").each(function(){
			$(this).removeClass("active");
		})
	},
	selectTypeBind : function(count, free){
		
		$("#select_full, #select_half").unbind("click").addClass("inactive");
		
		if(count.Full[0] > 0) {
			$("#select_full").bind("click", function(){
				Locker.reserve('full');	
			}).removeClass("inactive");
		}
		
		if(count.Half[0] > 0) {
			$("#select_half").bind("click", function(){
				Locker.reserve('half');	
			}).removeClass("inactive");
		}
		
		if(count.Full[0] == 0 && count.Half[0] == 0)
			$("#select_any").unbind("click").addClass("inactive");
		else {
			$("#select_any").bind("click", function(){
				Locker.reserve();	
			}).removeClass("inactive");
		}
			

		if(count.Accessible[0] > 0) {
			if($("#select_acc") != null){
				$("#select_acc").bind("click", function(){
					Locker.reserve('accessible');
				}).removeClass("inactive");
			}
		}

	},
	bind : function(){
		
		
		$("#locker_selects").html('<a id="select_full" class="wide_btn textcenter inactive">I want a full locker</a><a id="select_half" class="wide_btn textcenter inactive">I want a half locker</a><a id="select_any" class="wide_btn textcenter">I just want a locker</a>');
		
		User.checkLogin(function(){$("#locker_selects").append('<a id="select_acc" class="wide_btn textcenter inactive">I want an accessible locker</a>')});
		
		$(".building li a").each(function(){
			$(this).click(function(){
				id = $(this).attr("value");
				Section.set(this, id);
			});
		});
	}
}

