// $Archive: /www/en/includes/js/common/navigation.js $
// $Author: Kiep $ 
// $Modtime: 11/19/04 11:51a $ 
// $Revision: 8 $  

var oNav = new collMenus();

//-----------------------------------------------------------------
// menu collection class
//-----------------------------------------------------------------
function collMenus() {
	//collection
	this.pages  = new Array();      //store-retrieve page object ref by number
	this.hPages = new Array();      //store-retrieve page object ref pagename
	this.menus = new Array();       //by number
	this.hMenus = new Array();      //by menu id (part1)
	
    //properties
    this.imgHighSuffix = "_a.gif";   //button image suffix to use when highlighted
    this.imgBottomSuffix = "_b.gif";  //button image suffix to use to display in bottom nav prompt
    this.href = "";
    
	//init
	this.addMenu = mAddMenu;
	this.addPage = mAddPage;
		
	//retrieval
	this.getCurrentPage = mGetCurrentPage;
	this.getHtml = mGetHtml;
	this.getPageByNumber = mGetPageByNumber;
	this.getMenuById = mGetMenuById;
	
	//utility
	this.getHrefFromLocation = mGetHrefFromLocation;
	this.formatItem = mFormatItem;
}


//-----------------------------------------------------------------
function mGetHrefFromLocation(href) {
	var aParts = href.split("/");
	var pageKey = aParts[aParts.length-1];
	
	return pageKey;	
}

//-----------------------------------------------------------------
function mGetCurrentPage(href) {
	//called when a new paged is loaded 
	//returns an objects representing that page
	
	//break the href on slash, use the last index
	var pageKey = this.getHrefFromLocation(href);
	

	
	//locate the page object in the hPages href
	var oPage;
	for (x in this.hPages) {
		if (x == pageKey) {
			oPage =this.hPages[x];
		}
		
	}

	return oPage;
}


//-----------------------------------------------------------------
function mGetPageByNumber(x) {
	//retrieve a page object by number
	if ( x < this.pages.length && x>-1) {
			return this.pages[x];
	}
}


//-----------------------------------------------------------------
function mGetMenuById(x) {
	return this.hMenus[x];
}

//-----------------------------------------------------------------
function mGetHtml(zhref) {
	//spit out the html to build the sidebar menu
	
	//only keep the last part
	zhref = this.getHrefFromLocation(zhref);
	
	var a ="" ;
	
	
	//loop through the menu objects
	for (var x=0; x<this.menus.length; x++) {
		a += this.menus[x].getHtml(zhref);
	}
	
	return a;
}


//-----------------------------------------------------------------
function mAddMenu(id,description, imageName, zhref) {


	//to add a menu to the model
	var oMenu = new clsMenu();
	oMenu.id = id;
	oMenu.description = description;
	//use an image instead of text to display menu
	
	
	
	if (typeof imageName != 'undefined' && imageName != "") {
		oMenu.useImage = true;
		oMenu.image = imageName;		
	}
    
	this.hMenus[id] = oMenu;	
	this.menus[this.menus.length] = oMenu;
    
    if (typeof zhref == 'string') {
        oMenu.href = zhref;
    } 
    
}


//-----------------------------------------------------------------
function mAddPage(id, parentId, zhref, textDescription, linkDescription, imageName, pageVisible) {
	//to add a page to the model
	var oPage = new clsPage();
	
	var oMenu = this.hMenus[parentId];
	
	oPage.id = id;
	oPage.pageNumber = this.pages.length;
	oPage.href = zhref;
	oPage.description = textDescription;
	
	if (typeof linkDescription != 'undefined' && linkDescription != "" ) {
		oPage.linkDescription = linkDescription;
	} else {
		oPage.linkDescription = textDescription;
	}
	if (typeof imageName != 'undefined' && imageName != "" ) {
		oPage.useImage = true;
		oPage.image = imageName;
	}
	if (typeof pageVisible != 'undefined' ) oPage.visible = pageVisible;
    
    
	oMenu.pages[oMenu.pages.length] = oPage;
	this.hPages[zhref] = oPage;
	this.pages[this.pages.length] = oPage;
	
    
    
}



//-----------------------------------------------------------------
function mFormatItem(rowClass,cellClass,href, vsize, bulletimg) {
	//utility function to format a menu item
	var a = "";
	var h= 4;
	if (typeof vsize != 'undefined') h = vsize;
	if (typeof bulletimg == 'undefined') bulletimg = "images/pix1.gif";	
	
	a += '<tr class="'+rowClass+'">';
	a += '<td><img src="images/pix1.gif" width="6" height="'+h+'"></td><td><img src="images/pix1.gif" width="139" height="1"></td></tr>'
	a += '<tr class="'+rowClass+'">';
	a += '	<td valign="top" align="center"><img src="images/pix1.gif" width="1" height="5" border="0"><br><img src="'+bulletimg+'" width="2" height="2" border="0"></td>';
	a += '<td class="'+cellClass+'">' + href + '</a></td></tr>';
	a += '<tr class="'+rowClass+'">';
	a += '	<td><img src="images/pix1.gif" width="6" height="'+(h-1)+'"></td><td><img src="images/pix1.gif" width="139" height="1"></td></tr>'
	a += '<tr bgcolor="#ffffff">';
	a += '	<td><img src="images/pix1.gif" width="6" height="1"></td><td><img src="images/pix1.gif" width="139" height="1"></td></tr>'	
	return a;
}

//-----------------------------------------------------------------
// end collMenus class
//-----------------------------------------------------------------



//-----------------------------------------------------------------
// page class
//-----------------------------------------------------------------
function clsPage() {
	//class that represents a page
	this.id = "";
	this.href = "";

	this.description = "";
	this.linkDescription = "";
	this.pageNumber = 0;
	this.useImage = false;
	this.image = "";
	this.visible = true;
    
	//methods
	this.prev = mPrev;
	this.next = mNext;
	this.getPageHtml = mGetPageHtml;
}

function mGetPageHtml(zhref) {
    if (! this.visible) return;
	var a = "";
	var cellClass = "navblue";
	var rowClass = "lvl2";
	var imgHref = this.image;
	
	if (this.href == zhref) {
		cellClass = "navwhite";
		rowClass = "lvl2high";
        if (this.useImage) {
            imgHref = imgHref.replace(".gif",oNav.imgHighSuffix);
            
        }
	}
    
    if ( this.useImage ) {
	    zlink = '<a href="'+this.href+'" class="'+cellClass+'"><img src="' + imgHref + '"  border="0"></a>';
    } else {
	    zlink = '<a href="'+this.href+'" class="'+cellClass+'">' + this.linkDescription + '</a>';
    }
    
    a += oNav.formatItem(rowClass, cellClass, zlink);
	
    
	return a;
    
}

function mPrev() {
	//get the href for
	var oPage = oNav.getPageByNumber(this.pageNumber-1);
	
	return (typeof oPage == 'undefined')? "" : oPage.href;
}

function mNext() {
	var oPage = oNav.getPageByNumber(this.pageNumber+1);
	
	return (typeof oPage == 'undefined')? "" : oPage.href;
	
}
//-----------------------------------------------------------------
// end page class
//-----------------------------------------------------------------




//-----------------------------------------------------------------
// menu class
//-----------------------------------------------------------------
function clsMenu() {
	//class that models a menu
	this.id = "";
	this.pages = new Array();
	this.numberOfItems = this.pages.length;
	this.description = "";
	this.useImage = false;
	this.image = "";
	this.getHtml = mGetMenuHtml;
    this.href = false;
	
}

function mGetMenuHtml(zhref) {
	var a  = "";
	var zlink = "";
	

	
	
	//now find out if we need to display our children
	var showPages = false;
	
	for (var x=0; x<this.pages.length; x++) {
		if (this.pages[x].href == zhref) {
			showPages=true;
		}
	}
	
    if ( zhref == this.href ) {
        showPages = true;
    }
    
    var tgtHref;
    if (! this.href) {
        tgtHref = this.pages[0].href;
    } else {
        tgtHref = this.href;
        
    }
    
	if (this.useImage) {
        zlink = '<a href="'+ tgtHref+'" class="navwhite"><img src="' + this.image + '"  border="0"></a>';
    } else {
        zlink = '<a href="'+tgtHref+'" class="navwhite">' + this.description + '</a>';
    
    }
	
	if (showPages) {
        //make a button for this menu
		
		a += oNav.formatItem("lvl1", "navwhite", zlink,"","images/bullet.gif");
		//loop in our pages
		for (var x=0; x<this.pages.length; x++) {
			var oPage = this.pages[x];
            if (this.pages[x].visible) a += this.pages[x].getPageHtml(zhref);
			var oPage = null;
		}
	} else {
		a += oNav.formatItem("lvl1", "navwhite", zlink);

	}
	
    
	return a;
    
}


//-----------------------------------------------------------------
// end menu class
//-----------------------------------------------------------------