/*
+--------------------------------------------------------
+	Copyright (c) 2004-2007 WeSofts
+	JavaScript Project for wespace
+	http://www.wesofts.com
+--------------------------------------------------------
*/
var category_appended_container = 'category_appended_container';
document.write('<div id="'+category_appended_container+'" style="z-index:3;display:block;float:left;"></div>');
/*
+--------------------------------------------------------
+	Define mover object
+--------------------------------------------------------
*/
function mover_object(){
	this.mover_actived = '';
	this.mover_activer = function(eid){
		if (this.mover_actived == '' || this.mover_actived != eid){
			this.mover_actived = eid;
		}
	}
	this.mover_killer = function(eid){
		if (this.mover_actived != ''){
			this.mover_actived = '';
		}
	}
	this.mover_moves = function(){
	}
}
/*
+--------------------------------------------------------
+	Initialize Mover handle
+--------------------------------------------------------
*/
mover = new mover_object();
/*
+--------------------------------------------------------
+	Container fill appended element
+--------------------------------------------------------
*/
var category_last_o = '';
var category_last_l = '';
var category_last_m = '<div class="wrap">loading</div>';
var category_closer = false;
/*
+--------------------------------------------------------
+	Define category object
+--------------------------------------------------------
*/
function category_object(){
	this.autocloser = function(){
		if (!this.closetime){
			this.closetime = 0;
		}
		if (!category_closer){
			this.closetime = 0;
			return false;
		}else{
			this.closetime++;
		}


		/*

		if (this.closetime > 50){
			this.closetime = 0;
			category_closer = false;
			this.close();
		}else{
			setTimeout('category.autocloser()',1);
		} 

		*/


	}
	/*
	+----------------------------------------------------
	+	Initialize option
	+----------------------------------------------------
	*/
	this.targeturl = '';
	this.categoryoption = new Array();
	/*
	+----------------------------------------------------
	+	Append container to click element
	+----------------------------------------------------
	*/
	this.append = function(iscenter){
		/*
		+------------------------------------------------
		+	Exists checks
		+------------------------------------------------
		*/
		if (!$(this.categoryoption['container'])){
			/*
			+------------------------------------------------
			+	Group append container
			+------------------------------------------------
			*/
			var	appended = document.createElement('div');
			appended.setAttribute('id',this.categoryoption['container']);
			if (typeof mover != 'undefined'){
				appended.setAttribute('onmouseover','mover.mover_activer("'+this.categoryoption['container']+'")');
				appended.setAttribute('onmouseout','mover.mover_killer("'+this.categoryoption['container']+'")');
			}
			/*
			+------------------------------------------------
			+	Query grouped container
			+------------------------------------------------
			*/
			$(category_appended_container).appendChild(appended);
		}
		/*
		+----------------------------------------------------
		+	Parse appended element style
		+----------------------------------------------------
		*/
		$(this.categoryoption['container']).style.display = '';
		$(this.categoryoption['container']).className = 'catesload';
		$(this.categoryoption['container']).style.position = 'absolute';
		$(this.categoryoption['container']).style.zIndex = '104';
		/*
		+----------------------------------------------------
		+	Parse appended element position
		+----------------------------------------------------
		*//*
		if (this.categoryoption['position']){
			if (this.categoryoption['position'] == 'center'){
				this.mousexy = this.screenxy();
				this.mousexy_x = this.mousexy['x'];
				this.mousexy_y = this.mousexy['y'] + $(this.categoryoption['element']).offsetHeight + 3;
			}else{*/
				this.mousexy = this.offsetxy();
				this.mousexy_x = this.mousexy['x'];
				if ((this.mousexy_x + $(this.categoryoption['container']).offsetWidth + 100) > document.body.clientWidth){
					this.mousexy_x = this.mousexy['x'] + $(this.categoryoption['element']).offsetWidth - $(this.categoryoption['container']).offsetWidth;
					if (is_ie){
						this.mousexy_x += 2;
					}
				}
				//this.mousexy_y = this.mousexy['y'] + $(this.categoryoption['element']).offsetHeight + 3;
				this.mousexy_y = this.mousexy['y'] + $(this.categoryoption['element']).offsetHeight + 1;
			/*}
		}else{
			this.mousexy = this.offsetxy();
			this.mousexy_x = this.mousexy['x'];
			if (this.mousexy_x + $(this.categoryoption['element']).offsetWidth + $(this.categoryoption['container']).offsetWidth > document.body.clientWidth){
				this.mousexy_x -= $(this.categoryoption['container']).offsetWidth;
			}else{
				this.mousexy_x += $(this.categoryoption['element']).offsetWidth + 3;
			}
			this.mousexy_y = this.mousexy['y'];
		}*/

		$(this.categoryoption['container']).style.top  = this.mousexy_y + 'px';
		$(this.categoryoption['container']).style.left = this.mousexy_x + 'px';
	}
	this.offsetxy = function(eid) {
		this.clickede = eid && $(eid) ? $(eid) : $(this.categoryoption['element']);
		var x = this.clickede.offsetLeft;
		var y = this.clickede.offsetTop;
		while ((this.clickede = this.clickede.offsetParent) != null) {
			x += this.clickede.offsetLeft;
			y += this.clickede.offsetTop;
		}
		return {'x':x,'y':y};
	}
	this.screenxy = function(eid) {
		this.clickede = eid && $(eid) ? $(eid) : $(this.categoryoption['element']);
		var y = this.clickede.offsetTop;
		while ((this.clickede = this.clickede.offsetParent) != null) {
			y += this.clickede.offsetTop;
		}
		var bodyw = document.body.clientWidth;
		return {'x': (bodyw - 600) / 2 ,'y': y};
	}
	/*
	+----------------------------------------------------
	+	Direct: for exists container
	+	eid: Event element
	+	cid: Container element
	+	isright: true ? right : down
	+----------------------------------------------------
	*/
	this.directzindex = 5;
	this.directon = function(eid,cid,isright){
		if (!$(cid) || !$(eid)){ return false; }
		if ($(cid).style.display != 'none'){ $(cid).style.display = 'none'; return false; }
		$(cid).style.display = '';
		$(cid).style.position = 'absolute';
		this.directzindex++;
		$(cid).style.zIndex = this.directzindex;
		this.mousexy = this.offsetxy(eid);
		if (isright){
			this.mousexy_x = this.mousexy['x'] + $(eid).offsetWidth + 3;
			this.mousexy_y = this.mousexy['y'];
		}else{
			this.mousexy_x = this.mousexy['x'];
			if (this.mousexy_x + $(cid).offsetWidth > document.body.clientWidth){
				this.mousexy_x = this.mousexy_x + $(eid).offsetWidth - $(cid).offsetWidth;
			}
			this.mousexy_y = this.mousexy['y'] + $(eid).offsetHeight + 2;
		}
		$(cid).style.top  = this.mousexy_y + 'px';
		$(cid).style.left = this.mousexy_x + 'px';
	}
	this.directoff = function(cid){
		if (this.directzindex > 5){
			this.directzindex--;
		}
		if ($(cid)){
			$(cid).style.display = 'none';
		}
	}
	/*
	+----------------------------------------------------
	+	Full load
	+----------------------------------------------------
	*/
	this.load = function(eid,option,info,lang,position,extents){
		if (!$(eid) || !option){
			/*
			+--------------------------------------------
			+	un defined element!
			+--------------------------------------------
			*/
			return false;
		}
		this.close();
		/*
		+------------------------------------------------
		+	Initialize options
		+------------------------------------------------
		*/
		this.categoryoption['info'] = info && info != '' ? info : 0;
		this.categoryoption['lang'] = lang && lang == '1' ? 1 : 0;
		this.categoryoption['option'] = option;
		this.categoryoption['position'] = position ? (position == 'center' ? 'center' : true) : false;
		this.categoryoption['element'] = eid;
		this.categoryoption['extents'] = extents && extents!='' ? extents : false;
		this.categoryoption['container'] = eid + '_container';
		/*
		+------------------------------------------------
		+	Appended element initialize
		+------------------------------------------------
		*/
		this.append();
		this.contain();
	}
	/*
	+----------------------------------------------------
	+	Parse indexed key and value
	+----------------------------------------------------
	*/
	this.parse = function(key,val,eid){
		this.allowparse = false;
		if (eid){
			this.allowparse = true;
			var et = 't' + eid;
			var en = eid;
			var ed = 'd' + eid;
		}else if($(this.categoryoption['element'])){
			this.allowparse = true;
			var et = this.categoryoption['element'];
			var en = et.substr(1,et.length - 1);
			var ed = 'd' + et.substr(1,et.length-1);
		}
		if (this.allowparse){
			if ($(et)){$(et).innerHTML = val;}
			if ($(en)){$(en).value = key;}
			if ($(ed)){$(ed).value = val;}
		}
	}
	/*
	+----------------------------------------------------
	+	load selected mulity options
	+----------------------------------------------------
	*/
	this.muload = function(eid,option,isen){
		var tmp = '';
		var comma = '';
		var selects = '';
		var extinfo = '';
		var isen = isen ? true : false;
		if ($('d' + eid)){
			selects = $('d' + eid).value.split('^');
			for (var i=0;i<selects.length;i++){
				tmp = selects[i].split(',');
				if (tmp.length >= 3){
					extinfo += comma + tmp[0] + '-' + tmp[1];
					comma = ',';
				}
			}
		}
		this.load(eid,option,'',isen,false,'extinfo=' + extinfo);
	}
	/*
	+----------------------------------------------------
	+	Change options
	+----------------------------------------------------
	*/
	this.mulchange = function(key,eid){
		if ($(eid+'hiddens' + key) && $(eid+'options')){
			$(eid+'options').innerHTML = $(eid+'hiddens' + key).innerHTML;
		}
	}
	this.mulcheck = function(input,eid,keya,keyb,value){
		var tmp = '';
		var selects = '';
		var displayd = '';
		var displayt = '';
		var currentkey = keya + '-' + keyb;
		if ($('d' + eid)){
			selects = $('d' + eid).value;
		}else{
			return false;
		}
		if (input.checked){
			if (selects == ''){
				displayd = keya + ',' + keyb + ',' + value;
				displayt = value;
			}else{
				var comma = '';
				var commb = '';
				selects = selects.split('^');
				for (var n=0;n<selects.length;n++){
					tmp = selects[n].split(',');
					if (tmp.length >= 3){
						if ((tmp[0] == 0 && tmp[1] == keyb) || (tmp[1] == keya)){
							return false;
						}
					}
				}
				for (var i=0;i<selects.length;i++){
					tmp = selects[i].split(',');
					if (tmp.length >= 3){
						var tmps = tmp[0] + '-' + tmp[1];
						if (tmps == currentkey || (keya == '0' && keyb == tmp[0])){}else{
							displayd += comma + selects[i];
							displayt += commb + tmp[2];
							comma = '^';
							commb = ',';
						}
					}
				}
				displayd += comma + keya + ',' + keyb + ',' + value;
				displayt += commb + value;
			}
		}else{
			var comma = '';
			var commb = '';
			selects = selects.split('^');
			for (var i=0;i<selects.length;i++){
				tmp = selects[i].split(',');
				if (tmp.length >= 3){
					if (tmp[0] == keya && tmp[1] == keyb){ }else{
						displayd += comma + selects[i];
						displayt += commb + tmp[2];
						comma = '^';
						commb = ',';
					}
				}
			}
		}
		if (displayt == '' || displayt == ','){
			displayt = '&nbsp;';
			displayd = '';
		}
		$('d' + eid).value = displayd;
		$(eid).innerHTML = displayt;
		$(eid + 'selects').innerHTML = displayt;
	}
	/*
	+----------------------------------------------------
	+	Parse mulity indexed key and value
	+----------------------------------------------------
	*/
	this.mulparse = function(eid,val){
		this.mulchange('0',eid);
		if ($('d' + eid)){
			$('d' + eid).value = '';
		}
		if ($(eid)){
			$(eid).innerHTML = val;
		}
		if ($(eid + 'selects')){
			$(eid + 'selects').innerHTML = '&nbsp;';
		}
	}
	/*
	+----------------------------------------------------
	+	Simple load just change language
	+----------------------------------------------------
	*/
	this.language = function(){
		category_last_l = category_last_l == '1' ? '0' : '1';
		this.categoryoption['lang'] = category_last_l;
		this.contain();
	}
	/*
	+----------------------------------------------------
	+	Parse data to appended element
	+----------------------------------------------------
	*/
	this.contain = function(){
		if (typeof category_noneoption_defined != 'undefined' && typeof category_noneoption_hyperlink != 'undefined' && category_noneoption_hyperlink != ''){
			this.targeturl = category_noneoption_hyperlink;
		}else{
			this.targeturl = 'category&amp;option=' + this.categoryoption['option'] + '&amp;info=' + this.categoryoption['info'] + '&amp;isen=' + this.categoryoption['lang'] + (this.categoryoption['extents'] ? '&amp;' + this.categoryoption['extents'] : '');
		}
		Ajax.xml(this.targeturl,this.categoryoption['container']);
	}
	/*
	+----------------------------------------------------
	+	Hidden appended element!
	+----------------------------------------------------
	*/
	this.close = function(eid){
		if (eid && $(eid)){
			$(eid).style.display = 'none';
			if (typeof category_noneoption_defined != 'undefined'){
				$(eid).innerHTML = '';
			}
		}else if ($(this.categoryoption['container'])){
			$(this.categoryoption['container']).style.display = 'none';
			if (typeof category_noneoption_defined != 'undefined'){
				$(this.categoryoption['container']).innerHTML = '';
			}
		}
	}
}
/*
+--------------------------------------------------------
+	Initialize category handle
+--------------------------------------------------------
*/
category = new category_object();