jQ(function() {

	prayerTimes.init();

});

var prayerTimes = {

	init : function() {
		var currentTime = new Date();
		prayerTimes.setFajr(currentTime);
		prayerTimes.setZuhr(currentTime);
		prayerTimes.setAsr(currentTime);
		prayerTimes.setMaghrib(currentTime);
		prayerTimes.setIsha(currentTime);
	},

	setFajr : function(currentTime) {
		// get fajr Iqama time to compare.
		 var fajrIqama = jQ('#fajr-iqama'); 
		 var fajrIqamaTime = prayerTimes.parseTime(fajrIqama.attr('data-today'));
		  
		 // if current time has passed fajr iqama then display tomorrows time
		 if(prayerTimes.compare(currentTime, fajrIqamaTime) === 1) { 
			 var fajrAdhan = jQ('#fajr-adhan'); 
			 fajrAdhan.html(fajrAdhan.attr('data-tomorrow'));
			 fajrIqama.html(fajrIqama.attr('data-tomorrow')); 
			 jQ('#fajr-status').html("Tomorrow").css('color','#D92900');
		 }
	},

	setZuhr : function(currentTime) {
		// get zuhr Iqama time to compare.
		var zuhrIqama = jQ('#zuhr-iqama'); 
		var zuhrIqamaTime = prayerTimes.parseTime(zuhrIqama.attr('data-today'));

		// if current time has passed fajr iqama then display tomorrows time
		if(prayerTimes.compare(currentTime, zuhrIqamaTime) === 1) {
			var zuhrAdhan = jQ('#zuhr-adhan');
			zuhrAdhan.html(zuhrAdhan.attr('data-tomorrow'));
			zuhrIqama.html(zuhrIqama.attr('data-tomorrow')); 
			jQ('#zuhr-status').html("Tomorrow").css('color','#D92900');
		}
	},

	setAsr : function(currentTime) {
		// get zuhr Iqama time to compare.
		var asrIqama = jQ('#asr-iqama'); 
		var asrIqamaTime = prayerTimes.parseTime(asrIqama.attr('data-today'));

		// if current time has passed fajr iqama then display tomorrows time
		if(prayerTimes.compare(currentTime, asrIqamaTime) === 1) {
			var asrAdhan = jQ('#asr-adhan');
			asrAdhan.html(asrAdhan.attr('data-tomorrow'));
			asrIqama.html(asrIqama.attr('data-tomorrow')); 
			jQ('#asr-status').html("Tomorrow").css('color','#D92900');
		}
	},

	setMaghrib : function(currentTime) {
		// get zuhr Iqama time to compare.
		var maghribIqama = jQ('#maghrib-iqama'); 
		var maghribIqamaTime = prayerTimes.parseTime(maghribIqama.attr('data-today'));

		// if current time has passed fajr iqama then display tomorrows time
		if(prayerTimes.compare(currentTime, maghribIqamaTime) === 1) {
			var maghribAdhan = jQ('#maghrib-adhan');
			maghribAdhan.html(maghribAdhan.attr('data-tomorrow'));
			maghribIqama.html(maghribIqama.attr('data-tomorrow')); 
			jQ('#maghrib-status').html("Tomorrow").css('color','#D92900');
		}
	},

	setIsha : function(currentTime) {
		// get zuhr Iqama time to compare.
		var ishaIqama = jQ('#isha-iqama'); 
		var ishaIqamaTime = prayerTimes.parseTime(ishaIqama.attr('data-today'));

		// if current time has passed fajr iqama then display tomorrows time
		if(prayerTimes.compare(currentTime, ishaIqamaTime) === 1) {
			var ishaAdhan = jQ('#isha-adhan');
			ishaAdhan.html(ishaAdhan.attr('data-tomorrow'));
			ishaIqama.html(ishaIqama.attr('data-tomorrow')); 
			jQ('#isha-status').html("Tomorrow").css('color','#D92900');
		}
	},

	parseTime : function(timeString) {
		if (timeString == '')
			return null;
		var time = timeString.match(/(\d+)(:(\d\d))?\s*(p?)/i);
		if (time == null)
			return null;
		var hours = parseInt(time[1], 10);
		if (hours == 12 && !time[4]) {
			hours = 0;
		} else {
			hours += (hours < 12 && time[4]) ? 12 : 0;
		}
		var d = new Date();
		d.setHours(hours);
		d.setMinutes(parseInt(time[3], 10) || 0);
		d.setSeconds(0, 0);
		return d;

	},

	compare : function(date1, date2) {
		var Result;

		var date = new Date(date1);
		var dateToCompare = new Date(date2);

		date.setSeconds(1, 1);
		dateToCompare.setSeconds(1, 1);

		// Do the comparison
		if (date.getTime() == dateToCompare.getTime()) {
			Result = 0;
		} else if (date.getTime() < dateToCompare.getTime()) {
			Result = -1;
		} else {
			Result = 1;
		};

		// Return the results
		return Result;
	}
};
