/* Set frame height to match frame contents */
function iFrameHeightX(iFrameId) {
	var h = 0;
	if ( !document.all ) {				// Firefox & Safari
		h = document.getElementById(iFrameId).contentDocument.height;
		h = (h < 200) ? h+100 : h;
		document.getElementById(iFrameId).style.height = h + 'px';
	} else if( document.all ) {			// IE6, IE7
		h = document.frames(iFrameId).document.body.scrollHeight;
		h = (h < 200) ? h+100 : h;
		document.all[iFrameId].style.height = h + 'px';
	}
}

/* Update the page title */
function pageTitleUpdate(iFrameId) {
	var loFrameDoc = getFrameDoc(iFrameId);
	var oldTitle = document.title;
	var newTitle = loFrameDoc.title;
	document.title = newTitle;
}

/* Update the bread crumb trail */
function breadCrumbUpdate(iFrameId) {
	var loFrameDoc = getFrameDoc(iFrameId);
	
	// Get Zen Cart and Joomla crumbs
	var loZenCartCrumbs = loFrameDoc.getElementById('navBreadCrumb');
	var loJoomlaCrumbs  = document.getElementById('breadcrumbs');

	// Check if there are any Joomla crumbs
	if (!loJoomlaCrumbs) {
		return;
	}

	// Get Joola crumb separator
	loJoomlaSeparator = loJoomlaCrumbs.getElementsByTagName('img')[0];
	lsJoomlaSeparator = '<img alt="" src="' + loJoomlaSeparator.src + '"/>';

	// Update Joomla with Zen Cart's crumbs
	lsZenCartHTML1 = loZenCartCrumbs.innerHTML.replace('target="_parent"', '');
	lsZenCartHTML2 = lsZenCartHTML1.replace(/shop\/cartage.php/g, 'cartage.html');
	lsZenCartHTML3 = lsZenCartHTML2.replace(/&nbsp;::&nbsp;/g, lsJoomlaSeparator);
	loJoomlaCrumbs.innerHTML = '<div class="wrapper floatholder"><span class="breadcrumbs">' + lsZenCartHTML3 + '</span></div>'; 
}

/* Common function to get iFrame document object */
function getFrameDoc(iFrameId) {
	var frameInfo = document.getElementById(iFrameId);
	var loFrameDoc = frameInfo.contentWindow ? frameInfo.contentWindow.document : frameInfo.contentDocument;
	
	if(!loFrameDoc && window.frames[iFrameId]) {
		loFrameDoc = window.frames[iFrameId].window.document;
	}
	return loFrameDoc;
}

/* Main function - calls all others that are needed */
function ZenCartLoaded(iFrameId) {
	iFrameHeightX(iFrameId);
	pageTitleUpdate(iFrameId);
	breadCrumbUpdate(iFrameId);
}

