//Требует node-by-array.js

function mClear(){
	//Метод для любого элемента. Убивает все дочерние элементы.
	with(this) while(childNodes.length>0) removeChild(firstChild);
}

function Select(doc){
	return createNodeByArray(doc,['select',{
		clear:mClear,
		add:function(text,value){
			this.appendChild(createNodeByArray(this.ownerDocument,['option',{value:value},text]));
			this.selectedIndex=0;
		},
		addTo:function(text,value,index){
			if(index>=this.childNodes.length) add(text,value);
			else{
				var relative=this.childNodes[index];
				this.insertBefore(createNodeByArray(this.ownerDocument,['option',{value:value},text]),relative);
			}
			this.selectedIndex=0;
		},
		setList:function(arr,index){
			this.clear(); 
			if(arr){
				for(var i=0;i<arr.length;i+=2) this.add(arr[i],arr[i+1]);
				this.style.display='';
			}else this.style.display='none';
			if(index) this.selectedIndex=index;
		},
		getList:function(){
			var r=[];
			for(var i=0;i<this.childNodes.length;i++) with(this.childNodes[i]) if(nodeType==1) if(tagName=='OPTION'){
				r.push(firstChild.nodeValue);
				r.push(value);
			}
			return r;
		},
		remove:function(value){
			if(this.value==value) this.removeSelected();
			else for(var i=0;i<this.childNodes.length;i++){
				if(this.childNodes[i].value==value) this.removeChild(this.childNodes[i]);
			}
		},
		text:function(){with(this) return childNodes[selectedIndex].firstChild.nodeValue;},
		removeSelected:function(){
			with(this){
				removeChild(childNodes[selectedIndex]);
				selectedIndex=0;
				if(onclick)onclick();
			}
		},
		setValue:function(value){
			for(var i=0;i<this.childNodes.length;i++) if(this.childNodes[i].value==value){
				this.selectedIndex=i;
				return;
			}
			this.selectedIndex=-1;
		}
	}]);
}
Select.d=function(){return Select(document);};

function SelectText(doc){
	var obj=createNodeByArray.v1;
	alert(0);
	return createNodeByArray(doc,['div',{
			clear:function(){
				this.list.clear();
				this.list.add('[Свой вариант]','Empty');
			},
			add:function(text,value){
				this.list.addTo(text,value,this.list.childNodes.length-1);
			},
			addTo:function(text,value,index){
				if(index>=this.list.childNodes.length-1) this.add(text,value);
				else this.list.addTo(text,value,index);
			},
			setList:function(arr,index){
				arr.push('[Свой вариант]','Empty');
				alert(index);
				this.list.setList(arr,index);
			},
			getList:function(){
				var arr=this.list.getList();
				if(arr[arr.length-2]=='[Свой вариант]'){
					arr.pop();
					arr.pop();
				}
			},
			setValue:function(value){
				this.value=value;
				this.text.value=value;
				this.list.setValue(value);
				if(this.list.selectedIndex=-1) this.selectedIndex=this.childNodes.length-1;
			}
		},
		obj(doc,'list',[Select(doc),{
				style:{display:'block'},
				onchange:function(){
				    var text=this.container.text;
				    if(this.value=='Empty'){
				        if(text.style.display=='none'){
				            text.style.display='block';
							if(text.value) this.container.value=text.value;
				        }
				    }
					else{
				        if(text.style.display!='none'){
				            text.style.display='none';
				        }
				        this.container.value=this.value;
				    }
				}
			},
			['option',{value:'Empty'},'[Свой вариант]']
		]),
		obj(doc,'text',['input',{
			type:'text',
			style:{display:'none'},
			onchange:function(){
				this.container.value=this.value;
				if(this.container.onchange) this.container.onchange();
			}
		}])
	]);
}
SelectText.d=function(){return SelectText(document);};

function CommandButton(doc,value,onclick){
	return createNodeByArray(doc,['input',{type:'button',value:value,onclick:onclick}]);
}
CommandButton.d=function(value,onclick){return CommandButton(document,value,onclick);};

function LinkButton(doc,image,alt,href,target){
	return createNodeByArray(doc,['a',{href:href,title:alt,target:target},['img',{src:image,alt:alt,border:'0'}]]);
}
LinkButton.d=function(image,alt,href,target){return LinkButton(document,image,alt,href,target);};

//alert('ok');
