// <![CDATA[

function BDView(someXML)
{
	
	// Check for a redirection
	var redir = someXML.getElementsByTagName('redirectNow');
	if ( redir.length > 0 ) {
		window.location = redir[0].firstChild.nodeValue;
		return true;
	}
	
	// Objects (classic mode)
	var objects = someXML.getElementsByTagName('object');
	if ( objects.length > 0 ) {
		
		for ( var i = 0; i < objects.length; i++ ) {
			
			// Object id
			if ( objects[i].attributes.getNamedItem('id') == null ) {
				alert('Objects must have an id!');
			}
			var objectID = objects[i].attributes.getNamedItem('id').value;
			
			// Get the object action
			var objectAction = 'update';
			if ( objects[i].attributes.getNamedItem('action') != null ) {
				objectAction = objects[i].attributes.getNamedItem('action').value;
			}
			
			// Processing
			if ( ( objectAction == 'append' ) || ( objectAction == 'remove' ) ) {
				
				// Get transition
				var transition = (objectAction == 'append') ? 'appear' : 'fade';
				if ( objects[i].attributes.getNamedItem('transition') != null ) {
					transition = objects[i].attributes.getNamedItem('transition').value;
				}
					
				// Get transition duration
				var transitionDuration = 0.5;
				if ( objects[i].attributes.getNamedItem('transitionDuration') != null ) {
					transitionDuration = parseFloat(objects[i].attributes.getNamedItem('transitionDuration').value);
				}
				
				switch ( objectAction ) {
					
					case 'append':
						
						// Get parentID
						if ( objects[i].attributes.getNamedItem('parentID') != null ) {
							var parentID = objects[i].attributes.getNamedItem('parentID').value;
						} else {
							alert('parentID must be set to add an element!');
						}
						
						// Get object class
						var objectClass = false;
						if ( objects[i].attributes.getNamedItem('objectClass') != null ) {
							objectClass = objects[i].attributes.getNamedItem('objectClass').value;
						}
						
						// Get innerHTML tag
						var innerHTML = false;
						iHTMLs = objects[i].getElementsByTagName('innerHTML');
						if ( iHTMLs.length > 0 ) {
							innerHTML = iHTMLs[0].firstChild.nodeValue;
						}
						
						var foo = BDAddObjectWithTransition(parentID,objectID,objectClass,innerHTML,transition,transitionDuration);
						
						break;
						
					case 'remove':
						
						BDRemoveObjectWithTransition(objectID,transition,transitionDuration);
						
						break;
						
					default:
					
						alert('non implemented... yet');
					
				}
				
			} else {
			
				// Process html attributes
				// This don't work with event
				var htmlAttributes = objects[i].getElementsByTagName('htmlAttribute');
				for ( var hai = 0; hai < htmlAttributes.length; hai++ ) {
					var htmlAttributeName = htmlAttributes[hai].getElementsByTagName('htmlAttributeName')[0].firstChild.nodeValue;
					var htmlAttributeValue = htmlAttributes[hai].getElementsByTagName('htmlAttributeValue')[0].firstChild.nodeValue;
					eval('document.getElementById(\'' + objectID + '\').' + htmlAttributeName + ' = \'' + htmlAttributeValue + '\'');
				}
			
				// Process JS events
				var jsEvents = objects[i].getElementsByTagName('jsEvent');
				for ( var jse = 0; jse < jsEvents.length; jse++ ) {
					var jsEventName = jsEvents[jse].getElementsByTagName('jsEventName')[0].firstChild.nodeValue;
					var jsEventValue = jsEvents[jse].getElementsByTagName('jsEventValue')[0].firstChild.nodeValue;
					eval('document.getElementById(\'' + objectID + '\').' + jsEventName + ' = function() { ' + jsEventValue + ' };');
				}
			
				// CSS
			
				// Morph and morph duration
				var morph = false;
				var morphDuration = 0.5;
				var cssProperties = objects[i].getElementsByTagName('cssProperties');
				if ( cssProperties.length > 0 ) {
				
					if ( cssProperties[0].attributes.getNamedItem('morph') != null ) {
						
						if ( cssProperties[0].attributes.getNamedItem('morph').value == 'true' ) {
						
							morph = true;
					
							if ( cssProperties[0].attributes.getNamedItem('morphDuration') != null ) {
								morphDuration = parseFloat(cssProperties[0].attributes.getNamedItem('morphDuration').value);
							}
							
						}
					
					}
				
					// Process css properties
					var cssProperties = cssProperties[0].getElementsByTagName('cssProperty');
					var morphStyle = '';
					for ( var cpi = 0; cpi < cssProperties.length; cpi++ ) {
						var cssPropertyName = cssProperties[cpi].getElementsByTagName('cssPropertyName')[0].firstChild.nodeValue;
						var cssPropertyValue = cssProperties[cpi].getElementsByTagName('cssPropertyValue')[0].firstChild.nodeValue;
						if ( morph ) {
							morphStyle = morphStyle + ' ' + cssPropertyName + ': ' + cssPropertyValue + ';';
						} else {
							eval('document.getElementById(\'' + objectID + '\').style.' + cssPropertyName + '= \'' + cssPropertyValue + '\'');
						}
					}
					if ( morph && morphStyle != '' ) {
						new Effect.Morph(objectID, {style: morphStyle, duration: morphDuration});
					}
				
				}
			
				// Process innerHTML
				var iHTML = objects[i].getElementsByTagName('innerHTML');
				if ( iHTML.length > 0 ) {
					document.getElementById(objectID).innerHTML = iHTML[0].firstChild.nodeValue;
				}
			
				// Set JS global variables if any
				var jsVariables = objects[i].getElementsByTagName('jsVariable');
				for ( var jsvi = 0; jsvi < jsVariables.length; jsvi++ ) {
				
					var quoted = true;
					if ( jsVariables[jsvi].attributes.getNamedItem('quoted').value == "no" ) {
						quoted = false;
					}
				
					var jsVariableName = jsVariables[jsvi].getElementsByTagName('jsVariableName')[0].firstChild.nodeValue;
					var jsVariableValue = jsVariables[jsvi].getElementsByTagName('jsVariableValue')[0].firstChild.nodeValue;
					BDSetGlobalJSVariable(jsVariableName,jsVariableValue,quoted);
				
				}
			
			}
			
		}
	
		if ( currentContents != 'home' ) {
			ShowContents();
		}
		
	}
	
}

function BDAddObjectWithTransition(parentID,objectID,objectClass,objectInnerHTML,transition,transitionDuration)
{
	
	parentElement = document.getElementById(parentID);
	
	if ( parentElement != null ) {
		
		var newElement = document.createElement('div');
		newElement.setAttribute('id',objectID);
		
		if ( objectClass != false ) {
			newElement.setAttribute('class',objectClass);
		}
		
		newElement.setAttribute('style','display: none;');
		
		if ( objectInnerHTML != false ) {
			newElement.innerHTML = objectInnerHTML;
		} else {
			newElemnt.innerHTML = '';
		}
		
		parentElement.appendChild(newElement);
		
		switch ( transition ) {
			
			case 'Grow':
				
				$(objectID).grow({ duration: transitionDuration });
				
				break;
			
			case 'BlindDown':
			
				$(objectID).blindDown({ duration: transitionDuration });
				
				break;
				
			case 'SlideDown':

				$(objectID).slideDown({ duration: transitionDuration });

				break;
			
			default:
			
				$(objectID).appear({ duration: transitionDuration });
			
		}
		
		return true;
		
	}
	
	return false;
		
}

function BDRemoveObjectWithTransition(objectID,transition,transitionDuration)
{
	
	var obj = document.getElementById(objectID);
	
	if ( obj != null ) {
	
		switch ( transition ) {
			
			case 'Shrink':
				
				$(objectID).shrink({ duration: transitionDuration });
				
				break;
			
			case 'Fold':

				$(objectID).fold({ duration: transitionDuration });

				break;
				
			case 'BlindUp':

				$(objectID).blindUp({ duration: transitionDuration });

				break;
			
			case 'SlideUp':
			
				$(objectID).slideUp({ duration: transitionDuration });
				
				break;
				
			case 'Puff':
			
				$(objectID).puff({ duration: transitionDuration });
				
				break;
			
			case 'Squish':
			
				$(objectID).squish({ duration: transitionDuration });
				
				break;
			
			case 'SwitchOff':
			
				$(objectID).switchOff({ duration: transitionDuration });
				
				break;
				
			case 'DropOut':
			
				$(objectID).dropOut({ duration: transitionDuration });
				
				break;
				
			default:
			
				$(objectID).fade({ duration: transitionDuration });
			
		}
		
		setTimeout('BDRemoveObject(\'' + objectID + '\')',parseInt(transitionDuration)*1000 + 500);
		
	}
	
	return false;
	
}

function BDRemoveObject(objectID)
{
	
	var obj = document.getElementById(objectID)
	
	if ( obj != null ) {
		obj.parentNode.removeChild(obj);
		return true;
	}
	
	return false;
	
}

function BDSetGlobalJSVariable(globalVariableName,globalVariableValue,valueShouldBeQuoted)
{
	
	if ( valueShouldBeQuoted ) {
		eval('window.' + globalVariableName + ' = \'' + globalVariableValue + '\'');
	} else {
		eval('window.' + globalVariableName + ' = ' + globalVariableValue);
	}
	
}

// ]]>