//alert("sunce.js");
//*************************************************************************************************
//*************************************************************************************************
//*************************************************************************************************
//**********                                                                             **********
//**********                                                                             **********
//**********                           S U N C E                                         **********
//**********                                                                             **********
//**********                                                                             **********
//*************************************************************************************************
//*************************************************************************************************
//*************************************************************************************************

function sunce_pozicija(kada)
{
	var d=izracunaj_d(kada);
	var vrati_parametre= new Array();
	var ecl= izracunaj_ecl(kada);	
	/*****************************
	* Orbitalni elementi Sunca   *
	******************************/
	var N_sunce=0.0;
	var i_sunce=0.0;
	var w_sunce=282.9404 + 4.70935E-5*d;
	var a_sunce=1; //AU
	var e_sunce=0.016709-1.151E-9*d;
	var M_sunce=norm360(356.0470+0.9856002585*d);
	/****************************
	*  Pozicija Sunca           *
	*****************************/	
	var E_sunce=0; //eccentric anomaly 
	var r_sunce=0; // udaljenost sunca
	var v_sunce=0; // true anomaly of the sun
	var xv=0;
	var yv=0;
	var lon_sunce=0; // Suns true longitued
	// ecliptic rectangular geocentric coordinates of sun
	var x_sunce=0;
	var y_sunce=0;
	var z_sunce=0;
	// equatorial, rectangular, geocentric coordinates of sun
	var xe_sunce=0;
	var ye_sunce=0;
	var ze_sunce=0;
	// Sun's Right Ascension (RA) and Declination (Dec)
	var RA_sunce=0;
	var Dec_sunce=0;
	//var sradius=0;   //Sun's apparent radius 
    
	E_sunce = M_sunce+e_sunce*(180/Math.PI)*sinDeg(M_sunce)*(1+e_sunce*cosDeg(M_sunce));	
	xv=cosDeg(E_sunce)-e_sunce;
	yv=Math.sqrt(Math.abs(1- e_sunce*e_sunce))*sinDeg(E_sunce);		
	v_sunce=atan2Deg(yv,xv);
	r_sunce=Math.sqrt(xv*xv+yv*yv);	
	//sradius = 0.2666 / r_sunce;
	lon_sunce=norm360(v_sunce+w_sunce);	
	
	//  precession korekcija lon sunca za planete
	//lon_sunce += precession_corr(sad);

	/* Convert lon_sunce,r_sunce to ecliptic rectangular geocentric coordinates x_sunce,y_sunce */
	x_sunce=r_sunce*cosDeg(lon_sunce);
	y_sunce=r_sunce*sinDeg(lon_sunce);
	/* since the Sun always is in the ecliptic plane, zs is of course zero). 
	xs,ys is the Sun's position in a coordinate system in the plane of the ecliptic. 
	convert this to equatorial, rectangular, geocentric coordinates, compute */	
	
	xe_sunce = x_sunce;
	ye_sunce = y_sunce * cosDeg(ecl);
	ze_sunce = y_sunce * sinDeg(ecl);		
	RA_sunce  = norm360(atan2Deg( ye_sunce, xe_sunce ));
	Dec_sunce = atan2Deg( ze_sunce, Math.sqrt(xe_sunce*xe_sunce+ye_sunce*ye_sunce));	
		
	//alert("E="+E_sunce+" ye="+ye_sunce+" xe="+xe_sunce" RA_sunce="+RA_sunce);
	
	vrati_parametre["N"]  = N_sunce;
	vrati_parametre["i"]  = i_sunce;
	vrati_parametre["w"]  = w_sunce;
	vrati_parametre["a"]  = a_sunce;
	vrati_parametre["e"]  = e_sunce;
	vrati_parametre["M"]  = M_sunce;
	vrati_parametre["E"]  = E_sunce;
	vrati_parametre["v"]  = v_sunce;
	vrati_parametre["r"]  = r_sunce;
	vrati_parametre["lon"]  = lon_sunce;
	vrati_parametre["RA"] = RA_sunce;
	vrati_parametre["Dec"] = Dec_sunce;

	//alert("ecl="+ecl);
	//alert("M_sunce="+M_sunce+"w="+w_sunce+" a="+a_sunce+"e="+e_sunce);
	//alert("M_sunce="+M_sunce+"w="+w_sunce+" a="+a_sunce+"e="+e_sunce);
	//alert("xv="+xv+" yv="+yv);
	//alert("E_sunce="+E_sunce);
	//alert("v_sunce="+v_sunce+" r_sunce="+r_sunce+" lon_sunce="+lon_sunce+" x_sunce="+x_sunce+" y_sunce="+y_sunce);
	//alert("sunce_pozicija():: u "+kada.getHours()+":"+kada.getMinutes()+":"+kada.getSeconds()+" RA="+RA_sunce+" Dec="+Dec_sunce);
	
	return vrati_parametre;
	
} // end of sunce_pozicija()

function sunce_kalkulator(kada, h_konvencija, x_lon, x_lat)
{
	//alert("ENTER:sunce_kalkulator()");

	//***************************
	//*  izlazak zalazak Sunca  *
	//***************************
	var GMST0=0; // sidereal time at Greenwich at 00:00 Universal Time
	var GMST0_podne=0;
	var LST=0;   // Local Sidereal Time (LST)
	var LHA=0;   // Sun's Local Hour Angle (LHA): i.e. the angle the Earth has turned since the Sun last was in the south
	var sinh=0;  // sin(Sun's altitude above the horizon)
	var H_sunce; // Sun's altitude above the horizon
	var UT_Sun_in_south=0;
	var cosLHA=0;
	var UT=izracunaj_UT(kada);
	var izlazak_sunce=0;
	var zalazak_sunce=0;
	var sunce_polozaj=new Array();
	var sunce_polozaj_podne=new Array();
	var izlazak=new Date(kada);
	var zalazak=new Date(kada);
	var loc_podne=new Date(kada.getFullYear(), kada.getMonth(), kada.getDate(), 12,0,0,0);
	var vrati_parametre= new Array();
	var x,y,z, xhor, yhor,zhor, altitude, azimuth,d;


	//alert("sunce kada="+ispisiDate(kada)+"podne:"+ispisiDate(loc_podne));
	//window.status = "sunce";

	sunce_polozaj=sunce_pozicija(kada);
	
	vrati_parametre["N"]    = sunce_polozaj["N"];  // N_sunce 0
	vrati_parametre["i"]    = sunce_polozaj["i"];  // i_sunce 1
	vrati_parametre["w"]	= sunce_polozaj["w"];  // w_sunce 2
	vrati_parametre["a"]	= sunce_polozaj["a"];  // a_sunce 3
	vrati_parametre["e"]	= sunce_polozaj["e"];  // e_sunce 4
	vrati_parametre["M"]	= sunce_polozaj["M"];  // M_sunce 5 
	vrati_parametre["E"]	= sunce_polozaj["E"];  // E_sunce 6
	vrati_parametre["v"]	= sunce_polozaj["v"];  // v_sunce 7
	vrati_parametre["r"]	= sunce_polozaj["r"];  // r_sunce 8
	vrati_parametre["lon"]	= sunce_polozaj["lon"];  // lon_sunce 9 
	vrati_parametre["RA"]	= sunce_polozaj["RA"]; // RA_sunce 10
	vrati_parametre["Dec"]	= sunce_polozaj["Dec"]; // Dec_sunce 11
	 
	GMST0=norm360(sunce_polozaj["M"]+sunce_polozaj["w"]+180);	// u stupnjevima
	LST=GMST0+norm24(UT)*15+ x_lon;
	//LST=GMST0+0+ x_lon; // test time 0:00 UT

	LHA=LST-sunce_polozaj["RA"];	
	sinh=sinDeg(x_lat)*sinDeg(sunce_polozaj["Dec"])+cosDeg(x_lat)*cosDeg(sunce_polozaj["Dec"])*cosDeg(LHA);
	H_sunce=asinDeg(sinh); // Sun's altitude above the horizon

	// altitude and azimuth
	x = cosDeg(LHA) * cosDeg(sunce_polozaj["Dec"]); 
	y = sinDeg(LHA) * cosDeg(sunce_polozaj["Dec"]); 
	z = sinDeg(sunce_polozaj["Dec"]);          
	
	xhor = x * sinDeg(x_lat) - z * cosDeg(x_lat);
	yhor = y;
	zhor = x * cosDeg(x_lat) + z * sinDeg(x_lat);
	//alert("GMST0="+GMST0/15+" LST="+LST/15+" LHA="+LHA+" xhor="+xhor+" yhor="+yhor+" zhor="+zhor);
	
	azimuth  = atan2Deg( yhor, xhor ) + 180;
	altitude = atan2Deg( zhor, Math.sqrt(xhor*xhor+yhor*yhor));


	/* polozaj u podne po lokalnom vremenu */
	sunce_polozaj_podne=sunce_pozicija(loc_podne);
	GMST0_podne = norm360(sunce_polozaj_podne["M"]+sunce_polozaj_podne["w"]+180);
	UT_Sun_in_south = 12-norm180(norm360(GMST0_podne+180+x_lon)-sunce_polozaj_podne["RA"])/15;			
	cosLHA=(sinDeg(h_konvencija)-sinDeg(x_lat)*sinDeg(sunce_polozaj_podne["Dec"]))/(cosDeg(x_lat)*cosDeg(sunce_polozaj_podne["Dec"]));
	
	if ( cosLHA< 1.0 && cosLHA>-1.0)
	{			
		izlazak_sunce=zaokruzi(UT_Sun_in_south-acosDeg(cosLHA)/15,2);
		zalazak_sunce=zaokruzi(UT_Sun_in_south+acosDeg(cosLHA)/15,2);	
	
		izlazak.setHours(izlazak_sunce, (izlazak_sunce-Math.floor(izlazak_sunce))*60,0,0);
		zalazak.setHours(zalazak_sunce, (zalazak_sunce-Math.floor(zalazak_sunce))*60,0,0);
	}
		
	d = 1919.26/sunce_polozaj["r"];	
	
	vrati_parametre["altitude above the horizon"] = H_sunce;         //Sun's altitude above the horizon
	vrati_parametre["UT_Sun_in_south"]            = UT_Sun_in_south; // vrijeme  (UT) kada je sunce tocno na jugu
	vrati_parametre["cosLHA"] 					  = cosLHA;
	vrati_parametre["izlazak UT"] 				  = izlazak_sunce; // izlazak sunca u UT
	vrati_parametre["zalazak UT"]                 = zalazak_sunce; // zalazak sunca u UT
	vrati_parametre["izlazak local time"]         = izlazak;       // izlazak u lokalnom vremenu
	vrati_parametre["zalazak local time"]         = zalazak;  	     // zalazak u lokalnom vremenu
	vrati_parametre["azimuth"]                    = azimuth;
    vrati_parametre["altitude"]                   = altitude;
	vrati_parametre["d"]                         = d;
	vrati_parametre["magnitude"] = 0;
	vrati_parametre["elong"] = 0;
	vrati_parametre["faza"] = 0;


	//alert("sunce_izlazak_zalazak():: RA="+sunce_polozaj[10]+" Dec="+sunce_polozaj[11]);		
	//alert("u "+kada.getHours()+":"+kada.getMinutes()+":"+kada.getSeconds()+" u zg sunce je na: "+H_sunce+" stupnjeva");
	//alert(GMST0_podne);
	//alert("UT_Sun_in_south="+UT_Sun_in_south);
	//alert("cosLHA="+cosLHA);
	//alert("zg: izlazak "+izlazak_sunce+" zalazak="+zalazak_sunce);
	//alert("zg: izlazak "+izlazak.getHours()+":"+izlazak.getMinutes()+":"+izlazak.getSeconds());
	//alert("zg: zalazak="+zalazak.getHours()+":"+zalazak.getMinutes()+":"+zalazak.getSeconds());
	//alert("izlazak="+izlazak.getHours());
	//alert("zalazak="+zalazak.toString());

	return vrati_parametre;
	
} //end of sunce_kalkulator()
