/*
<script type="text/javascript">
<!--
*/

var AtatCate = {
	server: '/product/cate.ajax.php',
	xhr: null,
	categories:null,
	children: {},
	isBusy: false,
	width: null,
	layer: null,
	pos:null,
	select: function (d, src) {

		var catebox = getFirstParentByTagAndClassName(src, 'div', 'catebox');
		if (catebox==null)
			return false;
		AtatCate.categories = getElementsByTagAndClassName('select', null, catebox);
		if (AtatCate.categories==null)
		{
			return false;
		}

		if (d<1 || d>=4)
		{
			return true;
		}
		var c = AtatCate.categories[d-1];

		for (var i=d; i<4; i++)
		{
			AtatCate.remove(i);
		}

		if (c.value=='')
		{
			return true;
		}

		if (c.value in AtatCate.children)
		{
			// Ä³½¬¿¡ ÀÖÀ½
			AtatCate.show(d, c.value, AtatCate.children[c.value]);
		}
		else 
		{
			// db·Î ¿äÃ»
			AtatCate.load(c.value);
		}
		return true;
	},
	setWidth: function (w) {
		AtatCate.width = w;
	},
	getSelected: function() {
		if (AtatCate.categories==null)
		{
			var catebox = $('catebox');
			if (catebox==null)
				return null;
			AtatCate.categories = getElementsByTagAndClassName('select', null, catebox);
			if (AtatCate.categories==null)
			{
				return null;
			}
		}
		var c = null;
		var i=0;
		for (i=3; i>=0; i--)
		{
			if (AtatCate.categories[i].value!='')
			{
				c = AtatCate.categories[i];
				break;
			}
		}
		if (c==null)
		{
			return null;
		}
		var id = c.value;
		var name = '';
		for (; i>=0; i--)
		{
			c = AtatCate.categories[i];
			if (name.length>0) {
				name = ' > ' + name;
			}
			name = scrapeText(c.options[c.selectedIndex]) + name;
		}

		return {'id':id, 'name': name};
	},
	remove: function (d) {
		if (d<0 || d>=4)
		{
			return false;
		}
		var c = AtatCate.categories[d];

		for (var i=c.options.length-1; i>0; i--)
		{
			removeElement(c.options[i]);
		}
		if (AtatCate.width != null)
			updateNodeAttributes(c, {'style':'width:'+AtatCate.width});
	},
	show: function (d, parent, children) {
		var c = AtatCate.categories[d];
		for (id in children)
		{
			if (typeof(children[id])!='string')
			{
				continue;
			}

			var opt = OPTION({'value':id}, children[id]);
			appendChildNodes(c, opt);
		}
		if (AtatCate.width != null)
			updateNodeAttributes(c, {'style':'width:'+AtatCate.width});
		AtatCate.children[parent] = children;
	},
	loadUrl: function(url, callback) {
		if (AtatCate.xhr==null) {
			AtatCate.xhr = getXMLHttpRequest();
		}

		if (AtatCate.xhr==null) {
			alert("Browser does not support XMLHttpRequest");
			return ;
		}
		if (AtatCate.isBusy) {
			AtatCate.xhr.abort();
		}
		AtatCate.isBusy = true;
		AtatCate.xhr.open("GET", url, true);
		AtatCate.xhr.onreadystatechange = callback;
		AtatCate.xhr.send(null);
	},
	load: function(parent) {
		if (AtatCate.xhr==null) {
			AtatCate.xhr = getXMLHttpRequest();
		}

		if (AtatCate.xhr==null) {
			alert("Browser does not support XMLHttpRequest");
			return ;
		}
		if (AtatCate.isBusy) {
			AtatCate.xhr.abort();
		}
		AtatCate.isBusy = true;
		AtatCate.xhr.open("GET", AtatCate.server+'?act=getchild&parent='+parent, true);
		AtatCate.xhr.onreadystatechange = AtatCate.receiveResponse;
		AtatCate.xhr.send(null);
	},
	receiveResponse: function () {
		var state = AtatCate.xhr.readyState;

		if (state==4) {
			AtatCate.isBusy = false;
			AtatCate.callback(AtatCate.xhr.responseText, AtatCate.xhr.responseXML);
		}
	},
	callback: function(resText, resXML) {
		if (resText==null || resText.length==0) {
			return ;
		}

		var res = evalJSON(resText);

		if (res['result']!='success')
		{
			return ;
		}

		var d = res['parent'].length / 3;
		AtatCate.show(d, res['parent'], res['child']);
	},
	initSubCateLayer: function() {
		AtatCate.layer = DIV( {'style':'position:absolute; left:0; top:0; z-index:10; '}, null );
		//,mconnect(AtatCate.layer, 'onmouseout', AtatCate.hideSubCate);
		hideElement(AtatCate.layer);
		appendChildNodes(document.body, AtatCate.layer);
		var tops = getElementsByTagAndClassName('li', 'leftmenu', null);
		for (var i=0; i<tops.length; i++) {
			connect(tops[i], 'onmouseover', AtatCate.showSubCate);
		}
	},
	showSubCate: function (e) {
		var src = e.src();
		cate = src.id.substr(5);
		var url = '/product/sub_cate_layer.php?cate='+cate;

		AtatCate.pos = getElementPosition(src);

		AtatCate.pos.x += 30;

		AtatCate.loadUrl(url, showSubCateLayer);

		return true;
	},
	hideSubCate: function (e) {
		hideElement(AtatCate.layer);

		return true;
	}
};

function showSubCateLayer() {
	var state = AtatCate.xhr.readyState;

	if (state==4) {
		AtatCate.isBusy = false;
		AtatCate.layer.innerHTML = AtatCate.xhr.responseText;
		showElement(AtatCate.layer);
		setElementPosition(AtatCate.layer, AtatCate.pos);
	}
}

addLoadEvent(AtatCate.initSubCateLayer);

