/** 	$Id: 17.08.2006 11:54:24 krass $ **/
/* DIV SELECT ELEMENT JS LIB */

function dsDivSelect(owner_id, parent_id){
	/* Interface */

	/// Properties
	this.elements_arr = new Array; // Element Arr
	this.selected_id = 0;
	this.nobr = 0; // No <br/> after elements
	this.class_sub_names; // Array of sub elements to change class name

	/// Methods
	this.Load    = dsLoad;
	this.Show    = dsShow;
	this.Show_addText    = dsShow_addText;
	this.Select  = dsSelectId;
	this.Reset   = dsReset;
	this.ResetSelection = dsResetSelection;
	this.up      = dsUp;
	this.down    = dsDown;



	/// Events
	this.onSelect = '';
	this.onDblClick = '';

	/* Private */
	this.owner_id = owner_id;   // Element that will contain this object
	this.parent_id = parent_id; // Elements contaner
	this.UnSelectId = dsUnSelectId;
	this.first_id = null;
	this.defaultClass="ds_list";
	this.selectedClass="ds_list_selected";
	this.hoveredClass="ds_list_hovered";

	this.onSelectInternal = "dsObjectSelected";
}

function dsLoad(elements_arr){
	this.elements_arr = elements_arr;
}

function dsReset(){
  var contDIV = document.getElementById(this.parent_id+'_list');
  if (contDIV)
	  contDIV.innerHTML = '';
	this.selected_id = 0;
	this.first_id = null;
	return;
}

function dsShow(add, add_text){
  var parentDIV = document.getElementById(this.parent_id);
	if (add)
		parentDIV.innerHTML += document.getElementById(this.parent_id).select_init_text;
	else
		parentDIV.innerHTML = document.getElementById(this.parent_id).select_init_text;
	parentDIV.style.display = 'block';

  
  var ulList    = document.getElementById(this.parent_id+'_list');
  var i, j, sub,o_node, sub_names_exists, node_text, node_el_id;

  this.first_id = null;

  if (!this.elements_arr.length)
    return false;

  var o_clone_node;

  o_node = ulList.childNodes[0];
  if (isMSIE || isOpera){
		var fake_img = new Image();
		fake_img.src = portal_url+'/_images/spacer.gif';
		fake_img.style.width = '1px';
		fake_img.style.height = '1px';
		ulList.appendChild(fake_img);
		if (isOpera){
			o_clone_node = o_node.cloneNode(true);
			o_clone_node.innerHTML = '<br/>';
			ulList.appendChild(o_clone_node);
		}

  }
  var br='';
  if (!this.nobr)
  	br='<br />';
  if (this.class_sub_names)
  	 sub_names_exists = 1;
  var last_id=null;
  for(i in this.elements_arr)
  {
	if('function' == typeof this.elements_arr[i])
		continue;
 		if (!last_id){
 			this.first_id = i;
 		}
 		if (o_clone_node)
 			o_clone_node.next_id = i;

 		o_clone_node = o_node.cloneNode(true);

		node_text = this.elements_arr[i];

    if (sub_names_exists){
	    for (j in this.class_sub_names){
				if (node_text.indexOf(this.class_sub_names[j]) != -1){
					node_el_id = "li_"+this.parent_id+'_'+i+'_'+this.class_sub_names[j];
					node_text = node_text.replace(this.class_sub_names[j], 'id="'+node_el_id+'"');
				}
	    }
    }


   	o_clone_node.previous_id  = last_id;
   	o_clone_node.next_id	    = null;

		o_clone_node.innerHTML		= node_text+br;
		o_clone_node.id						= "li_"+this.parent_id+'_'+i;
		o_clone_node.className		=	this.defaultClass;

		o_clone_node.onmouseover	= new Function("if (!this.is_selected) this.className = '" + this.hoveredClass+"';");
		o_clone_node.onmouseout		= new Function("if (!this.is_selected) this.className = '" + this.defaultClass+"';");
		o_clone_node.onmousedown	= new Function(this.onSelectInternal+"('"+this.owner_id+"', '"+this.parent_id+"', '"+i+"', '"+0+"')");
		o_clone_node.ondblclick   = new Function(this.onSelectInternal+"('"+this.owner_id+"', '"+this.parent_id+"', '"+i+"', '"+1+"')");
		ulList.appendChild(o_clone_node);
		last_id = i;
	}
	ulList.childNodes[0].style.display='none';
	ulList.style.display = 'block';

	var mainDIV = document.getElementById(this.owner_id)
	if (mainDIV){
		var input_el  = document.getElementById(mainDIV.id_input)
		if (input_el && input_el.style.visibility != 'hidden'){
			input_el.focus();
		}
	}
	if (add_text){
		this.Show_addText(add_text);
	}
  this.selected_id = 0;
}

function dsShow_addText(add_text) {
	var ulList = document.getElementById(this.parent_id+'_list');
 	var new_element = document.createElement('div');
 	new_element.innerHTML = add_text;
 	ulList.appendChild(new_element);
	return;
}


function dsObjectSelected(owner_id,parent_id, id, is_dbl_click)
{
	var mainDIV = document.getElementById(owner_id);

  if (mainDIV.dsSelect.selected_id){
    var selected_el = document.getElementById("li_"+parent_id+'_'+mainDIV.dsSelect.selected_id);
    if (selected_el){
      selected_el.className = mainDIV.dsSelect.defaultClass;
      selected_el.is_selected = 0;
      dsSelectSubElements(mainDIV.dsSelect, mainDIV.dsSelect.selected_id, mainDIV.dsSelect.defaultClass);
    }
  }

  document.getElementById("li_"+parent_id+'_'+id).className = mainDIV.dsSelect.selectedClass;
  document.getElementById("li_"+parent_id+'_'+id).is_selected = 1;
  dsSelectSubElements(mainDIV.dsSelect, id, mainDIV.dsSelect.selectedClass);

	mainDIV.dsSelect.selected_id=id;

	if (mainDIV.dsSelect.onSelect)
	  mainDIV.dsSelect.onSelect();

	if (is_dbl_click==1 && mainDIV.dsSelect.onDblClick){
    if (mainDIV.dsSelect.onDblClick)
  		mainDIV.dsSelect.onDblClick();
	if (document.getElementById(mainDIV.id_input).style.visibility != 'hidden')
    	document.getElementById(mainDIV.id_input).focus();
	}



}


function dsSelectId(id){
  var parentDIV = document.getElementById(this.parent_id);
  var node = document.getElementById("li_"+this.parent_id+'_'+id);
  var sub_el;
  if (!node)
    return;

  if (this.selected_id){
  	this.UnSelectId(this.selected_id)
  }

	node.className = this.selectedClass;
	this.selected_id = id;
	node.is_selected = 1;
  dsSelectSubElements(this, id, this.selectedClass);
  if (!isOpera)
  	parentDIV.scrollTop = (node.offsetTop-35);
  else{
  	parentDIV.scrollTop = node.offsetTop;
  }
	if (this.onSelect)
		this.onSelect();
}

function dsUnSelectId(id){
  var parentDIV = document.getElementById(this.parent_id);
  var node = document.getElementById("li_"+this.parent_id+'_'+id);
  var sub_el;
  if (!node)
    return;

  this.selected_id = 0;
	node.className = this.defaultClass;
	node.is_selected = 0;
  dsSelectSubElements(this, id, this.defaultClass);
}

function dsResetSelection(){
	if (!this.selected_id)
		return;
	this.UnSelectId(this.selected_id);
}

function dsSelectSubElements(obj, id, clName){
	if (!obj.class_sub_names)
		return;

	for (j in obj.class_sub_names){
		node_el_id = "li_"+obj.parent_id+'_'+id+'_'+obj.class_sub_names[j];
		sub_el = document.getElementById(node_el_id);
		if (sub_el)
			sub_el.className		=	clName;
   }
}

function dsUp(){
	if (!this.selected_id)
		return;
	var node = document.getElementById("li_"+this.parent_id+'_'+this.selected_id);
	if (node && node.previous_id)
		this.Select(node.previous_id);

}

function dsDown(){

	if (!this.selected_id){
		if (this.first_id){
			this.Select(this.first_id);
			return;
		}
	}
	if (!this.selected_id)
		return;
	var node = document.getElementById("li_"+this.parent_id+'_'+this.selected_id);

	if (node && node.next_id)
		this.Select(node.next_id);
}


function isUpKey(n_keycode)
{
	if (n_keycode==38)
		return true;
	return false;
}

function isDownKey(n_keycode)
{
	if (n_keycode==40)
		return true;
	return false;
}

function isEnterKey(n_keycode)
{
	if (n_keycode==13)
		return true;
	return false;
}

function isDeleteKey(n_keycode)
{
	if (n_keycode==8 || n_keycode==46)
		return true;
	return false;
}

function isAlphaKey(n_keycode)
{
	var b_res = true;
	switch (n_keycode)
	{
		//case 8: // back space
		case 10: // shift
		case 11: // ctrl
		case 12: // alt
		case 13: // enter
		case 27: // esc
		case 33: // page up
		case 34: // page down
		case 35: // end
		case 36: // home
		case 37: // left arrow
		case 38: // up arrow
		case 39: // right arrow
		case 40: // down arrow
		case 45: //	????
//		case 46: // delete
			b_res = false;
			break;
		default:
			// regular keypress ...
			break;
	}
	return b_res;
}