var highlightedNavBoxes = new Array();
var invalidPages = new Array();

/**
* Open the navBox with the content of the block.
*/
function openBlockNavbox(blockId) {

	if(!blockId) return;

	var navBoxId = blockId+'_navBox';
	
	var navBox = bb.document.getElementById(navBoxId);
	
	if(!navBox) {
		
		var lastIdxPipe = blockId.lastIndexOf('|');
		return openBlockNavbox(blockId.substr(0, lastIdxPipe));
		
	} else {

		navBox.setProperty('open',true);
	}
	
}

/**
* Close all navboxes below the parent ele.
*/
function closeNavboxes(parent, excludeNavBoxId) {

	var navBoxes = bb.evaluateSmart('[b:navBox]', parent);
	
	for(navBoxIdx=0; navBoxIdx < navBoxes.length; navBoxIdx++) {
	
		var navBox = navBoxes[navBoxIdx];

		if(navBox.getAttribute('id') != excludeNavBoxId && navBox.getAttribute('class') != 'help') {
			navBox.setProperty('open', false);
		}
	}
}

/**
* Highlight the navBox with the content of the block.
*/
function highlightBlockNavbox(blockId) {

	if(!blockId) return;
	
	while(highlightedNavBoxes.length > 0) {
		unhighlightBlockNavbox(highlightedNavBoxes[0]);
		highlightedNavBoxes.splice(0, 1);
	}
	
	var navBoxLabelId = blockId+'_navBoxLabel';
	
	var navBoxLabel = bb.document.getElementById(navBoxLabelId);
	
	if(!navBoxLabel) {
		
		var fieldLabelId = blockId+'_label';
		var fieldLabel = bb.document.getElementById(fieldLabelId);
		
		if(fieldLabel) {
			fieldLabel.viewNode.style.fontWeight = 'bold';	
		}
		
		var lastIdxPipe = blockId.lastIndexOf('|');
		return highlightBlockNavbox(blockId.substr(0, lastIdxPipe));
		
	} else {

		bounceNavbox(navBoxLabel);
	
		navBoxLabel.viewNode.style.fontWeight = 'bold';	
	}
	
	highlightedNavBoxes.push(blockId);
}

/**
* un-Highlight the navBox with the content of the block.
*/
function unhighlightBlockNavbox(blockId) {

	if(!blockId) return;

	var navBoxLabelId = blockId+'_navBoxLabel';
	
	var navBoxLabel = bb.document.getElementById(navBoxLabelId);

	if(!navBoxLabel) {
		
		var fieldLabelId = blockId+'_label';
		var fieldLabel = bb.document.getElementById(fieldLabelId);
		
		if(fieldLabel) {
			fieldLabel.viewNode.style.fontWeight = 'normal';	
		}
		
		var lastIdxPipe = blockId.lastIndexOf('|');
		return highlightBlockNavbox(blockId.substr(0, lastIdxPipe));
	} else {
	
		navBoxLabel.viewNode.style.fontWeight = 'normal';
	}

	for(i=0; i < highlightedNavBoxes.length; i++) {
		
		if(highlightedNavBoxes[i]==blockId) {
			highlightedNavBoxes.splice(i, 1);
			i--;
		}
	}
}

/**
* Set the validity of the navbox. 
*/
function setBlockNavboxValidity(navBox, valid, highlightInvalid) {

	var navBoxLabel = bb.evaluateSmart("*[contains(@id, 'Label')]", navBox);

	if(navBoxLabel) {
		var checkImg = bb.evaluateSmart("img[@class='check']", navBoxLabel);
		checkImg.viewNode.style.display = (valid ? 'inline': 'none');

		var warningImg = bb.evaluateSmart("img[@class='warning']", navBoxLabel);
		warningImg.viewNode.style.display = (valid == 'warning' ? 'inline': 'none');
		
		if(highlightInvalid) {
			if(!valid) {
				navBoxLabel.viewNode.style.color = 'red';
			}
			else {
				navBoxLabel.viewNode.style.color = '';
			}
		}
	}

}

/**
* Validate all forms on the page.
*/
function validatePages(highlightInvalid) {

	var doc = bb.document.getElementById('column-container');

	var windows = bb.evaluateSmart("[//b:window[@class='visual-navboxes']]", doc);
	
	var valid = true;
	
	invalidPages = new Array();
	
	for(windowIdx=0; windowIdx < windows.length; windowIdx++) {
	
		var window = windows[windowIdx];
		
		if(!validatePage(window, highlightInvalid)) {
			valid = false;
			invalidPages.push(windowIdx);
		}
	}
	
	return valid;
}

/**
* Validates all navBox form's on the current page.
*/
function validatePage(window, highlightInvalid) {
	
	var navBoxes = bb.evaluate("descendant::b:navBox[@class='visual-navbox']/@id", window.modelNode, window.modelNode);
	
	var pageIsValid = true;
	var pageHasWarning = false;
	
	for(navBoxIdx in navBoxes) {
        if(typeof navBoxes[navBoxIdx] == 'function') continue;

		var navBox = bb.document.getElementById(navBoxes[navBoxIdx].value);
		
		var form = bb.evaluateSmart("descendant::form[@class='contentForm']", navBox);
	
		var formIsValid = validatePrintForm(form, highlightInvalid);
		
		setBlockNavboxValidity(navBox, formIsValid, highlightInvalid);
	
		if(formIsValid == 'warning') {
			pageHasWarning = true;
		}
		
		pageIsValid = formIsValid && pageIsValid;
	}
	
	var idxOfUnderscore = window.getAttribute('id').indexOf('_');
	var pageNr = window.getAttribute('id').substr(idxOfUnderscore+1);
	
	var tabPage = bb.document.getElementById('tabPage_'+pageNr);
	
	var imgWarning = bb.evaluateSmart("id('visual-page-label-"+pageNr+"')//img[@class='warning']", tabPage, tabPage);
	var imgCheck = bb.evaluateSmart("id('visual-page-label-"+pageNr+"')//img[@class='check']", tabPage, tabPage);

	imgWarning.viewNode.style.display = (pageHasWarning ? 'inline' : 'none');
	imgCheck.viewNode.style.display = (pageIsValid ? 'inline' : 'none');
	
	if(highlightInvalid) {
		if(!pageIsValid) {
			bb.document.getElementById('visual-page-label-'+pageNr).viewNode.style.color = 'red';
		}
		else {
			bb.document.getElementById('visual-page-label-'+pageNr).viewNode.style.color = '';
		}
	}
	
	return pageIsValid;
}

/**
* Bounce the navbox
*/
function bounceNavbox(ele) {

	var bounce = new Object();
	bounce.attributeName = 'margin-left';
	bounce.dur = "80ms";
	bounce.to = '10px';
	
	bb.smil.animate(ele, bounce, function(){
	
		bounce.dur = "60ms";
		bounce.to = '0px';
		
		bb.smil.animate(ele, bounce);
		
	});
}

function moveInsertObject(fieldName, x, y) {
	
	var blockLLXHidden = bb.document.getElementById(fieldName+'!llx');
	var blockLLYHidden = bb.document.getElementById(fieldName+'!lly');
	
	var saveUrl = '/print/contents/saveblockparams/name/'+fieldName;
	
	if(blockLLXHidden) {
		blockLLXHidden.viewNode.value = x;
		saveUrl += '/llx/'+x;
	}
	
	if(blockLLYHidden) {
		blockLLYHidden.viewNode.value = y;
		saveUrl += '/lly/'+y;
	}
	
	bb.command.load(saveUrl, 'POST', '', null);
}

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}
