
//i meni i submeniji su ista klasa, samo razlicitog izgleda
// za pocetak imamo dva razlicita izgleda: navBar i dropDown


// Konstruktor za menu objekt
// paramId - naziv meni varijable
// paramMenuType - izgled menija: dropWDown ili navBar
// paramX,paramY - pocetna pozicija menija
// paramItemWidth -sirina stavke
// paramItemHeight - visina stavke

function SimpleMenu(paramId,paramMenuType,paramX,paramY,paramItemWidth,paramItemHeight){
 //popis submenu-a

  this.itemLabels = new Array();
  this.itemHints = new Array();
  this.itemBgColors = new Array();
  this.itemBgAColors = new Array();
  this.itemColors = new Array();
  this.itemSeparators =new Array();
  this.itemActions= new Array();
  this.itemUrls= new Array();
  this.itemSubMenus= new Array();
  this.visible=false;

  //kaskada , valjda left ili right
  this.cascade='';
  this.CSSClass="simplemenu";

  this.id=paramId;
  this.top=paramY;
  this.left=paramX;
  this.itemHeight=paramItemHeight;
  this.itemWidth=paramItemWidth;
  this.itemSpacing=0;
  this.itemPadding=0;
  this.itemBorderWidth=0;
  this.plugin="";
  this.arrow="../include/images/imgright.gif";
  this.type=paramMenuType;
  this.timerId=false;

  this.parent=null;
  this.canAutoClose=true;
  this.autoCloseTimer=300
  this.subOpenTimer=150

  this.addItem=SimpleMenuAddItem;
  this.generate=SimpleMenuGenerate;

  this.moveTo=SimpleMenuMoveTo;
  this.clickMenu=SimpleMenuClickMenu;
  this.close=SimpleMenuClose;
  this.slowHide=SimpleMenuSlowHide;
  this.cancelSlowHide=SimpleMenuCancelSlowHide;
  this.dropDown=SimpleMenuDropDown;
  this.show=SimpleMenuShow;
  this.hide=SimpleMenuHide;
}

function SimpleMenuShow(){
  if (!this.visible){
  showDiv(this.id+"Div");
   //fadeIn(_divId,_startOp,_endOp,_step,_timer){
   //fadeIn(this.id+"Div",0,0.75,0.09,10);
  this.visible=true
  if (document.all){
    document.all(this.id+"Div").style.filter="alpha(opacity=90)"
  }

  }
}

function SimpleMenuHide(){
  this.visible=false
  if (document.all){
    document.all(this.id+"Div").style.filter="alpha(opacity=100)"
  }
  hideDiv(this.id+"Div");
}


function SimpleMenuDropDown(i,_bezTimera){
 if (this.timerId) {this.cancelSlowHide();}
 var _j=0;
 //prvo zatvaramo sve stavke osim odabrane
 for (var _j=0;_j<this.itemSubMenus.length;_j++){
  if ((_j!=i)&&(this.itemSubMenus[_j])){

    this.itemSubMenus[_j].hide();
  }
 }
 //zatim otvaramo podmeni sa zadrskom
 if (this.itemSubMenus[i]){
  if (_bezTimera){
     //alert(document.getElementById(this.itemSubMenus[i].id+'Div'))
     //ako je zahtjevano trenutno otvaranje zovemo bez timera (explorer 4)
     this.clickMenu(i,true);
  }else {
    //ako nije zovemo s timerom normalno
    this.timerId=setTimeout(this.id+'.clickMenu('+i+',true)',this.subOpenTimer);
  }
 }

}

function SimpleMenuSlowHide(){
 if (this.canAutoClose){
   this.timerId=setTimeout(this.id+".close()",this.autoCloseTimer);
   if (this.parent) {this.parent.slowHide();}
 }

}


function SimpleMenuCancelSlowHide(){
 if (this.parent) {this.parent.cancelSlowHide();}
 if (this.timerId) {clearTimeout(this.timerId);}
}

function SimpleMenuMoveTo(_px,_py){
	
 // _visina=getDivHeight(this.id+'Div');
 _visina=this.itemLabels.length*this.itemHeight;
 _visinaProzora=getWindowHeight();
 //  alert("visina:"+_py);
//  alert("visina menija:"+_visina);
//  alert("visinaprozora:"+_visinaProzora);
 if ((_visina+_py)>_visinaProzora) {_py=(_visinaProzora-_visina-10)}
 //  alert("visina:"+_py);
 moveDivTo(this.id+'Div',_px,_py);
 this.left=_px;this.top=_py;
 //sad se pomicu eventualna djeca

 n=this.itemSubMenus.length;
 for (var i=0;i<n;i++){
 if (this.itemSubMenus[i]){
    //sada odlucujemo kako cemo kaskadirati
    //ako nam cascade nije zadan 
    if (this.cascade==''){
         //alert(_px);
         //ako je mis previse desno, kaskadiramo na lijevo
      if (_px>470){
        this.itemSubMenus[i].cascade='left'
      } else {
        this.itemSubMenus[i].cascade='right'
      }
    } else { this.itemSubMenus[i].cascade=this.cascade;}
      
    //znamo visinu i sirinu svakog itema u meniju i sad trebamo izracunati poziciju podmenija
    switch (this.type){
     case "navBar":
      paramLeft=this.left+(this.itemWidth+this.itemPadding+this.itemSpacing+this.itemBorderWidth)*i+5;
      paramTop=this.top+this.itemHeight+this.itemPadding+this.itemSpacing+this.itemBorderWidth+3;
     break;
     case "dropDown":
      paramTop=this.top+(this.itemHeight+this.itemPadding+this.itemSpacing+this.itemBorderWidth)*i;
      if (this.itemSubMenus[i].cascade=='left'){
      paramLeft=this.left-this.itemSubMenus[i].itemWidth}
      else {
        paramLeft=this.left+this.itemWidth+this.itemPadding+this.itemSpacing+this.itemBorderWidth-4;
      }
     break;       
    }
    this.itemSubMenus[i].moveTo(paramLeft,paramTop)
  }
 }
  
}

function SimpleMenuClose(){
  //moramo zatvoriti sebe i svoje submenije 
  for (var i=0;i<this.itemSubMenus.length;i++){
   //zatvaramo svoju djecu
   //da li Div postoji
   if (this.itemSubMenus[i]){
     this.itemSubMenus[i].close();
   }
    //pa zatvorimo sebe
    this.hide();
   }
  
}

//reagiramo na klik misa
function SimpleMenuClickMenu(paramItem,paramNoAction){
 //this.cancelSlowHide();
 //Ako postoji submeni na tom mjestu otvaramo ga
 //i zatvaramo sve ostale
 for (var i=0;i<this.itemSubMenus.length;i++){
  //da li Div postoji
  if (this.itemSubMenus[i]){
  // ako se taj menu otvara
  if (i==paramItem){
     //showDiv(this.itemSubMenus[i].id+"Div");
     this.itemSubMenus[i].show();
  } else {
     this.itemSubMenus[i].close();
  }
 }
}
// nakon toga pozivamo zadani action ako je dozvoljen
if (!paramNoAction){
 if (this.itemActions[paramItem]) {
      eval(this.itemActions[paramItem])
  }
}


}

//dodajemo novi item na meni

function SimpleMenuAddItem(paramLabel,paramAction,paramUrl,paramSubMenu,paramSeparator,paramColor,paramBgColor,paramBgAColor){
 i=  this.itemLabels.length;
 this.itemLabels[i]=paramLabel;
 this.itemActions[i]=paramAction;
 this.itemUrls[i]=paramUrl;

 if (paramSubMenu) {
  paramSubMenu.parent=this;
  this.itemSubMenus[i]=paramSubMenu;
  } else {this.itemSubMenus[i]=false;}

  if (paramBgColor){
 this.itemBgColors[i]=paramBgColor;
 } else {this.itemBgColors[i]="#dddddd"}

 if (paramBgAColor) {
 this.itemBgAColors[i]=paramBgAColor;
  } else {this.itemBgAColors[i]="#eeeeee"}

 if (paramColor){
 this.itemColors[i]=paramColor;
 } else {this.itemColors[i]="black"}


 if (paramSeparator){
 this.itemSeparators[i]=paramSeparator;
 } else {this.itemSeparators[i]=false}



}


//ovdje generiramo vizualni izgled objekta na displayu zajedno sa njegovom djecom
//prvi je po defaultu vidljiv, dok su sva djeca nevidljiva

function SimpleMenuGenerate(vis){
 //prvo izbacujemo sebe, onda svoju djecu

var n=this.itemLabels.length;
if (vis=="visible") {this.visible=true}
//pocetak prljavog koda

 //crtamo sebe

 var out="";




 switch (this.type){
 //ako je navBar crtamo ga horizontalno
  case "navBar":
     _w=this.itemWidth;
     _h=this.itemHeight;
     if (browserName=="NS4"){
     out+="<layer id='"+this.id+"Div' class='simplemenudiv' onMouseOut='"+this.id+".slowHide()' "
     out+=" onMouseOver='"+this.id+".cancelSlowHide()' "
     this.visible?out+="":out+=" visibility=hide ";
     out+=" top="+this.top+" left="+this.left+" height="+_h+" clip='"+(_w*n)+","+_h+"'>";
      //sad ubacujemo iteme u fuckin div bez tablice - probat æemo sa blokanim a-ovima
     for (i=0;i<n;i++){
      _left=i*_w;
       out+="<layer class='NSL"+this.CSSClass+"' left="+_left+" width="+_w+" bgcolor="+this.itemBgColors[i];
        out+=" onMouseOver='this.bgColor=\""+this.itemBgAColors[i]+"\";"+this.id+".dropDown("+i+",true);"+this.id+".cancelSlowHide(); ' "
        out+=" onMouseOut ='this.bgColor=\""+this.itemBgColors[i]+"\"' >"
        out+= "<table width="+_w+" border=0 cellspacing=0 cellpadding=0><tr><td>"
        out+="<a href='"+this.itemUrls[i]+"' onClick='"+this.id+".clickMenu("+i+")' "
        out+= " class='NS"+this.CSSClass+"'  oonMouseOver='"+this.id+".dropDown("+i+",true);"+this.id+".cancelSlowHide();' >"
        out+= this.itemLabels[i]+"</a></td></tr></table>"
       out+="</layer>";
     }
     out+="</layer>";

     } else {
     out+="<div id='"+this.id+"Div' class='simplemenudiv' onMouseOut='"+this.id+".slowHide()' onMouseOver='"+this.id+".cancelSlowHide()' "
     out+=" style='position:absolute; width:"+(n*_w+3)+"px; height:"+(_h+1)+"px; overflow:hidden; visibility:"+vis+"; top:"+this.top+"px; left:"+this.left+"px; z-index:100; '>";
     //sad ubacujemo iteme u fuckin div bez tablice - probat æemo sa blokanim a-ovima
     for (i=0;i<n;i++){
      _left=i*_w;
      out+="<div style='position:absolute;overflow:hiden; left:"+ _left+"px; height:"+_h+"px; width:"+_w+"px;'>";
      out+= "<a href='"+this.itemUrls[i]+"' onClick='"+this.id+".clickMenu("+i+")' "
      out+= " onMouseMove='"+this.id+".cancelSlowHide();' onMouseOver='"+this.id+".dropDown("+i+",true);"+this.id
      out+= ".cancelSlowHide();' onSomething='"+this.id+".cancelSlowHide();' "
      out+= " class='"+this.CSSClass+"' style='display:block; ooverflow:hidden; "
      out+= " text-decoration:none; height:"+this.itemHeight+"px; line-height:"+this.itemHeight+"px;' >";
      out+= (browserName=="MS4" ? "&nbsp;&nbsp;":"")+this.itemLabels[i]+"</a>"
      out+="</div>";


     }
     out+="</div>";
     }




    break;
//ako je dropdown ide vertikalno
case "dropDown":
     _w=this.itemWidth;
     _h=this.itemHeight;
     if (browserName=="NS4"){
     out+="<layer id='"+this.id+"Div' class='simplemenudiv' onMouseOut='"+this.id+".slowHide()' "
     out+=" onMouseOver='"+this.id+".cancelSlowHide()' "
     this.visible?out+="":out+=" visibility=hide ";
     out+=" top="+this.top+" left="+this.left+">";
      //sad ubacujemo iteme u fuckin div bez tablice - probat æemo sa blokanim a-ovima
     for (i=0;i<n;i++){
      _top=i*_h;
       out+="<layer class='NSL"+this.CSSClass+"' top="+_top+" width="+_w+" bgcolor="+this.itemBgColors[i];
        out+=" onMouseOver='this.bgColor=\""+this.itemBgAColors[i]+"\"' "
        out+=" onMouseOut ='this.bgColor=\""+this.itemBgColors[i]+"\"' >"
        out+= "<table width="+_w+" border=0 cellspacing=0 cellpadding=0><tr><td><a href='"+this.itemUrls[i]+"' onClick='"+this.id+".clickMenu("+i+")' "
        out+= " class='NS"+this.CSSClass+"'>"
        out+= this.itemLabels[i]+"</a></td></tr></table>"
       out+="</layer>";
     }
     out+="</layer>";

     } else {
     out+="<div id='"+this.id+"Div' class='simplemenudiv' onMouseOut='"+this.id+".slowHide()' onMouseOver='"+this.id+".cancelSlowHide()' "
     out+=" style='position:absolute; width:"+(_w+1)+"px; height:"+((_h+2)*n+1)+"px; overflow:hidden; visibility:"+vis+"; top:"+this.top+"px; left:"+this.left+"px; z-index:100; '>";
     //sad ubacujemo iteme u fuckin div bez tablice - probat æemo sa blokanim a-ovima
          for (i=0;i<n;i++){
      _top=i*(2+_h);

      out+="<div style='position:absolute;overflow:hidden; top:"+ _top+"px; height:"+_h+"px; width:"+_w+"px;'>";
      out+= "<a href='"+this.itemUrls[i]+"' onClick='"+this.id+".clickMenu("+i+")' "
      out+= " onMouseMove='"+this.id+".cancelSlowHide();' onMouseOver='"+this.id+".dropDown("+i+",true);"+this.id
      out+= ".cancelSlowHide();' onSomething='"+this.id+".cancelSlowHide();' "
      out+= " class='"+this.CSSClass+"' style='display:block; ooverflow:hidden; "
      out+= " text-decoration:none; height:"+this.itemHeight+"px; line-height:"+this.itemHeight+"px;' >";
      out+= (browserName=="MS4" ? "&nbsp;&nbsp;":"")+this.itemLabels[i]+"</a>"
      out+="</div>";


     }
     out+="</div>";
     }



     break;

case "plugin":
      break;

default: break;

}

   out+="</div>"


//kraj prljavog koda
//sad se generiraju eventualna djeca
var paramLeft=0
var paramTop=0
for (var i=0;i<n;i++){
if (this.itemSubMenus[i]){
      //znamo visinu i sirinu svakog itema u meniju i sad trebamo izracunati poziciju podmenija
      switch (this.type){
      case "navBar":
        paramLeft=this.left+(this.itemWidth+this.itemPadding+this.itemSpacing+this.itemBorderWidth)*i+5;
        paramTop=this.top+this.itemHeight+this.itemPadding+this.itemSpacing+this.itemBorderWidth+3;
        break;
       case "dropDown":
         paramTop=this.top+(this.itemHeight+this.itemPadding+this.itemSpacing+this.itemBorderWidth)*i;
         paramLeft=this.left+this.itemWidth+this.itemPadding+this.itemSpacing+this.itemBorderWidth;
         break;
      }
      this.itemSubMenus[i].left=paramLeft;
      this.itemSubMenus[i].top=paramTop;

      out+=this.itemSubMenus[i].generate("hidden");


   }
}


 return(out);

}








