
// style enhancements

$(function () {
	$('.flowerHeader').append('<img src="/images/hibiscus-large.png" class="flower" />');
});

// png ie fix

$(function () {
	if ($.browser.msie && $.browser.version < 7) {
		$('div, a, input').each(function () {
			if ($(this).css('background-image').match(/\.png(['"]?\))?$/i)) {
				DD_belatedPNG.fixPng(this);
			}
		});
		$('img').each(function () {
			if ($(this).attr('src').match(/\.png$/i)) {
				DD_belatedPNG.fixPng(this);
			}
		});
	}
});

// setup pretty photo lightbox

$(function () {
	$("a[rel^='prettyPhoto']").prettyPhoto({'theme':'light_square'});
});

// voting

$(function () {
	function setupVoteState() {
		$('.voteArea .voteButton').each(function () {
			refreshVoteState(this);
		});
	}
	setupVoteState();
	
	function refreshVoteState(element) {
		var id = parseVoteId(element);
		$.get('/vote/ajax-check-vote-state', {'id': id}, function (data) {
			if (data != 0) {
				$(element).addClass('voted');
				$.get('/vote/ajax-get-votes', {'id': id}, function (data) {
					$(element).siblings('.voteBubble').text(data);
				});
			}
		});
	}
	function vote(element) {
		var id = parseVoteId(element);
		$('<div class="voteModal"><img src="/images/spinner.gif" alt="Please Wait" style="margin-top:29px;" /></div>').modal();
		$.get('/vote/ajax-vote', {'id': id}, function (data) {
			if (data == 'voting-disabled') {
				$('#simplemodal-data').html('<p style="margin-top:27px;">Voting is now closed. We will be showcasing the winners the week of September 13th.</p>');
			} else if (data == 'error') {
				$('#simplemodal-data').html('<p style="margin-top:37px;">You have no votes remaining for today!</p>');
			} else if (data == 'not-logged-in') {
				$('#simplemodal-data').html('<p style="margin-top:28px;">You must <a href="/vote/login">login</a> before you can vote.<br>If you haven\'t done so, please <a href="/vote/registration">register</a>.</p>');
			} else {
				$('#simplemodal-data').html('<h1>Thanks for voting!</h1><p style="margin-top:3px;">You have ' + data + ' votes remaining for today.</p>');
			}
			refreshVoteState(element);
			setTimeout(function () {
				$.modal.close();
			}, 4000);
		});
	}
	function parseVoteId(element) {
		var href = $(element).attr('href');
		var hash = href.substr(href.indexOf('#')+1);
		return hash;
	}
	
	$('.voteArea .voteButton').click(function (e) {
		e.preventDefault();
		vote(this);
	});
});

