/*

Author: Fa7c0n
Licence: Freeware GNU
Question & Info: ikjop@yahoo.com

rmcNav Version 1.0
Copyright (C) 2006 by Fa7c0n

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

ie = false
openedMnu = new Array(0);
function mnuCreateRmc(mnuId, mnuName, objId, mnuWidth, trans, imgs){
if (trans == "") trans = 100;
if (navigator.appName == "Microsoft Internet Explorer")  ie = true;  //ie
		mnuMainTable = document.createElement("table");
		if(mnuWidth > 0){
			mnuMainTableWidth = document.createAttribute("width");
			mnuMainTableWidth.value = mnuWidth;
			mnuMainTable.setAttributeNode(mnuMainTableWidth);
		}
		//give id to menu
		mnuMainTableId = document.createAttribute("id");
		mnuMainTableId.value = mnuId;
		mnuMainTable.setAttributeNode(mnuMainTableId);
		if(ie){
			mnuMainTable.style.filter = "alpha(opacity:"+trans+")";
		} else {
			mnuMainTable.style.opacity = (trans/100);
		}
	
		mnuMainTablePadding = document.createAttribute("cellpadding");
		mnuMainTablePadding.value = "1";
		mnuMainTable.setAttributeNode(mnuMainTablePadding);
	
		mnuMainTableSpacing = document.createAttribute("cellspacing");
		mnuMainTableSpacing.value = "0";
		mnuMainTable.setAttributeNode(mnuMainTableSpacing);
		
		mnuMainTable.style.display = "none";
		mnuMainTable.className = "mnuMainTable1"; //styles
		
		mnuMainTableBody = document.createElement("tbody");		
	
		mnuMainTable.appendChild(mnuMainTableBody);		
		document.body.appendChild(mnuMainTable);
		
		mnuMainTableRow  = new Array(mnuName.length);
		mnuMainTableCel  = new Array(mnuName.length);
		mnuSubTable  	 = new Array(mnuName.length);
		mnuSubTableBody  = new Array(mnuName.length);
		mnuSubTableRow   = new Array(mnuName.length);
		mnuSubTableCel   = new Array(mnuName.length);
		mnuSubTableCel2  = new Array(mnuName.length);
		mnuSubTableId	 = new Array(mnuName.length);
		//styles
		
	
		for(i=0;i<mnuName.length;i++){
		
			
			mnuMainTableRow[i]  = document.createElement("tr");
			mnuMainTableCel[i]  = document.createElement("td");
			
			mnuSubTable[i]		= document.createElement("table");
			//subtable props
			mnuSubTableWidth = document.createAttribute("width");
			mnuSubTableWidth.value = "100%";
			mnuSubTable[i].setAttributeNode(mnuSubTableWidth);
			
			mnuSubTablePadding = document.createAttribute("cellpadding");
			mnuSubTablePadding.value = "1";
			mnuSubTable[i].setAttributeNode(mnuSubTablePadding);
		
			mnuSubTableSpacing = document.createAttribute("cellspacing");
			mnuSubTableSpacing.value = "0";
			mnuSubTable[i].setAttributeNode(mnuSubTableSpacing);
			
			mnuSubTableBody[i]	= document.createElement("tbody");
			mnuSubTableRow[i]	= document.createElement("tr");
			if(imgs == true){
			mnuSubTableCel[i]	= document.createElement("td");
			mnuSubTableCelWidth = document.createAttribute("width");
			mnuSubTableCelWidth.value = "10%";
			mnuSubTableCel[i].setAttributeNode(mnuSubTableCelWidth);
			}
			mnuSubTableCel2[i] 	= document.createElement("td");	
			mnuSubTableCel2Width = document.createAttribute("width");
			mnuSubTableCel2Width.value = "90%";
			mnuSubTableCel2[i].setAttributeNode(mnuSubTableCel2Width);
			
			
			mnuSubTableId[i]	= document.createAttribute("id");
			mnuSubTableId[i].value = mnuId+"mnuCel"+i;  	
			mnuSubTable[i].setAttributeNode(mnuSubTableId[i]);
			
			//styles
			mnuSubTable[i].className = "menuSubTableNormal"; //style
	
			//building structure
	
			mnuMainTableBody.appendChild(mnuMainTableRow[i]);
			mnuMainTableRow[i].appendChild(mnuMainTableCel[i]);
			
			mnuMainTableCel[i].appendChild(mnuSubTable[i]);
			mnuSubTable[i].appendChild(mnuSubTableBody[i]);
			mnuSubTableBody[i].appendChild(mnuSubTableRow[i]);
			
			if((imgs == true) && (mnuName[i]["image"] != "-")){
			mnuSubTableRow[i].appendChild(mnuSubTableCel[i]);
			mnuSubTableCel[i].innerHTML = "<img src=\"" + mnuName[i]["image"] + "\"  />";
			}
			
			
			mnuSubTableRow[i].appendChild(mnuSubTableCel2[i]);
			
			if(mnuName[i]["image"] == "-"){
				mnuSubTableCel2[i].innerHTML = "<img src=\"rmvNavSpacer.jpg\" width=\"98%\" height=\"1\" />";
				mnuSubTableCel2[i].style.textAlign ="right";
			} else {
				mnuSubTableCel2[i].innerHTML = mnuName[i]["text"];
			}
		
			//events 
			if(mnuName[i]["collection_id"] == "-") {
                                var x = mnuName[i]["element_id"];
				mnuSubTable[i].onmouseover = function swapStyles(){ swapOverStyle(this.id,x,""); };
				mnuSubTable[i].onmouseout = function swapStyles2(){ swapOutStyle(this.id,x,""); };
			} else {
                                var x = mnuName[i]["element_id"];
				var y = mnuName[i]["collection_id"];
				mnuSubTable[i].onmouseover = function swapStyles(){ swapOverStyle(this.id,x,y); };
				mnuSubTable[i].onmouseout = function swapStyles2(){ swapOutStyle(this.id,x,y); };
			}
			mnuSubTable[i].onmousedown = function mnuGetUrl(){
				 mnuCelId = this.id;
				 
				 gotoUrl(this, mnuName[mnuCelId.replace(mnuId+"mnuCel","")]["link"]); 
				 };
		}	
		
		document.onmousedown = function mnuHideCaller(e) 
		
		{ 
			for(i=0;i<openedMnu.length; i++){
				mnuHide(e, openedMnu[i]);
				openedMnu.pop();
			}
		};
		
		document.getElementById(objId).onmouseup = function mnuShowCaller(e) { mnuShow(e, mnuId)}; 
		//DISABLE CONTEXTMENU
		document.getElementById(objId).oncontextmenu = function(){ return false};
		if(!ie){
			document.oncontextmenu = function(){ return false};
		}
	}

       // function to change the opacity of the the childnodes depending on the pattern to be matched with
       // the id of all child nodes

        function setOpacity(n,regpat,opacity) {                         
            if (n.id) {
                var pattern = new RegExp (regpat);
                var result = n.id.match(pattern);
                if (result != null)
//                    alert(n.id);
                    n.style.opacity = opacity;
            }

            var children = n.childNodes;                
            for(var i=0; i < children.length; i++) {    
                setOpacity(children[i],regpat,opacity);
            }
        }


	function swapOverStyle(divObject,element,collection){
		divObject = document.getElementById(divObject);
		if(divObject.innerHTML.indexOf("rmvNavSpacer.jpg") <= 0){
			divObject.className = "menuSubTableOver";
		}

                document.getElementById(element).style.opacity = 0.5;

                if (collection == "") { return; }
                
		if(ie){
			document.getElementById(collection).style.filter = "alpha(opacity:"+50+")";
		} else {
                        var kids = document.getElementById(collection).parentNode;
                        setOpacity(kids,"menu_caller","0.5");
		}
	}

        
	function swapOutStyle(divObject,element, collection){
		divObject = document.getElementById(divObject);
		divObject.className = "menuSubTableNormal";

//                document.getElementById(element).style.opacity = 1.0;

                if (collection == "") { return; }

		if(ie){
			document.getElementById(collection).style.filter = "alpha(opacity:"+50+")";
		} else {
                        var kids = document.getElementById(collection).parentNode;
                        setOpacity(kids,"menu_caller","1.0");
		}

	}
	
	function gotoUrl(divObject, getUrl){
	divObject = document.getElementById(divObject);
	if(getUrl != "-"){
		window.location = getUrl;
	} 
	}
	
	function mnuShow(e, divObject){
	openedMnu.push(divObject);
	divObject = document.getElementById(divObject);
	if (ie) {  //ie
		tempX = event.clientX + document.body.scrollLeft;
		tempY = (event.clientY) + document.documentElement.scrollTop;
		DisableButton = 2;
	}
	else {  // firefox
		tempX = e.pageX + "px";
		tempY = (e.pageY) + "px";
		DisableButton = 3;
	}  
	
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}  
	
	if((ie) && event.button == DisableButton){
		divObject.style.left = tempX;
		divObject.style.top = tempY;
		divObject.style.display = "block";
	} else if((!ie) && (e.which == DisableButton)){
		divObject.style.left = tempX;
		divObject.style.top = tempY;
		divObject.style.display = "block";
	}
}

function mnuHide(e, divObject){
	divObject = document.getElementById(divObject);
	if((!ie) && (e.which != 3)){
		divObject.style.display = "none";
	} else {
		divObject.style.display = "none";
	}
}	

function cloneMenu(menu1){
	menu2 = new Array(menu1.length);
	
	for(i=0;i<menu2.length;i++){
		menu2[i] = new Array(5);	
	}
	
	for(i=0;i<menu1.length;i++){
		menu2[i]["image"] = menu1[i]["image"];
		menu2[i]["text"] = menu1[i]["text"];
		menu2[i]["link"] = menu1[i]["link"];
		menu2[i]["element_id"] = menu1[i]["element_id"];
		menu2[i]["collection_id"] = menu1[i]["collection_id"];
	}
	return menu2;
}


function rmcMenu(elements){
	mnuArray = new Array(elements);
	
	for(i=0;i<mnuArray.length;i++){
		mnuArray[i] = new Array(5);	
	}
	
	return mnuArray;
}
