/* --------------------------------------------------
	1. Specs Configuration window
-------------------------------------------------- */

var SpecBrowser = {
	
	config: {

		dotNetDoubleClickHandlerAdd:undefined,
		dotNetDoubleClickHandlerRemove:undefined,
		available: "" ,
		selected: "" , 
		field: "",
		useButtons: true
	}, 

	timer: {
		infoBubble: null
	},

	selected: {
		current: null
	}, 

	readValue: function( )
	{
		var selectedList = document.getElementById( SpecBrowser.config.selected );
		if( !selectedList ) return;
		selectedList = selectedList .getElementsByTagName( "UL" );
		if( selectedList .length <= 0 ) return;
		selectedList = selectedList [0];
		var selectedField = document.getElementById( SpecBrowser.config.field );
		if( selectedField == undefined || selectedList == undefined ) return false;
		selectedField.value = "";
		var selection = new Array( );
		
		for( var i = 0; i < selectedList.childNodes.length; i++ )
		{
			if( selectedList.childNodes[i].nodeType != 1 || selectedList.childNodes[i].nodeName != "LI" ) continue;
			var thisValue = selectedList.childNodes[i].getElementsByTagName( "SPAN" )[0].innerHTML;
			selection.push( encodeURIComponent( thisValue ) );
		}
		
		selectedField.value = selection.join( "," );
	},
	
	sortList: function( )
	{
		var selectedList = document.getElementById( SpecBrowser.config.selected );
		if( !selectedList ) return;
		selectedList = selectedList .getElementsByTagName( "UL" );
		if( selectedList .length <= 0 ) return;
		selectedList = selectedList[0];
		selectedListChildren = selectedList.getElementsByTagName( "LI" );
		var selection = new Array( );
		var selectionNodes = new Object();
		
		var availableList = document.getElementById( SpecBrowser.config.available );
		if( !availableList) return;
		availableList = availableList.getElementsByTagName( "UL" );
		if( availableList.length <= 0 ) return;
		availableList = availableList[0];
		availableListChildren = availableList.getElementsByTagName( "LI" );
		var available = new Array( );
		var availableNodes = new Object();
		
		for( var i = 0; i < selectedListChildren.length; i++ )
		{
			var thisValue = selectedListChildren[i].getElementsByTagName( "SPAN" )[0].innerHTML;
			selection.push( encodeURIComponent( thisValue ) );
			selectionNodes[encodeURIComponent( thisValue )] = selectedListChildren[i];
			selectedList.removeChild( selectedListChildren[i] ); i--;
		}

		for( var i = 0; i < availableListChildren.length; i++ )
		{
			var thisValue = availableListChildren[i].getElementsByTagName( "SPAN" )[0].innerHTML;
			available.push( encodeURIComponent( thisValue ) );
			availableNodes[encodeURIComponent( thisValue )] = availableListChildren[i];
			availableList.removeChild( availableListChildren[i] ); i--;
		}
		
		selection = selection.sort( );
		available = available.sort( );
		
		for( var i = 0; i < selection.length; i++ )
		{
			selectedList.appendChild( selectionNodes[selection[i]] );
		}
		
		for( var i = 0; i < available.length; i++ )
		{
			availableList.appendChild( availableNodes[available[i]] );
		}
	}
};

General.event.register( window , 'load' , function( e ){
	SpecBrowser.readValue( );
} );

/* 
  Click on the 'browse up' button */
General.event.register( document , 'click' , function( e ){
	var theNode = e.targetNode;
	if( theNode.nodeName != "A" || theNode.className.indexOf( "move-up" ) == -1 ) return true;
	if( theNode.parentNode == undefined ) return true;
	e.cancelEvent( );
	var theParent = theNode.parentNode;
	if( theParent.className.indexOf( "options" ) == -1 ) return false;
	if( theParent.parentNode.className.indexOf( "specs-browser" ) == -1 ) return false;
	
	var theChildren = theParent.getElementsByTagName( "UL" );
	if( theChildren.length <= 0 ) return;
	var theBrowser = theChildren[0];
	if( ( theBrowser.scrollTop - 40 ) > 0 )
	{
		theBrowser.scrollTop -= 48;
	}
	
	return false;
} );

/* 
  Click on the 'browse down' button */
General.event.register( document , 'click' , function( e ){
	var theNode = e.targetNode;
	if( theNode.nodeName != "A" || theNode.className.indexOf( "move-down" ) == -1 ) return true;
	if( theNode.parentNode == undefined ) return true;
	e.cancelEvent( );
	var theParent = theNode.parentNode;
	if( theParent.className.indexOf( "options" ) == -1 ) return false;
	if( theParent.parentNode.className.indexOf( "specs-browser" ) == -1 ) return false;
	
	var theChildren = theParent.getElementsByTagName( "UL" );
	if( theChildren.length <= 0 ) return;
	var theBrowser = theChildren[0];

	if( ( theBrowser.scrollTop + 40 ) < theBrowser.scrollHeight )
	{
		theBrowser.scrollTop += 48;
	}
	
	return false;
} );

/*
  Remove all options from the selected window */
General.event.register( document , 'click' , function( e ){
		
	var theNode = e.targetNode;
	if( theNode.nodeName != "A" || theNode.className.indexOf( "remove-all" ) == -1 ) return true;
	if( theNode.parentNode == undefined ) return true;
	if( SpecBrowser.config.useButtons == true ) e.cancelEvent( );
	var theParent = theNode.parentNode;
	if( theParent.className.indexOf( "specs-controls" ) == -1 ) return false;
	var availableList = document.getElementById( SpecBrowser.config.available );
	if( !availableList ) return;
	availableList = availableList.getElementsByTagName( "UL" );
	if( availableList.length <= 0 ) return;
	availableList = availableList[0];
	var selectedList = document.getElementById( SpecBrowser.config.selected );
	if( !selectedList ) return;
	selectedList = selectedList .getElementsByTagName( "UL" );
	if( selectedList .length <= 0 ) return;
	selectedList = selectedList [0];

	if( availableList == undefined || selectedList == undefined ) return false;
		
	for( var i = 0; i < selectedList.childNodes.length; i++ )
	{
		if( selectedList.childNodes[i].nodeType != 1 || selectedList.childNodes[i].nodeName != "LI" ) continue;
		selectedList.childNodes[i].className = "";
		selectedList.childNodes[i].style.borderTopColor = "";
		availableList.appendChild( selectedList.childNodes[i] );
		i--;
	}
	
	var firstNode = selectedList.childNodes[0];
	if( firstNode )
	{
		if( firstNode.nodeType != 1 ) firstNode = firstNode.nextSibling;
		if( firstNode && ( firstNode.nodeType == 1 && firstNode.nodeName == "LI" ) )
		{
			firstNode.className = "first";
		}
	}
	
	var lastNode = selectedList.childNodes[selectedList.childNodes.length-1];
	if( lastNode )
	{
		if( lastNode.nodeType != 1 ) lastNode = lastNode.previousSibling;
		if( lastNode && ( lastNode.nodeType == 1 && lastNode.nodeName == "LI" ) )
		{
			lastNode.className = "last";
		}
	}
	
	availableList.scrollTop = 0;
	selectedList.scrollTop = 0;
	SpecBrowser.readValue( );
	SpecBrowser.sortList( );
	if( SpecBrowser.config.useButtons == false ) return true;
	return false;
} );

/*
  Remove the selected option from the selected window */
General.event.register( document , 'click' , function( e ){
	var theNode = e.targetNode;
	if( theNode.nodeName != "A" || theNode.className.indexOf( "remove-selected" ) == -1 ) return true;
	if( theNode.parentNode == undefined ) return true;
	if( SpecBrowser.config.useButtons == true ) e.cancelEvent( );
	var theParent = theNode.parentNode;
	if( theParent.className.indexOf( "specs-controls" ) == -1 ) return false;
	var availableList = document.getElementById( SpecBrowser.config.available );
	if( !availableList ) return;
	availableList = availableList.getElementsByTagName( "UL" );
	if( availableList.length <= 0 ) return;
	availableList = availableList[0];
	var selectedList = document.getElementById( SpecBrowser.config.selected );
	if( !selectedList ) return;
	selectedList = selectedList .getElementsByTagName( "UL" );
	if( selectedList .length <= 0 ) return;
	selectedList = selectedList [0];
	if( availableList == undefined || selectedList == undefined ) return false;
	
	if( !SpecBrowser.selected.current || !SpecBrowser.selected.current.parentNode ) return false;
	if( SpecBrowser.selected.current.parentNode == availableList ) return false;
	
	/* Next sibling of current selected should be turned back to normal */
	var nextNode = SpecBrowser.selected.current.nextSibling;
	if( nextNode )
	{
		if( nextNode.nodeType != 1 ) nextNode = nextNode.nextSibling;
		if( nextNode && nextNode.nodeType == 1 ) 
		{
			nextNode.style.borderTopColor = "";
		}
	}
	
	SpecBrowser.selected.current.className = SpecBrowser.selected.current.className.replace( "selected" , "" );
	availableList.appendChild( SpecBrowser.selected.current );
	SpecBrowser.selected.current = "";
	
	var firstNode = selectedList.childNodes[0];
	if( firstNode )
	{
		if( firstNode.nodeType != 1 ) firstNode = firstNode.nextSibling;
		if( firstNode && ( firstNode.nodeType == 1 && firstNode.nodeName == "LI" ) )
		{
			firstNode.className = "first";
		}
	}
	
	var lastNode = selectedList.childNodes[selectedList.childNodes.length-1];
	if( lastNode )
	{
		if( lastNode.nodeType != 1 ) lastNode = lastNode.previousSibling;
		if( lastNode && ( lastNode.nodeType == 1 && lastNode.nodeName == "LI" ) )
		{
			lastNode.className = "last";
		}
	}
	
	availableList.scrollTop = 0;
	selectedList.scrollTop = 0;
	
	SpecBrowser.readValue( );
	SpecBrowser.sortList( );
	if( SpecBrowser.config.useButtons == false ) return true;
	return false;
} );

/*
  Add all available options */
General.event.register( document , 'click' , function( e ){
	
	var theNode = e.targetNode;
	if( theNode.nodeName != "A" || theNode.className.indexOf( "add-all" ) == -1 ) return true;
	if( theNode.parentNode == undefined ) return true;
	if( SpecBrowser.config.useButtons == true ) e.cancelEvent( );
	var theParent = theNode.parentNode;
	if( theParent.className.indexOf( "specs-controls" ) == -1 ) return false;
	var availableList = document.getElementById( SpecBrowser.config.available );
	if( !availableList ) return;
	availableList = availableList.getElementsByTagName( "UL" );
	if( availableList.length <= 0 ) return;
	availableList = availableList[0];
	var selectedList = document.getElementById( SpecBrowser.config.selected );
	if( !selectedList ) return;
	selectedList = selectedList .getElementsByTagName( "UL" );
	if( selectedList .length <= 0 ) return;
	selectedList = selectedList [0];
	if( availableList == undefined || selectedList == undefined ) return false;
	
	for( var i = 0; i < availableList.childNodes.length; i++ )
	{
		if( availableList.childNodes[i].nodeType != 1 || availableList.childNodes[i].nodeName != "LI" ) continue;
		availableList.childNodes[i].className = "";
		availableList.childNodes[i].style.borderTopColor = "";
		selectedList.appendChild( availableList.childNodes[i] );
		i--;
	}
	
	var firstNode = selectedList.childNodes[0];
	if( firstNode )
	{
		if( firstNode.nodeType != 1 ) firstNode = firstNode.nextSibling;
		if( firstNode && ( firstNode.nodeType == 1 && firstNode.nodeName == "LI" ) )
		{
			firstNode.className = "first";
		}
	}
	
	var lastNode = selectedList.childNodes[selectedList.childNodes.length-1];
	if( lastNode )
	{
		if( lastNode.nodeType != 1 ) lastNode = lastNode.previousSibling;
		if( lastNode && ( lastNode.nodeType == 1 && lastNode.nodeName == "LI" ) )
		{
			lastNode.className = "last";
		}
	}
	
	availableList.scrollTop = 0;
	selectedList.scrollTop = 0;

	SpecBrowser.readValue( );
	SpecBrowser.sortList( );
	if( SpecBrowser.config.useButtons == false ) return true;
	return false;
} );

/*
  Add the select available option */
General.event.register( document , 'click' , function( e ){
	var theNode = e.targetNode;
	if( theNode.nodeName != "A" || theNode.className.indexOf( "add-selected" ) == -1 ) return true;
	if( theNode.parentNode == undefined ) return true;
	if( SpecBrowser.config.useButtons == true ) e.cancelEvent( );
	var theParent = theNode.parentNode;
	if( theParent.className.indexOf( "specs-controls" ) == -1 ) return true;
	var availableList = document.getElementById( SpecBrowser.config.available );
	if( !availableList ) return;
	availableList = availableList.getElementsByTagName( "UL" );
	if( availableList.length <= 0 ) return;
	availableList = availableList[0];
	var selectedList = document.getElementById( SpecBrowser.config.selected );
	if( !selectedList ) return;
	selectedList = selectedList .getElementsByTagName( "UL" );
	if( selectedList .length <= 0 ) return;
	selectedList = selectedList [0];
	if( availableList == undefined || selectedList == undefined ) return true;
	if( !SpecBrowser.selected.current || !SpecBrowser.selected.current.parentNode ) return false;
	if( SpecBrowser.selected.current.parentNode == selectedList ) return false;
	
	/* Next sibling of current selected should be turned back to normal */
	var nextNode = SpecBrowser.selected.current.nextSibling;
	if( nextNode )
	{
		if( nextNode.nodeType != 1 ) nextNode = nextNode.nextSibling;
		if( nextNode && nextNode.nodeType == 1 ) 
		{
			nextNode.style.borderTopColor = "";
		}
	}
	
	SpecBrowser.selected.current.className = SpecBrowser.selected.current.className.replace( "selected" , "" );
	selectedList.appendChild( SpecBrowser.selected.current );
	SpecBrowser.selected.current = "";
	
	var firstNode = selectedList.childNodes[0];
	if( firstNode )
	{
		if( firstNode.nodeType != 1 ) firstNode = firstNode.nextSibling;
		if( firstNode && ( firstNode.nodeType == 1 && firstNode.nodeName == "LI" ) )
		{
			firstNode.className = "first";
		}
	}
	
	var lastNode = selectedList.childNodes[selectedList.childNodes.length-1];
	if( lastNode )
	{
		if( lastNode.nodeType != 1 ) lastNode = lastNode.previousSibling;
		if( lastNode && ( lastNode.nodeType == 1 && lastNode.nodeName == "LI" ) )
		{
			lastNode.className = "last";
		}
	}
	
	availableList.scrollTop = 0;
	selectedList.scrollTop = 0;
	
	SpecBrowser.readValue( );
	SpecBrowser.sortList( );
	if( SpecBrowser.config.useButtons == false ) return true;
	return false;
} );

/*
  Select an option */
General.event.register( document , 'click' , function( e ){
	var theNode = e.targetNode;
	if( theNode.parentNode == undefined ) return true;
	if( theNode.parentNode.nodeName != "UL" )
	{
		theNode = theNode.parentNode;
		if( theNode.parentNode.nodeName != "UL" ) return true;
	}
	if( theNode.parentNode.className.indexOf( "options" ) != -1 ) return true;

	if( SpecBrowser.selected.current )
	{	
		SpecBrowser.selected.current.className = SpecBrowser.selected.current.className.replace( "selected" , "" );
		
		/* Next sibling of current selected should be turned back to normal */
		var nextNode = SpecBrowser.selected.current.nextSibling;
		if( nextNode )
		{
			if( nextNode.nodeType != 1 ) nextNode = nextNode.nextSibling;
			if( nextNode && nextNode.nodeType == 1 ) 
			{
				nextNode.style.borderTopColor = "";
			}
		}
	}
	
	theNode.className = theNode.className + " selected";
	SpecBrowser.selected.current = theNode;
	
	/* Next sibling's top border should be blue as well! */
	var nextNode = theNode.nextSibling;
	if( !nextNode ) return;
	if( nextNode.nodeType != 1 ) nextNode = nextNode.nextSibling;
	if( !nextNode || nextNode.nodeType != 1 ) return;
	nextNode.style.borderTopColor = "#09adef";
} );

/* 
	Add option on double click */
General.event.register( document , 'dblclick' , function( e ) {
	var theNode = e.targetNode;
	if( theNode.parentNode == undefined ) return true;
	if( theNode.parentNode.nodeName != "UL" )
	{
		theNode = theNode.parentNode;
		if( theNode.parentNode.nodeName != "UL" ) return true;
	}
	
	if( !theNode.parentNode.parentNode ) return true;
	if( theNode.parentNode.parentNode.id != SpecBrowser.config.available && theNode.parentNode.parentNode.id != SpecBrowser.config.selected ) return true;
	
	if( SpecBrowser.selected.current )
	{	
		var nextNode = SpecBrowser.selected.current.nextSibling;
		if( nextNode )
		{
			if( nextNode.nodeType != 1 ) nextNode = nextNode.nextSibling;
			if( nextNode && nextNode.nodeType == 1 ) 
			{
				nextNode.style.borderTopColor = "";
			}
		}
	
		SpecBrowser.selected.current.className = SpecBrowser.selected.current.className.replace( "selected" , "" );
	}

	var listContainer = theNode.parentNode.parentNode;
	var availableList = document.getElementById( SpecBrowser.config.available );
	if( !availableList ) return;
	availableList = availableList.getElementsByTagName( "UL" );
	if( availableList.length <= 0 ) return;
	availableList = availableList[0];
	var selectedList = document.getElementById( SpecBrowser.config.selected );
	if( !selectedList ) return;
	selectedList = selectedList .getElementsByTagName( "UL" );
	if( selectedList .length <= 0 ) return;
	selectedList = selectedList [0];
	if( availableList == undefined || selectedList == undefined ) return true;
	
	if( !selectedList || !availableList ) return true;
	
	var btn = null;
	
	if( listContainer.id == SpecBrowser.config.selected )
	{
		// fire remove event by faking a button click
		btn = General.getElementByClassName(document.getElementsByTagName("body")[0],"a","remove-selected");
		
		if (btn != null) {
			if(document.dispatchEvent) { // W3C
				var oEvent = document.createEvent( "MouseEvents" );
					oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, btn);
				btn.dispatchEvent( oEvent );
			}
			else if(document.fireEvent) { // IE
				btn.fireEvent("onclick");
			}
			
			if (typeof(SpecBrowser.config.dotNetDoubleClickHandlerRemove)!="undefined") {
				SpecBrowser.config.dotNetDoubleClickHandlerRemove();
			}
		}
	}
	else
	{
		// fire add event by faking a button click		
		btn = General.getElementByClassName(document.getElementsByTagName("body")[0],"a","add-selected")
				
		if (btn != null) {
	
			if(document.dispatchEvent) { // W3C
				var oEvent = document.createEvent( "MouseEvents" );
					oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, btn);
				btn.dispatchEvent( oEvent );
			}
			else if(document.fireEvent) { // IE
				btn.fireEvent("onclick");
			}
					
			if (typeof(SpecBrowser.config.dotNetDoubleClickHandlerAdd)!="undefined") {
				SpecBrowser.config.dotNetDoubleClickHandlerAdd();
			}
		}

	}
		
	return false;
} );

/* 
  Show information bubble */
General.event.register( document , 'mouseover' , function( e ){
	var theNode = e.targetNode;
	if( theNode.nextSibling == undefined ) return true;
	if( theNode.nodeName != "IMG" ) return true;
	if( theNode.className.indexOf( "open-info" ) == -1 ) return true;
	var infoBubble = theNode.nextSibling;
	if( infoBubble.nodeType != 1 ) infoBubble = infoBubble.nextSibling;
	if( infoBubble.nodeType != 1 ) return true;
	if( infoBubble.className.indexOf( "info-popup" ) == -1 ) return true;
	
	clearTimeout( SpecBrowser.timer.infoBubble );
	
	infoBubble.style.position = 'absolute';
	infoBubble.style.visibility = "hidden";
	
	if (theNode.className.indexOf('rel') != -1) {
		infoBubble.style.left = ( theNode.relPosX - 260 ) + "px";
		infoBubble.style.top = ( theNode.relPosY + theNode.offsetHeight ) + "px";;
	}
	else {
		infoBubble.style.left = ( theNode.posX - 260 ) + "px";
		infoBubble.style.top = ( theNode.posY + theNode.offsetHeight ) + "px";;
	}
	infoBubble.className = "info-bubble";
	infoBubble.style.zIndex = 50;
	infoBubble.style.visibility = "visible";
	
	infoBubble.onmouseover = function( )
	{
		clearTimeout( SpecBrowser.timer.infoBubble );
	};
	
	infoBubble.onmouseout = function( )
	{
		var self = this;
		SpecBrowser.timer.infoBubble = setTimeout( function( ) {
			self.className  = "info-popup";
		} , 100 );
	};
} );

General.event.register( document , 'mouseout' , function( e ){
	var theNode = e.targetNode;
	if( theNode.nextSibling == undefined ) return true;
	if( theNode.nodeName != "IMG" ) return true;
	if( theNode.className.indexOf( "open-info" ) == -1 ) return true;
	var infoBubble = theNode.nextSibling;
	if( infoBubble.nodeType != 1 ) infoBubble = infoBubble.nextSibling;
	if( infoBubble.nodeType != 1 ) return true;
	if( infoBubble.className.indexOf( "info-bubble" ) == -1 ) return true;
	
	SpecBrowser.timer.infoBubble = setTimeout( function( ) {
		infoBubble.className  = "info-popup";
	} , 100 );
} );



/* 
	Chassis drawings - Current selection */
	
var ChassisDetail = { 
	current: null 
};
	
/* 
	Chassis drawings - Expand / Collapse */
General.event.register( document , 'click' , function( e ){
	var theNode = e.targetNode;
	if( theNode.nodeName != "A" || theNode.className.indexOf( "expand" ) == -1 ) return true;
	if( theNode.parentNode == undefined ) return true;
	e.cancelEvent( );
	var theParent = theNode.parentNode;
	theParent = theParent.parentNode;
	
	if( theParent.className.indexOf( "chassis-open" ) != -1 )
	{
		theParent.className = theParent.className.replace( /chassis-open/ , "chassis" );
		// ChassisDetail.current = null;
	}else
	{
		// if( ChassisDetail.current ) 
		// {
		// 	ChassisDetail.current.className = ChassisDetail.current.className.replace( /chassis-open/ , "chassis" );
		// }
		
		theParent.className = theParent.className.replace( /chassis/ , "chassis-open" );
		// ChassisDetail.current = theParent;
	}
} );

/* @eof */
