if(!ArabicDate)
{
  var ArabicDate = {};
}

ArabicDate.arabicWeekDays = ["الأحد", "الإثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعة", "السبت"];
ArabicDate.arabicMonths = ["محرم","صفر","ربيع الأول","ربيع الثاني","جمادى الأولى","جمادى الأخرى","رجب","شعبان","رمضان","شوال","ذو القعدة","ذو الحجة"];
ArabicDate.gregMonths = ["يناير" ,"فبراير" ,"مارس" ,"ابريل" ,"مايو" ,"يونيو","يوليو","اغسطس","سبتمبر" ,"اكتوبر","نوفمبر","ديسمبر"];
ArabicDate.short = 1;
ArabicDate.long = 2;
ArabicDate.numeric = 3;
ArabicDate.hijriDate = 1;
ArabicDate.gregorianDate = 2;
ArabicDate.hijri_difference = 0;

function ard_int(num) { 
      return (num < -0.0000001) ? Math.ceil(num - 0.0000001) : Math.floor(num + 0.0000001); 
}


ArabicDate.toGregTime = function(date, format, hourFormat){
	return ArabicDate.toGregDate(date, format)+ " "+ArabicDate.arabicTimeString(date, hourFormat);
}

ArabicDate.toHijriTime = function(date, format, hourFormat){
	return  ArabicDate.toHijriDate(date, format)+" "+ ArabicDate.arabicTimeString(date, hourFormat);
}

ArabicDate.localize = function(date, type, format){
	if(type == ArabicDate.hijriDate)
		ArabicDate.toHijriDate(date,format);
	else
		ArabicDate.toGregDate(date,format);
}

ArabicDate.toGregDate = function(date, format, hourFormat){
	wd = ArabicDate.arabicWeekDays[date.getDay()];
	switch(format){
		case ArabicDate.short:
			return date.getDate() + " " + ArabicDate.gregMonths[date.getMonth()] + " " + date.getFullYear().toString();
		case ArabicDate.numeric:
			return date.getFullYear().toString() + "-" + (date.getMonth()+1).toString() + "-" + date.getDate().toString();
		default:
			return wd + " " + date.getDate()+ " " + ArabicDate.gregMonths[date.getMonth()] + " " + date.getFullYear().toString(); 
	}
}

ArabicDate.toHijriDate = function(date,format, hourFormat){
	if(hij_deff = $("hijri_difference"))
		ArabicDate.hijri_difference = hij_deff.value ;
	
	//day = date.getDay() + parseInt(ArabicDate.hijri_difference) ;
	
	//if(day < 0)
		//day += 7 ;	
		
	d = date.getDate() ;
	m = date.getMonth() + 1;
	y = date.getFullYear() ;	
	wd = ArabicDate.arabicWeekDays[date.getDay()];
	
	if ((y>1582) || ((y==1582) && (m>10)) || ((y==1582) && (m==10) && (d>14))) { 
		jd = ard_int((1461 * (y + 4800 + ard_int((m-14) / 12))) / 4); 
		jd += ard_int((367 * (m - 2 - 12 * (ard_int((m-14) / 12)))) / 12); 
		jd -= ard_int((3 * (ard_int((y + 4900 + ard_int((m - 14) / 12)) / 100))) / 4); 
		jd += d - 32075; 
	} 
	else { 
		jd = 367*y-ard_int((7*(y+5001 + ard_int((m-9)/7)))/4) + ard_int((275*m)/9)+d+1729777; 
	} 
		
	l = jd - 1948440 + 10632; 
	n = ard_int((l-1) / 10631); 
	l = l - 10631 * n + (355 +  parseInt(ArabicDate.hijri_difference)); // Correction: 355 instead of 354 
	j = (ard_int((10985-l)/5316)) * (ard_int((50*l)/17719)) + (ard_int(l/5670)) * (ard_int((43*l)/15238)); 
	l = l-(ard_int((30-j)/15)) * (ard_int((17719*j)/50)) - (ard_int(j/16)) * (ard_int((15238*j)/43))+29; 
	m = ard_int((24*l)/709); 
	d = l-ard_int((709*m)/24); 
	y = 30*n+j-30; 
	
	switch(format){
		case ArabicDate.short:
			return d+" "+ArabicDate.arabicMonths[m-1]+" "+y;
		case ArabicDate.numeric:
			return y+"-"+m+"-"+d;
		default:
			return  wd + " " + d + " "+ ArabicDate.arabicMonths[m-1] + " " + y; 
	}
}

ArabicDate.arabicTimeString = function(date, hourFormat){

	curr_hour = date.getHours();	
	curr_minute = date.getMinutes() ;

	if (curr_hour < 10)
		curr_hour = "0" + curr_hour ;

	if (curr_minute < 10)
		curr_minute = "0" + curr_minute ;
		
	if (hourFormat == "12"){
		if (date.getHours() < 12){
		 a_p = "ص";
		}
		else{
		 a_p = "م";
		}		
		
		if (curr_hour == 0) {
			curr_hour = 12;
		}
		else if (curr_hour > 12){
	 		curr_hour = date.getHours() - 12;
		}					
	}
			
	curr_hour += ":" + curr_minute ;
	
	if (hourFormat == "12") {
		curr_hour += " " + a_p ;				
	}	
		
	return curr_hour	
}
