/* (c) Brightlemon Ltd 2009 */

var Ecard = Class.create({
		
		initialize: function(title, url)
		{
			if((!title) || (!url) || (title.length < 2) || (url.length < 4))
			{
				alert("Insufficient parameters provided for Ecard");
				return false;
			}
			
			this.title = title;
			this.url = url;
		},
		
		generateHTML: function()
		{
			
			this.outerDiv = new Element('div');
			this.outerDiv.addClassName('ecard_item').observe('click', this._clickHandle.bindAsEventListener(this));
			
			var img = new Element('img', {'src' : SKIN_URL + 'images/ecard/' + this.url + '-mini.gif'});
			var title = new Element('a', {'href' : '#'}).setStyle({'display' : 'block'}).update(this.title);
			
			Element.insert(this.outerDiv, img);
			Element.insert(this.outerDiv, title);
			Element.insert('ecard_div', this.outerDiv);
		},
		
		_clickHandle: function(event)
		{
			$$('#ecard_form img')[0].src = SKIN_URL + 'images/ecard/' + this.url + '.gif';
			$('ecard_name').innerHTML = '"' + this.title + '"';
			$('submitButton').observe('click', this._sendToServer.bindAsEventListener(this));
			
			Effect.Appear('gray_bg', {duration: 0.5, to: 0.5});
			Effect.Appear('ecard_form', {duration: 0.5});

			
		},
		
		_sendToServer: function(event)
		{
			var success = validEmail($('my_email')) & validEmail($('friend_email'));
			
			if(!success)
				return false;
			
			var data = $('ecard_form').serialize(true);
			data.ecard_title = this.title;
			data.ecard_url = SKIN_URL + 'images/ecard/' + this.url + '.gif';
			
			new Ajax.Request(SKIN_URL + "../../../../ecard-send.php", 
				{
					method: 'post',
					parameters: data,
					onSuccess: this._sendDone.bind(this)
				});

		},
		
		_sendDone: function(transport)
		{
			$('ecard_form').innerHTML = "<h3>Thanks, and have a noodle-tastic day!</h3>";
			setTimeout("window.location = window.location;", 2000);
			
		}
});

var cards = [];
cards.push(	new Ecard('Ain\'t no sunshine', 'aint-no-sunshine'),
		new Ecard('Don\'t make me angry', 'hulk'),
		new Ecard('Wish you were here', 'wish-you-were-here'),
		new Ecard('Love hurts', 'love-hurts'),
		new Ecard('Noodoll Valentine', 'valentine1'),
		new Ecard('Wishing You Prosperity', 'prosperity'),
		new Ecard('Pencil Love', 'pencillove'),
		new Ecard('You\'re a Great Friend', 'friends'),
		new Ecard('You\'re Invited', 'invite'),
		new Ecard('Happy Holidays', 'happy-holidays'),
		new Ecard('Good Luck, You\'ll be Fine!', 'goodluck'),
		new Ecard('Good Luck, You\'ll need it!', 'goodluck2'),
		new Ecard('Happy Birthday to You', 'birthdaycake'),
		new Ecard('Happy Birthday to You 2', 'birthdaycake2'),
		new Ecard('We\'re Watching You', 'watchingyou')
	);


Event.observe(window, 'load', function()
	{
		cards.each(function(a){a.generateHTML();});
		
	});


function hidePopup()
{
	Effect.Fade('gray_bg', {duration: 0.5});
	Effect.Fade('ecard_form', {duration: 0.5});
}

function validEmail(email)
{
	if((email.value.length < 5)||(email.value.indexOf('@') < 0)||(email.value.indexOf(".") < 0))
	{
		email.style.border = "1px solid red";
		return false;
	}
	else
	{
		email.style.border = "";
		return true;
	}
}