/**
 * TOP 버튼 디자인 스크립트
 */

// image roll
function menuOver() {
	this.src = this.src.replace(".gif", "_ov.gif");
}
function menuOut() {
	this.src = this.src.replace("_ov.gif", ".gif");
}

function imageOver(imgEl) {
	imgEl.src = imgEl.src.replace(".gif", "_ov.gif");
}
function imageOut(imgEl) {
	imgEl.src = imgEl.src.replace("_ov.gif", ".gif");
}

// element toggle
function blockToggle(href) {
	targetElement = document.getElementById(href.split("#")[1]);

	if (targetElement && targetElement.style.display == "block") {
		targetElement.style.display = "none";
	} else {
		targetElement.style.display = "block";
	}
	return false;
}

// tabbed menu
function initTabMenu(menuElId) {
	var tabMenu = document.getElementById(menuElId).getElementsByTagName("a");
	for(i=0; i<tabMenu.length; i++) {
		thismenu = tabMenu.item(i);
		thismenu.menuContainer = document.getElementById(menuElId);
		thismenu.targetEl = document.getElementById(tabMenu.item(i).href.split("#")[1]);
		thismenu.targetEl.style.display = "none";
		thismenu.getElementsByTagName("img").item(0).onmouseover = menuOver;
		thismenu.getElementsByTagName("img").item(0).onmouseout = menuOut;
		thismenu.onclick = tabMenuClick;
	}

	initmenu = tabMenu.item(0);
	initmenu.targetEl.style.display = "block";
	initmenu.getElementsByTagName("img").item(0).onmouseover();
	initmenu.getElementsByTagName("img").item(0).onmouseover = null;
	initmenu.getElementsByTagName("img").item(0).onmouseout = null;
	initmenu.menuContainer.current = initmenu;
}
function tabMenuClick() {
	currentmenu = this.menuContainer.current;
	if (currentmenu != this) {
		currentmenu.targetEl.style.display = "none";
		currentmenu.getElementsByTagName("img").item(0).onmouseover = menuOver;
		currentmenu.getElementsByTagName("img").item(0).onmouseout = menuOut;
		currentmenu.getElementsByTagName("img").item(0).onmouseout();

		this.targetEl.style.display = "block";
		this.getElementsByTagName("img").item(0).onmouseover = null;
		this.getElementsByTagName("img").item(0).onmouseout = null;
		this.menuContainer.current = this;
	}
	return false;
}


/**
 * 메뉴, 현재위치 스크립트
 */

// convert menu text to image
function initTopNavigation() {

	// change top menu text to image
	topMenuEl = document.getElementById("top-navigation").getElementsByTagName("li");
	for (i=0; i<topMenuEl.length; i++) {
		if (topMenuEl[i].className.length == 6)
		{
			var menuAnchor = topMenuEl[i].getElementsByTagName("a")[0];
			menuAnchor.innerHTML = "<img src=\"/img/main/" + topMenuEl[i].className + ".gif\" border=\"0\" alt=\"" + menuAnchor.innerHTML + "\" />";
			menuAnchor.getElementsByTagName("img").item(0).onmouseover = menuOver;
			menuAnchor.getElementsByTagName("img").item(0).onkeyup = menuOver;
			menuAnchor.getElementsByTagName("img").item(0).onmouseout = menuOut;
			menuAnchor.getElementsByTagName("img").item(0).onkeyup = menuOut;
		}
	}

	// remove slashed background image of first menu item
	/*topMenuD2El = document.getElementsBySelector("#top-navigation ul.depth2");
	for (i=0; i<topMenuD2El.length; i++) {
		topMenuD2El[i].getElementsByTagName("li").item(0).style.background = "none";
	}*/
}

function initTopNavigationS(i) {

	switch(i){
		case 1 : var img = "01_apperal"; break;
		case 2 : var img = "02_news"; break;
		case 3 : var img = "03_intro"; break;
		case 4 : var img = "04_open"; break;
		case 5 : var img = "05_life"; break;
		case 6 : var img = "06_industry"; break;
		case 7 : var img = "07_data"; break;
		case 8 : var img = "08_guide"; break;
		default :  var img = "01_apperal"; break;
	}

	// change top menu text to image
	topMenuEl = document.getElementById("top-navigationS").getElementsByTagName("li");
	for (i=0; i<topMenuEl.length; i++) {
		if (topMenuEl[i].className.length == 6)
		{
			var menuAnchor = topMenuEl[i].getElementsByTagName("a")[0];
			menuAnchor.innerHTML = "<img src=\"/img/"+img+"/" + topMenuEl[i].className + ".gif\" border=\"0\" alt=\"" + menuAnchor.innerHTML + "\" />";
			menuAnchor.getElementsByTagName("img").item(0).onmouseover = menuOver;
			menuAnchor.getElementsByTagName("img").item(0).onmouseout = menuOut;
		}
	}

	// remove slashed background image of first menu item
	/*topMenuD2El = document.getElementsBySelector("#top-navigation ul.depth2");
	for (i=0; i<topMenuD2El.length; i++) {
		topMenuD2El[i].getElementsByTagName("li").item(0).style.background = "none";
	}*/
}

/* TODO : Sub Menu 수정시 사용 */
function initSubNavigation() {
	subMenuEl = document.getElementById("sub-navigation").getElementsByTagName("li");
	for (i=0; i<subMenuEl.length; i++) {
		var menuAnchor = subMenuEl[i].getElementsByTagName("a")[0];
		menuAnchor.innerHTML = "<img src=\"/images/menu/sub/" + subMenuEl[i].className + ".gif\" alt=\"" + menuAnchor.innerHTML + "\" />";
		menuAnchor.getElementsByTagName("img").item(0).onmouseover = menuOver;
		menuAnchor.getElementsByTagName("img").item(0).onmouseout = menuOut;
	}
}

// top menu action
var currentMenu;		// current shown menu (mouse over)
var focusMenu;			// current page menu
var menu = new Array();	// each menu array

function initNavigation() {
	for(i=1; i<9; i++) {
		menu[i] = document.getElementsBySelector("#top-navigation li.menu-" + i)[0];
		menu[i].onmouseover = navigationOver;
		menu[i].onkeyup = navigationOver;
		menu[i].onmouseout = navigationOut;

		menu[i].submenu = document.getElementsBySelector("#top-navigation li.menu-" + i +" ul.depth2")[0];
		menu[i].submenu.menu = menu[i];
		menu[i].submenu.onmouseover = subOver;
	}
}

function initNavigationS() {
	for(i=1; i<9; i++) {
		menu[i] = document.getElementsBySelector("#top-navigationS li.menu-" + i)[0];
		menu[i].onmouseover = navigationOver;
		menu[i].onkeyup = navigationOver;
		menu[i].onmouseout = navigationOut;

		menu[i].submenu = document.getElementsBySelector("#top-navigationS li.menu-" + i +" ul.depth2")[0];
		menu[i].submenu.menu = menu[i];
		menu[i].submenu.onmouseover = subOver;
	}
}
function initFocus() {		// current page menu focusing
	if (focusMenu) {
		currentMenu = focusMenu;
	} else {
		currentMenu = null;
	}
	showSubmenu();
}
function navigationOver() {		// onmouseover
	hideSubmenu();
	if (focusMenu) {
		focusMenu.submenu.style.display = "none";
	}
	currentMenu = this;
	showSubmenu();
}
function navigationOut() {		// onmouseout
	this.isMenuOver = false;
	setTimeout("hideSubmenu()", 700);
}
function showSubmenu() {
	if (currentMenu) {
		currentMenu.submenu.style.display = "block";
		currentMenu.isMenuOver = true;
	}
}
function hideSubmenu() {
	if (currentMenu && !currentMenu.isMenuOver) {
		currentMenu.submenu.style.display = "none";
		currentMenu.isMenuOver = false;
		initFocus();
	}
}
function subOver() {
	this.menu.isMenuOver = true;
}

// get location and set current menu focus
function initLocation() {
	initNavigation();

	if (document.getElementById("location")) {
		/*
		** [사용예] <div id="location">열려있는 민원</div>
		** [설명] div 안에 텍스트와 depth1을 비교하여 select 되도록 함
		*/
		if (document.getElementById("top-navigation") && document.getElementById("top-navigation").getElementsByTagName("li")) {
			topMenuLi = document.getElementById("top-navigation").getElementsByTagName("li");
			for(j=0; j<topMenuLi.length; j++) {
				if (topMenuLi[j].getElementsByTagName("img").item(0))
				{
					topMenuText = topMenuLi[j].getElementsByTagName("img").item(0).alt;
					if (topMenuText == document.getElementById("location").innerHTML) {		// top menu depth1
						focusMenu = menu[topMenuLi[j].className.substr(topMenuLi[j].className.length-1, 1)];
						initFocus();
						if (currentMenu.getElementsByTagName("img").item(0).src.indexOf("_ov.gif") == -1) {
							currentMenu.getElementsByTagName("img").item(0).onmouseover();
						}
						currentMenu.getElementsByTagName("img").item(0).onmouseover = null;
						currentMenu.getElementsByTagName("img").item(0).onmouseout = null;
					}
				}
			}
		}
		/*
		** [사용예] <div id="location"><a href="/">HOME</a> : <a href="#">열려있는 민원</a> : <a href="#">민원안내</a></div>
		** [설명] 두번째 a 태그는 depth1과 비교하고, 세번째 a 태그는 depth2와 비교하여 select 되도록 함

		loc = document.getElementById("location").getElementsByTagName("a");

		if (document.getElementById("top-navigation") && document.getElementById("top-navigation").getElementsByTagName("li")) {
			topMenuLi = document.getElementById("top-navigation").getElementsByTagName("li");
			for(j=0; j<topMenuLi.length; j++) {
				topMenuText = topMenuLi[j].getElementsByTagName("img").item(0).alt;
				if (loc.item(1) && topMenuText == loc.item(1).innerHTML) {		// top menu depth1
					focusMenu = menu[topMenuLi[j].className.substr(topMenuLi[j].className.length-1, 1)];
					initFocus();
					if (currentMenu.getElementsByTagName("img").item(0).src.indexOf("_ov.gif") == -1) {
						currentMenu.getElementsByTagName("img").item(0).onmouseover();
					}
					currentMenu.getElementsByTagName("img").item(0).onmouseover = null;
					currentMenu.getElementsByTagName("img").item(0).onmouseout = null;
				}
				if (loc.item(2) && topMenuText == loc.item(2).innerHTML) {		// top menu depth2
					topMenuLi[j].getElementsByTagName("img").item(0).onmouseover();
					topMenuLi[j].getElementsByTagName("img").item(0).onmouseover = null;
					topMenuLi[j].getElementsByTagName("img").item(0).onmouseout = null;
				}
			}
		}*/

		/* TODO : Sub Menu 수정시 사용 */
		if (document.getElementById("sub-navigation") && document.getElementById("sub-navigation").getElementsByTagName("li")) {
			subMenuLi = document.getElementById("sub-navigation").getElementsByTagName("li");
			for(i=0; i<subMenuLi.length; i++) {
				subMenuText = subMenuLi[i].getElementsByTagName("img").item(0).alt;
				if (loc.item(2) && subMenuText == loc.item(2).innerHTML) {		// sub menu depth2
					if (subMenuLi[i].getElementsByTagName("img").item(0).src.indexOf("_ov.gif") == -1) {
						subMenuLi[i].getElementsByTagName("img").item(0).onmouseover();
					}
					subMenuLi[i].getElementsByTagName("img").item(0).onmouseover = null;
					subMenuLi[i].getElementsByTagName("img").item(0).onmouseout = null;
					if (subMenuLi[i].getElementsByTagName("ul").item(0)) {
						subMenuLi[i].getElementsByTagName("ul").item(0).style.display = "block";
					}
				}
				if (loc.item(3) && subMenuText == loc.item(3).innerHTML) {		// sub menu depth3
					if (subMenuLi[i].getElementsByTagName("img").item(0).src.indexOf("_ov.gif") == -1) {
						subMenuLi[i].getElementsByTagName("img").item(0).onmouseover();
					}
					subMenuLi[i].getElementsByTagName("img").item(0).onmouseover = null;
					subMenuLi[i].getElementsByTagName("img").item(0).onmouseout = null;
				}
			}
		}
	}
}

function initLocationS() {
	initNavigationS();

	if (document.getElementById("location")) {
		/*
		** [사용예] <div id="location">열려있는 민원</div>
		** [설명] div 안에 텍스트와 depth1을 비교하여 select 되도록 함
		*/
		if (document.getElementById("top-navigationS") && document.getElementById("top-navigationS").getElementsByTagName("li")) {
			topMenuLi = document.getElementById("top-navigationS").getElementsByTagName("li");
			for(j=0; j<topMenuLi.length; j++) {
				if (topMenuLi[j].getElementsByTagName("img").item(0))
				{
					topMenuText = topMenuLi[j].getElementsByTagName("img").item(0).alt;
					if (topMenuText == document.getElementById("location").innerHTML) {		// top menu depth1
						focusMenu = menu[topMenuLi[j].className.substr(topMenuLi[j].className.length-1, 1)];
						initFocus();
						if (currentMenu.getElementsByTagName("img").item(0).src.indexOf("_ov.gif") == -1) {
							currentMenu.getElementsByTagName("img").item(0).onmouseover();
						}
						currentMenu.getElementsByTagName("img").item(0).onmouseover = null;
						currentMenu.getElementsByTagName("img").item(0).onmouseout = null;
					}
				}
			}
		}
		/*
		** [사용예] <div id="location"><a href="/">HOME</a> : <a href="#">열려있는 민원</a> : <a href="#">민원안내</a></div>
		** [설명] 두번째 a 태그는 depth1과 비교하고, 세번째 a 태그는 depth2와 비교하여 select 되도록 함

		loc = document.getElementById("location").getElementsByTagName("a");

		if (document.getElementById("top-navigation") && document.getElementById("top-navigation").getElementsByTagName("li")) {
			topMenuLi = document.getElementById("top-navigation").getElementsByTagName("li");
			for(j=0; j<topMenuLi.length; j++) {
				topMenuText = topMenuLi[j].getElementsByTagName("img").item(0).alt;
				if (loc.item(1) && topMenuText == loc.item(1).innerHTML) {		// top menu depth1
					focusMenu = menu[topMenuLi[j].className.substr(topMenuLi[j].className.length-1, 1)];
					initFocus();
					if (currentMenu.getElementsByTagName("img").item(0).src.indexOf("_ov.gif") == -1) {
						currentMenu.getElementsByTagName("img").item(0).onmouseover();
					}
					currentMenu.getElementsByTagName("img").item(0).onmouseover = null;
					currentMenu.getElementsByTagName("img").item(0).onmouseout = null;
				}
				if (loc.item(2) && topMenuText == loc.item(2).innerHTML) {		// top menu depth2
					topMenuLi[j].getElementsByTagName("img").item(0).onmouseover();
					topMenuLi[j].getElementsByTagName("img").item(0).onmouseover = null;
					topMenuLi[j].getElementsByTagName("img").item(0).onmouseout = null;
				}
			}
		}*/

		/* TODO : Sub Menu 수정시 사용 */
		if (document.getElementById("sub-navigation") && document.getElementById("sub-navigation").getElementsByTagName("li")) {
			subMenuLi = document.getElementById("sub-navigation").getElementsByTagName("li");
			for(i=0; i<subMenuLi.length; i++) {
				subMenuText = subMenuLi[i].getElementsByTagName("img").item(0).alt;
				if (loc.item(2) && subMenuText == loc.item(2).innerHTML) {		// sub menu depth2
					if (subMenuLi[i].getElementsByTagName("img").item(0).src.indexOf("_ov.gif") == -1) {
						subMenuLi[i].getElementsByTagName("img").item(0).onmouseover();
					}
					subMenuLi[i].getElementsByTagName("img").item(0).onmouseover = null;
					subMenuLi[i].getElementsByTagName("img").item(0).onmouseout = null;
					if (subMenuLi[i].getElementsByTagName("ul").item(0)) {
						subMenuLi[i].getElementsByTagName("ul").item(0).style.display = "block";
					}
				}
				if (loc.item(3) && subMenuText == loc.item(3).innerHTML) {		// sub menu depth3
					if (subMenuLi[i].getElementsByTagName("img").item(0).src.indexOf("_ov.gif") == -1) {
						subMenuLi[i].getElementsByTagName("img").item(0).onmouseover();
					}
					subMenuLi[i].getElementsByTagName("img").item(0).onmouseover = null;
					subMenuLi[i].getElementsByTagName("img").item(0).onmouseout = null;
				}
			}
		}
	}
}


function getAllChildren(e) {
	// Returns all children of element. Workaround required for IE5/Windows. Ugh.
	return e.all ? e.all : e.getElementsByTagName('*');
}

document.getElementsBySelector = function(selector) {
	// Attempt to fail gracefully in lesser browsers
	if (!document.getElementsByTagName) {
		return new Array();
	}
	// Split selector in to tokens
	var tokens = selector.split(' ');
	var currentContext = new Array(document);
	for (var i = 0; i < tokens.length; i++) {
		token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
		if (token.indexOf('#') > -1) {
			// Token is an ID selector
			var bits = token.split('#');
			var tagName = bits[0];
			var id = bits[1];
			var element = document.getElementById(id);
			if (tagName && element.nodeName.toLowerCase() != tagName) {
				// tag with that ID not found, return false
				return new Array();
			}
			// Set currentContext to contain just this element
			currentContext = new Array(element);
			continue; // Skip to next token
		}
		if (token.indexOf('.') > -1) {
			// Token contains a class selector
			var bits = token.split('.');
			var tagName = bits[0];
			var className = bits[1];
			if (!tagName) {
				tagName = '*';
			}
			// Get elements matching tag, filter them for class selector
			var found = new Array;
			var foundCount = 0;
			for (var h = 0; h < currentContext.length; h++) {
				var elements;
				if (tagName == '*') {
						elements = getAllChildren(currentContext[h]);
				} else {
						elements = currentContext[h].getElementsByTagName(tagName);
				}
				for (var j = 0; j < elements.length; j++) {
					found[foundCount++] = elements[j];
				}
			}
			currentContext = new Array;
			var currentContextIndex = 0;
			for (var k = 0; k < found.length; k++) {
				if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
					currentContext[currentContextIndex++] = found[k];
				}
			}
			continue; // Skip to next token
		}
		// Code to deal with attribute selectors
		if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
			var tagName = RegExp.$1;
			var attrName = RegExp.$2;
			var attrOperator = RegExp.$3;
			var attrValue = RegExp.$4;
			if (!tagName) {
				tagName = '*';
			}
			// Grab all of the tagName elements within current context
			var found = new Array;
			var foundCount = 0;
			for (var h = 0; h < currentContext.length; h++) {
				var elements;
				if (tagName == '*') {
						elements = getAllChildren(currentContext[h]);
				} else {
						elements = currentContext[h].getElementsByTagName(tagName);
				}
				for (var j = 0; j < elements.length; j++) {
					found[foundCount++] = elements[j];
				}
			}
			currentContext = new Array;
			var currentContextIndex = 0;
			var checkFunction; // This function will be used to filter the elements
			switch (attrOperator) {
				case '=': // Equality
					checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
					break;
				case '~': // Match one of space seperated words
					checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
					break;
				case '|': // Match start with value followed by optional hyphen
					checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
					break;
				case '^': // Match starts with value
					checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
					break;
				case '$': // Match ends with value - fails with "Warning" in Opera 7
					checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
					break;
				case '*': // Match ends with value
					checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
					break;
				default :
					// Just test for existence of attribute
					checkFunction = function(e) { return e.getAttribute(attrName); };
			}
			currentContext = new Array;
			var currentContextIndex = 0;
			for (var k = 0; k < found.length; k++) {
				if (checkFunction(found[k])) {
					currentContext[currentContextIndex++] = found[k];
				}
			}
			// alert('Attribute Selector: '+tagName+' '+attrName+' '+attrOperator+' '+attrValue);
			continue; // Skip to next token
		}
		// If we get here, token is JUST an element (not a class or ID selector)
		tagName = token;
		var found = new Array;
		var foundCount = 0;
		for (var h = 0; h < currentContext.length; h++) {
			var elements = currentContext[h].getElementsByTagName(tagName);
			for (var j = 0; j < elements.length; j++) {
				found[foundCount++] = elements[j];
			}
		}
		currentContext = found;
	}
	return currentContext;
}


