/**********************
* (C)2003 WebDesign Studio Praha, s.r.o. 
***********************/
// globals
	var G_imgExp
	var G_imgCol
	var G_imgVL
	var G_imgHL
	var G_imgSpace
	var G_imgSpace2ndLvl
	var G_imgSplit
	var G_imgEnd
	//var i=1;

function wdTree(divid_, myname_, root_){//abstract class
//props
	this.dbgFROM // cookie debug only
	this.divWidth=''
	this.divWidthi=''
	this.divid = divid_
	this.myName = myname_
	this.Nodes = new Array()
	this.recursionTreeSave // says if to save the tree when collapsed is true
	this.recursionTree=false // says when we should try if the cookie exists
	this.NodeStatusArr = new Array()
	this.cookieName = 'wd'
	this.useCookie= false
	this.collapsedArr = new Array() // when collapsed is TRUE and also cookie is TRUE 
	this.showRoot=true
	this.collapsAll=false
	this.collapsed=false
	this.root
	this.hrefTarget
	this.useImages = false
//draw
	this.reclvl=0
	this.levelLines = []
//methods
	this.cb //callback function defined in subclass
	this.newNode = newNode
	this.draw = drawTree
	this.treeDraw = treeDraw
	this.setRoot=function setRoot(id_){this.root = this.Nodes[id_];}
	this.swap = swap
	this.cookieExists = cookieExists
	this.colapsAll = colapsAll
	this.loadNodesFromCookie = loadNodesFromCookie
	this.saveNodes2Cookie = saveNodes2Cookie

//image preloading
	this.drawElementsImg=drawElementsImg;
	this.imgTypes = new Array();
	this.preloadImg=preloadImg;
	this.preloadImg('../admin/_img/');
	
	this.setImgPath = setImgPath;
//init
	this.setImgPath();
	this.newNode(0,0,0,root_);
	this.setRoot(0);	
}

function preloadImg( path_ ){
	this.imgTypes= new Array('imgExp','imgCol','imgVL','imgHL','imgSpace','imgSpace2ndLvl','imgSplit','imgEnd')
	this.imgPath =	path_
	G_imgExp =	new	wdImg(this.imgPath+'tex.gif',this.imgTypes[0])
	G_imgCol =	new	wdImg(this.imgPath+'tco.gif',this.imgTypes[1])
	G_imgVL =	new	wdImg(this.imgPath+'tvl.gif',this.imgTypes[2])
	G_imgHL =	new	wdImg(this.imgPath+'thl.gif',this.imgTypes[3])
	G_imgSpace =	new	wdImg(this.imgPath+'tspa.gif',this.imgTypes[4])
	G_imgSpace2ndLvl =	new	wdImg(this.imgPath+'t2lvl.gif',this.imgTypes[5])
	G_imgSplit =	new	wdImg(this.imgPath+'tspl.gif',this.imgTypes[6])
	G_imgEnd =	new	wdImg(this.imgPath+'tend.gif',this.imgTypes[7])
}

function drawElementsImg(){
	//alert("Drawing elements. We are haveing "+this.imgTypes.length+" image Types.");
	for(imgTypeNo=0; imgTypeNo < this.imgTypes.length; imgTypeNo++){
		elements=document.getElementsByName(this.imgTypes[imgTypeNo]+'img');
		if (elements.length>0){
			type=this.imgTypes[imgTypeNo];
			switch(type){
				case 'imgExp':
					src= this.imgExp.src;
				break;
				case 'imgCol':
					src= this.imgCol.src;
				break;
				case 'imgVL':
					src= this.imgVL.src;
				break;
				case 'imgHL':
					src= this.imgHL.src;
				break;
				case 'imgSpace':
					src= this.imgSpace.src;
				break;
				case 'imgSpace2ndLvl':
					src= this.imgSpace2ndLvl.src;
				break;
				case 'imgSplit':
					src= this.imgSplit.src;
				break;
				default:
					src= this.imgEnd.src;
				break;
			}
			for(accEl=0; accEl < elements.length; accEl++)
				elements[accEl].src=src;
		}
	}
}

function wdImg(src_,imgType_){
	var src;
	this.img=new Image();
	this.img.src=src_;
	this.src=src_;
	this.toStringP = 
		function(name_,id_){
			return '<img id="'+id_+'000" align="absmiddle" src="" name="'+imgType_+'img" onclick="'+name_+'.swap(this,'+id_+')" />'
		}

	this.toString = 
		function(id_){
	 		return '<img align="absmiddle"  name="'+imgType_+'img" src="" />'
		}
}

function newNode(pid_, cid_,ts_, txt_, href_, onclick_, styleClass_, divClass_){
	//pid = parent id; cid = child id
	node = new wdNode(cid_,ts_,txt_, href_, onclick_, styleClass_, divClass_);
	this.Nodes[cid_] = node;
	if( pid_ != cid_) this.Nodes[pid_].addNode( node );	
}

function setImgPath(){
	this.imgExp = G_imgExp
	this.imgCol =G_imgCol
	this.imgVL =G_imgVL
	this.imgHL =G_imgHL
	this.imgSpace =G_imgSpace
	this.imgSpace2ndLvl =G_imgSpace2ndLvl
	this.imgSplit =G_imgSplit
	this.imgEnd =G_imgEnd
}

function drawTree(){
	if(!this.recursionTree){
		this.recursionTreeSave	= (this.cookieExists() || this.useCookie==false || this.collapsed )
		this.recursionTree			= true
	}
	this.treeDraw(this.root)
	document.getElementById(this.divid).innerHTML = this.root.output
	this.drawElementsImg()
	if(this.useCookie)this.loadNodesFromCookie()
}

function cookieExists(){
	return (document.cookie.indexOf(this.cookieName+"NodeStatus")==-1)
}

function treeDraw(o_){
	this.reclvl++;
	var indent ='', i, j, subtxt='', imgLast;
	disp = this.collapsed ? 'none':'';
	imgCE = this.collapsed ? this.imgExp:this.imgCol;
	
	if(o_.Nodes.length){
		for(i=0; i<o_.Nodes.length; i++){
			if(i < o_.Nodes.length-1){
				this.levelLines[this.reclvl-1] = 1;
				imgLast = this.imgSplit;
			}
			else{ 
				this.levelLines[this.reclvl-1] = 0;
				imgLast = this.imgEnd;
			}
			
			for(k=this.showRoot?0:1; k<this.reclvl-1;k++) {
				indent += (this.levelLines[k])?this.imgVL.toString():this.imgSpace.toString();
			}

			this.treeDraw(o_.Nodes[i]);

			if(this.showRoot || this.root != o_) {
				/*if (this.useImages) subtxt += indent+imgLast.toString()+o_.Nodes[i].output;//'<br />'+
				else*/ subtxt += o_.Nodes[i].output;//'<br />'+
			}
			else {
				subtxt += indent+o_.Nodes[i].output;//'<br />'+
			}
			indent = '';
		}
		
		if(this.showRoot || this.root != o_){
			if (this.recursionTreeSave==false && this.collapsALL==false){ 
				imgCE =this.imgExp;
				if (this.useImages) o_.output = imgCE.toStringP(this.myName, o_.id);
				else o_.output = "";
				o_.output += this.cb(o_)+'<br />'+'<div class="'+o_.divClass+this.reclvl+'" style="'+this.divWidth+'display: none" id="'+o_.id+'">'+subtxt+'</div>';
			}
			else if (this.recursionTreeSave==true && this.collapsALL==false) { 
				imgCE = this.imgCol;
				if (this.useImages) o_.output = imgCE.toStringP(this.myName, o_.id);
				else o_.output = "";
				o_.output += this.cb(o_)+'<br />'+'<div class="'+o_.divClass+this.reclvl+'" style="'+this.divWidth+'display:'+disp+'" id="'+o_.id+'">'+subtxt+'</div>';
				if (this.useCookie) {
					this.NodeStatusArr.push(o_.id);
					this.saveNodes2Cookie();
				}
			}
			else{
				if (this.useImages) imgOutput = imgCE.toStringP(this.myName, o_.id);
				else imgOutput = "";
				for(k=this.showRoot?0:1; k<this.reclvl-1;k++) {
					indent += this.imgVL.toString();
				}
				if (!this.useImages) indent = "";
//				o_.output += this.cb(o_)+'<br />'+'<div class="'+o_.divClass+this.reclvl+'" style="'+this.divWidth+'display:'+disp+'" id="'+o_.id+'">'+subtxt+'</div>';
//				o_.output += '<div class="'+o_.divClass+'">'+this.cb(o_)+'</div>'+'<div class="'+o_.divClass+'" style="'+this.divWidth+'display:'+disp+'" id="'+o_.id+'">'+subtxt+'</div>';
				o_.output = '<div class="'+o_.divClass+'">'+indent+imgOutput+this.cb(o_)+'</div>'+'<div style="'+this.divWidth+'display:'+disp+'" id="'+o_.id+'">'+subtxt+'</div>';
			}
		}
		else {
//			o_.output = this.cb(o_)+'<div class="csmmenulvl'+this.reclvl+'" style="'+this.divWidth.replace(/;/,'')+'" id="'+o_.id+'">'+subtxt+'</div>';
			o_.output = '<div class="'+o_.divClass+'">'+this.cb(o_)+'</div>'+'<div style="'+this.divWidth.replace(/;/,'')+'" id="'+o_.id+'">'+subtxt+'</div>';
		}
	}
	else {
		if (this.useImages) {
			for(k=this.showRoot?0:1; k<this.reclvl-1;k++) {
				indent += this.imgVL.toString();
			}
			o_.output = '<div class="'+o_.divClass+'">'+indent+this.imgSpace2ndLvl.toString()+this.cb(o_)+'</div>';//<div style="'+this.divWidth.replace(/;/,'')+'" id="'+o_.id+'"></div>
		}
		else o_.output = '<div class="'+o_.divClass+'">'+this.cb(o_)+'</div><div style="'+this.divWidth.replace(/;/,'')+'" id="'+o_.id+'"></div>';
	}
	this.reclvl--;
}

function colapsAll(){
	this.recursionTreeSave==true
	this.collapsAll=true
	this.collapsed = !this.collapsed//? false:true
	this.treeDraw(this.root)
	document.getElementById(this.divid).innerHTML = this.root.output
	this.collapsAll=false
	this.drawElementsImg()
}

function saveNodes2Cookie(){
	//this function sets node into cookie for using after browser refreshing
	var today = new Date()
	var expires = new Date()
	expires.setTime(today.getTime() + 3600000*48);
	NodeStatus=this.NodeStatusArr.toString(",");
	document.cookie=this.cookieName+'NodeStatus='+NodeStatus+';	expires='+expires.toGMTString();
}

function loadNodesFromCookie(){
	//document.getElementById("down").innerHTML+="existuje cookie "+this.cookieName+"NodeStatus ? "+this.cookieExists()+"<br>";
	searchc=this.cookieName+"NodeStatus";
	offset = document.cookie.indexOf(searchc);
	if (offset != -1 ){ 
		offset += searchc.length
		end = document.cookie.indexOf("; ", offset)
		if(end == -1)	end = document.cookie.length

		out=unescape(document.cookie.substring(offset, end));
		str=out.replace(/^=/,"");
		this.NodeStatusArr=str.split(",");

		if (this.NodeStatusArr.length>0){				
			for(k=0; k<this.NodeStatusArr.length; k++) {
				if(this.NodeStatusArr[k]){
					var id_=this.NodeStatusArr[k]*1000;
					if (this.useImages) document.getElementById(id_).src=this.imgCol.src;
					o = document.getElementById(this.NodeStatusArr[k]);
					o.style.display = '';
					//document.getElementById("down").innerHTML+="nahravam id: "+this.NodeStatusArr[key];
				}
			}
		}
	}
}

function swap(img_, oid_){
	o = document.getElementById(oid_);
	if(o.style.display==''){
		img_.src=this.imgExp.src;
		o.style.display = 'none';

		for (pos=0; pos<=this.NodeStatusArr.length; pos++) {
			if (this.NodeStatusArr[pos]==oid_)this.NodeStatusArr.splice(pos,1);
		}
	}
	else{
		img_.src= this.imgCol.src;
		o.style.display = '';
		this.NodeStatusArr.push(oid_);
	}
	//document.getElementById("down").innerHTML+="display id: "+o.style.display+"<br>";
	if(this.useCookie)	this.saveNodes2Cookie();
}

/***********************/
function wdTreeComp(divid_, myname_, root_,type_){
	this.base = wdTree;
	this.base(divid_, myname_, root_);
	this.type = type_ || "";
	switch(this.type){
		case "checkbox":
			this.cb = cbCheckbox;
			this.newNode = newNodeCB;
			break;
		case "edit":	
			this.cb = cbEdit;
			this.newNode = newNodeEdit;
			break;
		default:
			this.cb = cb;
	}
}
wdTreeComp.prototype = new wdTree;
function cb( o_ ){
	if(this.showRoot || this.root != o_) {
		class_ = "";
		if (o_.styleClass != "") class_ = "class='"+o_.styleClass+"'";
		onclick_ = "";
		if (o_.onclick != "") onclick_ = "onclick='"+o_.onclick+"'";
		return o_.href?'<a '+class_+' href="'+o_.href+'" target="'+this.hrefTarget+'" '+onclick_+'>'+o_.txt+'</a>':'<span>'+o_.txt+'</span>'
	}
	return '';
}
function cbEdit( o_ ){
	if(this.showRoot || this.root != o_){
		res  = '<input class="inputfieldLong" type="text" name="name['+o_.id+']" value="'+o_.txt+'" />'
		res += '<input class="inputfieldLong" size="50" type="text" name="link['+o_.id+']" value="'+o_.href+'" />'
		res += '<input class="inputfieldShort" size="4" type="text" name="sequence['+o_.id+']" value="'+o_.sequence+'" />'
		res += '|del<input type="checkbox" name="delete['+o_.id+']" />'
		res += '|add<input type="checkbox" name="new['+o_.id+']" />'
		res += '<input type="hidden" name="controlID['+o_.id+']" value="'+o_.id+'" />'
		res += '<input type="hidden" name="controlTS['+o_.id+']" value="'+o_.ts+'" />'
		res += '<input type="hidden" name="level['+o_.id+']" value="'+this.reclvl+'" />'
		return res
	}
	return '';
}
function cbCheckbox( o_ ){
	if(this.showRoot || this.root != o_){
		ckd = o_.checked?'checked':''
		res  = '<input align="absmiddle" type="checkbox" name="grant['+o_.id+']" value="1" '+ckd+' />'+o_.txt
		res += '<input type="hidden" name="level['+o_.id+']" value="'+this.reclvl+'" />'
		return res
	}
	return '';
}
function newNodeCB(pid_,cid_,ts_,txt_, href_,checked_){
	//pid = parent id; cid = child id
	node = new wdNodeCB(cid_,ts_,txt_, href_,checked_);
	this.Nodes[cid_] = node;
	if( pid_ != cid_) this.Nodes[pid_].addNode( node );	
}
function newNodeEdit(pid_,cid_,ts_,txt_, href_,sequence_){
	//pid = parent id; cid = child id
	node = new wdNodeEdit(cid_,ts_,txt_, href_,sequence_);
	this.Nodes[cid_] = node;
	if( pid_ != cid_) this.Nodes[pid_].addNode( node );	
}

/**********************/
function wdNode(id_,ts_, txt_,href_,onclick_,styleClass_,divClass_){//class
	this.id = id_;
	this.ts = ts_;
	this.txt = txt_;
	this.href = href_;
	this.onclick = onclick_;
	this.styleClass = styleClass_;
	this.divClass = divClass_;
	this.Nodes = [];
	this.output;
	//Methods
	this.addNode = function addNode(node_){this.Nodes.push(node_)}
}

function wdNodeCB(id_,ts_, txt_,href_,checked_){//class
	this.base = wdNode
	this.base(id_,ts_, txt_,href_);
	this.checked = checked_ || '';
}
wdNodeCB.prototype = new wdNode;

function wdNodeEdit(id_,ts_, txt_,href_,sequence_){//class
	this.base = wdNode
	this.base(id_,ts_, txt_,href_);
	this.sequence = sequence_ || '';
}

wdNodeEdit.prototype = new wdNode;
