// ****************************
// 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;

	// Generate Solutions Menu
	var sb = new menuItem(this,'Solutions for Your Business','',0,'<p>Please select a Mitel solution from the menu.</p>','');
	var itemID = 0;
	$('MENU:first',xml).each(function() {
		menuRef.processMenuChildrenItems(sb, this, itemID,'SOLUTION');
		itemID++;
	});
	this.mi.push(sb);
	
	// Generate Verticals Menu
	var si = new menuItem(this,'Solutions for Your Industry','',1,'<p>Please select your industry from the menu.</p>','');
	$('MENU:first',xml).each(function() {
		menuRef.processMenuChildrenItems(si, this, itemID,'VERTICAL');
		itemID++;
	});
	this.mi.push(si);
}

columnMenu.prototype.setPageInstanceName = function(pageInstanceName) {
	this.pageInstanceName = pageInstanceName;
}

columnMenu.prototype.processMenuChildrenItems = function(child, xml, itemID,type) {
	var citemID=0;
	var menuRef = this;
	$(xml).children('PAGE').each(
		function() {
			if ($('PAGETYPE:first',this).text().toUpperCase() == type) {
				var m = new menuItem(menuRef,$('SUBJECT:first',this).text(),$('DOCUMENT_ID:first',this).text(),itemID+'_'+citemID,$('SYNOPSYS:first',this).text(),$(this).children('IMAGE2:first').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) {
	// Set tabs mouseover events
	$('#t1').mouseover(
		function(){
			p.menu.itemMenuOver('0');
			$(this).addClass('tabOver');
			$('#t2').removeClass('tabOver');
		}
	);
	$('#t2').mouseover(
		function(){
			p.menu.itemMenuOver('1');
			$(this).addClass('tabOver');
			$('#t1').removeClass('tabOver');
		}
	);
	// Activate first tab
	$('#t1').addClass('tabOver');
	p.menu.itemMenuOver('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;');
	// Clear all selections
	$('#menu .itemButtonOver').removeClass('itemButtonOver');
	$('#menu .itemButtonSelected').removeClass('itemButtonSelected');
	// Reset cDisplayed
	this.cDisplayed = -1;
	// Output content to content area
	$('#ca #content').html('<h1 id="title">'+p.title+'</h1>\n<div id="body">'+p.body+'</div>\n');
	$('#t1').removeClass('tabOver');
	$('#t2').removeClass('tabOver');
}

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.docID!='') {
		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.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)
		$('#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,im) {
	this.parentMenu = parentMenu;
	this.title = title;
	this.docID = docID;
	this.content = syn;
	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="width:350px;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');
}
