var writeCommute = {
	SCHOOL_ID_KEY : "id",
	TEMPLATE_XML : "../xml/commute_template.xml",
	DATA_XML_DIR : "../xml/commute/",
	query:{},
	t: new Date().getTime(),
	tmplXML : {},
	dataXML : {},
	init : function() {
		this.getQuery();
		var id = this.query[this.SCHOOL_ID_KEY];
		if(!id) {
			return;
		}
		this.tmplXML = this.readXML(this.TEMPLATE_XML)
		this.dataXML = this.readXML(this.DATA_XML_DIR + this.query[this.SCHOOL_ID_KEY] + ".xml", ["station", "lecture", "course", "bus_route", "privilege", "comment", "add", "separate"]);
		
		if(this.dataXML && this.dataXML.school && 
			this.dataXML && this.dataXML.school) {
			this.tmplXML = this.tmplXML.school;
			this.dataXML = this.dataXML.school;
			this.writeSchool();
			this.writeDailySheet();
			this.writeStation();
			this.writeAccess();
			this.createTimeSchedule();
			this.createPriceList();
			this.writeBusRoute();
			this.createBackInfo();
		}
	},
	
	getQuery : function() {
		var search = window.location.search;
		if(search) {
			search = search.substring(1);
			var params = search.split("&");
			for(var i=0; i<params.length; i++) {
				var index  = params[i].indexOf("=");
				var key = params[i].substring(0, index);
				var val = params[i].substring(index+1);
				if(key && val) {
					this.query[unescape(key)] = unescape(val);
				}
			}
		}
	},
	
	readXML : function(xml, arrayOption) {
		var jklParseXML = new JKL.ParseXML(xml + "?t=" + this.t);
		if(arrayOption) {
			jklParseXML.setOutputArrayElements(arrayOption);
		}
		return jklParseXML.parse();
	},
	
	replaceTemplate : function(temp, name, value) {
		return temp.replace(new RegExp("{" + name + "}", "g"), value);
	},
	
	getValue : function(xml) {
		var value = "";
		if(xml) {
			value = xml;
		}
		return value;
	},
	
	createImg : function(xml) {
		var tag = "";
		if(xml) {
			tag = 
				'<img src="' + this.getValue(xml.src) + 
				'" weight="' + this.getValue(xml.width) + 
				'" height="' + this.getValue(xml.height) + 
				'" alt="' + this.getValue(xml.alt) + '" />';
		}
		return tag;
	},
	
	writeSchool : function() {
		if(this.tmplXML.info && this.dataXML.info) {
			
			var tmpl = this.tmplXML.info;
			var data = this.dataXML.info;
			var id = tmpl.id;
			if(id) {
				var elm = document.getElementById(id);
				if(elm) {
					var output = tmpl.template;
					if(output) {
						output = this.replaceTemplate(output, "name", this.getValue(data.name));
						output = this.replaceTemplate(output, "address", this.getValue(data.address));
						output = this.replaceTemplate(output, "tel", this.getValue(data.tel));
						output = this.replaceTemplate(output, "mail", this.getValue(data.mail));
						output = this.replaceTemplate(output, "img", this.createImg(data.img));
						output = this.replaceTemplate(output, "comment", this.getValue(data.commnet));
						var privilegesData = data.privilege;
						var result = "";
						if(privilegesData){
							for (var i = 0; i < privilegesData.length; i++) {
								var each = tmpl.rows;
								if(!each) continue;
								each = this.replaceTemplate(each, "privilege", this.getValue(privilegesData[i]));
								result += each;
							}
						}
						output = this.replaceTemplate(output, "privileges", this.getValue(result));
						elm.innerHTML = output;
					}
				}
			}
		}
	},

	writeDailySheet : function() {

		if (!(this.tmplXML.daily_sheet && this.dataXML.daily_sheet)) return;

		var tmpl = this.tmplXML.daily_sheet;
		var data = this.dataXML.daily_sheet;
		if (!tmpl.id) return;

		var elm = document.getElementById(tmpl.id);
		if (!elm) return;

		var output =tmpl.template;
		if (!output) return;

//		var g = (function() {return data;}).apply(null, []);
//		for (each in g) {alert(each);}

		output = this.replaceTemplate(output, "completion", this.getValue(data.completion));
		output = this.replaceTemplate(output, "graduate", this.getValue(data.graduate));
		output = this.replaceTemplate(output, "closed", this.getValue(data.closed));
		output = this.replaceTemplate(output, "reception", this.getValue(data.reception));
		output = this.replaceTemplate(output, "timeschedule", this.getValue(data.timeschedule));
		output = this.replaceTemplate(output, "entrance", this.getValue(data.entrance));
		elm.innerHTML = output;
	},

	writeStation : function() {
		if (!(this.tmplXML.stations && this.dataXML.stations)) return;

		var tmpl = this.tmplXML.stations;
		if (!tmpl.id) return;

		var elm = document.getElementById(tmpl.id);
		if (!elm) return;

		var data = this.dataXML.stations.station;
		var result = "";
		for (var i = 0; i < data.length; i++) {
			var each = this.tmplXML.stations.rows;
			if (!each) continue;
			each = this.replaceTemplate(each, "name", this.getValue(data[i].name));
			each = this.replaceTemplate(each, "by_foot", this.getValue(data[i].by_foot));
			each = this.replaceTemplate(each, "by_bus", this.getValue(data[i].by_bus));
			result += each;
		}
		var output = tmpl.template;
		output = this.replaceTemplate(output, "station", this.getValue(result));
		elm.innerHTML = output;
	},

	writeAccess : function() {
		if(!(this.tmplXML.accessInfo && this.dataXML.accessInfo)) return;

		var tmpl = this.tmplXML.accessInfo;
		if (!tmpl.id) return;
		
		var elm = document.getElementById(tmpl.id);
		if (!elm) return;
		
		var output = tmpl.template;
		if (!output) return;
		
		var data = this.dataXML.accessInfo.bus_route;
		var result = "";
		for(var i = 0; i < data.length; i++){
			var each = this.tmplXML.accessInfo.rows;
			if(!each) continue;
			each = this.replaceTemplate(each, "bus_route", this.getValue(data[i]));
			result += each;
		}
		output = this.replaceTemplate(output, "bus_route", this.getValue(result));
		elm.innerHTML = output;
	},
	
	writeBusRoute : function() {
		if (!(this.tmplXML.bus_routes && this.dataXML.bus_routes)) return;

		var tmpl = this.tmplXML.bus_routes;
		if (!tmpl.id) return;

		var elm = document.getElementById(tmpl.id);
		if (!elm) return;

		var data = this.dataXML.bus_routes.bus_route;
		var result = "";
		for (var i = 0; i < data.length; i++) {
			var each = this.tmplXML.bus_routes.rows;
			if (!each) continue;
			each = this.replaceTemplate(each, "bus_route", this.getValue(data[i]));
			result += each;
		}
		var output = tmpl.template;
		output = this.replaceTemplate(output, "bus_route", this.getValue(result));
		elm.innerHTML = output;
	},

	createTimeSchedule : function() {
		if (!this.tmplXML.time_schedule) return;

		var tmpl = this.tmplXML.time_schedule;
		if (!tmpl.id) return;

		var elm = document.getElementById(tmpl.id);
		if (!elm) return;

		var output = tmpl.template;
		var skills = this.dataXML.time_schedule.skills;
		var rules = this.dataXML.time_schedule.rules;
		output = this.replaceTemplate(output, "schedule", this.createScheduleTableFor(skills, rules));
		elm.innerHTML = output;
	},

	createScheduleTableFor : function(skillsData, rulesData) {
		if (!skillsData || !rulesData) return;

		var rowTemplate = this.tmplXML.time_schedule.rows;
		if (!rowTemplate) return;
		var lectures = 0;
		var lecturesSkill = skillsData.lecture;
		var lecturesRule = rulesData.lecture;
		if (lecturesSkill.length > lecturesRule.length) {
			lectures = lecturesSkill.length;
		} else {
			lectures = lecturesRule.length;
		}
		var result = "";
		for (i = 0 ; lectures > i ; i++) {
			var each = this.tmplXML.time_schedule.rows;
			each = this.replaceTemplate(each, "name", (parseInt(i) + 1));
			if(lecturesSkill[i]){
				each = this.replaceTemplate(each, "startSkill", this.getValue(lecturesSkill[i].start));
				each = this.replaceTemplate(each, "endSkill", this.getValue(lecturesSkill[i].end));
			} else {
				each = this.replaceTemplate(each, "startSkill", "");
				each = this.replaceTemplate(each, "endSkill", "");
			}
			
			if(lecturesRule[i]){
				each = this.replaceTemplate(each, "startRule", this.getValue(lecturesRule[i].start));
				each = this.replaceTemplate(each, "endRule", this.getValue(lecturesRule[i].end));
			} else{
				each = this.replaceTemplate(each, "startRule", "");
				each = this.replaceTemplate(each, "endRule", "");
			}
			result += each;
		}
		return result;
	},

	createPriceList : function() {
		if (!(this.tmplXML.price_list && this.dataXML.price_list)) return;
		
		var tmpl = this.tmplXML.price_list;
		if (!tmpl.id) return;


		var elm = document.getElementById(tmpl.id);
		if (!elm) return;
		var output = tmpl.template;

		var courses = this.dataXML.price_list.course;
		output = this.replaceTemplate(output, "courses", this.createCourseFor(courses));
		
		var adds = this.dataXML.price_list.adds.add;
		var result = "";
		if(adds){
			for(var i = 0; i < adds.length; i++){
				var each = this.tmplXML.price_list.adds;
				each = this.replaceTemplate(each, "add", this.getValue(adds[i]));
				result += each;
			}
		}
		output = this.replaceTemplate(output, "adds", this.getValue(result));
		
		var separates = this.dataXML.price_list.separates.separate;
		result = "";
		if(separates){
			for(var i = 0; i < separates.length; i++){
				var each = this.tmplXML.price_list.separates;
				each = this.replaceTemplate(each, "separate", this.getValue(separates[i]));
				result += each;
			}
		}
		output = this.replaceTemplate(output, "separates", this.getValue(result));
		
		var comment = this.dataXML.price_list.comment;
		result = "";
		if(comment){
			for(var i = 0; i < comment.length; i++){
				var each = this.tmplXML.price_list.comments;
				each = this.replaceTemplate(each, "comment", this.getValue(comment[i]));
				result += each;
			}
		}
		output = this.replaceTemplate(output, "comments", this.getValue(result));
		elm.innerHTML = output;
	},

	createCourseFor : function(data) {
		if (!data) return;

		var rowTemplate = this.tmplXML.price_list.rows;
		if (!rowTemplate) return;

		var result = "";
		for (i in data) {
			var each = this.tmplXML.price_list.rows;
			each = this.replaceTemplate(each, "name", this.getValue(data[i].name));
			each = this.replaceTemplate(each, "price_mt", this.getValue(data[i].price_mt));
			each = this.replaceTemplate(each, "price_mate_mt", this.getValue(data[i].price_mate_mt));
			each = this.replaceTemplate(each, "price_at", this.getValue(data[i].price_at));
			each = this.replaceTemplate(each, "price_mate_at", this.getValue(data[i].price_mate_at));
			//each = this.replaceTemplate(each, "intramural", this.getValue(courses[i].intramural));
			//each = this.replaceTemplate(each, "extramural", this.getValue(courses[i].extramural));
			each = this.replaceTemplate(each, "explanation", this.getValue(data[i].explanation));
			result += each;
		}
		return result;	
	},

	createBackInfo : function() {
		if (!(this.tmplXML.back_info && this.dataXML.back_info)) return;
		
		var tmpl = this.tmplXML.back_info;
		if (!tmpl.id) return;


		var elm = document.getElementById(tmpl.id);
		if (!elm) return;
		var output = tmpl.template;

		var each = this.dataXML.back_info.viewBack;
		output = this.replaceTemplate(output, "viewBack", this.getValue(each));
		elm.innerHTML = output;
	}
};

(function(){writeCommute.init();})();
