function getAngle(xlat, xlong, xdec ){ 
	 
	var shadow = getShadow(xlat, xlong, xdec);
	
	var ss = ' ( ' + shadow[0]+', ' + shadow[1] + ', ' + shadow[2] + ' )'; 
	var N = North( xlat, xlong ); 
	var nn = ' ( ' + N[0]+', ' + N[1] + ', ' + N[2] + ' )'; 
	
	var z =  Math.acos(  vDot( vUnit( shadow ), vUnit(N) ) ); 
	
	if( N[2] < 0 ) { 
		z = -z;
	} 
	
	var txt = 'angle='+ (57.3*z) + ' clockHr='+clockHr + 
	          ' shadow= '+ ss + '  north = '+nn; 
	    
	   // alert(txt); 
	    
	
	return z; 
	//alert( 57.3*angle ); 
			
	
}
function testShadow(){
	getAngles(); 
	var shadow = getShadow(rlat, rlong, rdec);
	alert('x='+shadow[0]+' y='+shadow[1]+' z='+shadow[2]+' len='+Math.sqrt(vLen2(shadow))*1000000 ); 
}

var reportNode = document.getElementById('report'); 

var dLat = null; 
var dLong =  null; 
var dDec= null;  
var rlat =  null; 
var rlong =  null; 
var rdec = null;  
function getAngles(){
	//dLat = (document.getElementById('lat')).value; 
	//dLong = (document.getElementById('long')).value; 
	//dDec= (document.getElementById('dec')).value; 
	
	rlat = dLat/57.3; 
	rlong = dLong/57.3; 
	rdec = dDec/57.3; 
} 
function testNorth(){
	getAngles();
	var N = North( rlat, rlong ); 

	alert('x='+N[0]+' y='+N[1]+' z='+N[2] ); 
} 	
 
function computePhiOfSunrise(){ 
	
	//getAngles(); 
    
    for( var k= 1; k<3500; k++){ 
    	var phiRad = (k*.002)-1.5; 
  	
    	var xx1 = vUnit(Ray(rdec)); 
    	var xx2 = vUnit(R(rlat, phiRad )); 
    	var xx3 = vDot( xx1, xx2 ); 
    	var pAngle = 57.7 * Math.acos(xx3); 
    	
		if(  Math.floor(pAngle) == 90 ) { 
			return phiRad; 
		}
 /*     	
    	var angle = (57.3 * ( Math.acos(  vDot( Ray(rdec), R(rlat, phiRad) ) ) ));
 		var phi =   + Math.ceil(57.8*phiRad*100)/100 ; // round off to 1/100th degree for display. 
		if(  Math.floor(angle) == 90 ) { 
			return phiRad; 
		}
		*/ 
		
    } 
} 

 


function North(xlat,xphi){
	return [ Math.cos(xlat), -Math.sin(xlat)*Math.sin(xphi), Math.sin(xlat)*Math.cos(xphi) ]; 
} 

function R(xlat,xphi){ 
	var x = Math.sin(xlat);
	var y = Math.sin(xphi)*Math.cos(xlat);
	var z = -Math.cos(xphi)*Math.cos(xlat); 
	return [x,y,z]; 
}
function Ray(xdec){ 
	return [ -Math.sin(xdec), -Math.cos(xdec), 0 ] ; 
} 

 
var xh=.000001; // height of sundial 

function getShadow(xlat,xlong,xdec ) { 
	
	var shadow = new Array(3);  
	var found = false; 
	
	// follow sun ray from top point on sun dial until it hits the earth, increments of 1/100th sundial height (H). 
	var H= xh; 
	var zD=H/100;  
	var f=1.00000000+H; 
	
	var R0 = R( xlat,xlong ); 
	
	var Rtop =    vMult(R0, f) ; 
	var len2Rtop = vLen2(Rtop); 
	
	var ray= vMult( Ray( xdec ), H ); 
	
	var trialV = new Array(3); 
	
	while( true ) {
		
		trialV = vAdd( Rtop, ray ); 
		L = ( vLen2(trialV) ); 

//alert( 'L2trial='+L+'  len2Rtop='+len2Rtop ); 

		if( L > len2Rtop ) { 
			found = false; 
			break;
		} 
		if( L < 1 ) { 
			found = true; 
			shadow = vAdd( trialV, vMult(R0,-1) ); 
			break;
		} 
		
		H=H+zD; 
		var ray= vMult( Ray( xdec ), H ); 
	} 
	
	if( found ) { 
		 
		var angleOfShadow = Math.acos( vDot( shadow, North(xlat,xlong) ) ); 
		var lenOfShadow = Math.sqrt( vLen2(shadow) ) ; 
		
		//shadow = vUnit(shadow); 
		//alert('shadowLen in units of sundial height='+ lenOfShadow/xh ); 
		//alert( 'angle relative to North=' + angleOfShadow*57.3 ) ; 
		//return [ angleOfShadow, lenOfShadow ]; 
		return shadow; 
		
	} else { 
		return [ false, false, false ]; 
	} 
	
}
  

function vAdd(v1,v2){
	var vR = new Array(3); 
	for(var k=0; k<3; k++){ 
		vR[k] = v1[k]+v2[k]; 
	} 
	return vR;
}
//v3=vAdd( [1,2,3], [10,11,12] ); 
	
function vDot(v1,v2){
	var dot=0; 
	for(var k=0; k<3; k++){ 
		dot += v1[k]*v2[k]; 
	} 
	return dot; 
}

function vLen2(v1){ 
	var dot=0; 
	for(var k=0; k<3; k++){ 
		dot += v1[k]*v1[k]; 
	} 
	return dot; 
}
function vLen(v1){ 
	return Math.sqrt( vLen2(v1) )/xh; 
} 
function vUnit(v1){ 
	var len = Math.sqrt(vLen2(v1));  
	var vR = new Array(3); 
	for(var k=0; k<3; k++){ 
		vR[k]= v1[k]/len; 
	} 
	return vR; 
}
 	
function vMult(v1,f){
	var vR = new Array(3); 
	for(var k=0; k<3; k++){ 
		vR[k] = f*v1[k]; 
	} 
	return vR;
}


