var MenuCountNew = 0;
function CreateCollapseMenu(parent, data)
{
MenuCountNew++;
this.name = "MenuCollapse" + MenuCountNew;	 
this.obj = this.name + "Object";  
this.index = MenuCountNew;
this.depth = null;
this.parent = parent;
this.indent = '';
eval(this.obj+"=this");
this.template = CollapseTemplate1;  
this.container = '<DIV ID="'+this.name+'">{OBJECT}</DIV>';  
this.style = new Object();
this.style.display = 'block';
this.path = 'images/template10/';
this.icon = new Object();
this.icon.src = this.path+ "icon/icon.gif";
this.icon.width = 16;
this.icon.height = 16;
this.items = new Array(); 
this.selectedstyle1 = "oneLevelChild";
this.selectedstyle7 = "twoLevelChild";
this.selectedstyle8 = "threeLevelChild";
this.selectedstyle = "current";
this.selectedstyle2 = "current2";
this.selectedstyle3 = "current3";
//this.selectedstyle2 = "currentFirst";
//this.selectedstyle4 = "kkk222";
//this.selectedstyle5 = "levelTh";
this.add = function(draft, data, is_child)
{
var j = this.items.length;
this.items[j] = new Object();
this.items[j].draft = draft;
this.items[j].type = new Array();
this.items[j].key = 0;
this.items[j].open = false;
this.items[j].text = data[0];
this.items[j].alttext = data[1];
this.items[j].actived = false;
this.items[j].reference = data[2];
this.items[j].have_child = is_child?true:false;  
this.items[j].is_child = is_child;  
this.items[j].icon = new Object();
this.items[j].icon.src = data[5];  
this.items[j].icon.width = data[6]; 
this.items[j].icon.height = data[7];  
this.items[j].div = new Array();
this.items[j].div.name = this.name+ 'ItemBlock'+ j;
this.items[j].div.link = this.name+ 'ItemLink'+ j;
this.items[j].div.image = this.name+ 'ItemImages'+ j;
this.items[j].div.text = this.name+ 'ItemText'+ j;
}
this.build = function ()
{
var output = new String();
var menulist = new String();
var menuitem = new String();
var re1 = new RegExp("{item}(.*)\\{/item}", "gi");
var re2 = new RegExp("{object}", "gi");
menulist = this.template.replace(re1, '{content}');
menulist = this.container.replace(re2, menulist);
var re1 = new RegExp("{item}(.*)\\{/item}", "gi");
var re2 = new RegExp("{content}", "gi"); 
var re3 = new RegExp("{draft}", "gi"); 
var re4 = new RegExp("{text}", "gi"); 
var re5 = new RegExp("{block([-:;%\\s\\w=\"\']*)}", "gi"); 
var re6 = new RegExp("{/block}", "gi"); 
var re7 = new RegExp("{link([-:;%\\s\\w=\"\'_]*)}", "gi"); 
var re8 = new RegExp("{/link}", "gi"); 
var re9 = new RegExp("{textarea([-:;%\\s\\w=\"\']*)}", "gi"); 
var re10 = new RegExp("{/textarea}", "gi"); 
var re11 = new RegExp("{icon}", "gi"); 
menuitem = re1.exec(this.template);
for(var i=0; i < this.items.length; i++)
{
with(this.items[i])
{
layout = menuitem[1];
layout = layout.replace(re3, this.draft(i));
layout = layout.replace(re4, text);



//****************************************************************************

// первый уровень, с потомками
if(have_child && !actived && this.depth==0){
layout = layout.replace(re9, '<DIV style="padding-left:18px;" $1 ID="'+ div.text+ '">');
layout = layout.replace(re10, '</DIV>');
layout = layout.replace(re7, '<SPAN >');
layout = layout.replace(re8, '</SPAN>');
layout = layout.replace(re5, '<DIV $1 ID="'+ div.name+ '" CLASS="'+ this.selectedstyle1 +'">');
layout = layout.replace(re6, '</DIV>');
}

// первый уровень, без потомков
else if(!have_child && !actived && this.depth==0){

layout = layout.replace(re9, '<DIV style="padding-left:18px;" ID="'+ div.text+ '">');
layout = layout.replace(re10, '</DIV>');                       
								    //kkk222
layout = layout.replace(re5, '<DIV ID="'+ div.name+ '" CLASS="'+ this.selectedstyle1 +'" STYLE="cursor:hand;">');
layout = layout.replace(re6, '</DIV>');
layout = layout.replace(re7, '<A $1 HREF="'+ reference+ '" NAME="'+ div.link+ '">');
layout = layout.replace(re8, '</A>');
}
// второй уровень, с потомками
else if(have_child && !actived && this.depth==1){
layout = layout.replace(re9, '<DIV style="padding-left:26px;" $1 ID="'+ div.text+ '">');
layout = layout.replace(re10, '</DIV>');
layout = layout.replace(re7, '<SPAN >');
layout = layout.replace(re8, '</SPAN>');
layout = layout.replace(re5, '<DIV $1 ID="'+ div.name+ '" CLASS="'+ this.selectedstyle7 +'">');
layout = layout.replace(re6, '</DIV>');
}

// второй уровень, без потомков
else if(!have_child && !actived && this.depth==1){

layout = layout.replace(re9, '<DIV style="padding-left:26px;" ID="'+ div.text+ '">');
layout = layout.replace(re10, '</DIV>');                       
								    //kkk222
layout = layout.replace(re5, '<DIV ID="'+ div.name+ '" CLASS="'+ this.selectedstyle7 +'" STYLE="cursor:hand;">');
layout = layout.replace(re6, '</DIV>');
layout = layout.replace(re7, '<A $1 HREF="'+ reference+ '" NAME="'+ div.link+ '">');
layout = layout.replace(re8, '</A>');
}
//***********ТРЕТИЙ УРОВЕНЬ********************
else if(!actived && this.depth!=0 && this.depth!=1){
layout = layout.replace(re9, '<DIV style="padding-left:33px;" ID="'+ div.text+ '">');
layout = layout.replace(re10, '</DIV>');                       
								    //kkk222
layout = layout.replace(re5, '<DIV ID="'+ div.name+ '" CLASS="'+ this.selectedstyle8 +'" STYLE="cursor:hand;">');
layout = layout.replace(re6, '</DIV>');
layout = layout.replace(re7, '<A $1 HREF="'+ reference+ '" NAME="'+ div.link+ '">');
layout = layout.replace(re8, '</A>');
}

//*********************************************
//текущая 1 уровня
else if(actived && this.depth==0){
layout = layout.replace(re9, '<DIV style="padding-left:18px;" $1 ID="'+ div.text+ '">');
layout = layout.replace(re10, '</DIV>');
layout = layout.replace(re7, '<SPAN >');
layout = layout.replace(re8, '</SPAN>');
layout = layout.replace(re5, '<DIV $1 ID="'+ div.name+ '" CLASS="'+ this.selectedstyle +'">');
layout = layout.replace(re6, '</DIV>');
}
//текущая 2 уровня
else if(actived && this.depth==1){
layout = layout.replace(re9, '<DIV style="padding-left:26px;" $1 ID="'+ div.text+ '">');
layout = layout.replace(re10, '</DIV>');
layout = layout.replace(re7, '<SPAN >');
layout = layout.replace(re8, '</SPAN>');
layout = layout.replace(re5, '<DIV $1 ID="'+ div.name+ '" CLASS="'+ this.selectedstyle2 +'">');
layout = layout.replace(re6, '</DIV>');
}
//текущая 3 уровня
else if(actived && this.depth!=0 && this.depth!=1){
layout = layout.replace(re9, '<DIV style="padding-left:33px;" $1 ID="'+ div.text+ '">');
layout = layout.replace(re10, '</DIV>');
layout = layout.replace(re7, '<SPAN >');
layout = layout.replace(re8, '</SPAN>');
layout = layout.replace(re5, '<DIV $1 ID="'+ div.name+ '" CLASS="'+ this.selectedstyle3 +'">');
layout = layout.replace(re6, '</DIV>');
}







layout = layout.replace(re11, this.inserticon(i));
output += layout;
if(have_child)
{
output += '<TR><TD>';
output += is_child.build();
output += '</TD></TR>';
}
}
}
output = menulist.replace(re2, output);
return output;
}
this.draft = function(j)
{
plan = new String();
with(this.items[j])
{
var index = open ? 0 : 1;
for(var i = 0; i < draft.length; i++)
{
type[type.length] = div.image+ 'Draft'+ i;
if( draft[i][1] )
key = index;
else 
key = 0;
plan += '<IMG SRC="'+ draft[i][key].src+ '" WIDTH="'+ draft[i][key].width+ '" HEIGHT="'+ draft[i][key].height+ '" BORDER="0" ALT="" '+
'NAME="'+ type[type.length-1]+ '" '+
'>';
}
}
return plan;
}
this.inserticon = function(j)
{
var temp = new String();
if(this.icon.src)
{
with(this.items[j])
{
if(icon.src)  temp += '<IMG SRC="'+ icon.src+ '" WIDTH="'+ icon.width+ '" HEIGHT="'+ icon.height+ '">'; 
else  temp += '<IMG SRC="'+ this.icon.src+ '" WIDTH="'+ this.icon.width+ '" HEIGHT="'+ this.icon.height+ '">'; 
}
}
return temp;
}
this.css = function()
{
var output = setStyleSheet(this.name, this.style);
for(var i=0; i < this.items.length; i++)
{
with( this.items[i] )
{
if(have_child)
{
output += is_child.css(1);
}
}
}
if(!arguments.length)	 
{
document.write('<style type="text/css">'+ output+ '</style>');
}
return output;
}
this.init = function()
{
for(var i=0; i < this.items.length; i++)
{
with(this.items[i])
{
document.getElementById(div.name).onclick = new Function(this.obj+ '.click('+i+'); return true; ');
if(have_child)
{
document.getElementById(div.name).onclick = new Function(this.obj+ '.activated('+ i+ '); ');
is_child.init();
}
}
}
}
this.activated = function(i)
{
this.action(i);
this.reimage(i);
this.items[i].open = !this.items[i].open;
}
this.action = function(j)
{
with( this.items[j] )
{
if( open )
{
document.getElementById(is_child.name).style.display = 'none';
}
else
{
document.getElementById(is_child.name).style.display = 'block';
}
}
}
this.reimage = function(j)
{
with(this.items[j])
{
index = open ? 1 : 0;
for(var i = 0; i < draft.length; i++)
{
if( draft[i][1] )
{ 
document.images[type[i]].src = draft[i][index].src;
}
}
}
}
this.click = function(j)
{
document.links(this.items[j].div.link).click();
}
this.outblock = function()
{
for(var i =0; i < this.items.length; i++)
{
this.reimage(i);
if(this.items[i].have_child)
{
this.items[i].is_child.outblock();
}
}
}
this.setmouseevent = function()
{
for(var i=0; i < this.items.length; i++)
{  
with(this.items[i])
{
document.getElementById(div.name).onmouseover = new Function(this.obj+ '.selection('+i+'); return true; ');
document.getElementById(div.name).onmouseout = new Function(this.obj+ '.unselection('+i+'); return true; ');
if(have_child)
{
is_child.setmouseevent();
}
}
}
}
this.selection = function(j)
{
}
this.unselection = function(j)
{
}
}


   /*

    */

    CreateCollapseMenu.prototype.activelink = function()
    {
       var oItem = this.pathfinder();
       
       //this.setactivegroup(oItem);
    }

   /*

    */

    CreateCollapseMenu.prototype.pathfinder = function()
    {
       var oActive =  false;

       for(var i = 0; i < this.items.length; i++)
       {
          with(this.items[i])
          {
             if(this.defineactive(reference))
             {
                actived = true;

                this.style.visibility = "visible";

                return this;
             }

          	 if(have_child)  oActive = is_child.pathfinder();

          	 if(oActive)  return oActive;
          }
       }
    }

   /*

    */

    CreateCollapseMenu.prototype.defineactive = function(reference)
    {
       var re = new RegExp(reference, "gi");

       if( location.href.search(re) > -1 )	return true;

       return false;
    }

   /*

    */

    CreateCollapseMenu.prototype.selector = null;

    CreateCollapseMenu.prototype.setactivegroup = function(oItem)
    {
       while( oItem && oItem.indent!=null )
       {
          var indent = oItem.indent;

          var oPrevious = oItem;
          oItem = oItem.parent;

          if(indent)
          	oItem.items[indent].actived = true;
       }

       if(oPrevious)
       {
          this.selector = oPrevious.indent;
       }
    }

   /*

    */
