﻿/// <reference path="util.js" />

function ajaxFailure() {
	alert('Sorry, horoscopes are currently not available.');
}

function ajaxRequest(url, parameterInfo, successFunction) {
	new Ajax.Request(url, {
		method: 'post',
		parameters: parameterInfo,
		onSuccess: successFunction, 
		onFailure: ajaxFailure
	});
}	

function horoscopes() {
	var self = this;
	this.element = null;
	this.current = null;
	
	this.close = function() {
		if(self.current != null) {
			document.body.removeChild(self.current);
			self.current = null;
		}
	}
	
	this.loaded = function(transport, json) {
		var div = document.createElement('div');
		div.className = 'horoscopeWindow';
		var pos = util.findPos(self.element);
		div.style.left = (pos.left - 400) + 'px';
		div.style.top = (pos.top - 290) + 'px';
		div.innerHTML = transport.responseText;	
		div.innerHTML += '<p>Horoscope provided by <a target="blank" href="http://www.astrosage.com">www.astrosage.com</a></p>';

		var close = document.createElement('p');
		close.className = 'horoscopeClose';
		close.innerHTML = 'Close horoscope';
		close.onclick = self.close;
		div.appendChild(close);
		
		var close2 = document.createElement('div');
		close2.className = 'horoscopeClose2';
		close2.onclick = self.close;
		close2.innerHTML = '&nbsp;';
		div.appendChild(close2);
		
		self.current = div;
		
		document.body.appendChild(div);
	}
	
	this.click = function() {
		self.close();
		
		var params = new Object();
		params.value = this.innerHTML;
		self.element = this;
		
		ajaxRequest('components/getHoroscope.aspx', params, self.loaded);
		return false;
	}
}

var horoscope;
util.addEvent(window, 'load', function() {
	horoscope = new horoscopes();
	
	var div = document.getElementById('horoscope');
	var a = div.getElementsByTagName('a');
	for(var i=0; i<a.length; i++) {
		a[i].onclick = horoscope.click;
	}
	div.style.display = 'block';
	
});

