home *** CD-ROM | disk | FTP | other *** search
/ Enter 2003: The Beautiful Scenery / enter-parhaat-2003.iso / interface / menu / js / coolmenupro.js
Encoding:
Text File  |  2002-12-31  |  15.1 KB  |  355 lines

  1. //----------------------------------------------------------------------------------
  2. // Title: COOLjsMenuPRO
  3. // URL: http://javascript.cooldev.com/scripts/coolmenupro/
  4. // Version: 1.7.0
  5. // Last Modify: 11-11-2002
  6. // Author: Sergey Nosenko <darknos@cooldev.com>
  7. // Notes: Registration needed to use this script on your web site.
  8. // See official site for details
  9. // Copyright (c) 2001-2002 by CoolDev.Com
  10. // Copyright (c) 2001-2002 by Sergey Nosenko
  11. //----------------------------------------------------------------------------------
  12. window.CMenus=[];
  13. var BLANK_IMAGE="img/b.gif";
  14. function bw_check(){this.dom=document.getElementById?1:0;this.opera=window.opera?1:0;this.ns4=(document.layers && !this.dom)?1:0;this.ie4=(document.all&&!this.dom)?1:0;return this;}
  15. function none(){}
  16. function nn(val){return val != null;}
  17. function und(val){return typeof(val)=='undefined';}
  18. function COOLjsMenuPRO(name, items){
  19.     this.bw=new bw_check(); this.bi=new Image(); this.bi.src=BLANK_IMAGE;
  20.     if (this.bw.ns4) window.onresize=resizeHandler; window.CMenus[name]=this; window.CMenuHideTimers[name]=null;
  21.     this.name=name; this.rel=items[0].pos=="relative"; this.root=[]; this.root.par=null;this.root.cd=[];
  22.     this.root.fmt=items[0]; this.root.pos=this.rel?[0,0]:items[0].pos; this.root.fmt.pos=this.root.pos;this.root.frameoff=items[0].pos?items[0].pos:[0,0];
  23.     this.items=[]; this.root.lvl=new CMenuLevel(this, this.root);
  24.     for (var i=1;i<items.length;i++) if (!und(items[i])) new CMenuItem(this, this.root, items[i], und(items[i].format)?items[0]:items[i].format);
  25.     this.drawTop=function(){
  26.         var s="";
  27.         for (var i=0;i<this.items.length;i++) if (this.items[i].par==this.root) s+=this.items[i].draw();
  28.         if (this.rel){
  29.             var w=0; var h=0;
  30.             for (var i=0;i<this.root.cd.length;i++) {
  31.                 var n=this.root.cd[i];
  32.                 if (n.pos[1]+n.size[0]>h) h=n.pos[1]+n.size[0];
  33.                 if (n.pos[0]+n.size[1]>w) w=n.pos[0]+n.size[1];
  34.             }
  35.             s=this.bw.ns4?'<ilayer id="cm'+this.name+'_" >' + s + '</ilayer>':'<div id="cm'+this.name+'_" style="position:relative;left:0px;top:0px;width:'+w+'px;height:'+h+'px;">' + s +'</div>';
  36.         }
  37.         return s;
  38.     }
  39.     this.drawOther=function(){
  40.         var s="";
  41.         for (var i=0;i<this.items.length;i++) if (this.items[i].par!=this.root) s+=this.items[i].draw();
  42.         return s;
  43.     }
  44.     this.initTop=function(){document.write(this.drawTop());}
  45.     this.init=function(){document.write(this.drawOther());}
  46.     this.hide=function(){
  47.         if (this.root.fmt.popup) 
  48.             this.root.lvl.vis(0);
  49.         else {
  50.             for (var i=0;i<this.root.cd.length;i++) if (this.root.cd[i].lvl) this.root.cd[i].lvl.vis(0);
  51.             this.root.lvl.a=null;
  52.             this.root.lvl.draw();
  53.             if (this.root.fmt.hidden_top) this.root.lvl.vis(0);
  54.         }
  55.     }
  56.     this.mpopup=function(ev,offX,offY){
  57.         var x=ev.pageX?ev.pageX:(this.bw.opera?ev.clientX:this.bw.ie4?ev.clientX+document.body.scrollLeft:ev.x+document.body.scrollLeft);
  58.         var y=ev.pageY?ev.pageY:(this.bw.opera?ev.clientY:this.bw.ie4?ev.clientY+document.body.scrollTop:ev.y+document.body.scrollTop);
  59.         var po=this.root.fmt.popupoff;
  60.         y += offY?offY:po?po[0]:0;
  61.         x += offX?offX:po?po[1]:0;
  62.         this.popup(x, y);
  63.     }
  64.     this.popup=function(x,y){
  65.         this.moveXY(x,y);
  66.         this.root.lvl.a=null;
  67.         this.root.lvl.vis(1);
  68.         mEvent(this.name,0,'t');
  69.         mEvent(this.name,0,'0');
  70.     }
  71.     this.moveXY=function(x,y){
  72.         if (!this.root.pos || this.root.pos[0] != x || this.root.pos[1] != y) {
  73.             this.root.pos=[x,y]; this.root.loff=[0,0]; this.root.ioff=[0,0];
  74.             for (var i=0;i<this.items.length;i++){
  75.                 this.items[i].setPosFromParent();
  76.                 this.items[i].move(this.items[i].pos[0],this.items[i].pos[1]);
  77.             }
  78.         }
  79.     }
  80.     this.show=function(){
  81.         if (this.rel) this.move();
  82.         this.root.lvl.vis(1)
  83.     }
  84.     this.move=function(){
  85.         if (!this.rel) return;
  86.         var d=this.get_div('cm'+this.name+'_');
  87.         var x=this.bw.ns4?d.pageX:domPageX(d); 
  88.         var y=this.bw.ns4?d.pageY:domPageY(d);
  89.         //alert([x,y]);
  90.         var topNode=this.items[0];
  91.         this.root.pos=[x,y];
  92.         for (var i=0;i<this.items.length;i++){
  93.             this.items[i].setPosFromParent();
  94.             if (this.items[i].par !== this.root) 
  95.                 this.items[i].move(this.items[i].pos[0],this.items[i].pos[1]);
  96.         }
  97.     }
  98.     this.get_div=function (name){return this.bw.ns4?document.layers[name]:document.getElementById?document.getElementById(name):document.all[name];}
  99. }
  100.  
  101. function CMenuLevel(menu, par){
  102.     this.menu=menu;this.par=par;this.v=0;
  103.     this.vis=function(s){
  104.         var ss=this.v;
  105.         this.v=s; var l=this.par.cd.length;
  106.         if (this.menu.onlevelshow) this.menu.onlevelshow(this);
  107.         for (var i=0;i<l;i++){
  108.             var n=this.par.cd[i];
  109.             if ( n.hc() && n.lvl.v && !s ) n.lvl.vis(s);
  110.             n.vis(s);
  111.         }
  112.         if (!s) this.a=null;
  113.         if (this.v!=ss&&this.menu.onlevelshow) this.menu.onlevelshow(this);
  114.     }
  115.     this.setA=function(idx,s){
  116.         var n=this.menu.items[idx];
  117.         if (nn(this.a)&&n.par.lvl!=this.a.par.lvl) return;
  118.         if(s&&n.hc())n.lvl.vis(1);
  119.         if( s && n!= this.a && nn(this.a) && this.a.hc() && this.a.lvl.v ) this.a.lvl.vis(0);
  120.         this.a=n;
  121.         this.draw()
  122.     }
  123.     this.draw=function(){if (this.menu.root.lvl==this&&this.menu.root.fmt.hidden_top) return;for (var i=0;i<this.par.cd.length;i++) if (this.par.cd[i]==this.a) this.par.cd[i].setVis('o'); else this.par.cd[i].setVis('n');}
  124. }
  125.  
  126. function CMenuItem(menu, par, item, format){
  127.     if (und(item)) return;
  128.     this.lvl=null;this.par=par;this.code=item.code; this.ocode=item.ocode || item.code;
  129.     this.targ=und(item.target)?"":'target="'+item.target+'" ';this.url=und(item.url)?"javascript:none()":item.url;
  130.     this.fmt=format;this.menu=menu;this.bw=menu.bw;this.cd=[];this.divs=[];this.index=menu.items.length;menu.items=menu.items.concat([this]);this.pindex=par.cd.length;
  131.     par.cd=par.cd.concat([this]);this.id="cmi"+this.menu.name+"_"+this.index;this.v=0;this.state='n';
  132.     this.diva=["b","s","o","n","e"];
  133.     this.hc=function(){return this.cd.length > 0}; 
  134.     this.hac=function(){return this.hc()&&this.cd[0].vis}; 
  135.     this.div=function(n){return und(this.divs[n])?this.divs[n]=this.get_div(this.id+n):this.divs[n];}
  136.     this.gen_code=function(state, off){
  137.         var res='';
  138.         var table=(nn(this.arrow) && this.hc()) || nn(this.image);
  139.         var image=nn(this.image);
  140.         var arrow=nn(this.arrow)&& this.hc();
  141.         if (table) res += '<table cellpadding=0 cellspacing=0 width="100%" height="'+(parseInt(this.size[0])-parseInt(off))+'" border=0><tr>';                                                           
  142.         if (image) res += '<td bgcolor="'+(state=='n'?this.style.color.imagebg:this.style.color.oimagebg)+'" width='+this.imgsize[1]+'><img src="'+(state=='n'?this.image:this.oimage)+'" width='+this.imgsize[1]+' height='+this.imgsize[0]+'></td>';
  143.         if (table) res += '<td width="100%">';
  144.         res += '<div class="'+(state=='n'?this.style.css.ON:this.style.css.OVER)+'">'+(state=='n'?this.code:this.ocode)+'</div>';
  145.         if (table) res += '</td>';
  146.         if (arrow) res += '<td width='+this.arrsize[1]+'><img src="'+(state=='n'?this.arrow:this.oarrow)+'" width='+this.arrsize[1]+' height='+this.arrsize[0]+'></td>';
  147.         if (table) res += '</tr></table>';
  148.         return res;
  149.     }
  150.     this.draw=function(){    
  151.         var bl=bt=this.style.border;
  152.         var br=bb=this.style.border*2;
  153.         if (this.style.border && !und(this.style.borders)) {
  154.             bl=this.style.borders[0];
  155.             bt=this.style.borders[1];
  156.             br=this.style.borders[2]+bl;
  157.             bb=this.style.borders[3]+bt;
  158.         }
  159.         var s=this.style.shadow;
  160.         var z=(!this.style.shadow?"":adiv(this.menu.bw, this.id+"s", this.z, this.pos[0]+s, this.pos[1]+s, this.size[1], this.size[0], this.style.color.shadow, "", ""))+
  161.                 (!this.style.border?"":adiv(this.menu.bw, this.id+"b", this.z, this.pos[0], this.pos[1], this.size[1], this.size[0], this.style.color.border, "", ""))+
  162.                 adiv(this.menu.bw, this.id+"n", this.z, this.pos[0]+bl, this.pos[1]+bt, this.size[1]-br, this.size[0]-bb, this.style.color.bgON, this.gen_code('n', parseInt(bt)+parseInt(bb)))+
  163.                 adiv(this.menu.bw, this.id+"o", this.z, this.pos[0]+bl, this.pos[1]+bt, this.size[1]-br, this.size[0]-bb, this.style.color.bgOVER,this.gen_code('o', bt+bb))+
  164.                 adiv(this.menu.bw, this.id+"e", this.z, this.pos[0]+bl, this.pos[1]+bt, this.size[1]-br, this.size[0]-bb, "", '<a href="'+this.url+'" '+this.targ+'onclick="mEvent(\''+this.menu.name+'\','+this.index+',\'c\');">'+'<img src="'+this.menu.bi.src+'" width="'+this.size[1]+'" height="'+this.size[0]+'" border="0"></a>','','onmouseover="mEvent(\''+this.menu.name+'\','+this.index+',\'o\');" onmouseout="mEvent(\''+this.menu.name+'\','+this.index+',\'t\');"');
  165.         return z;
  166.     }
  167.     this.vis=function(s){
  168.             if (this.style.shadow) this.visDiv("s",s);
  169.             if (this.style.border) this.visDiv("b",s);
  170.             if (!s) {
  171.                 this.visDiv("o",0);
  172.                 this.visDiv("n",0);
  173.                 this.state="n";
  174.             }else if (this.state=="n")
  175.                 this.visDiv("n",1)
  176.             else 
  177.                 this.visDiv("o",1)
  178.             this.visDiv("e",s)
  179.     }
  180.     this.setVis = function (n){
  181.         if (this.state!=n)
  182.             switch (n){
  183.                 case "n":
  184.                     this.visDiv("n",1);this.visDiv("o",0);
  185.                     break;
  186.                 case "o":
  187.                     this.visDiv("n",0);this.visDiv("o",1);
  188.                     break;
  189.             }
  190.         this.state=n;
  191.     }
  192.     this.visDiv=this.bw.ns4? visDivNS:visDivDom;
  193.     this.getf=function(obj, name){
  194.         if (!und(obj) && nn(obj) && !und(obj.fmt)) {
  195.             if (!und(obj.fmt[name]))
  196.                 return obj.fmt[name];
  197.             if (obj.par!=this.menu.root && obj.par && obj.par.sub && obj.par.sub[0][name]) 
  198.                 return obj.par.sub[0][name];
  199.             return this.getf(obj.par, name);
  200.         }
  201.         return;
  202.     }
  203.     this.ioff=this.getf(this, "itemoff");
  204.     this.loff=this.getf(this, "leveloff");
  205.     this.imgsize=this.getf(this, "imgsize");
  206.     this.arrsize=this.getf(this, "arrsize");
  207.     this.image=this.getf(this, "image");
  208.     this.oimage=this.getf(this, "oimage") || this.image;
  209.     this.arrow=this.getf(this, "arrow");
  210.     this.oarrow=this.getf(this, "oarrow") || this.arrow;
  211.     this.style=this.getf(this, "style");
  212.     this.size=this.getf(this, "size");
  213.     if (this.par==this.menu.root) this.fmt.pos=this.getf(this, "pos");
  214.     this.prev=this.pindex==0? null : this.par.cd[this.pindex-1];
  215.     this.setPos=function(){
  216.         if (this.prev==null){
  217.             this.z=this.par==this.menu.root? 0: this.par.z+10;
  218.             this.pos=und(this.fmt.pos)?(this.par==this.menu.root? this.fmt.pos : this.pos=[this.par.pos[0]+this.loff[1], this.par.pos[1]+this.loff[0]]):this.fmt.pos;
  219.         }else{
  220.             this.prev.next=this;
  221.             this.z=this.prev.z;
  222.             this.pos=[this.prev.pos[0]+this.ioff[1], this.prev.pos[1]+this.ioff[0]];
  223.         }
  224.     }
  225.     this.setPosFromParent=function(){
  226.         if (this.index==0&&!this.menu.rel) {
  227.             this.pos=[this.menu.root.pos[0], this.menu.root.pos[1]]
  228.         } else 
  229.         if (this.prev==null){
  230.             this.pos=[this.par.pos[0]+this.loff[1], this.par.pos[1]+this.loff[0]];
  231.         }else{
  232.             this.pos=[this.prev.pos[0]+this.ioff[1], this.prev.pos[1]+this.ioff[0]];
  233.         }
  234.     }
  235.     this.setPos();
  236.     this.sub=item.sub;
  237.     if (!und(this.sub) && !und(this.sub.length)&& this.sub.length>0){
  238.         this.lvl=new CMenuLevel(menu, this);
  239.         for (var i=1;i<this.sub.length;i++)
  240.             if (!und(this.sub[i])) new CMenuItem(this.menu, this, this.sub[i], und(this.sub[i].format)?this.sub[0]: this.sub[i].format);
  241.     }
  242.     this.get_div=function (name){
  243.         if (this.bw.ns4 && this.menu.rel && this.par==this.menu.root) 
  244.             return document.layers["cm"+this.menu.name+"_"].layers[name]
  245.         else
  246.             return this.bw.ns4?document.layers[name]:document.getElementById?document.getElementById(name):document.all[name];
  247.     }
  248.     this.move=function( x, y ){
  249.         var bl=bt=this.style.border;
  250.         if (this.style.border && !und(this.style.borders)) {
  251.             bl=this.style.borders[0];
  252.             bt=this.style.borders[1];
  253.         }
  254.         if (this.style.shadow) this.moveTo(x+parseInt(this.style.shadow),y+parseInt(this.style.shadow),"s");
  255.         if (this.style.border) this.moveTo(x,y,"b");
  256.         this.moveTo(x+bl,y+bt,"o");
  257.         this.moveTo(x+bl,y+bt,"n");
  258.         this.moveTo(x+bl,y+bt,"e");
  259.     }
  260.     this.moveTo=function( x, y, b ){
  261.         if (this.bw.ns4)
  262.             this.div(b).moveTo(x,y);
  263.         else{
  264.             this.div(b).style.left=x;
  265.             this.div(b).style.top=y;
  266.         }
  267.     }
  268.     return this;
  269. }
  270. function adiv(bw,name,z,left,top,width,height,bgc,code,otherCSS, otherDIV){
  271.     return bw.ns4?
  272.         '<layer id="'+name+'" z-index="'+z+'" left="'+left+'" top='+top+'" width="'+width+'" height="'+height+'"'+(bgc!=""?' bgcolor="'+bgc+'"':'')+(otherCSS?' style="'+otherCSS:'')+'" visibility="hidden" '+(otherDIV?otherDIV:'')+'>'+code+'</layer>\n':
  273.         '<div id="'+name+'" style="position:absolute;clip:rect(0px '+width+'px '+height+'px 0px);z-index:'+z+';left:'+left+'px;top:'+top+'px;width:'+width+'px;height:'+height+'px;visibility:hidden'+(bgc!=""?';background-color:'+bgc+'':'')+';'+(otherCSS?otherCSS:'')+'" '+(otherDIV?otherDIV:'')+'>'+code+'</div>';
  274. }
  275. function visDivNS(d,s){
  276.     this.div(d).visibility=s?'show':'hide';
  277. }
  278. function visDivDom(d,s){
  279.     this.div(d).style.visibility=s?'visible': 'hidden';
  280. }
  281.  
  282. function mEvent(m,node_index,e) {
  283.     if (nn(window.CMenuHideTimers[m])) {
  284.         window.clearTimeout(window.CMenuHideTimers[m]);
  285.         window.CMenuHideTimers[m]=null;
  286.     }
  287.     switch (e){
  288.         case "o": 
  289.             window.CMenus[m].items[node_index].par.lvl.setA(node_index,1);
  290.             if (window.CMenus[m].onmouseover) window.CMenus[m].onmouseover(window.CMenus[m].items[node_index]);
  291.             break;
  292.         case "c":
  293.             if (window.CMenus[m].items[node_index].hc()) 
  294.                 window.CMenus[m].items[node_index].lvl.vis(!window.CMenus[m].items[node_index].lvl.v);
  295.             else
  296.                 for (var i=0;i<window.CMenus[m].root.cd.length;i++)
  297.                     if (nn(window.CMenus[m].root.cd[i].lvl)) window.CMenus[m].root.cd[i].lvl.vis(0);
  298.             if (window.CMenus[m].onclick) window.CMenus[m].onclick(window.CMenus[m].items[node_index]);
  299.             break;
  300.         case "t": 
  301.             window.CMenuHideTimers[m]=setTimeout('window.CMenus["'+m+'"].hide()', und(window.CMenus[m].root.fmt.delay)?600:window.CMenus[m].root.fmt.delay);
  302.             if (window.CMenus[m].onmouseout) window.CMenus[m].onmouseout(window.CMenus[m].items[node_index]);
  303.             break;
  304.     }
  305.     return true;
  306. }
  307. function domPageX(el) {
  308.   var x=el.offsetLeft;
  309.   var parent=el.offsetParent;
  310.   while(parent && parent!=document.body) {
  311.     x += parent.offsetLeft;
  312.     parent=parent.offsetParent;
  313.   }
  314.   return x;
  315. }
  316. function domPageY(el) {
  317.   var x=el.offsetTop;
  318.   var parent=el.offsetParent;
  319.   while(parent && parent!=document.body) {
  320.     x += parent.offsetTop;
  321.     parent=parent.offsetParent;
  322.   }
  323.   return x;
  324. }
  325. if (und(window.CMenuHideTimers)) window.CMenuHideTimers=[];
  326. window.oldCMOnLoad=window.onload;
  327. function CMOnLoad(){
  328.     var bw=new bw_check();
  329.     if (bw.ns4 || bw.opera){
  330.         window.origWidth=window.innerWidth;
  331.         window.origHeight=window.innerHeight;
  332.         if (bw.opera && !window.operaResizeTimer) resizeHandler();
  333.         if (bw.ns4) window.onresize=resizeHandler;
  334.     } else {
  335.         domOnResize();
  336.         window.onresize=new domOnResize();
  337.     }
  338.     if (typeof(window.oldCMOnLoad)=='function') window.oldCMOnLoad();
  339. }
  340. window.onload=new CMOnLoad();
  341. function resizeHandler() {
  342.     if (window.reloading) return;
  343.     var reload=window.innerWidth != window.origWidth || window.innerHeight != window.origHeight;
  344.     window.origWidth=window.innerWidth; window.origHeight=window.innerHeight;
  345.     if (reload) {window.reloading=1;document.location.reload();return};
  346.     if (new bw_check().opera) window.operaResizeTimer=setTimeout('resizeHandler()',500);
  347. }
  348. function CMenuPopUp(menu, evn, offX, offY){window.CMenus[menu].mpopup(evn, offX, offY);}
  349. function CMenuPopUpXY(menu,x,y){window.CMenus[menu].popup(x,y);}
  350. function domOnResize(){for (var i in CMenus) if (CMenus[i].rel) CMenus[i].move();}
  351. if (new bw_check().ns4) 
  352.     window.onresize=resizeHandler
  353. else
  354.     window.onresize=domOnResize;
  355.