var open_close = true;
var height = 17; // height of the menu headers
var iheight =17; // height of the menu_items

var bgc = "#768BBC";	// background color of the item
var tc = "#768BBC";		// text color of the item
var cc = "#003399";		// text color of the item
var over_bgc = "";
var over_tc = "black";

var speed = 100; // time between changing heigth of the item_panel
var steps = 8; // changing heigth in steps of
var timerID = "";
var N = (document.all)?0:1;
var width =126;
var self_menu = new Array();
var html="";

// สร้างเมนูทั้งหมดที่ใช้
function write_menu() {
	smc = 0; // count the position of the self_menu
	document.write("<div style=\"position:absolute;\">");
	html+="<div style=\"position:absolute;\">\n";
	mn = 0;
	mni = 1;
	start = 1;
	csmc = 0; // count smc

	for(i=0;i<Link.length;i++) {
		la = Link[i].split("|"); // la[0] = position, la[1] = link name or name's image, la[2] = url, la[3] = target
		if(la[0] == "0" || la[0] == "1" || la[0] == "startx" || la[0] == "endx") {
			if(start == 0) {	// ครั้งแรกยังไม่ทำที่นี่
				document.write("</div>"); // ปิด div class=item_panel
html+="</div>\n";
				self_menu[smc] = new Array(mn,height,0,mni);
				smc++;
				mni++;
//				mn += height;
				start = 0;
			}
			funky = "funky" + i ;	// ชื่อ name ของ image

			beforex = "images/" + la[1] + ".jpg";
			imgxs = "<img src=\"" + beforex + "\" border=\"0\" name=\"" + funky + "\">";

			if(la[0] == "0") {
				document.write("<div class=\"menux\" style=\"top:" + mn + ";height:" + height + "\" id=\"down" + smc + "\"  onMouseOver=\"prepare(" + (smc-1) + "," + (mni-1) + ")\">" + imgxs + "</div>");
html+="<div class=\"menux\" style=\"top:" + mn + ";height:" + height + "\" id=\"down" + smc + "\"  onMouseOver=\"prepare(" + (smc-1) + "," + (mni-1) + ")\">" + imgxs + "</div>\n";
			} else if(la[0] == "1") {
				document.write("<div class=\"menux\" style=\"top:" + mn + ";height:" + height + "\" id=\"down" + smc + "\"  onmouseover=\"overMenu(" + funky + ")\"  onmouseout=\"outMenu(" + funky + ")\"><a href=\"" + linkx + "\" Target=\"" + targetx + "\">" + imgxs + "</a></div>");
			} else if(la[0] == "startx") {
				document.write("<div class=\"menux\" style=\"top:" + mn + ";height:" + height + "\" id=\"down" + smc + "\">" + imgstartx + "</div>");
			} else if(la[0] == "endx") {
				document.write("<div class=\"menux\" style=\"top:" + mn + ";height:" + height + "\" id=\"down" + smc + "\">" + imgendx + "</div>");
			}
			h = csmc * iheight;
			tmn = mn;
			self_menu[smc] = new Array(tmn,h,0,-2);
		} else {
			if(start == 1) {
				if(N) mn += 2;
				document.write("<div class=\"item_panel\" id=\"down" + smc + "\" style=\"top:" + mn + "\">");
html+="<div class=\"item_panel\" id=\"down" + smc + "\" style=\"top:" + mn + "\">\n";
				start = 0;
			}
			document.write("<div class=\"item\" id=\"d" + i + "\" style=\"height:" + iheight + "\">" + la[1] + "</div>");
html+="<div class=\"item\" id=\"d" + i + "\" style=\"height:" + iheight + "\">" + la[1] + "</div>\n";
			csmc++; // นับจำนวนเมนูย่อย
		} // if(la[0] == "0" || la[0] == "1" || la[0] == "startx" || la[0] == "endx")
	} // for

	if(start == 0) {
		h = csmc * iheight;
		tmn = mn + height + 5; 
		self_menu[smc] = new Array(tmn,h,0,-2);
	}
	document.write("</div>");
	html+="</div>\n";
}

var prevnr = -1;
var prevc = -1;
var vprepare = "stop";
var step = 0;

function prepare(nr,c,urlx,targetx) {
	if(open_close) {
		if(prevnr == -1) {
			if(self_menu[nr+1][2] == 0) {
				prevnr = nr;
				prevc = c;
			} else {
				prevnr = -1;
				prevc = -1;
			}
			step = 0;
			pull_down(nr,c);
		} else {
			if((step == 0) && (prevnr != -1)) {
				if (self_menu[nr+1][2] == 1) {
					step = 0;
					prevnr = -1;
					prevc = -1;
					pull_down(nr,c);
				} else {
					step = 1;
					pull_down(prevnr,prevc);
					prevnr = nr;
					prevc = c;
				}
			} else {
				step = 0;
				pull_down(prevnr,prevc);
			}
		}
	} else {
		pull_down(nr,c);
		vprepare = "stop";
	}
}

function end_prepare() {
	if (step == 1) prepare();
	else {
		if (timerID != "") clearTimeout(timerID);
	}
}

function pull_down(nr,c) {
	if (timerID == "") {
		to = self_menu[nr+1][1];
		begin = nr + 2;
		if (timerID != "") clearTimeout(timerID);
		if (self_menu[nr+1][2] == 0) {
			self_menu[nr+1][2] = 1;
			if(nr == self_menu.length-2) to++;
			epull_down(begin,to-1,0);
		} else {
			to = 0;
			self_menu[nr+1][2] = 0;
			name = "down"+(nr+2);
			open_item = 0;
			for(i=0;i<nr;i++) {
				if(self_menu[i][2] == 1) open_item += self_menu[i][1];
			}
			if (N == false) open_item-= (c*1);
			if (nr== self_menu.length-2) {
				val = self_menu[self_menu.length-1][1];
				to=-1;
			} else val = parseInt(document.getElementById(name).style.top) -(open_item)-(c*height);
			epull_up(begin,to+1,val);
		}
	}
}


function epull_down(nr,to,nowv) {
	if((nowv + steps) >= to) vsteps = to - nowv;
	else vsteps = steps;
	name = "down" + (nr-2);
	obj = document.getElementById(name).style.clip = "rect(0,"+width+","+(nowv+vsteps)+",0)";
	// ====== obj of Image ======
	obj = document.getElementById("down1");
	obj.style.top = parseInt(obj.style.top)+vsteps;
/*
	for (i=nr;i<self_menu.length;i++) {
		name = "down" + i;
		obj = document.getElementById(name);
		obj.style.top = parseInt(obj.style.top)+vsteps;
	}
*/
	nowv+=vsteps;
	if(nowv < to) timerID = setTimeout("epull_down("+nr+","+to+","+nowv+")",speed);
	else {
		timerID = "";
		end_prepare();
	}
}

function epull_up(nr,to,nowv) {
	if((nowv-steps) <= to) {
		vsteps = nowv-to;nowv = 0;
		if(to == -1) vstep = nowv;
	} else vsteps = steps;
	name = "down" + (nr-2);
	obj = document.getElementById(name).style.clip = "rect(0,"+width+","+nowv+",0)";
	// ====== obj of Image ======
	obj = document.getElementById("down1");
	obj.style.top = parseInt(obj.style.top)-vsteps;
/*
	for(i=nr;i<self_menu.length;i++) {
		name = "down" + i;
		obj = document.getElementById(name);
		obj.style.top = parseInt(obj.style.top)-vsteps;
	} // for
*/
	nowv-=vsteps;
	if(nowv > to) timerID = setTimeout("epull_up("+nr+","+to+","+nowv+")",speed);
	else {
		timerID = "";
		end_prepare();
	}
}

var lb = new Array();

function startup(nr1) {
	write_menu(); // สร้างเมนู
	if(nr1) {
		pull_down(nr1,1);
		prevnr = nr1;
		prevc = nr1 + 1;
	} else startid = "stop";
}

var startid = 0;
var open_part = 0;

function countdown() {
	var today = new Date();
	document.getElementById("d1").innerHTML = "<strong>เวลา : " + ((today.getHours().toString().length == 1)?"0"+today.getHours():today.getHours()) + ":" + 
																		((today.getMinutes().toString().length == 1)?"0"+today.getMinutes():today.getMinutes()) + ":" + 
																		((today.getSeconds().toString().length == 1)?"0"+today.getSeconds():today.getSeconds()) + "</strong>";
	document.getElementById("d2").innerHTML = "<strong>วันที่ : " + ((today.getDate().toString().length == 1)?"0"+today.getDate():today.getDate()) + "/" + 
																			(((today.getMonth()+1).toString().length == 1)?"0"+(today.getMonth()+1):(today.getMonth()+1)) + "/" + 
																			(today.getFullYear()+543).toString() + "</strong>";
	var counter = setTimeout("countdown()", 1000);
}

function runTime() {
	var today = new Date();
	document.getElementById("d1").innerHTML = ((today.getHours().toString().length == 1)?"0"+today.getHours():today.getHours()) + ":" + 
																		((today.getMinutes().toString().length == 1)?"0"+today.getMinutes():today.getMinutes()) + ":" + 
																		((today.getSeconds().toString().length == 1)?"0"+today.getSeconds():today.getSeconds());
	var counter = setTimeout("runTime()", 1000);
}