var hoursParser = function (targetObject, onLoadFunction){
	var include = "http://"+ window.location.hostname +"/mu/map/mu_building_hours.html";
	var weekday = ["Mon", "Tue", "Wed", "Thr", "Fri", "Sat", "Sun"];
	var that = this;
	var processFunction = onLoadFunction;
	var gDate = new Date();
	var today = gDate.getTime();

	this.levelInfo = [{id:-1, abr:"ALL", name:"Building Hours"}, {id:1, abr:"ML", name:"Main Level"}, {id:0, abr:"LL", name:"Lower Level"}, {id:2, abr:"2L", name:"Second Level"}, {id:3, abr:"3L", name:"Third Level"}];
	this.currentSession = "";
	this.sessions = new Array();
	this.business = new Array();

	this.settings = {
		showSession: "",
		showFloor: "",
		businessTypes: "",
		showTypeR: true,
		showTypeF: true,
		filterBusinessName: "",
		showByName: false,
		showTitle:true,
		showBuildingHours:true
	};
	
	this._getSessionName = function(sessionID){
		var s=0;
		for (s=0; s<that.sessions.length; s++)
			if (that.sessions[s].id==sessionID) return that.sessions[s].name;
		return "";
	}

	this._getSessionIndex = function(sessionID){
		var s=0;
		for (s=0; s<that.sessions.length; s++)
			if (that.sessions[s].id==sessionID) return s;
		return -1;
	}
	
	this._loadHours = function(){
		$.get(include,{}, function (data){
			s = data.indexOf("<body>");
			e = data.indexOf("</body>")+7;
			var temp = document.createElement("div")
			temp.innerHTML = data.substr(s,e-s).replace(/\r/g, '').replace(/\n/g, '');
			semesterTable = $(temp).find("#semester");
			hoursTable = $(temp).find("#hours");
			//get session data
			cnt=0;
			$(semesterTable).find("tr").each(function(rowNo){
				if ($(this).find("td:eq(0)").text()!=""){
					that.sessions[cnt] = {
							id : $(this).find("td:eq(0)").text(),
							name : $(this).find("td:eq(1)").text(),
							start : $(this).find("td:eq(2)").text(),
							end : $(this).find("td:eq(3)").text()
						};
					cnt++;
				}
			});
			
			for (i=0; i<that.sessions.length; i++){
				staDate = Date.UTC(gDate.getFullYear(), parseFloat(that.sessions[i].start.split("/")[0])-1, parseFloat(that.sessions[i].start.split("/")[1]),0,0,0,0);
				endDate = Date.UTC(gDate.getFullYear(), parseFloat(that.sessions[i].end.split("/")[0])-1, parseFloat(that.sessions[i].end.split("/")[1]),0,0,0,0);
				if (today >= staDate && today <endDate ){
					that.currentSession = that.sessions[i];
					that.settings.showSession = that.currentSession.id;
				}
			}
			
			cnt =0;
			$(hoursTable).find("tr").each(function(rowNo){
				if ($(this).find("td:eq(0)").text().replace(/ /g, '').length>1){
					that.business[cnt] = {
							level : $(this).find("td:eq(0)").text(),
							name : $(this).find("td:eq(1)").text(),
							type : $(this).find("td:eq(2)").text(),
							session : $(this).find("td:eq(3)").text(),
							hours : that._processHours([$(this).find("td:eq(4)").text().replace(/ /g, ""), $(this).find("td:eq(5)").text().replace(/ /g, ""), $(this).find("td:eq(6)").text().replace(/ /g, ""), $(this).find("td:eq(7)").text().replace(/ /g, ""), $(this).find("td:eq(8)").text().replace(/ /g, ""), $(this).find("td:eq(9)").text().replace(/ /g, ""), $(this).find("td:eq(10)").text().replace(/ /g, "")])
						}
					cnt++;
				}
			});		
			if (typeof(processFunction) == "function") processFunction.call(that);
		});
	}
	this._getLevelID = function(levelAbr){
		var t = 0;
		for (t=0; t<that.levelInfo.length; t++)
			if (that.levelInfo[t].abr == levelAbr) return that.levelInfo[t].id;
		return -1;
	}
	
	this._getLevelName = function(levelId){
		return that.levelInfo[levelId].name;
	}
	
	this.show = function(filters){
		
		if (filters!=null){
			that.settings.showByName = false;
			if (filters.showSession!=null) that.settings.showSession = filters.showSession;
			if (filters.showFloor!=null) that.settings.showFloor = filters.showFloor;
			if (filters.filterBusinessName!=null && filters.filterBusinessName!=""){ that.settings.showByName = true; that.settings.showFloor = ""; that.settings.filterBusinessName = filters.filterBusinessName;}
			if (filters.showTitle!=null) that.settings.showTitle = filters.showTitle;
			if (filters.alwaysShowBuildingHours!=null) that.settings.alwaysShowBuildingHours = filters.alwaysShowBuildingHours;
			if (filters.businessTypes!=null) that.settings.businessTypes=filters.businessTypes;
			
			if (that.settings.businessTypes==""){that.settings.businessTypes = null; that.settings.showTypeR = true; that.settings.showTypeF = true;}
			if (that.settings.businessTypes!=null){
				that.settings.showTypeR = false; that.settings.showTypeF = false;
				if (filters.businessTypes.indexOf("R")>=0) that.settings.showTypeR = true;
				if (filters.businessTypes.indexOf("F")>=0) that.settings.showTypeF = true;
			}
		}

		var sessionName = that._getSessionName(that.settings.showSession);
		if (sessionName == "") {alert("Session abbreviation does not exist"); return;}
		
		$(targetObject).html("Loading hours...");
		var shownCount = 0;
		var title = "";
		var html = "";
		if (that.settings.showTitle==true) title = "<h2>Memorial Union "+sessionName+" Session Hours</h2>";
		html += "<table width=\"90%\" cellspacing=\"0\" cellpadding=\"3\" border=\"0\" class=\"lightTable\">";
		var wdays = "<td>&nbsp;</td>";
		for (i=0; i<7; i++) wdays += "<td align=\"center\"><b>"+weekday[i]+"</b></td>";
		var level = new Array();
		var levelCnt = new Array();
		for (i=0; i<that.levelInfo.length; i++){level[i] = "", levelCnt[i] = 0;}
		var lvlId=0;
		for (i=0; i<that.business.length; i++){
			if (that.settings.showSession==that.business[i].session){
				lvlId = that._getLevelID(that.business[i].level);
				if (lvlId==0 || (!that.settings.showByName && that.settings.showTypeR && that.business[i].type=="R") || (!that.settings.showByName && that.settings.showTypeF && that.business[i].type=="F") || (that.settings.showByName && that.business[i].name.indexOf(that.settings.filterBusinessName)==0)){
					if (levelCnt[lvlId]==0) level[lvlId] += "<tr><th colspan=\"8\">"+that._getLevelName(lvlId)+"</th><tr class=\"Small\">" + wdays + "</tr>"
					level[lvlId] += "<tr class=\"Small\"><td><b>"+that.business[i].name+"</b></td>";
					for (t=0; t<7; t++)
						level[lvlId] += "<td align=\"center\">"+that.business[i].hours[t]+"</td>";
					level[lvlId] += "</tr>";
					levelCnt[lvlId]++;
				}
			}
		}
		
		if (that.settings.showFloor==""){
			for (i=1; i<5; i++){
				if (levelCnt[i] > 0){
					html+=level[i];
					shownCount++
				}
			}
			if (shownCount==0) html +="<tr><td>Information not available at this time.</td></tr>";
		}else{
			html+=level[that._getLevelID(that.settings.showFloor)];
			if (level[that._getLevelID(that.settings.showFloor)] !="") shownCount++;
			if (shownCount==0) html +="<th>"+that._getLevelName(that._getLevelID(that.settings.showFloor))+"</th><tr><td>No information available.</td></tr>";
		}
		
		$(targetObject).html(title+"<table width=\"90%\" cellspacing=\"0\" cellpadding=\"3\" border=\"0\" class=\"lightTable\">"+level[0]+"</table><br />"+html+"</table>");
	}
	
	this._getWeekdayIndex = function (weekdayAbr){
		var k=0;
		for (k=0; k<7; k++){
			if (weekdayAbr.toUpperCase() == weekday[k].toUpperCase()) return k;
		}
		return -1;
	}
	
	this._processHours = function (times){
		var ret = ["","","","","","",""];
		
		for (i=0; i<7; i++){
			if (!isNaN(parseFloat(times[i].substr(0,1))))
				ret[i] = times[i].replace("a", " a.m.").replace("p", " p.m.").replace(/:00/g, "");
			else
				if (times[i].toUpperCase()!="CLOSED")
					ret[i] = times[that._getWeekdayIndex(times[i])].replace("a", " a.m.").replace("p", " p.m.").replace(/:00/g, "");
				else
					ret[i] = "Closed";
		}
		return ret;
	}
	
	this.groupHours = function (times){
		//6:30a-12:00p
		ret = "";
		fday = "";
		time = "";
		lday = "";
		for (t=0; t<7; t++){
			if (" ("+ times[t] + "), " != time){
				ret += fday + lday + time;
				time = " ("+ times[t] + "), ";
				fday = weekday[t];
				lday = "";
			}else{
				//if (times[i] == "closed") time = "closed";
				lday = "-" + weekday[t];
			}
		}
		ret += fday + lday + time.replace(",", "");
		return ret;
	};
	
	this.getBusinessIDByName = function(bname){
		for (var i=0; i<that.business.length; i++){
			if (that.business[i].name.replace(/ /g, '') == bname.replace(/ /g, '')) return i;
		}
		return -1;
	}
	
	this._loadHours();
}
