//An array that hold the IDs of images that we mentioned in their DIV blocks



function DiningList(day, time){
//	document.write('<div class="img"><img src="Background.jpg" width="100%" border="0" galleryimg="no" /></div>');
	document.write("<table class='hours'>");
		document.write("<tr class='info'><td>It is "+niceTime(time)+" on a "+day+"</td><tr>");
	var heap = new Array();
	
	for(var j = 0; j< hall.length;j++){
		var open = false;
		var until = -1;
		var closedUntil = Infinity;
		var thisHall = hall[j];
		for(var k = 2; k< thisHall.length;k++){
			var thisDay = thisHall[k];
			for(var l=1;l<(thisDay.length-1)/2*2+1;l=l+2){
				if(thisDay[0].match(day)){
					var s = thisDay[l];
					var f = thisDay[l+1];
					if(!open){
						open = (time>s&&time<f);
						until = f;
					}
					if(open){
						//if(f>until)
						//	until=f;
					}else{
						if(s<closedUntil&&s>time)
							closedUntil=s;
					}
				}
			}
		}

		var val = "";
		var key = -Infinity;
		
		if(open){
			key = until;
			val = val + ("<tr class='open'>");
			val = val + ("<td ><a class='tableLink' href='"+hall[j][0]+"'>"+hall[j][1]+"</a> is open until "+niceTime(until)+"</td>");
			val = val + ("</tr>");
		}else{
			key = closedUntil;
			val = val + ("<tr class='closed'>");
			var untilStr = " until "+niceTime(closedUntil);
			if(closedUntil == Infinity)
				untilStr = " for the day...";
			val = val + ("<td > <a class='tableLink' href='"+hall[j][0]+"'>"+hall[j][1]+"</a> is closed"+untilStr+"</td>");
			val = val + ("</tr>");
		}
		heap[j] = new Array(open, key, val);
		
	}

	heap = heap.sort(sortD);

	for(var j = 0; j< heap.length;j++){
		var thisVal = heap[j];
		document.write(thisVal[2]);
	}

	document.write("<tr ><td class='email'> Email alecjacobson [AT] nyu [DOT] edu for corrections, questions or concerns.</td></tr>");
	document.write("</table>");
//	document.write('<div class="img"><img src="blank.png" width="100%" border="0" galleryimg="no" /></div>');
	

}

function sortD(a,b){
	var val = 0;
	if(a[0]&&b[0]){
		val = a[1]-b[1];
	}else if (!a[0]&&b[0]){
		val = b[1];//oposite so opens are on top
	}else if (a[0]&&!b[0]){
		val = -a[1];//oposite so opens are on top
	}else if (!a[0]&&!b[0]){
		if(a[1]==Infinity&&b[1]==Infinity){
			val = 0;
		}else if(a[1]==Infinity){
			val = 1;
		}else if (b[1]==Infinity){
			val = -1;
		}else{
			val = a[1]-b[1];
		}
	}
	/**else if (!a[0]&&!b[0]){
		val = a[1]-b[1];
	}*/
	
	if(val>0)
		return 1;
		
	if(val<0)
		return -1;
		
	if(val==0)
		return 0;
}


function niceTime(t){
	var str = "";
	var min = (t%1)*60;
	min = min - (min%1);
	t = t - (t%1);
	var minStr  = min;
	if(min<10)
		minStr= "0"+min;
	var half = "a.m.";
	if(t == 0){
		t = 12;
		half = "a.m.";
	}else if(t == 24){
		t = 12;
		half = "a.m.";
	}else if(t == 12){
		t = 12;
		half = "p.m.";
	}else if(t >24 ){
		t = t%12;
		half = "a.m.";
	}else{
		if(t>=12) half = "p.m.";
		t = t % 12;
	}
	str = str+t+":"+minStr+" "+half;
	return str;
}
