// Create an empty object as our namespace
if (typeof ASTROSIGN == "undefined") {
	var ASTROSIGN = {};
}

(function () {

	//----------------------------------------------------------------------
	//	Function:	getSignFromDatesArray (private)
	//
	//	Purpose:	Obtain the sun  sign from a passed
	//				javascript Date() object defining a birthday. 
	//				and an array defining the limiting dates and signs
	//
	//	Returns:	Astrological sign as a string.
	//				null if not foud or bad input.
	//
	//	Date		Initials	Version		Comments
	//  ----------	---------	----------	---------------------------
	//	2007/10/10	DWW			1.0.0		New for Birthday Sky page at LiveScience
	//
	//----------------------------------------------------------------------
	function getSignFromDatesArray(inBirthday, inDatesSignsArray)
	{
		if (	(inBirthday	=== undefined)				||
				(typeof inBirthday != "object")			||
				(inBirthday.getMonth() === undefined)	||
				(inBirthday.getDate() === undefined)	||
				(inDatesSignsArray === undefined)		||
				(inDatesSignsArray.length === undefined) ||
				(inDatesSignsArray.length === 0))
		{
			return null;
		}
	
		// iterate through the traditional dates array
		for (var i = 0; i < inDatesSignsArray.length; i++)
		{
			var signObject = inDatesSignsArray[i];
			if (fallsWithinDates(inBirthday, signObject)) {
				return signObject["s"];
			}
		}
	
		return null; // if we get here without returning we haven't found the sign
	}
	
	//----------------------------------------------------------------------
	//	Function:	fallsWithinDates (private)
	//
	//	Purpose:	Determine whether the passed birthday falls witin the dates
	//				of the passed signObject
	//
	//	Returns:	True if it does. False otherwise!
	//
	//	Date		Initials	Version		Comments
	//  ----------	---------	----------	---------------------------
	//	2007/10/12	DWW			1.0.0		New for Birthday Sky page at LiveScience
	//
	//----------------------------------------------------------------------
	function fallsWithinDates(inBirthday, signObject)
	{
		var birthMonth	= inBirthday.getMonth() + 1; // JS Date objects have month [0..11]
		var birthDay	= inBirthday.getDate();
		
		var startMonth = signObject["sm"];
		var endMonth = signObject["em"];

		var startDay = signObject["sd"];
		var endDay = signObject["ed"];
		
		if (startMonth == endMonth)
		{
			if ((birthMonth == startMonth) && (birthDay >= startDay) && (birthDay <= endDay)) {
				return true;
			}
			else {
				return false;
			}
		}
		
		if ((birthMonth >= startMonth) && (birthMonth <= endMonth) || (startMonth > endMonth))
		{
			if ((birthMonth == startMonth) && (birthDay >= startDay)) {
				return true;
			}
			
			if ((birthMonth == endMonth) && (birthDay <= endDay)) {
				return true;
			}
			
			if ((birthMonth > startMonth) && (birthMonth < endMonth)) {
				return true; // rare case (nonexistant as at authoring time) when a sign spans three calendar months
			}
		}
		
		return false;
	}

	//----------------------------------------------------------------------
	//	Function:	getRealSign (public)
	//
	//	Purpose:	Obtain the true Sun sign from a passed
	//				javascript Date() object defining a birthday. 
	//				(Valid for birthdays in roughly the 1960's - 1990's)
	//
	//	Returns:	Astrological sign as a string.
	//				null if not foud or bad input.
	//
	//	Date		Initials	Version		Comments
	//  ----------	---------	----------	---------------------------
	//	2007/10/10	DWW			1.0.0		New for Birthday Sky page at LiveScience
	//
	//----------------------------------------------------------------------
	function getRealSign(inBirthday)
	{
		var realDates	= [		{sm:1, sd:20, em:2, ed:16, s:"Capricorn"},
								{sm:2, sd:17, em:3, ed:11, s:"Aquarius"},
								{sm:3, sd:12, em:4, ed:18, s:"Pisces"},
								{sm:4, sd:19, em:5, ed:13, s:"Aries"},
								{sm:5, sd:14, em:6, ed:21, s:"Taurus"},
								{sm:6, sd:22, em:7, ed:20, s:"Gemini"},
								{sm:7, sd:21, em:8, ed:10, s:"Cancer"},
								{sm:8, sd:11, em:9, ed:16, s:"Leo"},
								{sm:9, sd:17, em:10, ed:30, s:"Virgo"},
								{sm:10, sd:31, em:11, ed:23, s:"Libra"},
								{sm:11, sd:24, em:11, ed:29, s:"Scorpio"},
								{sm:11, sd:30, em:12, ed:17, s:"Ophiuchus"},
								{sm:12, sd:18, em:1, ed:19, s:"Sagittarius"},
						];
							
		return getSignFromDatesArray(inBirthday, realDates);
	}

	//----------------------------------------------------------------------
	//	Function:	getTraditionalSign (public)
	//
	//	Purpose:	Obtain the traditional astrological sign from a passed
	//				javascript Date() object defining a birthday.
	//
	//	Returns:	Astrological sign as a string.
	//				null if not foud or bad input.
	//
	//	Date		Initials	Version		Comments
	//  ----------	---------	----------	---------------------------
	//	2007/10/10	DWW			1.0.0		New for Birthday Sky page at LiveScience
	//
	//----------------------------------------------------------------------
	function getTraditionalSign(inBirthday) 
	{
		var traditionalDates= [	{sm:12, sd:22, em:1, ed:19, s:"Capricorn"},
								{sm:1, sd:20, em:2, ed:18, s:"Aquarius"},
								{sm:2, sd:19, em:3, ed:20, s:"Pisces"},
								{sm:3, sd:21, em:4, ed:19, s:"Aries"},
								{sm:4, sd:20, em:5, ed:20, s:"Taurus"},
								{sm:5, sd:21, em:6, ed:21, s:"Gemini"},
								{sm:6, sd:22, em:7, ed:22, s:"Cancer"},
								{sm:7, sd:23, em:8, ed:22, s:"Leo"},
								{sm:8, sd:23, em:9, ed:22, s:"Virgo"},
								{sm:9, sd:23, em:10, ed:22, s:"Libra"},
								{sm:10, sd:23, em:11, ed:21, s:"Scorpio"},
								{sm:11, sd:22, em:12, ed:21, s:"Sagittarius"},
						];
	
		return getSignFromDatesArray(inBirthday, traditionalDates);
	}
		
	// expose the methods we want to be public through our ASTROSIGN object
	ASTROSIGN.getTraditionalSign	= getTraditionalSign;
	ASTROSIGN.getRealSign			= getRealSign;
})();


