
var giCanvasSize = 590;
var giOffsetLeft = 10;
var giPageCount = 0;
var giCurrentViewPage = -1;
var giDefaultOpacity = 0.2;

function showPageDetails ( iLinkPageIndex )
{
	iPageIndex = is_page( iLinkPageIndex );
	
	// Move the old connection lines into the background
	if ( giCurrentViewPage != -1 )
	{
		var nOldPaths = getPathElementsByPage( giCurrentViewPage );
		for ( var i=0 ; i<nOldPaths.length ; i++ )
		{
			nOldPaths[i].setAttribute( 'stroke-width', (nOldPaths[i].getAttribute('stroke-width').replace('px','')*1)-1 );
			nOldPaths[i].setAttribute( 'stroke-opacity', giDefaultOpacity );
		}
	}
	
	// Bring new page lines into the foreground
	var nPaths = getPathElementsByPage( iLinkPageIndex );
	for ( i=0 ; i<nPaths.length ; i++ )
	{
		nPaths[i].setAttribute( 'stroke-width', (nPaths[i].getAttribute('stroke-width').replace('px','')*1)+1 );
		nPaths[i].setAttribute( 'stroke-opacity', 1 );
	}
	
	/*
	 * Show the link information about this page
	 */
	// Page name
	var sHead = "<h1>"+gawlLinks[iLinkPageIndex].sUrl+"</h1>";
	
	// Outbound links
	var sOut = "<h2>Outbound links</h2>";
	sOut += '<div class="rc_head">';
	sOut += '<span class="responseCode">HTTP Response</span>URL';
	sOut += '</div>';
	
	if ( gawpPages[iPageIndex].aiLinks.length != 0 )
	{
		for ( i=0 ; i<gawpPages[iPageIndex].aiLinks.length ; i++ )
		{
			wl = gawlLinks[ gawpPages[iPageIndex].aiLinks[i] ];
			if ( wl.iResponseCode == "401" || wl.iResponseCode == "402" || 
				   wl.iResponseCode == "403" || wl.iResponseCode == "404" ||
				   wl.iResponseCode == "500" )
			{
				sOut += '<div class="rc_error">';
			}
			else if ( wl.iResponseCode == "301" || wl.iResponseCode == "302" )
			{
				sOut += '<div class="rc_moved">';
			}
			else if ( wl.iResponseCode == "0" )
			{
				sOut += '<div class="rc_not_checked">';
			}
			else
			{
				sOut += '<div class="rc_okay">';
			}
			
			sOut += '<span class="responseCode">'+wl.iResponseCode +'</span>'+
			        wl.sUrl +'<br/>';
			sOut += '</div>';
		}
	}
	else
	{
		sOut += '<div class="rc_okay">';
		sOut += '<span class="responseCode"></span>No outbound links';
		sOut += '</div>';
	}
	
	// Inbound links
	var sIn = "<h2>Inbound links</h2>";
	sIn += '<div class="rc_head">';
	sIn += '<span class="responseCode">HTTP Response</span>URL';
	sIn += '</div>';
	for ( i=0 ; i<gawpPages.length ; i++ )
	{
		if ( gawpPages[i].iIndex != iLinkPageIndex )
		{
			for ( j=0 ; j<gawpPages[i].aiLinks.length ; j++ )
			{
				if ( gawpPages[i].aiLinks[j] == iLinkPageIndex )
				{
					wl = gawlLinks[ gawpPages[i].iIndex ];
					sIn += '<span class="responseCode">-</span>'+ wl.sUrl +'<br/>';
				}
			}
		}
	}
	
	document.getElementById('integrity_page_details').innerHTML = sHead + sOut + sIn;
	
	giCurrentViewPage = iLinkPageIndex;
}

function getPathElementsByPage ( iPageIndex )
{
	var nElements = new Array();
	var nPaths = document.getElementsByTagName('path');
	var rxOutPatten = new RegExp("^"+iPageIndex+"_");
	var rxInPatten = new RegExp("_"+iPageIndex+"$");
	
	for ( i=0 ; i<nPaths.length ; i++ )
	{
		// Find outbound paths
		if ( rxOutPatten.test( nPaths[i].getAttribute('id') ) )
		{
			nElements[nElements.length++] = nPaths[i];
		}
		
		// Find inbound paths
		if ( rxInPatten.test( nPaths[i].getAttribute('id') ) )
		{
			nElements[nElements.length++] = nPaths[i];
		}
	}
	
	return nElements;
}



function scanningPage ( sPageUrl )
{
	giPageCount++;
	
	var nScanning = document.getElementById('integrity_scanning');
	nScanning.innerHTML += giPageCount +": "+ sPageUrl +'<br/>';
	nScanning.scrollTop = nScanning.scrollHeight;
}

function drawMap ()
{
	document.getElementById('integrity_scanning').innerHTML += 'Generating map';
	calculatePositions();
	drawPageLinks();
	drawPagePoints();
	drawPageNames();
	document.getElementById('integrity_scanning').style.display = "none";
}

function drawPageNames()
{
	var nDisplayContainer = document.getElementById('integrity_svg_wrapper');
	var nLabel;
	
	for ( i=0 ; i<gaiLevels.length ; i++ )
	{
		for ( j=0 ; j<gaiLevels[i].length ; j++ )
		{
			iLinkSourceIndex = gaiLevels[i][j];
			
			/* Attach the name of the page */
			aDisplay = gawlLinks[iLinkSourceIndex].sUrl.split('/');
			sDisplay = aDisplay[aDisplay.length-1];
			if ( sDisplay == "" )
				sDisplay = "<i>Root doc</i>";
			
			nLabel = document.createElement('div');
			nLabel.style.position = 'absolute';
			nLabel.setAttribute( 'id', 'label_'+iLinkSourceIndex );
			nLabel.setAttribute( 'class', 'label' );
			nLabel.appendChild( document.createTextNode(sDisplay) );
			nLabel.setAttribute( 'onclick', 'showPageDetails('+iLinkSourceIndex+');' );
			nLabel.style.left = parseInt( gawlLinks[iLinkSourceIndex].fX+10 )+'px';
			nLabel.style.top = parseInt( gawlLinks[iLinkSourceIndex].fY )+'px';
			nDisplayContainer.appendChild( nLabel );
			
			// Shift the display names on the left half - causes a slight flicker
			// in FF - but tough.
			if ( gawlLinks[iLinkSourceIndex].fX < giCanvasSize/2 )
			{
				nLabel.style.left = (gawlLinks[iLinkSourceIndex].fX - 
					document.getElementById('label_'+iLinkSourceIndex).offsetWidth -10)+'px';
			}
		}
	}
}

function drawPageLinks ()
{
	var l=4;
	var nPath;
	var nSvg = document.getElementById('svg');
	
	for ( var i=gaiLevels.length-1 ; i>=0 ; i-- )
	{
		if ( i == 0 ) {
			sStroke = "fuchsia";
			iStrokeWidth = 3;
		} else if ( i == 1 ) {
			sStroke = "blue";
			iStrokeWidth = 2;
		} else if ( i == 2 ) {
			sStroke = "green";
			iStrokeWidth = 1;
		} else if ( i == 3 ) {
			sStroke = "yellow";
			iStrokeWidth = 1;
		} else if ( i == 4 ) {
			sStroke = "silver";
			iStrokeWidth = 1;
		}
		
		for ( j=0 ; j<gaiLevels[i].length ; j++ )
		{
			iLinkSourceIndex = gaiLevels[i][j];
			
			iWpIndex = is_page( iLinkSourceIndex );
			
			/* Draw links between all pages */
			if ( iWpIndex >= 0 )
			{
				for ( k=0 ; k<gawpPages[iWpIndex].aiLinks.length ; k++ )
				{
					iLinkTargetIndex = gawpPages[iWpIndex].aiLinks[k];
					
					if ( is_page( iLinkTargetIndex ) >= 0 )
					{
						// d="M sx sy l tx-sx ty-sy"
						nPath = document.createElementNS('http://www.w3.org/2000/svg','path');
						nPath.setAttribute( 'id', iLinkSourceIndex+'_'+iLinkTargetIndex );
						nPath.setAttribute( 'd', 
							  'M '+gawlLinks[iLinkSourceIndex].fX+' '+gawlLinks[iLinkSourceIndex].fY+' '+
							  'l '+(gawlLinks[iLinkTargetIndex].fX - gawlLinks[iLinkSourceIndex].fX)+' '+
							    (gawlLinks[iLinkTargetIndex].fY - gawlLinks[iLinkSourceIndex].fY) );
						nPath.setAttribute( 'stroke', sStroke );
						nPath.setAttribute( 'stroke-width', iStrokeWidth );
						nPath.setAttribute( 'stroke-opacity', giDefaultOpacity );
						nSvg.appendChild( nPath );
					}
				}	
			}
		}
		
		l--;
	}
}


function drawPagePoints ()
{
	var nPoint;
	var nSvg = document.getElementById('svg');
	
	for ( i=0 ; i<gaiLevels.length ; i++ )
	{
		for ( j=0 ; j<gaiLevels[i].length ; j++ )
		{
			iLinkSourceIndex = gaiLevels[i][j];
			
			// Add a page's point
			if ( !document.getElementById('page_'+iLinkSourceIndex) )
			{
				var iTmp = pageGotError( iLinkSourceIndex );
				if ( iTmp == 1 )
					sFill = "red";
				else
					sFill = "white";
				
				nPagePoint = document.createElementNS('http://www.w3.org/2000/svg','circle');
				nPagePoint.setAttribute( 'id', 'page_'+iLinkSourceIndex );
				nPagePoint.setAttribute( 'cx', gawlLinks[iLinkSourceIndex].fX );
				nPagePoint.setAttribute( 'cy', gawlLinks[iLinkSourceIndex].fY );
				nPagePoint.setAttribute( 'r', 5 );
				nPagePoint.setAttribute( 'fill', sFill );
				nPagePoint.setAttribute( 'stroke', 'black' );
				nPagePoint.setAttribute( 'stroke-width', 2 );
				nPagePoint.setAttribute( 'onclick', 'showPageDetails('+iLinkSourceIndex+');' );
				nSvg.appendChild( nPagePoint );
			}
			// Move page's point
			else
			{
				nPoint = document.getElementById('page_'+iLinkSourceIndex);
				nPoint.setAttribute( 'cx', gawlLinks[iLinkSourceIndex].fX );
				nPoint.setAttribute( 'cy', gawlLinks[iLinkSourceIndex].fY );
			}
		}
	}
}

function pageGotError ( iLinkSourceIndex )
{
	var iPageIndex = is_page( iLinkSourceIndex );
	
	for ( var k=0 ; k<gawpPages[iPageIndex].aiLinks.length ; k++ )
	{
		var iR = gawlLinks[ gawpPages[iPageIndex].aiLinks[k] ].iResponseCode;
		if ( iR == 401 || iR == 402 ||  iR == 403 ||  iR == 404 ||  iR == 500 )
		{
			return 1;
		}
	}
	
	return 0;
}

function calculatePositions ()
{
	var iConcentricRadius = giCanvasSize / (2*(gaiLevels.length-1));
	
	for ( var i=0 ; i<gaiLevels.length ; i++ )
	{
		fAngle = 0;
		iRadius = i * iConcentricRadius;
		fAngleDelta = (2*3.141592) / gaiLevels[i].length;
		
		for ( var j=0 ; j<gaiLevels[i].length ; j++ )
		{
			iLinkIndex = is_page( gaiLevels[i][j] );
			iLinkIndex = gaiLevels[i][j];
			
			gawlLinks[iLinkIndex].fX = iRadius * Math.cos( fAngle );
			gawlLinks[iLinkIndex].fY = iRadius * Math.sin( fAngle );
			
			// Translate coords
			gawlLinks[iLinkIndex].fX += (giCanvasSize/2) + giOffsetLeft;
			gawlLinks[iLinkIndex].fY -= giCanvasSize/2 + 15; // 15 to shift eveything down a little
			gawlLinks[iLinkIndex].fY *= -1;
			
			fAngle += fAngleDelta;
		}
	}
}

function is_page ( iIndex )
{
	for ( m=0 ; m<gawpPages.length ; m++ )
	{
		if ( gawpPages[m].iIndex == iIndex )
		{
			return m;
		}
	}
	
	return -1;
}
