markingText = "Marking";

function allprods(){
        
		this.getData = getData;
		this.toggleId = toggleId;
		this.init = init;
		var lang = "ru";
		var colors = ["#FFB659","#FFC779","#FFD798","FFE2B1","#FFF3DD"];
		var isIE = false;

		var openingLevel;
		var openingId;

		function init(l){
		   lang = l;
		}

		function getData(parent,level,id){
	
	        url = "/get_structure2.php?lang="+lang+"&parent="+parent;
	
				openingLevel = level;
				openingId = id;
			if (window.XMLHttpRequest) {
				req = new XMLHttpRequest();
				req.onreadystatechange = processReqChange;
				req.open("GET", url, true);
				req.send(null);
			// branch for IE/Windows ActiveX version
			} else if (window.ActiveXObject) {
				isIE = true;
				req = new ActiveXObject("Microsoft.XMLHTTP");
				if (req) {
					req.onreadystatechange = processReqChange;
					req.open("GET", url, true);
					req.send();
				}
			}
		
		}

        
		function processReqChange() {
			// only if req shows "loaded"
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {
		
					its  = req.responseXML.getElementsByTagName("response");
					v = getTheFirstElement("","result", its[0]);

					type = getElementAttribute(v,"type");

					elems = getElements("","row", v);
					
					buildLevel(elems,openingLevel,openingId,type);

					
				 } else {
					alert("There was a problem retrieving the XML data:\n" +
						req.statusText);
				 }
			}
		}


		function getElements(prefix, local, parentElem){
			var result = "";
			if (prefix && isIE) {
				result = parentElem.getElementsByTagName(prefix + ":" + local);
			} else {
				result = parentElem.getElementsByTagName(local);
			}
			return result;
		}

		function getTheFirstElement(prefix, local, parentElem){
		    elems = getElements(prefix, local, parentElem);
			return elems[0];
		}

		function getElementText(result) {
				    if(result.firstChild){
					   return result.firstChild.nodeValue;
					}else{
					   return false;
					}
		}

		function getElementAttribute(result,att) {  	   
				return result.getAttribute(att);
		}


		function buildLevel(elems,level,id,type){

			//alert(id);

			if(id==0){
			   ancor = document.getElementById("mainAllProdsAncor");
			}else{
			   ancor =  document.createElement('div');
			   ancor.id = id;
			   //ancor.innerHTML = id;

			   par = document.getElementById("parent"+id);

			   if(par){
			     par.appendChild(ancor);
			   }
			}
			
		   for(i=0;i<elems.length;i++){
			  data = getElementText(elems[i]);
			  thisId = getElementAttribute(elems[i],"id");
			  thisSeries = getElementAttribute(elems[i],"series");
			  thisMarking = getElementAttribute(elems[i],"marking");


			  f = (i==0);
			  l = (i==(elems.length-1));

		      html = nodeHtml(data,level,thisId,type,thisSeries,f,l,thisMarking);
			  addNode(ancor,html,thisId,type);
		   }
		}


		function addNode(node,html,id,type){
		    newElem = document.createElement('div');
			if(type=="products"){
			   newElem.id = "product"+id;
			}else{
			   newElem.id = "parent"+id;
			}
			
			newElem.innerHTML = html;

            node.appendChild(newElem);
		}


		function toggleId(id,level){
			img = document.images["bullet"+id];
			if(img)
              curSrc = img.src;

			if(!document.getElementById(id)){
			   getData(id,level,id);
			   if(img)
				   img.src = curSrc.replace("closed","open"); 
			}else{
			   obj = document.getElementById(id);	
				   
			   if(obj.style.display=="block" || obj.style.display==""){
			      obj.style.display="none";
				  if(img)
				    img.src = curSrc.replace("open","closed");
			   }else{
			      obj.style.display="block";
				  if(img)
				    img.src = curSrc.replace("closed","open"); 
			   }

			}
		      
		}



		function nodeHtml(data,level,id,type,series,first,last,marking){

			if(type=="groups"){
				   color = colors[level-1];
			       data = parseLevel(data,level,id);

				   html = '<TABLE CELLPADDING="0" CELLSPACING="0" WIDTH="588" BORDER="0" style="cursor:pointer;" onClick="prodsObj.toggleId('+id+','+(parseInt(level)+1)+')"><TR><TD width="3"><img src="/images/allprods/level'+level+'_top_left.gif" width="3" height="3"></TD><TD width="582" style="background:'+color+' url(\'/images/allprods/border'+level+'.gif\') top left repeat-x;"><img src="/images/1x1.gif" width="1" height="1"></TD><TD width="3"><img src="/images/allprods/level'+level+'_top_right.gif" width="3" height="3"></TD></TR><TR><TD width="3" style="background:'+color+' url(\'/images/allprods/border'+level+'.gif\') top left repeat-y;"><img src="/images/1x1.gif" width="3" height="22"></TD><TD width="582" bgcolor="'+color+'">'+data+'</TD><TD width="3" style="background:'+color+' url(\'/images/allprods/border'+level+'.gif\') top right repeat-y;"><img src="/images/1x1.gif" width="3" height="22"></TD></TR><TR><TD width="3"><img src="/images/allprods/level'+level+'_bottom_left.gif" width="3" height="3"></TD><TD width="582" style="background:'+color+' url(\'/images/allprods/border'+level+'.gif\') bottom left repeat-x;"><img src="/images/1x1.gif" width="1" height="1"></TD><TD width="3"><img src="/images/allprods/level'+level+'_bottom_right.gif" width="3" height="3"></TD></TR></TABLE><img src="/images/1x1.gif" width="1" height="1">';
			}else{
				   data = '<a href="/'+lang+'/products/groups/?product='+id+'" target="_new">'+data+'</a> <nobr><span class="allprodsSeriesTitle">'+series+'</span> ('+markingText+': '+marking+')</nobr>';

			       html = '<TABLE CELLPADDING="0" CELLSPACING="0" WIDTH="588" BORDER="0"><TR><TD width="45"><img src="/images/1x1.gif" width="45" height="1"></TD><TD width="543">'+data+'</TD></TR></TABLE>';

				   if(first){
				      html = '<img src="/images/1x1.gif" width="1" height="10">' + html;
				   }else{
				      html = '<img src="/images/1x1.gif" width="1" height="13">' + html;
				   }

				   if(last){
				      html = html + '<img src="/images/1x1.gif" width="1" height="10">';
				   }
				   

				   //html = 'p';
			}



		   return html;
		}


		function parseLevel(data,level,id){

			
		
		   if(level==1){
		     data = '<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR><TD><img src="/images/1x1.gif" width="11" height="1"></TD><TD><span style="font-weight:bold;font-family:Tahoma;font-size:11px;color:#FFFFFF;">' + data + '</span></TD></TR></TABLE>';
		   }else if(level==2){
		     data = '<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR><TD><img src="/images/1x1.gif" width="10" height="1"></TD><TD><img src="/images/allprods/bigbullet_closed.gif" name="bullet'+id+'" width="5" height="5"></TD><TD><img src="/images/1x1.gif" width="16" height="1"></TD><TD><span class="allprodsTitle">' + data + '<span></TD></TR></TABLE>';
		   }else if(level==3 || level==5){
		     data = '<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR><TD><img src="/images/1x1.gif" width="21" height="1"></TD><TD><img src="/images/allprods/bigbullet_closed.gif" name="bullet'+id+'" width="5" height="5"></TD><TD><img src="/images/1x1.gif" width="5" height="1"></TD><TD><span class="allprodsTitle">' + data + '<span></TD></TR></TABLE>';
		   }else if(level==4){
		     data = '<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR><TD><img src="/images/1x1.gif" width="22" height="1"></TD><TD><img src="/images/allprods/smallbullet_closed.gif" name="bullet'+id+'" width="3" height="3"></TD><TD><img src="/images/1x1.gif" width="6" height="1"></TD><TD><span class="allprodsTitle">' + data + '<span></TD></TR></TABLE>';
		   }
			 return data;

		}


}
