/** Image Object **/
/*
function Image(id)
{
	
	if(id == undefined)
	{
		return;
	}
	
	this.id = id;
	this.element = $('img#' + this.id);
	
	var extension = '';
	if (this.element.attr('src')) {
		extension = this.element.attr('src').split('.').pop();
	} else {
		extension = '';
	}
	var id_name = '';
	var pl = this.id.toString();

	if (pl.indexOf('_') > 0) {
		id_name = this.id.split('_')[0];
	}
	
	
	this.normal = getJQueryImage(id_name + '_normal.' + extension);
	this.over = getJQueryImage(id_name + '_over.' + extension);
	this.selected = getJQueryImage(id_name + '_selected.' + extension);
}

Image.prototype.setNormalState = function()
{
	this.element.attr('src', ((this.id.search('selected') == -1) ? this.normal : this.selected).attr('src'));
}

Image.prototype.setOverState = function()
{
	if(this.id.search('selected') != -1)
	{
		return;
	}
	this.element.attr('src', this.over.attr('src'));
}
*/
function submitToSocialEngine(engine){
	local_url=window.location;
	switch(engine){
	case 'facebook':
		url = 'http://www.facebook.com/share.php?u=';
		break;
	case 'twitter':
		var location = String(window.location);
		location = "location" + location.split("location").pop();
		var locationParts = location.split("_");
		var pageSubNumber = 0;
		var pageItemNumber = 0;
		pageRootNumber = locationParts[1];
		if(locationParts[2])
			pageSubNumber = locationParts[2];
		if(locationParts[3])
			pageItemNumber = locationParts[3];
		url = 'http://twitter.com/home?status=http://www.bemoneyconfident.com/cms/pages/permalink/'+pageRootNumber + '_' + pageSubNumber + '_' + pageItemNumber;		
		window.location = url;
		return; 
		break;
	case 'delicious':
		url = 'http://del.icio.us/post?url=';		
		break;
	case 'stumble':
		url = 'http://www.stumbleupon.com/submit?url=';	
		break;
	case 'digg':
		url = 'http://digg.com/submit?phase=2&url=';	
		break;
	}
	
	window.location = url+local_url;
	
}

function getJQueryImage(src)
{
	return $('<img />').attr('src', 'img/' + src);
}

/** Content Menu Model Objects & Functions */

function hasChildren()
{
	return (this.children == undefined) ? false : true;
}

function addChild(child)
{
	if(this.hasChildren() === false)
	{
		this.children = new Array();
	}
	this.children.push(child);
}

function getChildren()
{
	return this.children;
}

function getElement()
{
	return $('div#' + this.element);
}

function getID()
{
	return this.id;
}

function getXMLFile()
{
	return this.xmlFile;
}

function getName()
{
	return this.name;
}

function getBannerImage()
{
	return this.bannerImage;
}

function getCalculatorText()
{
	return this.calculatorText;
}
function getCalculatorUrl()
{
	return this.calculatorUrl;
}

function getBannerURL()
{
	return this.bannerURL;
}


function TopMenuModel(id, element, xmlFile, name, bannerImage, bannerURL, stylesheet)
{
	this.id = id;
	this.element = element;
	this.xmlFile = xmlFile;
	this.name = name;
	this.bannerImage = bannerImage;
	this.bannerURL = bannerURL;
	this.stylesheet = stylesheet;
}

function SubMenuModel(id, element, xmlFile, name, bannerImage, bannerURL)
{
	this.id = id;
	this.element = element;
	this.xmlFile = xmlFile;
	this.name = name;
	this.bannerImage = bannerImage;
	this.bannerURL = bannerURL
}

function ItemModel(id, xmlFile, bannerImage, bannerURL)
{
	this.id = id;
	this.xmlFile = xmlFile;
	this.bannerImage = bannerImage;
	this.bannerURL = bannerURL;
}

TopMenuModel.prototype.hasChildren = hasChildren;
TopMenuModel.prototype.addChild = addChild;
TopMenuModel.prototype.getChildren = getChildren;
TopMenuModel.prototype.getElement = getElement;
TopMenuModel.prototype.getID = getID;
TopMenuModel.prototype.getXMLFile = getXMLFile;
TopMenuModel.prototype.getName = getName;
TopMenuModel.prototype.getBannerImage = getBannerImage;
TopMenuModel.prototype.getBannerURL = getBannerURL;

TopMenuModel.prototype.getStylesheet = function()
{
	return this.stylesheet;
}

SubMenuModel.prototype.hasChildren = hasChildren;
SubMenuModel.prototype.addChild = addChild;
SubMenuModel.prototype.getChildren = getChildren;
SubMenuModel.prototype.getElement = getElement;
SubMenuModel.prototype.getID = getID;
SubMenuModel.prototype.getXMLFile = getXMLFile;
SubMenuModel.prototype.getName = getName;
SubMenuModel.prototype.getBannerImage = getBannerImage;
SubMenuModel.prototype.getBannerURL = getBannerURL;

ItemModel.prototype.getID = getID;
ItemModel.prototype.getXMLFile = getXMLFile;
ItemModel.prototype.getBannerImage = getBannerImage;
ItemModel.prototype.getBannerURL = getBannerURL;

/** Animation Model Object **/

function AnimationModel(object, height, duration)
{
	this.object = object;
	this.height = height;
	this.duration = duration;
}

AnimationModel.prototype.getObject = function()
{
	return this.object;
}

AnimationModel.prototype.getHeight = function()
{
	return this.height;
}

AnimationModel.prototype.getDuration = function()
{
	return this.duration;
}

/** Ajax Content Model Object **/
function ContentModel(contentXML, contentBanner)
{
	this.contentXML = contentXML;
	this.contentBanner = contentBanner;
}

/** Global variables **/

var globalAnimationPlaying;
var globalLastKnownLocation = "";
var globalAnimationLocationQueue;
var globalLoadingContent;
var globalAnimationLocationQueueIterator;

var globalContentSectionTotals = 7;
var globalContentPreload;
var globalShowContentBanner;


/** Anonymous Functions **/
var resizeCount = 0;
function resize()
{
	var clientHeight = document.documentElement.clientHeight;  /* Height of browser window */
	var documentHeight = $(document).height(); /* Height of HTML document */
	var height = (documentHeight <= clientHeight) ? clientHeight : documentHeight; /* Use window height if document is smaller than window */
	$('div.container').height(height);
	if($.browser.msie && $.browser.version < 7)
	{
		$('div.containerInner').height(height);
	} 
	
	if(pageRootNumber == 7 && pageSubNumber == 6 && pageItemNumber == 2) {
		if(resizeCount == 0) {
			setTimeout('resize()','600');
			resizeCount++;
		} else {
			resizeCount = 0;
		}
	}
}

function preloadContentImages()
{
	globalContentPreload = new Array();
	
	var container_background = 'containerBackground_';
	var contentMenu_top = 'contentMenuTop_';
	var contentMenu_bottom = 'contentMenuBottom_';
	var contentMenuBranch_top = 'contentMenuBranchTop_';
	var contentMenuBranch_sub = 'contentMenuBranchSub_';
	var contentMenuBranch_item = 'contentMenuBranchItem_';
	var contentMenuBranch_selected = 'contentMenuSelected_';
	
	var container_background_i;
	var contentMenu_top_i;
	var contentMenu_bottom_i;
	var contentMenuBranch_top_i;
	var contentMenuBranch_sub_i;
	var contentMenuBranch_item_i;
	var contentMenuBranch_selected_i;
	
	for(var i = 1; i <= globalContentSectionTotals; i++)
	{
		container_background_i = getJQueryImage(container_background + i + ".jpg");
		contentMenu_top_i = getJQueryImage(contentMenu_top + i + ".png");
		contentMenu_bottom_i = getJQueryImage(contentMenu_bottom + i + ".png");
		contentMenuBranch_top_i = getJQueryImage(contentMenuBranch_top + i + ".jpg");
		contentMenuBranch_sub_i = getJQueryImage(contentMenuBranch_sub + i + ".jpg");
		contentMenuBranch_item_i = getJQueryImage(contentMenuBranch_item + i + ".jpg");
		contentMenuBranch_selected_i = getJQueryImage(contentMenuBranch_selected + i + ".jpg");
		
		globalContentPreload.push
		(
			container_background_i, 
			contentMenu_top_i, 
			contentMenu_bottom_i, 
			contentMenuBranch_top_i, 
			contentMenuBranch_sub_i,
			contentMenuBranch_item_i,
			contentMenuBranch_selected_i
		);
	}
}

function redirectPage(page, newWindow)
{
	if(newWindow === true)
	{
		window.open(page)
		return;
	}
	if($.browser.msie)
	{
		window.location.href(page);
	}
	else
	{
		window.location.href = page;
	}
}

/* Content Functions */

function initContentNavigation()
{
	collapseNavigation();
	setInterval(urlNavigationChanged, 10);
}

function urlNavigationChanged()
{
	var location = String(window.location);
	if(location.indexOf('location') == -1)
	{
		return;
	}
	location = "location" + location.split("location").pop();
	navigate(location);
	
}

function collapseNavigation()
{
	
	var childrenRootLevel;
	var childrenSubLevel;
	
	for(var i = 0; i < globalListRootLevel.length; i++)
	{
		if(globalListRootLevel[i].hasChildren())
		{
			childrenRootLevel = globalListRootLevel[i].getChildren();
			for(var x = 0; x < childrenRootLevel.length; x++)
			{
				if(childrenRootLevel[x].hasChildren())
				{
					childrenSubLevel = childrenRootLevel[x].getChildren();
					for(var y = 0; y < childrenSubLevel.length; y++)
					{
						childrenSubLevel[y].getElement().hide();
					}
				}
				childrenRootLevel[x].getElement().hide();
			}
		}
	}
}

var locationRootLevel; /* Set in global scope to use elsewhere in the system */
function navigate(location)
{	
	if(globalAnimationPlaying || location == globalLastKnownLocation || globalLoadingContent)
	{
		return;
	}
	
	buildBreadCrumbs(location);
	
	var locationParts = location.split("_");
	locationParts.shift();
	
	locationRootLevel = locationParts[0];
	var locationSubLevel = locationParts[1];
	
	/* Reset selected menu title */
	setSelectedMenuTitle();
	
	var lastKnownLocationParts = globalLastKnownLocation.split("_");
	lastKnownLocationParts.shift();
	
	var lastKnownLocationRootLevel = lastKnownLocationParts[0];
	var lastKnownLocationSubLevel = lastKnownLocationParts[1];
	
	globalLastKnownLocation = location;
	
	loadContent(location);
	
	if(locationSubLevel && lastKnownLocationSubLevel && (locationSubLevel == lastKnownLocationSubLevel))
	{
		return;
	}
	
	globalAnimationLocationQueue = new Array();	

	var objectRootLevel;
	var objectSubLevel;
	var objectItemLevel;
	
	var heightRootLevel;
	var heightSubLevel;
	var heightItemLevel;
	
	var heightModifierOpened = 19;
	var heightModifierClosed = 0;

	var durationRootLevel = 250;
	var durationSubLevel = 250;
	var durationItemLevel = 250;
	
	var childrenRootLevel;
	var childrenSubLevel;
	
	var childrenRootLevelLen;
	var childrenSubLevelLen;
	
	var openQueue = new Array();
	var closeQueue = new Array();
	
	if(locationRootLevel && lastKnownLocationRootLevel)
	{
		objectRootLevel = getObjectFromListByID(locationRootLevel, globalListRootLevel);
		if(objectRootLevel)
		{
			if(locationRootLevel == lastKnownLocationRootLevel)
			{
				if(lastKnownLocationSubLevel && locationSubLevel != lastKnownLocationSubLevel)
				{
					if(objectRootLevel.hasChildren())
					{
						childrenRootLevel = objectRootLevel.getChildren();
						objectSubLevel = getObjectFromListByID(lastKnownLocationSubLevel, childrenRootLevel);
						if(objectSubLevel)
						{
							if(objectSubLevel.hasChildren())
							{
								childrenSubLevel = objectSubLevel.getChildren();
								for(var i = 0; i < childrenSubLevel.length; i++)
								{
									objectItemLevel = childrenSubLevel[i];
									closeQueue.push(new AnimationModel(objectItemLevel, heightModifierClosed, durationItemLevel));
								}
							}
							closeQueue.push(new AnimationModel(objectSubLevel, heightModifierOpened, durationSubLevel));
							closeQueue.push(new AnimationModel(objectRootLevel, (childrenRootLevel.length + 1) * heightModifierOpened, durationRootLevel));
						}
					}
				}
				
				if(locationSubLevel)
				{
					if(objectRootLevel.hasChildren())
					{
						childrenRootLevel = objectRootLevel.getChildren();
						objectSubLevel = getObjectFromListByID(locationSubLevel, childrenRootLevel);

						if(objectSubLevel && objectSubLevel.hasChildren())
						{
							heightRootLevel = (childrenRootLevel) ? childrenRootLevel.length * heightModifierOpened : heightModifierOpened;
							
							childrenSubLevel = objectSubLevel.getChildren();
							heightSubLevel = (childrenSubLevel) ? (childrenSubLevel.length + 1) * heightModifierOpened : heightModifierOpened;
							heightRootLevel += heightSubLevel;
							
							openQueue.push(new AnimationModel(objectRootLevel, heightRootLevel, durationRootLevel));
							openQueue.push(new AnimationModel(objectSubLevel, heightSubLevel, durationSubLevel));

							for(var i = 0; i < childrenSubLevel.length; i++)
							{
								objectItemLevel = childrenSubLevel[i];
								openQueue.push(new AnimationModel(objectItemLevel, heightModifierOpened, durationItemLevel));
							}
						}

					}
				}

			}
			else
			{
				objectRootLevel = getObjectFromListByID(lastKnownLocationRootLevel, globalListRootLevel);

				if(objectRootLevel)
				{
					if(objectRootLevel.hasChildren())
					{
						childrenRootLevel = objectRootLevel.getChildren();
						for(var i = 0; i < childrenRootLevel.length; i++)
						{
							objectSubLevel = childrenRootLevel[i];
							
							if(lastKnownLocationSubLevel && objectSubLevel.getID() == lastKnownLocationSubLevel && objectSubLevel.hasChildren())
							{
								childrenSubLevel = objectSubLevel.getChildren();
								for(var x = 0; x < childrenSubLevel.length; x++)
								{
									objectItemLevel = childrenSubLevel[x];
									closeQueue.push(new AnimationModel(objectItemLevel, heightModifierClosed, durationItemLevel));
								}
							}
							
							closeQueue.push(new AnimationModel(objectSubLevel, heightModifierClosed, durationSubLevel));
							
						}
					}
					
					closeQueue.push(new AnimationModel(objectRootLevel, heightModifierOpened, durationRootLevel));
					
					objectRootLevel = getObjectFromListByID(locationRootLevel, globalListRootLevel);

					if(objectRootLevel)
					{
						if(objectRootLevel.hasChildren())
						{
							childrenRootLevel = objectRootLevel.getChildren();
						}
						
						heightRootLevel = (childrenRootLevel) ? (childrenRootLevel.length + 1) * heightModifierOpened : heightModifierOpened;
						openQueue.push(new AnimationModel(objectRootLevel, heightRootLevel, durationRootLevel));
						
						if(childrenRootLevel)
						{
							
							for(var i = 0; i < childrenRootLevel.length; i++)
							{
								objectSubLevel = childrenRootLevel[i];
								if(locationSubLevel && objectSubLevel.getID() == locationSubLevel && objectSubLevel.hasChildren())
								{
									
									childrenSubLevel = objectSubLevel.getChildren();
									heightSubLevel = (childrenSubLevel) ? childrenSubLevel.length * heightModifierOpened : heightModifierOpened;
									
									openQueue.push(new AnimationModel(objectSubLevel, heightSubLevel + heightModifierOpened, durationSubLevel));
									openQueue.push(new AnimationModel(openQueue[0].getObject(), openQueue[0].getHeight() + heightSubLevel, durationRootLevel));
									
									for(var x = 0; x < childrenSubLevel.length; x++)
									{
										objectItemLevel = childrenSubLevel[x];
										openQueue.push(new AnimationModel(objectItemLevel, heightModifierOpened, durationItemLevel));
									}
									
								}
								else
								{
									openQueue.push(new AnimationModel(objectSubLevel, heightModifierOpened, durationSubLevel));
								}
							}
						}
						
					}
					
				}		
			}	
		}
	}
	else
	{
		objectRootLevel = getObjectFromListByID(locationRootLevel, globalListRootLevel);

		if(objectRootLevel)
		{

			if(objectRootLevel.hasChildren())
			{
				childrenRootLevel = objectRootLevel.getChildren();
			}
			
			heightRootLevel = (childrenRootLevel) ? (childrenRootLevel.length + 1) * heightModifierOpened : heightModifierOpened;
			openQueue.push(new AnimationModel(objectRootLevel, heightRootLevel, durationRootLevel));
			
			if(childrenRootLevel)
			{
				
				for(var i = 0; i < childrenRootLevel.length; i++)
				{
					objectSubLevel = childrenRootLevel[i];
					if(locationSubLevel && objectSubLevel.getID() == locationSubLevel && objectSubLevel.hasChildren())
					{
						
						childrenSubLevel = objectSubLevel.getChildren();
						heightSubLevel = (childrenSubLevel) ? childrenSubLevel.length * heightModifierOpened : heightModifierOpened;
						
						openQueue.push(new AnimationModel(objectSubLevel, heightSubLevel + heightModifierOpened, durationSubLevel));
						openQueue.push(new AnimationModel(openQueue[0].getObject(), openQueue[0].getHeight() + heightSubLevel, durationRootLevel));
						
						for(var x = 0; x < childrenSubLevel.length; x++)
						{
							objectItemLevel = childrenSubLevel[x];
							openQueue.push(new AnimationModel(objectItemLevel, heightModifierOpened, durationItemLevel));
						}
						
					}
					else
					{
						openQueue.push(new AnimationModel(objectSubLevel, heightModifierOpened, durationSubLevel));
					}
				}
			}
			
	
		}
		
	}
	
	if(closeQueue.length > 0)
	{
		globalAnimationLocationQueue.push(closeQueue);
	}
	
	if(openQueue.length > 0)
	{
		globalAnimationLocationQueue.push(openQueue);
	}	
	
	animationLocationQueueRunner();
}

function getObjectFromListByID(id, list)
{
	for(var i = 0; i < list.length; i++)
	{
		if(list[i].getID() == id)
		{
			return list[i];
		}
	}
}
var bg_colorcode = new Array(7);
function setSelectedMenuTitle(){
	for(i=1;i<=7;i++) {
		if(i == locationRootLevel) {
			$('#container_location_' + locationRootLevel).css('background-image','url(/img/contentMenuBranchSelected_'+locationRootLevel+'.jpg)');
			$('#container_location_' + i + ' > p > a').css("color","black");
		} else {
			$('#container_location_' + i).css('background-image','url(/img/contentMenuBranchTop_'+locationRootLevel+'.jpg)');
			$('#container_location_' + i + ' > p > a').css("color","white");
		}
	}

	var bg_colorcode = new Array(7);
	bg_colorcode[1] = '#e8d463';
	bg_colorcode[2] = '#73cda7';
	bg_colorcode[3] = '#81ca85';
	bg_colorcode[4] = '#f2a464';
	bg_colorcode[5] = '#ced956';
	bg_colorcode[6] = '#82e3da';
	bg_colorcode[7] = '#ac83df';

	//$('div.footer').css('background-color',bg_colorcode[locationRootLevel]);
	$('div.contentMenuSub').css('background-image','url(/img/contentMenuBranchSub_'+locationRootLevel+'.jpg)');
	$('.contentMenuTopSection').css('background-image','url(/img/contentMenuTop_'+locationRootLevel+'.png)');
	$('.contentMenuBottomSection').css('background-image','url(/img/contentMenuBottom_'+locationRootLevel+'.png)');
	
    var cssObj = {
            'background-color' : bg_colorcode[locationRootLevel],
            'background-image' : 'url(/img/containerBackground_'+locationRootLevel+'.jpg)'
    }
    $('div.containerInner').css(cssObj);
	
	
	// $('div.containerInner').css('background', 'url(img/containerBackground_'+locationRootLevel+'.jpg) no-repeat scroll 0 0');
	
	var colorcode = new Array(7);
	colorcode[1] = '#b18201';
	colorcode[2] = '#2c7d6b';
	colorcode[3] = '#268645';
	colorcode[4] = '#e76501';
	colorcode[5] = '#7a9301';
	colorcode[6] = '#82e3da';
	colorcode[7] = '#673c96';
	
	$('div.contentXMLTitle').css('color',colorcode[locationRootLevel]);
	$('div.contentMenuInnerContainer').css('background-color',colorcode[locationRootLevel]);
	$('div.contentBreadCrumb').css('background-color',colorcode[locationRootLevel]);
	
}

function menuHover(element,level){
	
	switch(level){
	case 'top':	
		$('#' + element.id).css('background-image','url(img/contentMenuBranchSelected_'+locationRootLevel+'.jpg)');
		$('#' + element.id + ' > p > a').css("color","black");
		break;
	case 'sub':
		$('#' + element.id).css('background-image','url(img/contentMenuBranchSubRollover_'+locationRootLevel+'.jpg)');
		break;
	case 'third':
		$('#' + element.id).css('background-image','url(img/contentMenuBranchItemRollover_'+locationRootLevel+'.jpg)');
		break;
	}
	
}
function menuOut(element,level){
	switch(level){
		case 'top':	
			if(element.id != 'container_location_'+locationRootLevel) {
				$('#' + element.id ).css('background-image','url(img/contentMenuBranchTop_'+locationRootLevel+'.jpg)');
				$('#' + element.id + ' > p > a').css("color","white");
			}
			break;
		case 'sub':
			$('#' + element.id ).css('background-image','url(img/contentMenuBranchSub_'+locationRootLevel+'.jpg)');
			break;
		case 'third':
			$('#' + element.id ).css('background-image','url(img/contentMenuBranchItem_'+locationRootLevel+'.jpg)');
			break;
	}
}

function animationLocationQueueRunner()
{
	if(globalAnimationLocationQueue.length > 0)
	{
		globalAnimationPlaying = true;
		globalAnimationLocationQueueIterator = 0;
		var queue = globalAnimationLocationQueue[0];
		for(var i = 0; i < queue.length; i++)
		{
			var element = queue[i].getObject().getElement();
			
			var height = queue[i].getHeight();
			var duration = queue[i].getDuration();
			element.animate({height: height}, {queue: true, duration: duration, complete: animationLocationQueueRunnerComplete}, 'swing');
		}
	}
	else
	{
		
		globalAnimationPlaying = false;
		
		var locationParts = globalLastKnownLocation.split("_");
		locationParts.shift();
		
		var objectRootLevel = getObjectFromListByID(locationParts[0], globalListRootLevel);
		if(!objectRootLevel)
		{
			return;
		}
		
		$('link[@rel*=style][title]').each
		(
			function()
			{
				if(this.getAttribute('title') == 'menuStyle')
				{
					this.setAttribute('href', '/css/' + objectRootLevel.getStylesheet());
					return;
				}
			}
		);
		
		resizeContent();
		
	}
}

function animationLocationQueueRunnerComplete()
{
	globalAnimationLocationQueueIterator++;
	if(globalAnimationLocationQueueIterator == globalAnimationLocationQueue[0].length)
	{
		var queue = globalAnimationLocationQueue[0];
		for(var i = 0; i < queue.length; i++)
		{
			if(queue[i].getHeight() == 0)
			{
				queue[i].getObject().getElement().hide();
			}
		}
		globalAnimationLocationQueue.shift();
		animationLocationQueueRunner();
	}
}

function buildBreadCrumbs(location)
{
	if(location == globalLastKnownLocation && location != undefined)
	{
		return;
	}
	var locationParts = location.split("_");
	locationParts.shift();
	
	var breadCrumb = new Array(getBreadCrumbLink('Start'));
	var breadCrumbLocation = 'location_';
	
	var contentTitle = '';
	
	var objectRootLevel = getObjectFromListByID(locationParts[0], globalListRootLevel);
	var nameRootLevel;
	var nameSubLevel;
	var nameItemLevel;
	if(objectRootLevel)
	{
		nameRootLevel = contentTitle = objectRootLevel.getName();
		breadCrumbLocation += locationParts[0];
		breadCrumb.push(getBreadCrumbLink(nameRootLevel, breadCrumbLocation));
		
		if(locationParts[1] && objectRootLevel.hasChildren())
		{
			var objectSubLevel = getObjectFromListByID(locationParts[1], objectRootLevel.getChildren());
			if(objectSubLevel)
			{
				nameSubLevel = contentTitle = objectSubLevel.getName();
				breadCrumbLocation += '_' + locationParts[1];
				breadCrumb.push(getBreadCrumbLink(nameSubLevel, breadCrumbLocation));
				if(locationParts[2] && objectSubLevel.hasChildren())
				{
					objectItemLevel = getObjectFromListByID(locationParts[2], objectSubLevel.getChildren());
					if(objectItemLevel)
					{
						nameItemLevel = contentTitle = objectItemLevel.getName();
						breadCrumbLocation += '_' + locationParts[2];
						breadCrumb.push(getBreadCrumbLink(nameItemLevel, breadCrumbLocation));
					}
				}
			}
		}
	}
	
	$('span.contentBreadCrumb').html(breadCrumb.join(' > '));
	$('div.contentXMLTitle').html(contentTitle);
	
	var tkpageName = 'microsite:bmc';
	var tkhier1 = 'microsite|bmc';
	var tkchannel = tkpageName;
	var tkprop3 = tkpageName;
	if (nameRootLevel) {
		tkpageName += ':' + nameRootLevel;
		tkhier1 += '|' + nameRootLevel;
		tkchannel += ':' + nameRootLevel; 
	}
	if (nameSubLevel) {
		tkpageName += ':' + nameSubLevel;
		tkhier1 += '|' + nameSubLevel;
		tkprop3 += ':' + nameSubLevel;
	}
	if (nameItemLevel) {
		tkpageName += ':' + nameItemLevel;
		tkhier1 += '|' + nameItemLevel;
	}
	oar(tkpageName.toLowerCase(), tkchannel.toLowerCase(), tkprop3.toLowerCase(), tkhier1.toLowerCase());
	
}

function oar(pagename, channel, prop3, hier1){
        // Beginning of AJAX code here
		/*s.un="anzcom-cms-dev"; //Development Report Suite - Remove on Launch*/
		s.prop1="microsite";	// Division
		s.prop2="microsite:bmc";
        s.pageName=pagename;
        s.channel=channel;
        s.prop3=prop3;
		s.hier1=hier1;
        void(s.t());
}



function getBreadCrumbLink(name, location)
{
	if(!location)
	{
		return '<a href="/" class="breadCrumb">' + name + '</a>';
	}
	return '<a href="#noanchor?' + location + '" class="breadCrumb">' + name + '</a>'
}

var bannerImage;
var bannerURL;
function loadContent(location)
{
	bannerImage = null;
	bannerURL = null;
	globalLoadingContent = true;
	
	var xmlFile;

	
	var itemObject = getObjectFromListByID(location, globalListHidden);
	
	if(itemObject)
	{
		xmlFile = itemObject.getXMLFile();
	}
	
	if(!xmlFile)
	{
	
		var locationParts = location.split("_");
		locationParts.shift();
		
		var locationRootLevel = locationParts[0];
		var locationSubLevel = locationParts[1];
		var locationItemLevel = locationParts[2];
		
		if(!locationRootLevel)
		{
			globalLoadingContent = false;
			return;
		}
		
		var objectRootLevel = getObjectFromListByID(locationRootLevel, globalListRootLevel);
		if(objectRootLevel)
		{
			if(locationSubLevel && objectRootLevel.hasChildren())
			{
				var objectSubLevel = getObjectFromListByID(locationSubLevel, objectRootLevel.getChildren());
				if(objectSubLevel)
				{
					if(locationItemLevel && objectSubLevel.hasChildren())
					{
						var objectItemLevel = getObjectFromListByID(locationItemLevel, objectSubLevel.getChildren());
						if(objectItemLevel)
						{
							xmlFile = objectItemLevel.getXMLFile();
							bannerImage = objectItemLevel.getBannerImage();
							bannerURL = objectItemLevel.getBannerURL();
						}
					}
					if(!xmlFile)
					{
						xmlFile = objectSubLevel.getXMLFile();
					}
					if(!bannerImage)
					{
						bannerImage = objectSubLevel.getBannerImage();
						bannerURL = objectSubLevel.getBannerURL();
					}
				}
			}
			if(!xmlFile)
			{
				xmlFile = objectRootLevel.getXMLFile();
			}
			if(!bannerImage)
			{
				bannerImage = objectRootLevel.getBannerImage();
				bannerURL = objectRootLevel.getBannerURL();
			}
		}
	}
	
	if(!bannerImage)
	{
		globalShowContentBanner = false;
		// $('div#contentRightBanner').css('visibility', 'hidden');
	}
	else
	{
		globalShowContentBanner = true;
		// $('img#bannerImage').attr('src', bannerImage);
		// $('a#bannerLink').attr('href', bannerURL)
	}

	if(!xmlFile)
	{
		globalLoadingContent = false;
		return;
	}
	
	$("div.contentXML").html('<br/><img src="img/loader_2.gif" /><br/><br/>Loading, please Wait ...<br />');
	//$('div.contentRight').css('visibility', 'hidden');
	resize();
	ajaxLoadContentRequest(xmlFile);
	renderNewSidebar();
}

function ajaxLoadContentRequest(xmlFile)
{
	
	var data = 'xmlFile=' + xmlFile;
	$.ajax
	({
		type: "POST",
		url: '/proc.contentLoad.php',
/*		url: '/cms/site_contents', */
/*		url: 'http://localhost/bmc/development/bemc/proc.contentLoad.php',*/
		data: data,
		success: ajaxLoadContentResponse
	});
			
}
function ajaxLoadContentResponse(response)
{
	
	globalLoadingContent = false;
	var element_contentXML = $("div.contentXML");
	element_contentXML.html(response);	
	
	resizeContent();
	
	if(globalShowContentBanner === true)
	{
		$('div.contentRightBanner').css('visibility', 'visible');
	}
}

function resizeContent()
{
	
	var element_contentContainer = $('div.contentContainer');
	var top_contentContainer = Number(element_contentContainer.css('top').split('px').shift());
	
	var height_contentXML = $('div.contentXML').height();
	var height_contentMenuContainer = $('div.contentMenuContainer').height();
	var height = ((height_contentXML < height_contentMenuContainer) ? height_contentMenuContainer : height_contentXML) + top_contentContainer;
	element_contentContainer.height(height);
	//$('div.container').height(height);
	resize();
}

// this function is to support legacy codebase from the xml content pulled back
function internalPageLink(pageLink)
{
	var location = String(window.location);
	if(location.indexOf('location') == -1)
	{
		return;
	}
	window.location = location.split("location").shift() + "location_" + pageLink;
}

// this function is to support legacy codebase from teh xml content pulled back
function internalAnchorLink(anchor)
{
	var url = String(window.location);
	var urlParts = url.split("#");
	window.location = urlParts[0] + "#" + anchor;
	window.location +=  "?" + urlParts[1].split('?').pop();
}

function getNumberFromPixel(pixels)
{
	return pixels.split('px').shift();
}

/** home navigation functions **/
function animateGrowNavigationItem(element)
{
	
	var duration = 150;
	
	var element_parent = element.parent();
	var element_img = element_parent.find('img.homeNavigationItem');
	var element_container = element_parent.find('div.homeNavigationItemContainer');
	var element_slider = element_container.find('div.homeNavigationItemContainerSlider');
	
	element.height(200);
	element_img.stop().animate({height: 200, width: 200, left: -25, top: -25}, {queue: false, duration: duration}, 'swing');
	element_container.stop().animate({top: 145}, {queue: false, duration: duration}, 'swing');
	element_slider.slideDown(duration);
	
}

function animateShrinkNavigationItem(element)
{
	
	var duration = 150;
	
	var element_parent = element.parent();
	var element_img = element_parent.find('img.homeNavigationItem');
	var element_container = element_parent.find('div.homeNavigationItemContainer');
	var element_slider = element_container.find('div.homeNavigationItemContainerSlider');

	element.height(125);
	element_img.stop().animate({height: 150, width: 150, left: 0, top: 0}, {queue: false, duration: duration}, 'swing');
	element_container.stop().animate({top: 125}, {queue: false, duration: duration}, 'swing');
	element_slider.slideUp(duration);
	
}

/** DOC NAV NAVIGATION ITEM **/
var globalDocNavItems = new Array();

function registerDocNavItem(element)
{
	globalDocNavItems.push(element.parent().attr('id'));
}

function animateDocNav(element)
{
	animateGrowNavigationItem(element);
	
	
}

// PageLoad function
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser
function pageload(hash) {
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		if($.browser.msie) {
			// jquery's $.load() function does't work when hash include special characters like едц.
			hash = encodeURIComponent(hash);
		}
		$(".contentXMLContainer").load(hash);

	} else {
		// start page
		$(".contentXMLContainer").empty();
	}
	
}

/** Document Ready Handler **/
$(document).ready
(
	function()
	{
		if(!isNaN(locationRootLevel)) {
			// Initialize history plugin.
			// Only do this for non FF browsers, FF is fine its just IE who fail to record the anchor history
			if($.browser.msie) {
	
				// The callback is called at once by present location.hash. 
				$.historyInit(pageload, "content.php");
				
				// set onlick event for buttons
				$("a[rel='history']").click(function(){
					// 
					var hash = this.href;
					hash = hash.replace(/^.*#/, '');
					
					// moves to a new page. 
					// pageload is called at once. 
					// hash don't contain "#", "?"
					$.historyLoad(hash);
					//return false;
				});
			}
		}
		if($('div.contentMenuContainer').attr('class') != undefined)
		{
			preloadContentImages();
			initContentNavigation();
		}
		
		var element_contentLogo = $('img.contentLogo');
		if(element_contentLogo.attr('class') != undefined)
		{
			element_contentLogo.click
			(
				function()
				{
					redirectPage('/');
				}
			);
		}
	
		if($('div.navbar').attr('class') != undefined)
		{
			$('#mnagi').Fisheye
			(
				{
					maxWidth: 70,
					items: 'a',
					itemsText: 'span',
					container: 'div.layer',
					itemWidth: 130,
					proximity: 100,
					alignment : 'left',
					valign: 'bottom',					
					halign : 'center'
				}
			);
			
		}
		
		
		resize();
	}
);

$(window).resize
(
	function()
	{
		resize();
	}
);