/**
 * The main JS file contains the algorithm for random show a horoscope Sign
 * author: Derick Koo / ConRic Consultancy Services (http://www.conric.com/)
 */
 
 
var zodiacNames = [
 'aries',
 'taurus',
 'gemini',
 'cancer',
 'leo',
 'virgo',
 'libra',
 'scorpio',
 'sagittarius',
 'capricorn',
 'aquarius',
 'pisces'
];

var zodiacDates = [
  'March 21-April 19',
  'April 20-May 20',
  'May 21-June 21',
  'June 22-July 22',
  'July 23-August 22',
  'August 23-September 22',
  'September 23-October 22',
  'October 23 - November 21',
  'November 22-December 21',
  'December 22-January 19',
  'January 20-February 18',
  'February 19-March 20'
];


/**
 * print date in simple format MM DD, YYYY
 */
function getSimpleDate(date) {
	var months = [
      'January',
      'February',
      'March',
      'April',
      'May',
      'June',
      'July',
      'August',
      'September',
      'October',
      'November',
      'December'
    ];
	if (!date) {
		date = new Date();
	}
	return months[date.getMonth()] + ' ' + date.getDate() + ', ' + date.getFullYear();
}

/**
 * The main function used to output horoscope sign to the page
 */
function printHoroscopeImages() {
	var seed = Math.ceil((Math.random() * 15)); // generate a decimal btw. 0 - 15
	if (seed < 12) { // output single zodiact
		renderSign(seed);
	} else { // out full zodiacs with 12 signs
		for (var i = 0; i < 12; i++) {
			renderSign(i);
		}
	}
}

/**
 * render out all 12 signs
 */
function printAllHoroscopeImages() {
	for (var i = 0; i < 12; i++) {
		renderSign(i);
	}
}

/**
 * render out the cooresponding zodiac image
 */
function renderSign(index) {
	var pattern = '<a href="free_daily_horoscope_view.html?#INDEX" ' +
			'onmouseover="swapImage(\'zodiac-#NAME\', \'#INDEXo.gif\');" ' +
			'onmouseout="swapImage(\'zodiac-#NAME\', \'#INDEX.gif\');" class="Sign">' +
			'<img src="#IMAGE" alt="#NAME" name="zodiac-#NAME"/></a>';
    var out = pattern.replace(/#NAME/g, zodiacNames[index])
           .replace(/#IMAGE/g, '../_images/signs/' + index + '.gif')
           .replace(/#INDEX/g, index);
           
    document.writeln(out);
}

/**
 * swap sign images, 
 */
function swapImage(name, timage) {
	document[name].src = '../_images/signs/' + timage;
}

/**
 * load all the 12 horoscope signs
 */
function preloadSignImages() {
	var img = new Image();
    for (var i = 0; i < 12; i++) {
    	img.src = i + '.gif';
    	img.src = i + 'o.gif';
    }	
}

/**
 * used in the view page
 */
var __zodiacIndex = 0;

function retrieveZodiacIndex() {
    if (location.search.charAt(0) == '?') {
	    __zodiacIndex = parseInt(location.search.substring(1));
	    if (__zodiacIndex > 11) {
	    	__zodiacIndex = 11;
	    }
    }	
}

function printHoroscopeContent() {
	if (!__zodiacIndex) {
		__zodiacIndex = 0;
	}
	var seed = Math.ceil(Math.random() * 11);
	var zodiacContent = Horoscope.Content[__zodiacIndex][seed];
	var pattern = '<img src="#IMAGE" alt="#NAME" />' +
			'<div style="font-weight: bold;font-size: 18px; color: #900;">#UNAME <span style="font-size: 11px; font-weight: normal; font-style: italic; color: #36c;">#DATES</span></div>' +
			'<div style="color: #900;">Daily Overview: #CURRENT_DATE</div><br />#CONTENT';
	var content = pattern.replace(/#IMAGE/, '../_images/signs/' + __zodiacIndex + 'z.gif')
	        .replace(/#NAME/, zodiacNames[__zodiacIndex])
	        .replace(/#UNAME/, zodiacNames[__zodiacIndex].toUpperCase())
	        .replace(/#DATES/, zodiacDates[__zodiacIndex])
	        .replace(/#CURRENT_DATE/, getSimpleDate())
	        .replace(/#CONTENT/, zodiacContent);
	document.writeln(content);
}

function printZodiacName() {
	if (!__zodiacIndex) {
		__zodiacIndex = 0;
	}
	var pattern = '#CONTENT';
	document.writeln(pattern.replace(/#CONTENT/g, zodiacNames[__zodiacIndex]));
}

/**
 * if use enter view.html directly, redirect to horoscope.html page automatically
 */
function redirectIfNessary() {
	if (location.search.charAt(0) !== '?') {
		location.href = 'horoscope.html';
	}
}