// MENU PROTOTYPE
// (c) Kruglov S.A. 2003
// www.kruglov.ru

// string functions /////////////////////////////////////////////////////////////////////////////////////

function isspace(c){
	if(c==' ' || c=="\t" || c=="\n" || c=="\r") return true
	return false
}

function isquote(c){
	if(c=='\'' || c=="\"") return true
	return false
}

function phpstring(c){ // undefined to ''
	if(!c) return ''
	return c
}

function phpnumber(c){ // undefined to 0
	if(!c) return 0
	return 1*c
}

// parsing attributes from:  var='zaza' xxx=oops super="yeah"
// to variables

function parseAttributes(str, to, ignoreSpacesInName){

	if(!to){
		attr=[]
	}

	r_a_name=false
	r_a_eq=false
	r_a_value=false
	r_complete=false

	a_name=[]
	a_eq=[]
	a_value=[]
	a_value_quote=""

	s=(str+" ").split('')

	l=s.length
	for(i=0;i<l;i++){
		c=s[i]
		if(!r_a_name && !r_a_eq && !r_a_value){
			if(isquote(c) || c=="="){
				break
			}
			if(!isspace(c)){
				r_a_name=true
				a_name[a_name.length]=c
			}
		}else
		if(r_a_name){
			if(isquote(c)){
				break
			}
			if(isspace(c) || c=="="){
				r_a_name=false
				r_a_eq=true
				a_eq[a_eq.length]=c
			}else{
				a_name[a_name.length]=c
			}
		}else
		if(r_a_eq){
			if(isspace(c) || c=="="){
				array_push(a_eq,c)
			}else{
				t=a_eq.join('').replace(/(^\s*)|(\s*$)/,"") // trim
				if(t!="="){
					if(t!=""){
						break
					}
					i-- // ungetc()
					r_complete=true
				}else{
					if(isquote(c)){
						a_value_quote=c
					}else{
						a_value[a_value.length]=c
					}
					r_a_value=true
					r_a_eq=false
				}
			}
		}else

		if(r_a_value){
			if((a_value_quote=="" && isspace(c))
				||
				(a_value_quote==c)){
					r_complete=true
			}else{
				a_value[a_value.length]=c
			}
		}

		if(r_complete){
			r_a_name=false
			r_a_eq=false
			r_a_value=false
			a_value_quote=""

			a_name=a_name.join('')

			if(ignoreSpacesInName){
				a_name=a_name.replace(/_/g,"")
			}

			a_value=a_value.join('')

			if(to){
				to[a_name]=a_value
			}else{
				attr[a_name]=a_value
			}

			a_name=[]
			a_eq=[]
			a_value=[]
			r_complete=false
		}
	}

	if(!to){
		return attr
	}
}

// class kmenu /////////////////////////////////////////////////////////////////////////////////////

k_counter=1 // global counter

function kmenu(argv,inheriter){
	this.number=k_counter++
	this.inheriter=inheriter

	if(!self.kmenus){
		kmenus=[]
	}

	kmenus[this.number]=this.inheriter

	this.attr=[]
	this.level=1

	this.parent=-1

	parseAttributes(argv,this.attr,true)
	
	this.items=[]
	this.id_to_items=[]

	this.add=function(argv){
		var i=new kmenuitem(argv)

		if(!i.level){
			i.level=this.level
		}else{
			this.level=i.level
		}

		var its=this.items;

		var l=its.length;
		if(l>0 && its[l-1].level<i.level){
			its[l-1].haschildren=true
		}
		if(!i.id) i.id=l
		this.id_to_items[i.id]=l
		its[l]=i

		var plevel=i.level
		if(i.current){
			for(j=l-1;j>=0;j--){
				if(its[j].level<plevel){
					its[j].currentsection=true
					plevel=its[j].level
				}
				if(its[l].level==1) break
			}
		}
	}
	
	this.level_plus=function(){
		this.level++
	}
	this.level_minus=function(){
		this.level--
	}

	this.inheriter.attr=this.attr
	this.inheriter.items=this.items
	this.inheriter.id_to_items=this.id_to_items
	this.inheriter.number=this.number

	this.inheriter.add=function(argv){
		this.menu.add(argv)
	}

	this.inheriter.level_plus=function(){
		this.menu.level_plus()
	}

	this.inheriter.level_minus=function(){
		this.menu.level_minus()
	}

	// otladka
	this.trace=function(start){
		if(!start) start=0
		var level=this.items[start].level
		var i
		document.write("<ul>")
		for(i=start; i < this.items.length; i++){
			var it=this.items[i]
			if(it.level>level){
				i=this.trace(i)
			}else if(it.level<level){
				break;
			}else{
				document.write("<li><a href='"+it.href+"'>"+it.title+"</a>"+(it.current?" *":"")+(it.currentsection?" +":""))
			}
		}
		document.write("</ul>")
		return i-1
	}
}

// class kmenuitem ////////////////////////////////////////////////////////////////////////////////

function kmenuitem(argv){
	parseAttributes(argv,this,true)
}

// Array patch

if(![].push){
	Array.prototype.push=function(item){
		this[this.length]=item
	}
}
