// ****************************
// Initialize columnMenu object
// ****************************
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,$('SUBJECT:first',this).text(),$('DOCUMENT_ID:first',this).text(),itemID);
			m.content = $('BODY:first',this).text();
			if ($('MENU:first',this).children().length>0) {
				menuRef.processMenuChildrenItems(m, $('MENU:first',this), itemID);
			}
			menuRef.mi.push(m);
			itemID++;
		}
	);
}

columnMenu.prototype.setPageInstanceName = function(pageInstanceName) {
	this.pageInstanceName = pageInstanceName;
}

columnMenu.prototype.processMenuChildrenItems = function(child, xml, itemID) {
	var citemID=0;
	var menuRef = this;
	$(xml).children('PAGE').each(
		function() {
			if (jQuery.inArray($('PAGETYPE:first',this).text().toUpperCase(),['PRODUCT','SOLUTION','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());
				if ($('MENU:first',this).children().length>0) {
					menuRef.processMenuChildrenItems(m, $('MENU:first',this),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');
	}
}

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>'+p.title+'</h1>\n'+p.body+'\n');
	// Apply event handlers to accordion menu
	p.accordion.setup('originalAccordion');
}

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);
	location.href = "/DocController?documentId=" + ti.docID;
}

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.itemMenuClick(\''+itemInPathID+'\')">'+itemInPath.title+'</a>';
		}
	}
	breadcrumb += '<p>\n';
	// Display currently select content
	$('#content').html(breadcrumb+'<div id="am"></div><h1>'+currentItem.title+'</h1>'+currentItem.content);
	// 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) {
	this.parentMenu = parentMenu;
	this.title = title;
	this.docID = docID;
	this.content = syn;
	this.image = '';
	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');
}
