﻿function MM_findObj(n, d) { //v4.01
var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function OpenCenterWindow(sUrl,sName,iHeight,iWidth)
{
	var top		= (screen.height-iHeight)/2;
	var left	= (screen.width-iWidth)/2;
	window.open(sUrl,sName,"'status=yes,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=no,"+"width="+iWidth+",height="+iHeight+",top="+top+",left="+left+"'");
}

function RightClose(id)
{
	tbl = MM_findObj(id);
	if (tbl)
	{
		var display = tbl.style.display ? '' : 'none';
		tbl.style.display = display;
	}
}

function loadflash(holderid, id,theurl,width,height) 
{
	MM_findObj(holderid).innerHTML ='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width='+ width +'" height="'+ height +'"><param name="movie" value="'+ theurl +'" \/><param name="quality" value="high" \/><param name="wmode" value="transparent" \/><embed src="'+ theurl + '" width="'+ width + '" height="'+ height + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" \/><\/embed><\/object>';
}

function ConvertToUnicode(s)
{
	return s.replace(/0/g, '\u06F0').replace(/1/g, '\u06F1').replace(/2/g, '\u06F2').replace(/3/g, '\u06F3').replace(/4/g, '\u06F4').replace(/5/g, '\u06F5').replace(/6/g, '\u06F6').replace(/7/g, '\u06F7').replace(/8/g, '\u06F8').replace(/9/g, '\u06F9');
}
function SetTitle(d)
{
	var month = document.getElementById("month");
	var city  = document.getElementById("cities")
	document.getElementById("tdTitle").innerHTML = "اوقات شرعی " + ConvertToUnicode(d + " " + month.options[month.selectedIndex].text + " " + document.getElementById("spanYear").innerHTML) + " (به افق " + city.options[city.selectedIndex].text + ")";
}

function Calculate()
{
	var i = document.getElementById("cities").selectedIndex;
	if(i==0)
	{
		alert('لطفا مرکز استان را انتخاب کنید');
		return;
	}

	var m=document.getElementById("month").selectedIndex+1;
	var d=eval(document.getElementById("day").value);
	var lg=eval(document.getElementById("longitude").value);
	var lat=eval(document.getElementById("latitude").value);
    document.getElementById('t9').innerHTML=ConvertToUnicode(gheble[i].toString())+" درجه از جنوب به سمت غرب";	
	main(m, d, lg, lat);
	
	SetTitle(d);
}

function GetAzanMorning(m, d, lg, lat)
{
//	var lg=52.5125;
//	var lat=29.635;
	var ep=sun(m,d,4,lg);
	var zr=ep[0];
	delta=ep[1];
	ha=loc2hor(108.0,delta,lat);
	var t1=Round(zr-ha,24);
	ep=sun(m,d,t1,lg);
	zr=ep[0];
	delta=ep[1];
	ha=loc2hor(108.0,delta,lat);
	var t1=Round(zr-ha,24);

	return hms(t1, m);
}

function GetSunrise(m, d, lg, lat)
{
//
//   t2= Sun rise
//
	var ep=sun(m,d,6,lg);
	var zr=ep[0];
	delta=ep[1];
	ha=loc2hor(90.833,delta,lat);
	var t2=Round(zr-ha,24);
	ep=sun(m,d,t2,lg);
	zr=ep[0];
	delta=ep[1];
	ha=loc2hor(90.833,delta,lat);
	t2=Round(zr-ha,24);
	
	return hms(t2, m);
}

function GetAzanNoon(m, d, lg, lat)
{
//	
//	zr=Zohr
//
	var ep=sun(m,d,12,lg)
	ep=sun(m,d,ep[0],lg)
	var zr=ep[0];
	
	return hms(zr, m);
}

function GetSunset(m, d, lg, lat)
{
//
//   t2= Sun set
//
	var ep=sun(m,d,18,lg)
	var zr=ep[0];
	delta=ep[1];
	ha=loc2hor(90.833,delta,lat)
	var t3=Round(zr+ha,24)
	ep=sun(m,d,t3,lg)
	zr=ep[0];
	delta=ep[1];
	ha=loc2hor(90.833,delta,lat)
	t3=Round(zr+ha,24)
	
	return hms(t3, m);
}

function GetAzanNight(m, d, lg, lat)
{
//
//   t2= Maghreb
//
	var ep=sun(m,d,18.5,lg)
	var zr=ep[0];
	delta=ep[1];
	ha=loc2hor(94.3,delta,lat)
	var t4=Round(zr+ha,24)
	ep=sun(m,d,t4,lg)
	zr=ep[0];
	delta=ep[1];
	ha=loc2hor(94.3,delta,lat)
	t4=Round(zr+ha,24)
	
	return hms(t4, m);
}

function ShowInfo(m, d, lg, lat, morning, sunrise, noon, sunset, night)
{
	var t1 = GetAzanMorning(m, d, lg, lat);
	MM_findObj(morning).innerHTML=ConvertToUnicode(t1);
	temp = MM_findObj("spanMorning");
	if (temp) temp.innerHTML = t1.substring(0,5);
	
	var t2 = GetSunrise(m, d, lg, lat);
	MM_findObj(sunrise).innerHTML=ConvertToUnicode(t2);
	
	var zr = GetAzanNoon(m, d, lg, lat);
	MM_findObj(noon).innerHTML=ConvertToUnicode(zr);
	temp = MM_findObj("spanNoon");
	if (temp) temp.innerHTML = zr.substring(0,5);
	
	var t3 = GetSunset(m, d, lg, lat);
	MM_findObj(sunset).innerHTML=ConvertToUnicode(t3);
	
	var t4 = GetAzanNight(m, d, lg, lat);
	MM_findObj(night).innerHTML=ConvertToUnicode(t4);
	temp = MM_findObj("spanNight");
	if (temp) temp.innerHTML = t4.substring(0,5);
}

function main(m, d, lg, lat)
{
	ShowInfo(m, d, lg, lat, "t1", "t2", "zr", "t3", "t4");
}

function sun(m,d,h,lg)
{
	if(m<7)
		d= 31*(m-1)+d+h/24;
	else
		d=6+30*(m-1)+d+h/24;
	var M=74.2023+0.98560026*d;
	var L=-2.75043+0.98564735*d;
	var lst=8.3162159+0.065709824*Math.floor(d)+1.00273791*24*(d%1)+lg/15;	
	var e=0.0167065;
	var omega=4.85131-0.052954*d;
	var ep=23.4384717+0.00256*cosd(omega);
	var ed=180.0/Math.PI*e;
	var u=M;
	for(var i=1;i<5;i++)
		u=u-(u-ed*sind(u)-M)/(1-e*cosd(u));
	var v=2*atand(tand(u/2)*Math.sqrt((1+e)/(1-e)));
	var theta=L+v-M-0.00569-0.00479*sind(omega);
	var delta=asind(sind(ep)*sind(theta));
	var alpha=180.0/Math.PI*Math.atan2(cosd(ep)*sind(theta),cosd(theta));
	if(alpha>=360)
		alpha-=360;
	var ha=lst-alpha/15;
	var zr=Round(h-ha,24);
	return ([zr,delta])
}

function ephemeris_init()
{
lgs= [0,49.28,50.53,49.22,49.75,50.06,50.06,49.69,49.51,49.74,49.20,49.76,49.44,50.47,49.70,48.30,45.07,51.64,48.68,46.42,57.33,56.29,50.84,59.21,46.28,51.41,48.34,49.59,60.86,48.50,53.06,53.39,47.00,50.86,52.52,50.00,50.88,57.06,47.09,54.44,59.58,48.52,51.59,54.35];
lats=[0,35.75,36.03,35.57,35.93,50.06,35.76,36.07,35.92,35.81,36.64,35.89,35.99,36.45,34.09,38.25,37.55,32.68,31.32,33.64,37.47,27.19,28.97,32.86,38.08,35.70,33.46,37.28,29.50,36.68,36.57,35.58,35.31,32.33,29.62,36.28,34.64,30.29,34.34,36.84,36.31,34.80,30.67,31.89];
gheble=[0,32.5,35.46,32.64,33.5,33.92,34.67,33.11,32.85,33.71,30.73,33.62,32.54,34.57,37.03,25.84,17.15,45.97,40.79,27.16,48.2,72.53,55.33,61.23,20.33,38.42,34.18,30.77,71.81,28.67,40.84,43.59,26.24,44.7,57.38,33.57,39.2,64.27,28.21,43.46,54.48,31.99,51.56,54.61];
}

function coord()
{
	var c=document.getElementById("cities");
	var i = c.selectedIndex;
	if(i==0)
	{
		document.getElementById("longitude").value="";
		document.getElementById("latitude").value="";
	}
	else
	{
		document.getElementById("longitude").value=lgs[i].toString();
		document.getElementById("latitude").value=lats[i].toString();
//		document.getElementById("t9").value=gheble[i].toString();		
		
	}
}
function sind(x){return(Math.sin(Math.PI/180.0*x));}
function cosd(x){return(Math.cos(Math.PI/180.0*x));}
function tand(x){return(Math.tan(Math.PI/180.0*x));}
function atand(x){return(Math.atan(x)*180.0/Math.PI);}
function asind(x){return(Math.asin(x)*180.0/Math.PI);}
function acosd(x){return(Math.acos(x)*180.0/Math.PI);}
function sqrt(x){return(Math.sqrt(x));}
function frac(x){return(x%1);}
function floor(x){return(Math.floor(x));}
function ceil(x){return(Math.ceil(x));}
function loc2hor(z,d,p){
	return(acosd((cosd(z)-sind(d)*sind(p))/cosd(d)/cosd(p))/15);
}
function Round(x,a){
	var tmp=x%a;
	if(tmp<0)
		tmp+=a;
	return(tmp)
}
function hms(x, month)
{
	x=Math.floor(3600*x);
	h=Math.floor(x/3600);
	mp=x-3600*h;
	m=Math.floor(mp/60);
	s=Math.floor(mp-60*m);
	if (month<6)
		h++;	
	return(((h<10)? "0" : "")+h.toString()+":"+((m<10)? "0" : "")+m.toString()+":"+((s<10)? "0" : "")+s.toString())
}



var alarmTime = new Array(3);
var curTime;

function IfZero(num) 
{
	return ((num <= 9) ? ("0" + num) : num);
}
function GetTime() 
{
	var dt = new Date();
	setTimeout("GetTime()", 1000);
	curTime = (IfZero(dt.getHours()) + ":" + IfZero(dt.getMinutes()));
}
function AlarmSet() 
{
	alarmTime[0] = MM_findObj("spanMorning").innerHTML;
	alarmTime[1] = MM_findObj("spanNoon").innerHTML;
	alarmTime[2] = MM_findObj("spanNight").innerHTML;
}

function ShowAzanWindow()
{
	azanWindow = MM_findObj("divAzan");
	if (azanWindow)
	{
		azanWindow.style.left = document.body.scrollLeft+ (screen.width-400)/2;
		azanWindow.style.top  = document.body.scrollTop + (screen.height-500)/2;
		azanWindow.style.display = "block";
		
		var AzanType = "اذان صبح";
		var AzanImage = "morning";
		if (alarmTime[1] == curTime)
		{ 
			AzanType = "اذان ظهر";
			AzanImage = "noon";
		 }
		else {
			if (alarmTime[2] == curTime)
			{ 
				AzanType = "اذان مغرب"; 
				AzanImage = "night";
			}
		}
		MM_findObj("spanAzanType").innerHTML = AzanType;
		MM_findObj("imgAzan").src = "images/azan-" + AzanImage + ".jpg";

		opaque = MM_findObj("divOpaque");
		if (opaque)
		{
			opaque.style.display = "block";
			opaque.style.width  = document.body.scrollWidth;
			opaque.style.height = document.body.scrollHeight;
			opaque.style.filter = "alpha(opacity=60)";
		}
	}
}

function HideAzanWindow()
{
	azanWindow = MM_findObj("divAzan");
	if (azanWindow)
	{
		azanWindow.style.display = "none";
	}
	
	opaque = MM_findObj("divOpaque");
	if (opaque)
	{
		opaque.style.display = "none";
	}
}

function AlarmOn() 
{
	// alert(curTime + "\n" + alarmTime[0]+" - "+ alarmTime[1] + " - " + alarmTime[2]);
	if ((alarmTime[0] == curTime) | (alarmTime[1] == curTime) | (alarmTime[2] == curTime))
	{
		loadflash('flashazan', 'flashazan', './images/' + MM_findObj("spanMuezzin").innerHTML, '60', '20');
		setTimeout("AlarmOn()", 600000);
		ShowAzanWindow();
	}
	else 
	{
		setTimeout("AlarmOn()", 1000)
	}
}
function AlarmOff() 
{
	MM_findObj("flashazan").innerHTML = "";
	alarmTime[0]="";
	alarmTime[1]="";
	alarmTime[2]="";
}
