var isIE = window.navigator.userAgent.indexOf("MSIE") != -1;

/*************************UTIL BEGIN**************************/
Object.extend = function(destination, source) {
  for (var property in source)
    destination[property] = source[property];
  return destination;
};

Object.prototype.extend = function(object) { 
   return Object.extend.apply(this, [this, object]); 
};

/* Object.extend(Object, {
	toJSON: function(object) {
	    var type = typeof object;
	    switch (type) {
	      case 'undefined':
	      case 'function':
	      case 'unknown': return;
	      case 'boolean': return object.toString();
	    }

	    if (object === null) return 'null';
	    if (object.toJSON) return object.toJSON();
	    if (Object.isElement(object)) return;

	    var results = [];
	    for (var property in object) {
	      var value = Object.toJSON(object[property]);
	      if (!Object.isUndefined(value))
	        results.push(property.toJSON() + ': ' + value);
	    }

	    return '{' + results.join(', ') + '}';
	},
	clone: function(object) {
	    return Object.extend({ }, object);
	},

	isElement: function(object) {
		return object && object.nodeType == 1;
	},
	
	isUndefined: function(object) {
		return typeof object == "undefined";
	}
}); */

var Class = { 
    create: function() { 
		return function() { 
			this.init.apply(this, arguments); 
		}
    }
}

function $(e){
	return typeof e == 'object' ? e : document.getElementById(e);
}

var Try = {
  these: function() {
    var returnValue;

    for (var i = 0, length = arguments.length; i < length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) { }
    }

    return returnValue;
  }
};

function htmlentitie(s){
	return s.replace(
		/&/g, "&amp;").replace(
		/'/g, "&#039;").replace(
		/"/g, "&quot;").replace(
		/</g, "&lt;").replace(
		/>/g, "&gt;");
}

function unhtmlentitie(s){
	return s.replace(
		/&#039;/g, "'").replace(
		/&quot;/, "\"").replace(
		/&lt;/, "<").replace(
		/&gt;/, ">").replace(
		/&amp;/g, "&");
}

String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
}

function get_style_left(v){
	if(!v) return 0;
	if(v.indexOf('%') != -1)
		return screen.availWidth * parseInt(v)/100;
	else
		return parseInt(v);
}

function get_style_top(v){
	if(!v) return 0;
	if(v.indexOf('%') != -1)
		return screen.availHeight * parseInt(v)/100;
	else
		return parseInt(v);
}

function get_scrollTop(){
	return (document.documentElement.scrollTop || document.body.scrollTop);
}

function get_scrollHeight(){
	var height = (document.documentElement.scrollHeight || document.body.scrollHeight);
	height = height < screen.availHeight ? screen.height : height;
	return height;
}

function get_screen_width(){
	return (document.documentElement.clientWidth || document.body.clientWidth);
}

function get_screen_height(){
	return (document.documentElement.clientHeight || document.body.clientHeight);
}

function ajax_parameters(o){
	var p = '';
	for(var k in o){
		if(typeof o[k] == 'function') continue;
		if(typeof o[k] == 'object'){
			for(var i = 0; i < o[k].length; i++)
				p += '&'+ k +'[]='+ encodeURIComponent(o[k][i]);
		}else{
			p += '&'+ k +'='+ encodeURIComponent(o[k]);
		}
	}
	return '_='+p;
}

var Element = {
	show: function(e){
		if(typeof e == 'string') e = $(e);
		
		e.style.display = 'block';
	},
	
	hide: function(e){
		if(typeof e == 'string') e = $(e);
		e.style.display = 'none';
	},
	
	visible: function(e){
		if(typeof e == 'string') e = $(e);
		
		e.style.visibility = 'visible';
	},
	
	invisible: function(e){
		if(typeof e == 'string') e = $(e);
		
		e.style.visibility = 'hidden';
	},
	
	create: function(e){
		if(typeof e == 'object'){
			var o = document.createElement(e.tag);
			for(var k in e){
				if(k == 'tag' || typeof e[k] == 'function') continue;
				o.setAttribute(k, e[k]);
				eval('o.'+k +'=e[k];');
			}
			return o;
		}
		return document.createElement(e);
	},
	
	getY: function(e){
		var offset = e.offsetTop;
		if(e.offsetParent!=null) offset += Element.getY(e.offsetParent);
		return offset;
	},
	
	getX: function(e){
		var offset = e.offsetLeft;
		if(e.offsetParent!=null) offset += Element.getX(e.offsetParent);
		return offset;
	},
	
	remove: function(e){		//complete remove
		if(typeof e != 'object') e = $(e);
		try{
			e.removeNode(true);
		}catch(ex){
			e.parentNode.removeChild(e);
		}
	},
	
	clear: function(e){		//clear child node
		if(typeof e != 'object') e = $(e);
		while(e.hasChildNodes()){
			e.removeChild(e.lastChild);
		}
	}
};

Form = {
	input: function(i){
		if(typeof i == 'object'){
			if(isIE){
				var s = '<input ';
				for(var k in i){
					if(typeof i[k] == 'function') continue;
					s += ' '+ k + '="'+ i[k] +'" ';
				}
				s += '/>';
				return Element.create(s);
			}
			i.tag = 'input';
		}
		return Element.create(i);
	}
}
/*************************UTIL END**************************/


/*************************UI BEGIN**************************/

/*************************************************************/
//drag_window.window	为全局对象,将append到页面中
/*************************************************************/
var drag_window = Class.create();
drag_window.prototype = {
	init: function(options){
		if(!options) options = {};
		this.options = {
			id: options.id || '',							//id of window div
			title: options.title || '',						//window title
			content: options.content || '',					//window content
			draggable: options.draggable,					//是否可以拖拽,默认no
			width: options.width || 400,
			height: options.height || '',
			zIndex: options.zIndex || 1000,					// style.zIndex
			className: options.className || '',				//window class
			close_text: options.close_text || '',			//window closable if set
			cover_screen: options.cover_screen || null,		//cover screen options
			close_handle: options.close_handle || ''		//do before close,if closable
		}
		
		this.window = Element.create('div');
		document.body.appendChild(this.window);
		
		this.title_bar = Element.create('div');				//title bar include title and close
		this.title_bar.id = this.options.id + '_title';
		
		if(this.options.close_text){
			this.close_text = document.createElement('span');
			this.close_text.onclick = function(){
				_this.close();
			}
			this.close_text.style.cssFloat = 'right';		//ie
			this.close_text.style.styleFloat = 'right';		//ff
			if(typeof this.options.close_text == 'object'){
				this.close_text.appendChild(this.options.close_text);
			}else{
				this.close_text.innerHTML = this.options.close_text;
			}
			this.close_text.onmouseover = function(){
				_this.close_text.style.cursor = 'pointer';
			}
			this.title_bar.appendChild(this.close_text);
		}
		
		this.title = Element.create('span');				//title text
		if(typeof this.options.title == 'object'){
			this.title.appendChild(this.options.title);
		}else{
			this.title.innerHTML += this.options.title;
		}
		this.title_bar.appendChild(this.title);
		
		this.window.id = this.options.id;
		this.mouseX = '';
		this.mouseY = '';
		this.x = '';
		this.y = '';
		this.dragging = 0;
		var _this = this;
		
		this.title_bar.onmouseover = function(){	//change the cursor
			_this.title_bar.style.cursor = 'move';
		}
		
		
		this.title_bar.onmousedown = function(event){	//start drag
			if(!_this.options.draggable) return;
			
			_this.window.onselectstart = function(){	//no select text on title
				return false;
			}
			_this.drag_border.style.width = _this.window.offsetWidth + 'px';
			_this.drag_border.style.height = _this.window.offsetHeight + 'px';
			_this.drag_border.style.left = _this.window.style.left;
			_this.drag_border.style.top = _this.window.style.top;
			
			event = !event ? window.event : event;
			_this.mouseX = event.clientX;
			_this.mouseY = get_scrollTop() + event.clientY;
			_this.x = get_style_left(_this.window.style.left);
			_this.y = get_style_top(_this.window.style.top);
			_this.dragging = 1;
			
			document.onmousemove = function(event){		//mouse move event, must attach to document, not title bar
				if(!_this.dragging) return;
				
				Element.show(_this.drag_border);
				event = !event ? window.event : event;
				_this.drag_border.style.left =  (_this.x + event.clientX - _this.mouseX ) + 'px';
				_this.drag_border.style.top = (_this.y + get_scrollTop() + event.clientY - _this.mouseY ) + 'px';
			};
			var tmp = document.onmouseup;
			document.onmouseup = function(event){			// end drag
				event = !event ? window.event : event;
				_this.window.onselectstart = function(){	//no select text on title
					return true;
				}
				_this.window.style.left =  _this.drag_border.style.left;
				_this.window.style.top = _this.drag_border.style.top;
				Element.hide(_this.drag_border);
				_this.dragging = 0;
				document.onmouseup = tmp;
			};
		}
		
		this.content = document.createElement('div');
		this.content.id = this.options.id + '_content';
		if(typeof this.options.content =='object'){
			this.content.appendChild(this.options.content);
		}else{
			this.content.innerHTML = this.options.content;
		}
		
		this.window.appendChild(this.title_bar);			
		this.window.appendChild(this.content);				// finish filling the window
		
		this.drag_border = Element.create('div');
		this.drag_border.id = this.options.id +'_border';
		this.drag_border.style.position = 'absolute';
		Element.hide(this.drag_border);
		document.body.appendChild(this.drag_border);
		
		if(this.window.className)
			this.window.className = this.options.className;
		else
			this.set_default_style();
		
		Element.hide(this.window);
	},
	
	show: function(x, y){
		if(this.options.cover_screen){
			this.cover_screen = new cover_screen(this.options.cover_screen);
			this.cover_screen.show();
		}
		Element.show(this.window);
		
		if(x != null && y != null){
			x = x < 0 ? 0 : x;
			y = y < 0 ? 0 : y;
			x = x > get_screen_width() - this.window.offsetWidth ?  get_screen_width() - this.window.offsetWidth : x;
			y = y > get_scrollTop() + get_screen_height() - this.window.offsetHeight ?  get_scrollTop() + get_screen_height() - this.window.offsetHeight : y;
			this.window.style.left = x + 'px';
			this.window.style.top = y + 'px';
		}else{
			this.window.style.left = parseInt((get_screen_width() - this.window.offsetWidth)/2) + 'px';
			this.window.style.top = parseInt(get_scrollTop() + (get_screen_height() - this.window.offsetHeight)/2) + 'px';
		}
	},
	
	hide: function(){
		Element.hide(this.window);
		if(this.options.cover_screen){
			this.cover_screen.hide();
		}
	},
	
	set_default_style: function(){		//set window default style if className not set
		this.window.style.position = 'absolute';
		this.content.style.padding = '5px';
		this.window.style.width = this.options.width + 'px';
		this.window.style.height = (this.options.height ? this.options.height + 'px' : '');
		this.window.style.zIndex = this.options.zIndex;
		this.window.style.backgroundColor = 'white';
		this.window.style.border = '1px solid #6394bd';
		
		this.drag_border.style.border = '2px solid #333333';
		this.drag_border.style.zIndex = this.window.style.zIndex +1;
		
		this.title_bar.style.backgroundColor = '#dee7ef';
		this.title_bar.style.padding = '2px';
	},
	
	set_title: function(title){
		this.title.innerHTML = '';
		if(typeof title =='object'){
			this.title.appendChild(title);
		}else{
			this.title.innerHTML = title;
		}
	},
	
	set_content: function(content){
		this.content.innerHTML = '';
		if(typeof content =='object'){
			this.content.appendChild(content);
		}else{
			this.content.innerHTML = content;
		}
	},
	
	append_content: function(content){
		if(typeof content =='object'){
			var tmp = Element.create('p');
			tmp.appendChild(content)
			this.content.appendChild(tmp);
		}else{
			this.content.innerHTML += '<p>'+ content +'</p>';
		}
	},
	
	close: function(){
		Element.hide(this.window);
		if(this.cover_screen) this.cover_screen.remove();
		if(this.options.close_handle)
			this.options.close_handle();
	},
	
	remove: function(){
		Element.remove(this.window);
	}
};

var cover_screen = Class.create();
cover_screen.prototype = {
	init: function(options){
		this.set_options(options);
		
		this.cover = document.createElement('div');
		this.cover.id = this.options.id;
		this.cover.style.top = '0px';
		this.cover.style.left = '0px';
		this.cover.style.width = get_screen_width() + 'px';
		this.cover.style.height = (get_scrollHeight()) + 'px';
		this.cover.style.position = 'absolute';
		this.cover.style.zIndex = this.options.zIndex;
		this.cover.style.backgroundColor = this.options.backgroundColor;
		this.cover.style.opacity = this.options.opacity;
		this.cover.style.filter = 'alpha(opacity='+ this.options.opacity*100 +')';
		this.cover.className = this.options.className;
		document.body.appendChild(this.cover);
		Element.hide(this.cover);
	},
	
	set_options: function(options){
		if(!options) options = {};
		
		this.options = {
			id: options.id || '',
			backgroundColor: options.backgroundColor || '#333333',
			opacity: options.opacity || 0.3,
			zIndex: options.zIndex || 999,
			className: options.className || ''
		};
	},
	
	show: function(){
		Element.show(this.cover);
	},
	
	remove: function(){
		Element.remove(this.cover);
	}
};


var tool_tip = Class.create();
tool_tip.prototype = {
	init: function(options){
		if(!options) options = {};
		this.options = {
			id: options.id || '',
			className: options.className || null,
			zIndex: options.zIndex || 998,
			hide_timeout: options.hide_timeout || 300,
			show_timeout: options.show_timeout || 0
		};
		this.tooltip = Element.create('div');
		this.tooltip.id = this.options.id;
		this.tooltip.style.position = 'absolute';
		this.hide_timeout_id = 0;
		this.show_timeout_id = 0;
		
		if(this.options.className != null)
			this.tooltip.className = this.options.className;
		else
			this.set_default_style();
		
		document.body.appendChild(this.tooltip);
		this.hide();
		
		var _this = this;
		
		this.tooltip.onmouseover = function(){
			if(_this.hide_timeout_id) clearTimeout(_this.hide_timeout_id);
			_this.show();
			
		}
		this.tooltip.onmouseout = function(){
			_this.hide_timeout_id = setTimeout(function(){_this.hide();}, _this.options.hide_timeout);
		}
	},
	
	set_content: function(value){
		this.tooltip.innerHTML = '';
		if(typeof value == 'object'){
			this.tooltip.appendChild(value);
		}else{
			this.tooltip.innerHTML = value;
		}
	},
	
	set_default_style: function(){
		this.tooltip.style.zIndex = this.options.zIndex;
		this.tooltip.style.border = '1px solid #000000';
		this.tooltip.style.padding = '5px';
		this.tooltip.style.backgroundColor = '#FFFFE1';
	},
	
	width: function(){
		return this.tooltip.offsetWidth;
	},
	
	height: function(){
		return this.tooltip.offsetHeight;
	},
	
	show: function(x, y){
		var _this = this;
		if(this.show_timeout_id) clearTimeout(this.show_timeout_id);
		
		this.show_timeout_id = setTimeout(
		function(){
			Element.visible(_this.tooltip);
			if(x != null && y != null){
				x = x < 0 ? 0 : x;
				y = y < 0 ? 0 : y;
				x = x > get_screen_width() - _this.width() ?  get_screen_width() - _this.width() : x;
				y = y > get_scrollTop() + get_screen_height() - _this.height() ?  get_scrollTop() + get_screen_height() - _this.height() : y;
				_this.tooltip.style.left = x + 'px';
				_this.tooltip.style.top = y + 'px';
			}else{
				_this.tooltip.style.left = x + 'px';
				_this.tooltip.style.top = y + 'px';
			}
		}, this.options.show_timeout);
	},
	
	hide: function(){
		if(this.show_timeout_id) clearTimeout(this.show_timeout_id);
		Element.invisible(this.tooltip);
	}
};


var slide_bar = Class.create();
slide_bar.prototype = {
	init: function(options){
		this.options = {
			id: options.id || '',
			max_value: options.max_value,		//required
			min_value: options.min_value,		//required
			step: options.step,					//required
			type: options.type || 'vertical',	//horizon or vertical
			low_bar: options.low_bar || 0,		// low_bar default value
			low_bar_onDrag: options.low_bar_onDrag || null,		//dragging handle with value
			low_bar_endDrag: options.low_bar_endDrag || null,	//after dragging handle with value
			high_bar: options.high_bar || 0,	// same as low_bar
			high_bar_onDrag: options.high_bar_onDrag || null,		//dragging handle with value
			high_bar_endDrag: options.high_bar_endDrag || null,	//after dragging handle with value
			className: options.className || ''
		};
		
		this.dragged = 0;//滑条是否被拖拽过
		this.low_bar_dragging = 0;
		this.low_bar_value = 0;	//low_bar 值
		this.low_bar_piece = 0;	//low_bar 第几值块
		this.low_bar_position = 0; //low_bar 像素值
		this.value_range = this.options.max_value - this.options.min_value;	//值范围
		//最小值小数位
		this.float_point = this.options.min_value.toString().indexOf('.') != -1 ? this.options.min_value.toString().substr(this.options.min_value.toString().indexOf('.') +1).length : 0;
		
		this.value_pieces = this.value_range / this.options.step;	//多少块值
		this.pixel_piece = 0;	//每块值多少像素
		this.width_range = 0;	//像素宽度范围
		this.height_range = 0;	//像素高度范围
		
		this.x = 0;		//像素坐标
		this.y = 0;
		
		this.container = Element.create('div');
		this.container.id = this.options.id;
		
		this.low_bar = Element.create('div');
		this.low_bar.id = this.options.id + '_lower_bar';
		this.container.appendChild(this.low_bar);
		
		if(this.options.high_bar){
			this.high_bar_dragging = 0;
			this.high_bar_value = 0;
			this.high_bar_piece = 0;	//high_bar 第几值块
			this.high_bar_position = 0; //high_bar 像素值
			this.high_bar = Element.create('div');
			this.high_bar.id = this.options.id + '_high_bar';
			this.container.appendChild(this.high_bar);
			
			this.high_bar.onmouseover = function(){
				_this.high_bar.style.cursor = 'pointer';
			}
			
			this.high_bar.onmousedown = function(){
				_this.high_bar_dragging = 1;
				_this.dragged = 1;
				document.onmousemove = function(e){
					if(!_this.high_bar_dragging) return;
					
					e = !e ? window.event : e;
					
					if(_this.options.type == 'vertical')
						var position = get_scrollTop() + e.clientY - _this.y;
					else
						var position = e.clientX - _this.x;
					
					_this.move_to(position, 'high_bar');
				}
				
				document.onmouseup = function(){
					_this.high_bar_dragging = 0;
					_this.dragged = 0;
					if(_this.options.high_bar_endDrag)
						_this.options.high_bar_endDrag(_this.high_bar_value);
				}
			}
		}
		
		var _this = this;
		
		if(this.options.className)
			this.container.className = this.options.className;
		else
			this.set_default_style();
		
		this.container.onmousedown = function(e){
			if(_this.dragged) return;
			e = !e ? window.event : e;
			
			if(_this.options.type == 'vertical'){
				var position = get_scrollTop() + e.clientY - _this.y;
				var range = _this.height_range;
			}else{
				var position = e.clientX - _this.x;
				var range = _this.width_range;
			}
			
			if(_this.options.high_bar){
				if(position < range/2){
					_this.move_to(position, 'low_bar');
					if(_this.options.low_bar_endDrag)
						_this.options.low_bar_endDrag(_this.low_bar_value);
				}else{
					_this.move_to(position, 'high_bar');
					if(_this.options.high_bar_endDrag)
						_this.options.high_bar_endDrag(_this.high_bar_value);
				}
			}else{
				_this.move_to(position, 'low_bar');
				if(_this.options.low_bar_endDrag)
						_this.options.low_bar_endDrag(_this.low_bar_value);
			}
		}
		
		this.low_bar.onmouseover = function(){
			_this.low_bar.style.cursor = 'pointer';
		}
			
		this.low_bar.onmousedown = function(){
			_this.low_bar_dragging = 1;
			_this.dragged = 1;
			
			document.onmousemove = function(e){
				if(!_this.low_bar_dragging) return;
				
				e = !e ? window.event : e;
				
				if(_this.options.type == 'vertical')
					var position = get_scrollTop() + e.clientY - _this.y;
				else
					var position = e.clientX - _this.x;
					
				_this.move_to(position, 'low_bar');
			}
			
			document.onmouseup = function(){	//此mouseup优先于container的mouseup
				_this.low_bar_dragging = 0;
				_this.dragged = 0;
				if(_this.options.low_bar_endDrag)
					_this.options.low_bar_endDrag(_this.low_bar_value);
			}
		}
	},
	
	start: function(){		//init position
		this.get_property();
		
		this.low_bar_value = this.options.low_bar < this.options.min_value ? this.options.min_value : this.options.low_bar;
		this.low_bar_position = this.value_to_position(this.low_bar_value);
		this.low_bar_piece = this.position_to_piece(this.low_bar_position);
		
		if(this.options.type == 'vertical'){
			this.low_bar.style.top = this.low_bar_position + 'px';
		}else{
			this.low_bar.style.left = this.low_bar_position + 'px';
		}
		
		if(this.options.high_bar){
			this.high_bar_value = this.options.high_bar > this.options.max_value ? this.options.max_value : this.options.high_bar;
			this.high_bar_position = this.value_to_position(this.high_bar_value);
			this.high_bar_piece = this.position_to_piece(this.high_bar_position);
			
			if(this.options.type == 'vertical'){
				this.high_bar.style.top = this.high_bar_position + 'px';
			}else{
				this.high_bar.style.left = this.high_bar_position + 'px';
			}
		}
	},
	
	move_to: function(position, bar){
		if(bar == 'low_bar'){
			this.low_bar_piece = this.position_to_piece(position);
			if(this.options.high_bar){
				this.low_bar_piece = this.low_bar_piece >= this.high_bar_piece -1 ? this.high_bar_piece -2 : this.low_bar_piece;
				this.high_bar_piece = this.high_bar_piece <= this.low_bar_piece +1 ? this.low_bar_piece +2 : this.high_bar_piece;
			}
			
			this.low_bar_position = this.low_bar_piece * this.pixel_piece;
			this.low_bar_position = this.low_bar_position < 0 ? 0 : this.low_bar_position;
			if(this.options.type == 'vertical'){
				this.low_bar_position = this.low_bar_position > this.height_range ? this.height_range : this.low_bar_position;
				this.low_bar.style.top = this.low_bar_position + 'px';
			}else{
				this.low_bar_position = this.low_bar_position > this.width_range ? this.width_range : this.low_bar_position;
				this.low_bar.style.left = this.low_bar_position + 'px';
			}
			
			this.low_bar_value = this.low_bar_value = this.position_to_value(this.low_bar_position);
			
			if(this.options.low_bar_onDrag)
				this.options.low_bar_onDrag(this.low_bar_value);
		}else{
			this.high_bar_piece = this.position_to_piece(position);
			this.high_bar_piece = this.high_bar_piece <= this.low_bar_piece ? this.low_bar_piece +2 : this.high_bar_piece;
			
			this.high_bar_position = this.high_bar_piece * this.pixel_piece;
			this.high_bar_position = this.high_bar_position < 0 ? 0 : this.high_bar_position;
			if(this.options.type == 'vertical'){
				this.high_bar_position = this.high_bar_position > this.height_range ? this.height_range : this.high_bar_position;
				this.high_bar.style.top = this.high_bar_position + 'px';
			}else{
				this.high_bar_position = this.high_bar_position > this.width_range ? this.width_range : this.high_bar_position;
				this.high_bar.style.left = this.high_bar_position + 'px';
			}
			
			this.high_bar_value = this.position_to_value(this.high_bar_position);
			
			if(this.options.high_bar_onDrag)
				this.options.high_bar_onDrag(this.high_bar_value);
		}
	},
	
	position_to_value: function(p){
		if(this.options.type == 'vertical'){
			var percent = p / this.height_range;
		}else{
			var percent = p / this.width_range;
		}
		
		return Math.round((percent * this.value_range + this.options.min_value) * Math.pow(10, this.float_point)) / Math.pow(10, this.float_point);
	},
	
	value_to_position: function(v){
		return Math.round(v / this.options.step) * this.pixel_piece;
	},
	
	position_to_piece: function(p){
		return Math.round(p / this.pixel_piece);
	},
	
	get_property: function(){
		this.width_range = this.container.offsetWidth - this.low_bar.offsetWidth;
		this.height_range = this.container.offsetHeight - this.low_bar.offsetHeight;
		this.x = Element.getX(this.container);
		this.y = Element.getY(this.container);
		if(this.options.type == 'vertical'){
			this.pixel_piece = this.height_range / this.value_pieces;
		}else{
			this.pixel_piece = this.width_range / this.value_pieces;
		}
	},
	
	set_default_style: function(){
		
		this.container.style.position = 'relative';
		this.container.style.border = '1px solid #000000';
		this.container.style.cssFloat = 'left';
		this.container.style.styleFloat = 'left';
		
		this.low_bar.style.position = 'absolute';
		this.low_bar.style.border = '1px solid #000000';
		this.low_bar.style.overflow = 'hidden';
		this.low_bar.style.backgroundColor = '#000000';
		
		if(this.options.type == 'vertical'){
			this.container.style.width = '80px';
			this.container.style.height = '150px';
			
			this.low_bar.style.width = '80px';
			this.low_bar.style.height = '5px';
		}else{
			this.container.style.width = '150px';
			this.container.style.height = '20px';
			
			this.low_bar.style.width = '5px';
			this.low_bar.style.height = '20px';
		}
		
		if(this.options.high_bar){
			this.high_bar.style.position = 'absolute';
			this.high_bar.style.border = '1px solid #000000';
			this.high_bar.style.overflow = 'hidden';
			this.high_bar.style.backgroundColor = '#ffffff';
			
			if(this.options.type == 'vertical'){
				this.high_bar.style.width = '80px';
				this.high_bar.style.height = '5px';
			}else{
				this.high_bar.style.width = '5px';
				this.high_bar.style.height = '20px';
			}
		}
	}
};
/*************************UI END**************************/


/***************AJAX********************/

var ajaxing = Class.create();
ajaxing.prototype = {
	init: function(options){
		if(!options) options = {};
		this.options = {
			style: options.style || 1,		//style	1 屏幕中央 2 元素中间 
			element: options.element || '',
			gif: options.gif || '',
			text: options.text || '',
			className: options.className || '',
			zIndex: options.zIndex || 1001
		};
		
		this.processing = Element.create('div');
		this.processing.style.position = 'absolute';
		this.processing.style.zIndex = this.options.zIndex;
		this.processing.className = this.options.className;
		
		document.body.appendChild(this.processing);
		if(this.options.gif){
			var img = Element.create('img');
			img.src = this.options.gif;
			this.processing.appendChild(img);
		}
		if(this.options.text){
			this.processing.innerHTML += this.options.text;
		}
	},
	
	show: function(){
		switch(this.options.style){
			case 1:
				this.processing.style.left = parseInt((get_screen_width() - this.processing.offsetWidth)/2) + 'px';
				this.processing.style.top = parseInt(get_scrollTop() + (get_screen_height() - this.processing.offsetHeight)/2) + 'px';
			break;
			
			case 2:
				
				this.processing.style.left = parseInt((get_screen_width() - this.processing.offsetWidth)/2) + 'px';
				this.processing.style.top = parseInt(get_scrollTop() + (get_screen_height() - this.processing.offsetHeight)/2) + 'px';
			break;
		}
	},
	
	hide: function(){
		Element.remove(this.processing);
	}
};



CYAJAX = function(options){
	this.completed = 0;
	this.query_count = 0;		//请求次数
	this.start_time = 0;		//开始时间
	this.retry_times = 0;		//己重试次数
	this.timeout_interval = 0;	//ajax超时间隔
	
	this.ajax_error_text = '';	//错误信息
	this.response_xml = '';		//交互完成后返回的XML
	this.response_text = '';	//responseText
	
	this.ajax_options = {
		url: '',				//请求URL
		parameters: '',			//请求参数
		asynchronous: true,		//异步?
		method: 'GET',			//method
		response_type: 'xml',	//返回类型 xml or text,默认xml
		timeout: 10,			//重新执行延时,单位 秒
		retry: 2,				//重新执行次数
		charset: 'gbk',			//编码
		string_handle: null,	//对结果处理的字符串函数	if complete handle set, this is useless
		ajaxing_options: {},
		complete_handle: null,	//do while ajax complete
		//以上参数须均在继承的类中出现
		
		contentType: 'application/x-www-form-urlencoded'
	};
	//if(options) this.set_options(options);
	this.cyajax = this.initcyajax();
}

CYAJAX.prototype.initcyajax = function(){
		try{
			var ajax = new XMLHttpRequest();
		}catch(e){
			var oAjax = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
			for(var i=0;i<oAjax.length;i++){
				try{
					ajax = new ActiveXObject(oAjax[i]);
				}catch(e){}
			}
		}
		if(!ajax){
			alert("Failed to enable Ajax");
		}else{
			return ajax;
		}
}

CYAJAX.prototype.set_ajax_options = function(options){
		if(options && options.method) options.method = options.method.toUpperCase();
		if(options && options.response_type) options.response_type = options.response_type.toLowerCase();
		
		this.ajax_options = {			//重新设置时只是新值部分覆盖
			url: options.url || this.ajax_options.url,										//请求URL		required
			parameters: options.parameters || this.ajax_options.parameters,					//请求参数		
			asynchronous: options.asynchronous || true,										//异步?		
			method: options.method || this.ajax_options.method,								//method
			response_type: options.response_type || this.ajax_options.response_type,		//	required
			timeout: options.timeout || this.ajax_options.timeout,							//重新执行延时,单位 秒
			retry: options.retry || this.ajax_options.retry,								//重新执行次数
			charset: options.charset || this.ajax_options.charset,							//编码
			string_handle: options.string_handle ||this.ajax_options.string_handle,			//字符串处理
			ajaxing_options: options.ajaxing_options || this.ajax_options.ajaxing_options,		//do while ajaxing
			complete_handle: options.complete_handle || this.ajax_options.complete_handle,	//do while ajax complete
			
			contentType: this.ajax_options.contentType + (this.ajax_options.charset != '' ? '; charset='+this.ajax_options.charset : '')
		};
		if(this.ajax_options.ajaxing_options)
			this.cyajaxing = new ajaxing(this.ajax_options.ajaxing_options);
}

CYAJAX.prototype.send = function(){
		if(this.cyajaxing)
			this.cyajaxing.show();
		this.dosend(this);
}

CYAJAX.prototype.dosend = function(ajax){
	if(this.cyajax){
			this.completed = 0;
			//$('debug').innerHTML+='send<br>';
			if(!this.ajax_options.url) return; //no url set then do nothing
			var body = this.ajax_options.method == 'POST' ? this.ajax_options.parameters : null;
			var url = this.ajax_options.method == 'POST' ? this.ajax_options.url : this.ajax_options.url+ '?' + this.ajax_options.parameters;
			
			this.cyajax.open(this.ajax_options.method.toUpperCase(), url, this.ajax_options.asynchronous);
			
			this.cyajax.setRequestHeader('Content-type', this.ajax_options.contentType);
			this.cyajax.setRequestHeader('Accept', 'text/javascript, text/html, application/xml, text/xml, */*');
			
			this.cyajax.onreadystatechange = function(){
				//$('debug').innerHTML+=ajax.cyajax.readyState+':'+ajax.cyajax.status+'<br />';
				if (ajax.cyajax && ajax.cyajax.readyState == 4 && ajax.cyajax.status == 200) {
					ajax.completed = 1;
					//$('debug').innerHTML+='complete<br>';
					ajax.complete();
					return;
				}
			};
			this.cyajax.send(body);
			this.start_time = (new Date).getTime();
			this.timeout_interval = setTimeout( function(){ajax.do_timeout(ajax);} , this.ajax_options.timeout * 1000);
	}
}

/* CYAJAX.prototype.abort = function(){
		this.cyajax.abort();
		this.clear_timeout();
} */

CYAJAX.prototype.do_timeout = function(ajax){
		if(ajax.completed){
			ajax.clear_timeout();
			return;
		}
		if(ajax.retry_times == ajax.ajax_options.retry ){
			ajax.clear_timeout();
			this.error('请求超时');
			return;
		}
		
		ajax.cyajax.abort();
		ajax.retry_times += 1;
		ajax.send();
}

CYAJAX.prototype.clear_timeout = function(){
		clearTimeout(this.timeout_interval);
		this.retry_times = 0;
		this.timeout_interval = -1;
}

CYAJAX.prototype.error = function(text){
	if(this.cyajaxing)
		this.cyajaxing.hide();
	alert("Ajax发生错误!\n错误提示:"+ text);
}

CYAJAX.prototype.complete = function(){
		this.clear_timeout();
		
		if(!this.completed) return;
		
		if (this.cyajax.responseXML && this.cyajax.responseXML.documentElement) {
			var xml = this.cyajax.responseXML.documentElement.getElementsByTagName("response");
			if(xml.length) 
				this.response_xml = xml[0];
		}
			
		if(this.cyajax.responseText)
			this.response_text = this.cyajax.responseText;
		
		if(this.ajax_options.response_type == 'xml'){
			if(this.ajax_options.complete_handle){
				xml = this.response_xml;
				this.ajax_options.complete_handle(xml);
				this.cyajax.abort();//this have to do after get response
				
				if(this.cyajaxing)
					this.cyajaxing.hide();
				return;
			}
		}else if(this.ajax_options.response_type == 'text'){
			if(this.ajax_options.complete_handle){
				text = this.response_text;
				this.ajax_options.complete_handle(text);
				this.cyajax.abort();//this have to do after get response
				
				if(this.cyajaxing)
					this.cyajaxing.hide();
				return;
			}
		}
		
		this.do_complete();
		this.cyajax.abort();
		
		if(this.cyajaxing)
			this.cyajaxing.hide();
}
// complete handle like this 	handle	function handle(text_or_xml){// deal with text_or_xml}
/*****************AJAX END*************************/


/*****************ajax send*************************/
var ajax_send = Class.create();
ajax_send.prototype = (new CYAJAX()).extend({
	init: function(options){
		this.options = {};
		this.set_options(options);
	},
	
	set_options: function(options){
		if(!options) options = {};
		this.set_ajax_options(options);
	},
	
	do_complete: function(){
		if(this.ajax_options.response_type == 'xml'){
			try{
				var content = this.response_xml.getElementsByTagName("content")[0].firstChild.nodeValue;
				if(this.ajax_options.string_handle)
					content = this.ajax_options.string_handle(content);
				alert(content);
			}catch(e){this.error('无数据');}
		}else{
			try{
				content = this.response_text;
				if(this.ajax_options.string_handle)
					content = this.ajax_options.string_handle(content);
				alert(content);
			}catch(e){this.error('无数据');}
		}
	}
});
/*****************ajax send end*************************/


/*****************ajax load html*************************/
var ajax_load_html = Class.create();
ajax_load_html.prototype = (new CYAJAX()).extend({
	init: function(options){
		this.set_options(options);
	},
	
	set_options: function(options){
		if(!options) options = {};
		this.set_ajax_options(options);
		this.options = {
			element : options.element || '',
			element_property : options.element_property || 'innerHTML'
		};
	},
	
	do_complete: function(){
		if(this.ajax_options.response_type == 'xml'){
			try{
				var content = this.response_xml.getElementsByTagName("content")[0].firstChild.nodeValue;
				if(this.ajax_options.string_handle)
					content = this.ajax_options.string_handle(content);
			}catch(e){
				content = '';
			}
			eval('$(this.options.element).'+this.options.element_property+' = content');
		}else{
			try{
				var content = this.response_text;
				if(this.ajax_options.string_handle)
					content = this.ajax_options.string_handle(content);
			}catch(e){
				content = '';
			}
			eval('$(this.options.element).'+this.options.element_property+' = content');
		}
	}
});
/*****************ajax load html end*************************/


var autocomplete = Class.create();
autocomplete.prototype = (new CYAJAX()).extend({
	init: function(options){
		this.options = {
			url: options.url || '',
			option : options.option || '',
			text_box : options.text_box,									//input text id
			text_box_class : options.text_box_class || '',					//normal class
			text_ajaxing_class : options.text_ajaxing_class || '',			//processing class
			method: options.method || 'get',
			response_type: options.response_type || 'xml',
			id : options.id || '',											//div id
			result_popup_class : options.result_popup_class || '',			//div class
			result_item_class : options.result_item_class || '',			//li class
			result_item_focus_class : options.result_item_focus_class || '',//li on class
			item_handle: options.item_handle || null,						//when click or press Enter then do with the value
			keyup_timeout : options.keyup_timeout || 2						//keyup do timeout in n second
		};
		//this.set_options(options);
		this.result_offset = -1;			//结果偏移
		this.result_count = 0;				//结果数
		this.result_hide_timeout = 0;		//结果隐藏Timeout id
		this.timeout_id = 0;				//查询Timeout ID
		this.select_option_changed = false;	//查询选择是否改变
		this.select_option = '';			//查询选项
		this.last_select_option = '';		//上一个查询选项
		this.keyword = '';					//当前关键词
		this.last_keyword = '';				//上一个关键词
		this.item_height = 0;
		var _this = this;
		
		this.result_popup = Element.create('div');
		this.result_popup.id = this.options.id;
		this.result_popup.className = this.options.result_popup_class;
		this.result_popup.style.position = 'absolute';
		this.result_popup.style.left = Element.getX($(this.options.text_box)) + 'px';
		this.result_popup.style.top = (Element.getY($(this.options.text_box)) + $(this.options.text_box).offsetHeight + 1) + 'px';
		this.result_popup.style.width = $(this.options.text_box).offsetWidth +'px';
		this.result_popup.onfocus = function(){_this.result_popup_onfocus();}
		this.result_popup.onblur = function(){_this.result_popup_onblur();}
		this.result_popup_piece = 0;	//每版多少条结果才滚动
		
		$(this.options.text_box).autocomplete = 'off';
		$(this.options.text_box).className = this.options.text_box_class;
		$(this.options.text_box).onkeyup = function(event){_this.text_box_onkeyup(event);};
		$(this.options.text_box).onfocus = function(){_this.text_box_onfocus();}
		$(this.options.text_box).onblur = function(){_this.text_box_onblur();}
	},
	
	do_complete: function(){
		var xml = this.response_xml.getElementsByTagName('item');
		this.result_count = xml.length;
		var array = new Array();
		for(var i = 0; i < xml.length; i++){
			array[i] = {text: xml[i].getElementsByTagName('text')[0].firstChild.nodeValue, value: xml[i].getElementsByTagName('value')[0].firstChild.nodeValue}
		}
		this.list_result_items(array);
	},
	
	popup_switch: function(s){
		switch(s){
			case 'on':
				if(this.result_hide_timeout)
					clearTimeout(this.result_hide_timeout);
				Element.show(this.result_popup);
			break;
			
			case 'off':
				var _this = this;
				this.result_hide_timeout = setTimeout(function(){Element.hide(_this.result_popup);}, 300);
				//IE输入框失焦后结果会马上消失,所以要设个timeout阻止过快消失
		}
	},
	
	text_box_onkeyup: function(e){
		if(!e) e = window.event;
		if(e.type != 'keyup') return;
		if (e.keyCode) { key = e.keyCode; }else if (typeof(e.which)!= 'undefined') { key = e.which; }
	  
		switch(key){
			case 38://up
				if(!this.result_count) return;
				
				var tmp_offset = this.result_offset -1;
				tmp_offset = tmp_offset < 0 ? this.result_count - 1 : tmp_offset;
				
				$(this.options.id + tmp_offset).className = this.options.result_item_focus_class;
				if(this.result_offset != -1)
					$(this.options.id +this.result_offset).className = this.options.result_item_class;
				this.result_offset = tmp_offset;
				this.result_popup.scrollTop = this.result_offset / this.result_popup_piece * this.result_popup.offsetHeight;
			break;
			
			case 40://down
				if(!this.result_count) return;
				
				var tmp_offset = this.result_offset + 1;
				tmp_offset = tmp_offset > this.result_count - 1 ? 0 : tmp_offset;
				
				$(this.options.id + tmp_offset).className = this.options.result_item_focus_class;
				if(this.result_offset != -1)
					$(this.options.id + this.result_offset).className = this.options.result_item_class;
				this.result_offset = tmp_offset;
				this.result_popup.scrollTop = this.result_offset / this.result_popup_piece * this.result_popup.offsetHeight;
			break;
			
			case 13://enter
				if(this.result_offset == -1) return;
				
				if(this.options.item_handle)
					this.options.item_handle($(this.options.id + this.result_offset).title);
				else
					$(this.options.text_box).value = $(this.options.id + this.result_offset).title;
			break;
			
			default:
				if(this.options.option){
					this.select_option_changed = true;
					this.select_option = $(this.options.option).value.trim();
					if(this.select_option == '' || this.select_option == this.last_option)
						this.select_option_changed = false;
				}
				
				this.keyword = $(this.options.text_box).value.trim();
				//关键词不能为空不查,查询时查询选项和关键词都不变的话也不查
				if( this.keyword == '' || this.keyword == this.last_keyword && !this.select_option_changed){
					clearTimeout(this.timeout_id); 
					$(this.options.text_box).className = this.options.text_box_class;
					
					if(this.keyword == '')
						this.popup_switch('off');
					return;
				}
				
				$(this.options.text_box).className = this.options.text_ajaxing_class;
				//$('debug').innerHTML+='keywor:'+this.keyword+'<br>lk:'+this.last_keyword+'<br>';
				if(this.options.option)
					this.options.parameters = ajax_parameters({options: $(this.options.option).value, key: $(this.options.text_box).value});
				else
					this.options.parameters = ajax_parameters({key: $(this.options.text_box).value});
				
				this.set_ajax_options({url: this.options.url, method: this.options.method, response_type: this.options.response_type, parameters: this.options.parameters});
				//如果在keyup_timeout时间内还有键盘输入则取消上次请求
				if(this.timeout_id)
					clearTimeout(this.timeout_id);
				
				Element.clear(this.result_popup);
				var _this = this;
				this.timeout_id = setTimeout(function(){_this.send();}, this.options.keyup_timeout * 1000);
		}
	},
	
	list_result_items: function(array){		//传入一个数组布置结果列表
		this.last_keyword = this.keyword;
		if(this.options.option)
			this.last_option = this.select_option;
		
		$(this.options.text_box).className = this.options.text_box_class;
		this.result_popup.innerHTML = '';
		var ul = Element.create('ul');
		var li_focus_class = this.options.result_item_focus_class;
		var _this = this;
		//$('debug').innerHTML+='class:'+li_onclass+'<br>';
		
		for(i = 0; i < array.length; i++){
			var li = Element.create('li');
			li.id = this.options.id + i;	//id格式为 当前结果集的div的id + 循环值,如 id[i]
			li.className = this.options.result_item_class;
			li.innerHTML = array[i].text;
			li.title = array[i].value;		//选择title属性保存实际值,因为title是标准的w3c属性
			
			li.onmouseover = function(){
				if(_this.result_offset != -1) $(_this.options.id + _this.result_offset).className =  _this.options.result_item_class;
				_this.result_offset = parseInt(this.id.replace(/[^0-9]/g, ''));
				this.className = _this.options.result_item_focus_class;
			}
			
			/* li.onmouseout = function(){
				if(_this.result_offset == parseInt(this.id.replace(/[^0-9]/g, ''))){
					_this.result_offset = -1;
					this.className = _this.options.result_item_class;
				}
			} */
			
			li.onclick = function(){
				if(_this.options.item_handle)
					_this.options.item_handle(this.title);
				else
					$(_this.options.text_box).value = this.title;
			}
			
			ul.appendChild(li);
		}
		
		this.result_popup.appendChild(ul);
		this.popup_switch('on');
		document.body.appendChild(this.result_popup);
		if(li) this.item_height = li.offsetHeight;
		this.result_popup_piece = this.result_popup.offsetHeight / li.offsetHeight;
		this.result_offset = -1;
	},
	
	//弹出结果获得焦点事件
	result_popup_onfocus: function(){
		this.popup_switch('on');
	},
	//弹出结果失去焦点事件
	result_popup_onblur: function(){
		this.popup_switch('off');
	},
	
	text_box_onfocus: function(){
		$(this.options.text_box).select();
		if(this.result_count){
			this.popup_switch('on');
		}
	},
	
	text_box_onblur: function(){
		this.popup_switch('off');
	}
	
});