/**
 * http://www.jobcn.com/
 * By zhangping
 *
 * For: IE6,IE7,Firefox	
 * 
 * Required :	 masker.js;
 */
 
 
//default config
var FunctionSelectorConfig = {
	width:"500px", 
	height:"450px", 		
	className: "function_selector",	
	title: "Selector Box",	
	searchLabel:		"请输入要查找的工作岗位关键字：",	
	//top: "0px",
	//left: "0px",
	//topOffset: 320,	
	
	catelogArray: catelogs,
	
	modelDialog: true,
	closeText: "[关闭窗口]",
	submit: function(){void(0);} 
}

function FunctionSelector(config){
	for(var prop in FunctionSelectorConfig)
		if(config[prop] == null) config[prop] = FunctionSelectorConfig[prop];	

	var selector = null;		
	var titleBox = null;
	var searchBox = null;	
	var panelBox = null;	
	var functionBox = null;	
	var bottomBox  = null;
	
	function init(){
		var body = document.getElementsByTagName('BODY')[0];
		selector = document.createElement('div');
		body.appendChild(selector);

		selector.id = config.id;
		selector.style.zIndex = 500;
		selector.style.position = "absolute";
		selector.style.display = "none";
		selector.style.width = config.width;
		//if(config.height) selector.style.height = config.height;

		selector.className = config.className;

		genTitleBox();
		
		panelBox = document.createElement('DIV');	
 		panelBox.className = "win_panel";
		selector.appendChild(panelBox);	
			
		genSearchBox();		
		genFunctionBox();
		genBottomBox();		
	}
	
	function genTitleBox() {
		if(selector==null || selector.nodeName!="DIV") return;				
		
		titleBox = document.createElement('DIV');
		titleBox.className = "win_title";
		selector.appendChild(titleBox);
		
		var boxTitle = document.createElement('DIV');
		boxTitle.className = "win_title_L";
		boxTitle.appendChild(document.createTextNode(config.title));
		
		var boxClose = document.createElement('DIV');
		boxClose.className = "win_title_R";
		var link = document.createElement('A');
		link.href="javascript:void(0)";
		link.onclick = hide;
		link.style.color = "#FFF";
		link.appendChild(document.createTextNode(config.closeText));
		boxClose.appendChild(link);
				
		titleBox.appendChild(boxTitle);
		titleBox.appendChild(boxClose);				
	}
	
	function genSearchBox(){
		searchBox = document.createElement('DIV');	
 		searchBox.className = "search_box";
 		searchBox.appendChild(document.createTextNode(config.searchLabel));
 		
 		var input = document.createElement('INPUT');
 		input.type = "text";
 		input.id = "id_text_search_field";
 		input.size = 15;
 		input.onkeyup = function(eve){
 			if(!eve) eve = window.event;
 			if(eve.keyCode == 13) genFunctionBox(input.value);			
 		}
 		
 		var button = document.createElement('INPUT');
 		button.type = "button";
 		button.value = "查询";
 		button.onclick = function(){ genFunctionBox(input.value);};
 		
 		searchBox.appendChild(input);
		searchBox.appendChild(document.createTextNode(' '));	
 		searchBox.appendChild(button);	
		panelBox.appendChild(searchBox);		
	}
	
	function genBottomBox(){
		bottomBox  = document.createElement('DIV');
		bottomBox.className = "win_foot";
		selector.appendChild(bottomBox);		
	}	
	
	function genFunctionBox(){
		if(selector==null || selector.nodeName!="DIV") return;
		
		if(!functionBox){
			functionBox = document.createElement('DIV');
			functionBox.className = "function_box";			
			if(config.height) functionBox.style.height = parseInt(config.height) - 50 + 'px';			
			//for ie6, the place fro scroll bar
			if(Masker.ie6) functionBox.style.width = parseInt(config.width) - 18 + 'px';		
			panelBox.appendChild(functionBox);			
		}else {
			while(functionBox.childNodes.length!=0)
				functionBox.removeChild(functionBox.firstChild);
		}
		
		
				
		var key = (arguments[0]) ? arguments[0].toString().replace(/(^\s*)|(\s*$)/g, "") : '';	
		
		var table = document.createElement('TABLE');
		table.style.width = "100%";
		table.cellSpacing = 0;
		table.cellPadding = 0;
		var tbody = document.createElement('TBODY');
		table.appendChild(tbody);		
		
		var tr = null;
		var td = null;
		var header = null;
		
  	for(var i = 0; i < config.catelogArray.length; i++){
    	if(config.catelogArray[i][3]==1){
    		header = document.createElement('TR');    		
    		header.className = 'sup';
  			td = document.createElement('TD');
    		header.appendChild(td);
    		td.appendChild(document.createTextNode(config.catelogArray[i][0].replace("-不限","")));
    		td = document.createElement('TD');
    		header.appendChild(td);
    		td.appendChild(document.createTextNode(config.catelogArray[i][1]));
    	}
    	
    	if(config.catelogArray[i][0].indexOf(key) > -1 || config.catelogArray[i][1].indexOf(key) > -1){
    		if(!!header){
    			tbody.appendChild(header);
    			header = null;
    		}    		
				tr = document.createElement('TR');
	  		tbody.appendChild(tr); 
	  		tr.className = 'sub';   
				td = document.createElement('TD');
	  		tr.appendChild(td);
	  		td.appendChild(document.createTextNode(config.catelogArray[i][0]));
	  		td = document.createElement('TD');
	  		tr.appendChild(td);
	  		td.appendChild(document.createTextNode(config.catelogArray[i][1]));
										
	  		tr.onmouseover = util.hilight;
	  		tr.onmouseout = util.unhilight;
	  		tr.value = config.catelogArray[i][2]
	  		tr.onclick = function(){ 
		  			config.submit(this.value);  hide(); 
		  			if(document.getElementById('id_text_search_field').value) reset();
	  			};  
	  	}
   }
   functionBox.appendChild(table);	
}

	//public methods
	this.reset = reset;
	this.show = show;
	this.hide = hide;
	this.init = init;	
	
	function reset(){
		while(selector.childNodes.length!=0)
			selector.removeChild(selector.firstChild);
		//document.body.removeChild(selector);
		selector = null;
		selector = null;		
		titleBox = null;
		searchBox = null;	
		panelBox = null;	
		functionBox = null;	
		bottomBox  = null;
	
		if(config.modelDialog) Masker.unmask();
		
		init();	
	}

	function show(){
		if(config.modelDialog) {
			Masker.mask();
			if(Masker.ie6) Masker.showSelect(selector);		
		}		
		
		var body = document.getElementsByTagName('BODY')[0];		
		var width  = selector.style.width? selector.style.width : selector.offsetWidth;
		var height = selector.style.height? selector.style.height : selector.offsetHeight;

		if(config.left) selector.style.left = config.left;
		else selector.style.left =  (body.clientWidth -parseInt(width))/2 + "px";
		
		if(config.top) selector.style.top = config.top;
		else selector.style.top = (body.clientHeight -parseInt(height))/2 - config.topOffset + "px";		

		selector.style.display = 'block';
	}
	
	function hide(){
		if(config.modelDialog) Masker.unmask();		
		selector.style.display = 'none';	
	}
	
	
		//util functions	
	var util = {};
	util.hilight = function(){
		this.className = 'hilight';
	};
	
	util.unhilight = function(){
		this.className = 'unhilight';
	};
}
