


//
//  CentrixMenu Constructor 
//
//  usage:
//      var pepe = new CentrixMenu();
//
//


function CentrixMenu() {
    this.last_out = new Date();         // used to figure out the last mouse out operation
    this.out = 0;                       // flag to determine if mouse is out
    this.pressed = false;               // hmmm
    this.timeOut = 250;                 // timeout on which menu should hide after mouse gets out
    this.submenu = new Object();        // oh boy --- no freaking idea
    this.currentMenu = null;
	this.currentMenu2 = null;
}



CentrixMenu.prototype.isMenuOpen = function ( ) {
	return !( this.currentMenu == null && this.currentMenu2 == null );
}




// All menu operations use this single instance CentrixMenu
var CentrixMenu = new CentrixMenu();





//
//  Really complicated function...... shows something.... figure it out
//


function show ( e ) {

	//HideAllSelects();

    var menu = e.getElementsByTagName("UL");
    
    menu[0].style.display = "block";
    
    var label = e.getElementsByTagName("DIV");
    
	if ( label.length > 0 ) {
	
		if ( label[0].className == 'label' ) {
			e.style.backgroundColor = "#E6CE73";
			e.style.color= "black"; 		
			
			e.style.backgroundColor = "white";
			e.style.color= "black"; 		
			
		}
		
	}
	

    e.CentrixMenuOn = 1;
    
    
    CentrixMenu.out = 0;
	

}




//
//  Another masterpiece  it does the opposite of the previous function
//


function hide ( e ) {
		
	try {
	
		var menu = e.getElementsByTagName("UL");
		
		menu[0].style.display = "none";
		menu[0].style.backgroundImage = "";
		
		
		var label = e.getElementsByTagName("DIV");
		
		if ( label[0] ) {
			label[0].style.backgroundImage = "";
		}
					
					
		e.CentrixMenuOn = 0;
		
		
		e.style.backgroundColor = "";
		e.style.color = "";
		
		
		//CentrixMenu.currentMenu = e;
		CentrixMenu.currentMenu = null;
		
		

		hide2( CentrixMenu.currentMenu2 );
		
		// Intregracion con action tables
		if ( RowMarked != null ) {
			RowMarked.UnMark();
		}
		
		if ( RowSelected != null ) {
			RowSelected.UnSelect();
		}
		
	} catch ( ex ) { 

	}
}













function show2 ( e ) {


	var menu = e.getElementsByTagName("UL");
	
	menu[0].style.display = "block";
	
	e.className = "option-hover";

	
	CentrixMenu.currentMenu2  = e;

}


function hide2 ( e ) {
	
	//ShowAllSelects();
	
	try {
	
		var menu = e.getElementsByTagName("UL");
		
		menu[0].style.display = "none";
		menu[0].style.backgroundImage = "";
		
		
		e.className = "option";
		
		CentrixMenu.currentMenu2 = null;
		
	} catch ( ex ) {}
}




function menuClick( e ) {

    if ( CentrixMenu.pressed == true ) {
	
        if ( CentrixMenu.currentMenu != null ) {
            hide( CentrixMenu.currentMenu );
        }

        if ( e.parentNode.CentrixMenuOn == 1 ) {
            hide( e.parentNode );
        } else {
            show( e.parentNode );
			CentrixMenu.currentMenu  = e.parentNode;
        }
		
    }
}

function menuClick2( e ) {
	show2( e.parentNode );
}


        
function popup( e ) {

    if ( CentrixMenu.currentMenu != null ) {
        hide( CentrixMenu.currentMenu );
    }

    if ( e.parentNode.CentrixMenuOn == 1 ) {
        hide( e.parentNode );
        show( e.parentNode );
		CentrixMenu.currentMenu  = e;
    } else {
        show( e.parentNode );
		CentrixMenu.currentMenu  = e;
    }
	
}


//
//      Dont know why I did a function for this
//      Remove whenever possible :)
//


function subMenuPopup( e ) {
    e.style.display = "block";
}

        
function subMenuHide( e ) {
	e.style.display = "none";
}
        
        
//
//  Se debe correr onload para verificar si el mouse se salio de los menus
// 

function checkPopupMenu() {
    now = new Date();


    if ( now - window.CentrixMenu.last_out > CentrixMenu.timeOut && CentrixMenu.out == 1 ) {
        hide( CentrixMenu.currentMenu );
        CentrixMenu.pressed = false;
    }

    setTimeout("checkPopupMenu()", CentrixMenu.timeOut );
}


        
        
        
//
//   Funcion para inicializar los menu pull down de CMS ( top )
// 

function centrixMenuInit( id ) {


// 	var menu = null;
// 	
// 	if ( id == null ) {
// 		menu = document;
// 	} else {
// 		menu = document.getElementById( id );
// 	}
// 	


    var menu = document.getElementById( id );
    
    
    // Menu headers
    if ( menu != null ) {
    
        var menudivs = document.getElementsByTagName("DIV");
        
        for ( var n=0; n < menudivs.length; n++ ) { 
        
			if ( menudivs[n].className == "label" ) {
			
				var links = menudivs[n].parentNode.getElementsByTagName("A");
				
				for ( var x = 0; x < links.length; x++ ) {
				
// 					links[x].style.border = "1px solid red";
					links[x].onmouseover = function () {
						if ( CentrixMenu.currentMenu2 != this.parentNode.parentNode )
							hide2( CentrixMenu.currentMenu2 );
					}
					
				}
			
				menudivs[n].onmouseover = function () {
				
					menuClick( this );
					
				}
				
				
				menudivs[n].onclick = function () {
				
					if ( CentrixMenu.pressed == false ) {
						CentrixMenu.pressed = true;
						menuClick( this );
					} else {
						CentrixMenu.pressed = false;
						hide( CentrixMenu.currentMenu );
					}
				}
			}
			
			if ( menudivs[n].className == "option" ) {
			
				
				menudivs[n].onmouseover = function ( ev ) {
					if ( CentrixMenu.currentMenu2 != this ) {
						hide2( CentrixMenu.currentMenu2);
						show2( this );
					}
				}
			}
        }
		
 
		
		// Option bar buttons popup
		menudivs = null;
		
        menudivs = document.getElementsByTagName("DIV");
        
        for ( var n=0; n < menudivs.length; n++ ) { 
        
			if ( menudivs[n].className == "popup_trigger" ) {
			
				menudivs[n].onmouseover = function () {
				
					//menuClick( this );
					//this.onclick();
					
				}
				
				menudivs[n].onclick = function () {
				
					if ( CentrixMenu.pressed == false ) {
						CentrixMenu.pressed = true;
						menuClick( this );
					} else {
						CentrixMenu.pressed = false;
						hide( CentrixMenu.currentMenu );
					}
					
				}
			}
        }   
		
		
		
        
        
    }

	
        // Menu options
        
        var a = document.getElementsByTagName("A");
        
        for ( var n = 0; n < a.length; n++ ) {
        

			if ( a[n].getAttribute("confirm") != null ) {
			
				a[n].confirm = function () {
					return confirm( this.getAttribute("confirm") );
				}
				
			}
			
			if ( a[n].onclick != null ) {
				a[n].old_onclick = a[n].onclick;
			}
			
			a[n].onclick = function ( ev ) {  
			
				// 					try {
				// 						return this.old_onclick();
				// 					} catch (ex) {
				// 					}
							
				try { window.event.cancelBubble = true; } catch(ex) {}
				try { ev.cancelBubble  = true; } catch (ex) {}
				try { return this.confirm() } catch (ex) {}
									
				//return true;
			}
        }
	
	
	

        // Popup Menus
        
        var ul = document.getElementsByTagName("UL");
        
        for ( var n = 0; n < ul.length; n++ ) {
        
            ul[n].popup = function() {
                popup( this.parentNode );
            }
            
            ul[n].onmouseout = function () {
				try {
					CentrixMenu.last_out = new Date();
					CentrixMenu.out = 1;
				} catch (shtf) {}
            }
            
            ul[n].onmouseover = function () {
				try {
                CentrixMenu.out = 0;
				} catch (shtf) {}
            }
        }

		
        
}



