/*
 * Copyright (c) 2007, www.jc001.cn! All rights reserved.
 *
 * web         : http://www.jc001.cn
 * author      : stcer (ab12cxyh@163.com)
 * version     : 0.1.0 
 */ 

var Class = Class || {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

Object.extend = Object.extend || function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}

Function.prototype.bind = Function.prototype.bind || function() {
	var __method = this, args = new Array();
	for(var __i = 0; __i < arguments.length; __i++){
		args.push(arguments[__i]);
	}
	var object = args.shift();
	return function() {
		return __method.apply(object, args);
	}
}

// Class SCategory
var SCategory = SCategory || Class.create();
if(typeof(SCategory.counter) == 'undefined'){
	SCategory.counter = 0;
}

SCategory.prototype = {
	initialize : function(name){
		this.cats = [];
		this.cats_tree = [];
		this.dk_name = '_deep';
		this.pk_name = '_pid';
		this.ck_name = '_cid';
		this.name_key = 'name';
		
		this.change = null;
		
		this.set_name(name);
		return this;
	},
	
	// init select's container
	set_name : function(name){
		var id = name + SCategory.counter++;
		this.size = 0;
		this.name_prefix  = '__category_';
		this.container_name = this.name_prefix + id;
		if(!this.gel(this.container_name)){
			document.write('<span id="' + this.container_name + '"></span>');
			document.write('<input id="' + id + '" name="' + name + '" type="hidden" />');
		}
		this.container = this.gel(this.container_name);
		this.valueHandle = this.gel(id);
	},
	
	set : function(key, value){
		this[key] =  value;
	},
	
	// -------------------------------------------
	// render select elements
	// -------------------------------------------
	render : function(catid, size, level){

		// set hidden element value
		this.valueHandle.value = catid; 
		
		if(parseInt(size) > 0){
			this.size = parseInt(size);
		}
		
		if(!level){
			level = 10;		
		}

		// init select
		if(catid == 0 
		   	|| typeof(catid) == 'undefined' 
			|| typeof(this.cats_tree[catid]) == 'undefined'
			|| catid == ''){
			
			this.select_init(0);
		
		}else if(pids = this.cats_tree[catid][this.pk_name]){
			
			pids[pids.length] = 0;
			for(var i = pids.length - 1; i > -1; i--){
				this.select_init(pids[i], pids[i-1]);
			}
			
		}
	},

	// -------------------------------------------
	// init select element 
	// -------------------------------------------
	select_init : function(catid, selected_catid){
		if(typeof this.cats_tree[catid] == 'undefined'){
			return;
		}
	
		var child_ids = this.cats_tree[catid][this.ck_name];
		if(!child_ids){
			return	
		}
		
		oSelect_added = true;
		deep = this.cats_tree[catid][this.dk_name] + 1;

		// create select
		oSelect_id =  this.get_select_id(deep); // select id
		oSelect = this.gel(oSelect_id)
		if(!oSelect){
			oSelect = document.createElement('select');
			oSelect.id = oSelect_id;
			oSelect_added = false;
		}
		oSelect.options.length = 0;
		if(this.size > 0){
			oSelect.size = this.size;
		}
		
		// append options
		oOption = document.createElement("OPTION");
		oOption.text = '- 请选择 -';
		oOption.value = '-1';
		oSelect.options.add(oOption);
		
		for(var i = 0, j = 0, selectIndex = 0; i < child_ids.length; i++){
			if(this.cats_tree[child_ids[i]][this.dk_name] == deep){
				oOption = document.createElement("OPTION");
				oOption.text = this.cats[child_ids[i]][this.name_key];
				oOption.value = child_ids[i];
				oSelect.options.add(oOption);
				
				j++;
				if((child_ids[i] + '') == (selected_catid + '')){
					selectIndex = j;
				}
			}
		}
		
		// set selected item
		oSelect.selectedIndex = selectIndex
		
		// set event for onchange
		oSelect.onchange = this.select_change.bind(this, oSelect);
		
		if(!oSelect_added){
			this.container.appendChild(oSelect);
		}
	},
	
	get_select_id : function(deep){
		var name_prefix = this.container_name +  '__';
		return 	name_prefix + deep;
	},
	
	get_deep_from_id : function(id){
		var name_prefix = this.container_name +  '__';
		return 	id.substr(name_prefix.length);
	},
	
	// -------------------------------------------
	// onchange event
	// -------------------------------------------
	select_change : function(oSelect){
		var deep;
		var catid = oSelect.value;
		
		// remove selects
		var oSelects = this.container.getElementsByTagName('select');
		deep = parseInt(this.get_deep_from_id(oSelect.id)) + 1;
		for(var i = deep; i < oSelects.length + 1; i++){
			if(delSelect = this.gel(this.get_select_id(i))){
				this.container.removeChild(delSelect);
			}
		}
		
		if(typeof this.cats_tree[catid] == 'undefined'){
			deep = this.get_deep_from_id(oSelect.id);
			p_deep = parseInt(deep) - 1;
			if(p_deep > -1){
				this.valueHandle.value = this.gel(this.get_select_id(p_deep)).value;
			}else{
				this.valueHandle.value = '';	
			}
			return;	
		}
		
		// change function
		if(this.change && typeof(this.change) == 'function'){
			this.change(catid);
		}
		
		// set hidden element value
		this.valueHandle.value = catid; 

		// init child select
		this.select_init(catid);
	},
	
	gel : function(element) {
	  if(typeof(element) == 'object')
	  	return element;
	  if(typeof(element) == 'string')
	  	if(htmlObject = document.getElementById(element)){
			return 	htmlObject;
		}else{
			htmlObject = document.getElementsByName(element);
			if(htmlObject.length == 0){
				return null;
			}else{
				return 	htmlObject[0];
			}
		}
	}
}
