function select_all() { var text_val=eval("document.widget.widget"); text_val.focus(); text_val.select(); }
if (typeof TP === 'undefined') {
	var TP = {};
}

TP.gratitudeWidget = function(containerDomId) {
	this.containerDomId = containerDomId;
	this.containerEl = $('#'+ this.containerDomId);

	var widget = this;
	$.sifr({
		build: 436,
		version: 3,
		save: true,
		font: widget.messageSifrFontUrl // Avoid broken '.swf' request
	});

	this.containerEl.css({
		position: 'relative',
		width: '235px',
		height: '320px',
		background: 'url('+ widget.imageBase +'widget.png) no-repeat'
	});

	this.challengeLinkEl = $('#'+ this.challengeLinkDomId);
	this.messageAreaEl = $('#'+ this.messageAreaDomId);
	this.shareEl = $('#'+ this.shareDomId);
	this.linkAreaEl = $('#'+ this.linkAreaDomId);
	this.joinLinkEl = $('#'+ this.joinLinkDomId);
	this.blogsLinkEl = $('#'+ this.blogsLinkDomId);
	this.tpLinkEl = $('#'+ this.tpLinkDomId);

	this.challengeLinkEl.css({
		position: 'absolute',
		left: '30px',
		top: '5px',
		width: '175px',
		height: '45px'
	});
	this.challengeLinkEl.find('a').html('<div style="width:175px;height:45px;cursor:pointer;"></div>');

	this.shareEl.css({
		position: 'absolute',
		left: '60px',
		top: '212px'
	});
	this.shareEl.find('a').html('<img src="'+ this.imageBase +'share.png" style="border:0;" />');

	this.linkAreaEl.css({
		position: 'absolute',
		left: '8px',
		top: '258px',
		fontSize: '11px'
	});
	var linkAreaLinks = this.linkAreaEl.find('a');
	linkAreaLinks.css('color', '#38736d');
	linkAreaLinks.hover(
		function () {
			this.style.color = '#183a37';
		},
		function () {
			this.style.color = '#38736d';
		}
	);

	this.tpLinkEl.css({
		position: 'absolute',
		left: '130px',
		top: '289px',
		width: '95px',
		height: '21px'
	});
	this.tpLinkEl.find('a').html('<div style="width:95px;height:21px;cursor:pointer;"></div>');

	this.renderBase();
	this.changeDay(this.displayDay);
};
TP.gratitudeWidget.prototype = {
	containerDomId: null,
	challengeLinkDomId: 'tp_gratitude_widget_gc_link',
	messageAreaDomId: 'tp_gratitude_widget_message_area',
	dayDomId: 'tp_gratitude_widget_day',
	messageDomId: 'tp_gratitude_widget_daily_message',
	nextDomId: 'tp_gratitude_widget_next',
	previousDomId: 'tp_gratitude_widget_previous',
	shareDomId: 'tp_gratitude_widget_share',
	linkAreaDomId: 'tp_gratitude_widget_link_area',
	joinLinkDomId: 'tp_gratitude_widget_join',
	blogsLinkDomId: 'tp_gratitude_widget_blogs',
	tpLinkDomId: 'tp_gratitude_widget_tp_link',

	containerEl: null,
	challengeLinkEl: null,
	messageAreaEl: null,
	messageEl: null,
	dayEl: null,
	messageEl: null,
	nextEl: null,
	previousEl: null,
	shareEl: null,
	linkAreaEl: null,
	joinLinkEl: null,
	blogsLinkEl: null,
	tpLinkEl: null,

	imageBase: 'http://www.gratitudechallenge.com/wp-content/themes/the_gratitude_challenge/widget/',
	daySifrFontUrl: 'http://www.gratitudechallenge.com/wp-content/themes/the_gratitude_challenge/flash/fonts/alabama.swf',
	messageSifrFontUrl: 'http://www.gratitudechallenge.com/wp-content/themes/the_gratitude_challenge/flash/fonts/lindsey.swf',

	startDate: new Date('2009/09/22'),
	currentDate: new Date('2009/09/20'),
	displayDay: null,

	preChallengeMessage: 'Sign the pledge, make a commitment.',
	// preChallengeMessage: 'Welcome to the Gratitude Challenge. Look at the brighter side of life.',
	postChallengeMessage: 'Thanks for taking the time to take note and give thanks.',
	dailyMessages: [
		'Sign the pledge, make a commitment.',
		'Use the alphabet to take note of what you\'re grateful for.',
		'Write about something you feel grateful for today.',
		'Give thanks for something negative in your life.',
		'Write a list of all the blessings you currently have.',
		'Call someone you haven\'t talked to in a while.',
		'Take a picture of something that makes you feel grateful.',
		'Send thank you notes to 5 people in your life.',
		'Enjoy the people around you.',
		'Pick one of your senses and give thanks for it.',
		'See the world through the eyes of a child.',
		'Dare to see the glass half full today.',
		'Assume goodness in people today.',
		'Reflect on how the challenge has changed your perspective.',
		'Appreciate and give thanks for your unique qualities.',
		'Look in the mirror, focus on what you love about yourself.',
		'Perform random acts of kindness throughout the day.',
		'Write about all the things that make you so lovable.',
		'Have confidence in all the choices you make today.',
		'Thank yourself for taking the time to be grateful.',
		'Reflect on the 21 days of The Gratitude Challenge.'
	],

	renderBase: function renderBase() {
		var widget = this;

		if (this.containerEl) {
			this.messageAreaEl.html('<div id="'+ this.dayDomId +'"></div>\
				<div id="'+ this.messageDomId +'"></div>\
				<div id="'+ this.previousDomId +'"><button style="border:0;background:transparent;cursor:pointer;text-align:left;"><img src="'+ this.imageBase +'previous.png" style="margin-bottom:-3px;" /> Previous</button></div>\
				<div id="'+ this.nextDomId +'"><button style="border:0;background:transparent;cursor:pointer;text-align:right;">Next <img src="'+ this.imageBase +'next.png" style="margin-bottom:-3px;" /></button></div>\
			');
			this.dayEl = $('#'+ this.dayDomId);
			this.messageEl = $('#'+ this.messageDomId);
			this.nextEl = $('#'+ this.nextDomId);
			this.previousEl = $('#'+ this.previousDomId);

			this.messageAreaEl.css({
				position: 'absolute',
				left: '20px',
				top: '55px',
				width: '193px',
				height: '142px',
				textAlign: 'center'
			});
			var messageAreaButtons = this.messageAreaEl.find('button');
			messageAreaButtons.css({
				margin: '0',
				padding: '0',
				color: '#38736d',
				fontSize: '11px'
			});
			messageAreaButtons.hover(
				function () {
					this.style.color = '#183a37';
				},
				function () {
					this.style.color = '#38736d';
				}
			);

			this.dayEl.css({
				color: '#0a5657',
				margin: '5px 0 8px 0'
			});

			this.messageEl.css({
				position: 'relative',
				left: '5px',
				color: '#48403a'
			});

			this.nextEl.css({
				position: 'absolute',
				right: '0px',
				bottom: '1px',
				color: '#38736d'
			});
			this.previousEl.css({
				position: 'absolute',
				left: '0px',
				bottom: '1px',
				color: '#38736d'
			});
		}
	},

	changeDay: function changeDay(day) {
		var widget = this;

		if (day === undefined || day === null) {
			var daysDiff = Math.floor((this.currentDate - this.startDate) / 86400000);
			if (daysDiff < 0) {
				daysDiff = -1;
			}
			day = daysDiff;
		}

		var title = '';
		var message = '';

		this.displayDay = day;
		if (day < 0) {
			day = -1;
			this.displayDay = day;
			message = this.preChallengeMessage;
		} else if (day >= this.dailyMessages.length) {
			day = this.dailyMessages.length;
			this.displayDay = day;
			message = this.postChallengeMessage;
		} else {
			title = 'DAY '+ (this.displayDay + 1);
		}
		if (!message) {
			message = this.dailyMessages[this.displayDay]
		}

		this.renderBase(); // Hack for sIFR removal (jquery.sifr sucks)

		this.dayEl.text(title);
		this.messageEl.text(message);

		if (title) {
			this.dayEl.sifr({
				font: widget.daySifrFontUrl,
				fontSize: '18',
				overX: 3,
				overY: 3
			});
		}

		this.messageEl.sifr({
			font: widget.messageSifrFontUrl,
			fontSize: '19',
			overX: ((widget.displayDay == 7 && ($.browser.msie || $.browser.safari)) || widget.displayDay == 13) ? 1.2 : 1.1,
			overY: 3
		});

		$('.sIFR-alternate').css('visibility', 'hidden'); // IE8 workaround

		if (this.displayDay < this.dailyMessages.length) {
			this.nextEl.css({
				opacity: '1',
				filter: ''
			});
			var next = function () {
				widget.nextDay();
				return false;
			};
			this.nextEl.find('button').removeAttr('disabled');
		} else {
			this.nextEl.css({
				opacity: '0.6',
				filter: 'alpha(opacity=60)'
			});
			var next = function() {
				return false;
			};
			this.nextEl.find('button').attr('disabled', 'disabled');
		}
		this.nextEl.find('button').click(next);

		if (this.displayDay > -1) {
			this.previousEl.css({
				opacity: '1',
				filter: ''
			});
			var previous = function () {
				widget.previousDay();
				return false;
			};
			this.previousEl.find('button').removeAttr('disabled');
		} else {
			this.previousEl.css({
				opacity: '0.6',
				filter: 'alpha(opacity=60)'
			});
			var previous = function() {
				return false;
			};
			this.previousEl.find('button').attr('disabled', 'disabled');
		}
		this.previousEl.find('button').click(previous);
	},

	nextDay: function nextDay() {
		this.changeDay(this.displayDay + 1);
	},

	previousDay: function previousDay() {
		this.changeDay(this.displayDay - 1);
	}
};

$(document).ready(function () {
	var tpGratitudeWidget = new TP.gratitudeWidget('tp_gratitude_widget');
});