function Connections(nlifts, ntrails, s1, s2, s3)
{
	this.lifts = document.getElementById( nlifts );
	this.trails = document.getElementById( ntrails );
	this.t1 = s1;
	this.t2 = s2;
	this.t3 = s3;
	this.current = 0;
	this.ordered = new Array();
	this.keyd = new Array();
	this.aired = new Array();
	this.floors = new Array();
	this.lkeyd = new Array();
	this.highlight = null;
	
	var trailels = this.trails.getElementsByTagName( "DIV" );

	for( var j = 0; j < trailels.length; j ++ ) {
		var cn = "";
		
		if( trailels[ j ].className !== undefined ) {
			cn = trailels[ j ].className;
		} else if( trailels[ j ].getAttribute ) {
			cn = trailels[ j ].getAttribute( "class" );
		}
		
		if( cn != "trailAround" ) continue;
		
		var tid = trailels[ j ].getAttribute( "tid" );
		if( !tid ) continue;
		
		this.ordered[ this.ordered.length ] = trailels[ j ];
		this.keyd[ tid ] = trailels[ j ];
	}
	
	var liftels = this.lifts.getElementsByTagName( "DIV" );
	
	for( var j = 0; j < liftels.length; j ++ ) {
		var cn = "";
		
		if( liftels[ j ].className !== undefined ) {
			cn = liftels[ j ].className;
		} else if( liftels[ j ].getAttribute ) {
			cn = liftels[ j ].getAttribute( "class" );
		}
		
		if( cn != "liftAround" ) continue;
		
		var lid = liftels[ j ].getAttribute( "lid" );
		
		if( !lid ) continue;
		
		this.floors[ lid ] = getPageCoords( liftels[ j ] );
		this.lkeyd[ lid ] = liftels[ j ];
	}
}

Connections.prototype.resetOrder = function()
{
	if( this.current == 0 ) return;

	for( var j = this.aired.length-1; j >= 0; j -- ) {
		var el = this.aired[ j ];
		this.highlight.removeChild( el );
		
		for( var i = 0; i < this.ordered.length; i ++ ) {
			if( this.ordered[ i ] == el ) {
				if( (i+1) < this.ordered.length ) {
					this.trails.insertBefore( el, this.ordered[ i + 1 ] );
				} else {
					this.trails.insertBefore( el, null );
				}
			}
		}
	}
	
	for( var j = 0; j < this.ordered.length; j ++ ) {
		this.ordered[ j ].style.marginTop = "";
	}
	
	var liftData = findElementIn( this.lkeyd[ this.current ], "liftData", "DIV" );
	
	if( liftData ) {
		liftData.style.borderColor = "#3780be";
	}

	this.current = 0;

	document.body.removeChild( this.highlight );
	this.highlight = null;
	this.trails.style.filter = "";
	this.trails.style.opacity = "1";
	if( this.trails.style.setAttribute )
		this.trails.style.setAttribute( "-moz-opacity", 1 );
}

Connections.prototype.clickLift = function(liftId, tids)
{
	if( liftId == this.current ) {
		this.resetOrder();
		return;
	}
	
	this.resetOrder();
	this.highlight = document.createElement( "DIV" );
	this.aired = new Array();
	
	for( var j = 0; j < tids.length; j ++ ) {
		var el = this.keyd[ tids[ j ] ];
		if( !el ) continue;
		this.trails.removeChild( el );
		this.highlight.appendChild( el );
		this.aired[ this.aired.length ] = el;
	}
	
	if( this.aired.length <= 0 ) {
		this.highlight = null;
		return;
	}
	
	var floor = this.floors[ liftId ];
	var sid = getPageCoords( this.trails );
	var ph = ((this.aired.length * 22) + 6);

	this.highlight.style.position = "absolute";
	this.highlight.style.left = (sid.x - 38) + "px";
	this.highlight.style.top = floor.y + "px";
	this.highlight.style.display = "block";
	this.highlight.style.margin = "0px";
	this.highlight.style.padding = "0px 0px 20px 0px;";
	this.highlight.style.height = ph + "px";
	this.highlight.style.width = "245px";
	this.highlight.style.background = "#fcfcfc";
	this.highlight.style.border = "dotted 1px #3780be";
	
	document.body.appendChild( this.highlight );

	this.current = liftId;
	
	var liftData = findElementIn( this.lkeyd[ this.current ], "DIV", "liftData" );
	
	if( liftData ) {
		liftData.style.borderColor = "#3780be";
	}

	this.trails.style.filter = "alpha(opacity=45)";
	this.trails.style.opacity = "0.45";
	if( this.trails.style.setAttribute )
		this.trails.style.setAttribute( "-moz-opacity", "0.45" );
		
	for( var j = 0; j < this.ordered.length; j ++ ) {
		var nouse = false;
		for( var i = 0; i < this.aired.length; i ++ ) {
			if( this.ordered[ j ] == this.aired[ i ] ) {
				nouse=true;
				break;
			}
		}
		
		if( nouse ) continue;
		var xy = getPageCoords( this.ordered[ j ] );
		
		if( xy.y >= (floor.y - 22) ) {
			var m = 22 - Math.abs( xy.y - (floor.y - 22) );
			this.ordered[ j ].style.marginTop = ((m * 2) + ph) + "px";
			break;
		}
	}
}

