// ****************************
// Initialize columnMenu object
// ****************************
/*

  <REDIRECT_URL>http://www.mitel.com/DocController?documentId=25744</REDIRECT_URL> 
  <NEW_WINDOW>N</NEW_WINDOW> 

*/
function columnMenu(page, xml) {
	// Reference to the containing page object
	this.page = page;
	// Initialized currently displayed item
	this.cDisplayed = -1;
	// Initialize collection of menu items
	this.mi = [];

	// Reference to current columnMenu object
	var menuRef = this;
	var itemID = 0;

	$('MENU:first',xml).children('PAGE').each(
		function() {
			var m = new menuItem(menuRef,$(this).children('SUBJECT').text(),$(this).children('DOCUMENT_ID').text(),itemID,$(this).children('SYNOPSYS').text(),$(this).children('REDIRECT_URL').text(),$(this).children('NEW_WINDOW').text(),$(this).children('IMAGE2').text());
			if ($(this).children('MENU').length>0) {
				menuRef.processMenuChildrenItems(m, $(this).children('MENU'), itemID);
			}
			menuRef.mi.push(m);
			itemID++;
		}
	);
}

columnMenu.prototype.setPageInstanceName = function(pageInstanceName) {
	this.pageInstanceName = pageInstanceName;
}

function byTitle(a,b) {
	var aa, bb;
	aa = $('SUBJECT:first',a).text();
	bb = $('SUBJECT:first',b).text();
	if (aa<bb) return -1;
	if (aa>bb) return 1;
	return 0;
}

columnMenu.prototype.processMenuChildrenItems = function(child, xml, itemID) {
	var citemID=0;
	var menuRef = this;
	$(xml).children('PAGE').sort(byTitle).each(
		function() {
			if ( jQuery.inArray( $(this).children('PAGETYPE').text().toUpperCase(),['PRODUCT','PRODUCT CATEGORY','PRODUCT SUBCATEGORY'] ) != -1 ) {
				var m = new menuItem(menuRef,$('SUBJECT:first',this).text(),$('DOCUMENT_ID:first',this).text(),itemID+'_'+citemID,$('SYNOPSYS:first',this).text(),$('REDIRECT_URL:first',this).text(),$('NEW_WINDOW:first',this).text(),$(this).children('IMAGE2:first').text());
				if (($(this).children('MENU').length>0) && ($(this).children('PAGETYPE').text().toUpperCase() != 'PRODUCT')) {
					menuRef.processMenuChildrenItems(m, $(this).children('MENU'),itemID+'_'+citemID);
				}
				child.items.push(m);
				citemID++;
			}
		}
	);
}

columnMenu.prototype.setup = function(target) {
	// Create menu items in first column
	$('#'+target+' #c1').html('');
	for (var t=0; t<this.mi.length; t++) {
		this.mi[t].toHTML('#'+target+' #c1');
	}
	this.itemMenuOver('0');
	this.itemMenuOver('0_0');
	this.itemMenuOver('0_0_0');
}

columnMenu.prototype.findTargetItem = function(i) {
	if (i.indexOf('_')==-1) {
		return this.mi[i];
	} else {
		var splitItem = i.split('_');
		i = this.mi[splitItem[0]];
		for (var t=1; t<splitItem.length; t++) {
			i = i.items[splitItem[t]];
		}
		return i;
	}
}

columnMenu.prototype.returnToRoot = function() {
	// Clear columns 2 and 3
	$('#c2').html('&nbsp;');
	$('#c3').html('&nbsp;');
	// Clear all selections
	$('#menu .itemButtonOver').removeClass('itemButtonOver');
	$('#menu .itemButtonSelected').removeClass('itemButtonSelected');
	// Reset cDisplayed
	this.cDisplayed = -1;
	// Output content to content area
	$('#ca #content').html('<div id="originalAccordion"></div>\n<h1>Products</h1>\n'+p.body+'\n');
}

columnMenu.prototype.itemMenuOver = function(i) {
	//alert(this.cDisplayed + " " + i);
	if (this.cDisplayed==-1 || this.cDisplayed!=i) {
		// Add rollover to current menu item
		$('#i'+i).addClass('itemButtonOver');
		targetMenuItem = this.findTargetItem(i);
		$('#i'+targetMenuItem.itemID).addClass('itemButtonSelected');
		//targetMenuItem.menu = new accordionMenu(null,'rightNav');
		this.displayItemContent(targetMenuItem);
	}
}

columnMenu.prototype.itemMenuOut = function(i) {
	// Remove rollover to current menu item if not in cDisplayed path
	if (this.cDisplayed==-1 || this.cDisplayed.indexOf(i)!=0) {
		$('#i'+i).removeClass('itemButtonOver');
	} else {
		$('#i'+i).addClass('itemButtonSelected');
	}
}

columnMenu.prototype.itemMenuClick = function(i) {
	var ti = this.findTargetItem(i);
	if (ti.redirect=='') {
		location.href = "/DocController?documentId=" + ti.docID;
	} else {
		if (ti.newWindow=='N') {
			location.href = ti.redirect;
		} else {
			open_Window(ti.redirect);
		}
	}
}

columnMenu.prototype.displayItemContent = function(currentItem) {
	// Generate breadcrumb navigation for current item;
	var breadcrumb = '<p class="itemBreadcrumbs"><a href="javascript:p.menu.returnToRoot();">'+p.title+'</a>';
	if (currentItem.level==1) {
		var itemSplit = [currentItem.itemID];
	} else {
		var itemSplit = currentItem.itemID.split('_');
	}
	for (var t=0; t<itemSplit.length; t++) {
		var itemInPathID = [];
		for (var u=0; u<=t; u++) {
			itemInPathID.push(itemSplit[u]);
		}
		itemInPathID = itemInPathID.join('_');
		var itemInPath = this.findTargetItem(itemInPathID);
		// Add item to breadcrumb
		if (t==itemSplit.length-1) {
			breadcrumb += ' / '+itemInPath.title;
		} else {
			breadcrumb += ' / <a href="javascript:p.menu.itemMenuOver(\''+itemInPathID+'\')">'+itemInPath.title+'</a>';
		}
	}
	breadcrumb += '<p>\n';
	// Display currently select content
	var itembody = '<div id="am"></div><h1 id="title">'+currentItem.title+'</h1><div id="body">';
	if (currentItem.image != '') {
		itembody += '<img src="/images/'+currentItem.image+'" alt="'+currentItem.title+'" style="float:right;margin-left:20px;margin-bottom:20px" />';
	}
	itembody += currentItem.content;
	itembody += '</p><h3><a href="javascript:void(0);" onclick="p.menu.itemMenuClick(\''+currentItem.itemID+'\')">More...</a></h3>'+'</div>';
	$('#content').html(breadcrumb+itembody);
	
	// Apply event handlers to accordion menu
	//currentItem.menu.setup('am');
	// Does current item have any children items?
	if (currentItem.items.length==0) {
		// No - clear columns for all subsquent levels
		if (currentItem.level == 1) {
			$('#c'+(currentItem.level+1)).html('&nbsp;');
			$('#c'+(currentItem.level+2)).html('&nbsp;');
		} else {
			$('#c'+(currentItem.level+1)).html('&nbsp;');
		}
	} else {
		// Yes - Clear empty menu columns (based on item level - clear all columns above item's level+1)
		if (currentItem.level == 1) {
			$('#c3').html('&nbsp;');
		}
		$('#c'+(currentItem.level+1)).html('');
		// Populate menu child items
		for (var t=0; t<currentItem.items.length; t++) {
			currentItem.items[t].toHTML('c'+(currentItem.level+1));
		}
	}
	// Remove rollover and selected class on all menu items
	$('#menu .itemButtonOver').removeClass('itemButtonOver');
	$('#menu .itemButtonSelected').removeClass('itemButtonSelected');
	// Add rollover on all menu items in currentItem's path
	if (currentItem.level==1) {
		var itemSplit = [currentItem.itemID];
	} else {
		var itemSplit = currentItem.itemID.split('_');
	}
	for (var t=0; t<itemSplit.length; t++) {
		var itemInPathID = [];
		for (var u=0; u<=t; u++) {
			itemInPathID.push(itemSplit[u]);
		}
		itemInPathID = itemInPathID.join('_');
		// Add rollover on current level's selected item
		$('#i'+itemInPathID).addClass('itemButtonSelected');
	}
	this.cDisplayed = currentItem.itemID;
}

// **************************
// Initialize menuItem object
// **************************
function menuItem(parentMenu,title,docID,itemID,syn,red,nw,im) {
	this.parentMenu = parentMenu;
	this.title = title;
	this.docID = docID;
	this.content = syn;
	this.redirect = red;
	this.newWindow = nw;
	this.image = im;
	this.menu = null;
	this.loaded = false;
	this.items = [];
	this.itemID = itemID.toString();
	this.level = 1;
	if (this.itemID.indexOf("_")!=-1) {
		var splitItem = this.itemID.split('_');
		this.level = splitItem.length;
	}
}

menuItem.prototype.toHTML = function(container) {
	var s='';
	s+='<div class="itemButton" style="position:relative;" id="i'+this.itemID+'" ';
	s+='onmouseover="p.menu.itemMenuOver(\''+this.itemID+'\')" ';
	s+='onmouseout="p.menu.itemMenuOut(\''+this.itemID+'\')" ';
	s+='onclick="p.menu.itemMenuClick(\''+this.itemID+'\')">';
	s+='<div class="itemContent"><a href="javascript:void(0);">'+this.title+'</a></div></div>';
	$('#'+container).append(s);
	$('#i'+this.itemID).hide();
	$('#i'+this.itemID).slideDown('fast');
}