
$(document).ready(function() {
	animateLeafes();

	$(".vote-btn").livequery(function() { $(this).click(chooseGirl); }); 
	$('#another-friend').click(anotherFriendClick);
	$('#remove-friend').livequery(function() {
		$(this).click(removeFriendClick);
	});
	$("input#sendemail-manualy").click(sendEmailManClick);

	$("a.next-link").click(function() {
		if ($('.voted').length < 1) {
			$('#choose-girl-message').show();
			return false;
		}
	});

	$("#choose-girl-message a").click(function() {
		$('#choose-girl-message').hide();
		return false;
	});


	$('a.show-invite').click(function() {
		showInvite($(this).attr('href'));
		return false;
	});

	$('div.bg .photo').click(function() {
		var girlNumber = $(this).attr('value');
		$('.girl-profile').hide();
		$('.profile-' + girlNumber).show();
		return false;
	});

	$('.girl-profile .close').click(function() {
		$('.girl-profile').hide();
		return false;
	});

	$('input#select-all-contacts').change(function() {
		$('input.checkbox-friend')
			.attr('checked', $(this).attr('checked'));
	});

	$('.close-form .close').click(function() {
		window.close();
	});
});

function showInvite(href) {
	wd = 670;
	hg = 470;
	var lf = screen.availWidth / 2 - wd / 2;
	var tp = screen.availHeight / 2 - hg / 2;

	window.open(href, 'invite', 'width=' + wd + ',height=' + hg + ',top=' + tp + ',left=' + lf + ',toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,directories=no,status=no');
};

function animateLeafes() {
	if ((!$.browser.msie) || ($.browser.version != 6.0)) {
		wd = document.body.clientWidth;

		$("#main-layout").mousemove(function(e) {
			$("#leaves-bottom-2").css("background-position", wd / 2 - 100 - (e.pageX - wd / 2) / 8);
			$("#flower").css("background-position", wd / 2 - (e.pageX - wd / 2) / 4);
			$("#leaves-bottom-1").css("background-position", wd / 2 - 350 - (e.pageX - wd / 2) / 2);
		});
	}
};

function chooseGirl() {
	var girlNumber = ($(this).attr('value') - 1) % 6 + 1;
	
	var vote = $('.profile-' + girlNumber + ' .vote-btn, ' +
		'#girl-' + girlNumber + ' .vote-btn');
	if (vote.hasClass('voted')) return false;
	vote.hide();

	var spinner = $('#girl-' + girlNumber + ' span.loading, .profile-' + 
		girlNumber + ' span.loading');
	spinner.css('display', 'block');

	var self = $(this);

	$.ajax({
		type: "POST",
		url: "/vote",
		data: ({ "contestant-id": self.attr('value') }),
		dataType: "html",
		success: function(data, textStatus) {
			var voted = $('.voted:not(.profile-' + girlNumber + ' .voted)');
			voted.addClass('vote-btn');
			voted.removeClass('voted');

			spinner.css('display', 'none');

			$('#girl-' + girlNumber + ' .girl-profile .voting span').addClass('voted');

			vote.addClass('voted');
			vote.removeClass('vote-btn');
			vote.show();

			$('.extra').css('display', 'none');

			$.ajax({
				type: "POST",
				url: "/getvotes",
				data: ({ "contestant-id": self.attr('value') }),
				dataType: "html",
				success: function(data, textStatus) {
					$('.profile-' + girlNumber + ' .extra strong,'
						+ ' #girl-' + girlNumber + ' .extra strong').html(data);
					$('.profile-' + girlNumber + ' .extra,' 
						+ ' #girl-' + girlNumber + ' .extra').css('display', 'block');
				}
			});
		}
	});

	return false;
}

function sendEmailManClick() {
	$('.name-validation').hide();
	$('.email-validation').hide();
	var formdata = $('div#manual-invite-container div.form-content form').serialize();
	var self = $(this);
	var valid = true;

	if ($('ul.form.manual #invite-name').attr('value') == '') {
		valid = false;
		$('.name-validation').show();
	}

	var re = /\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b/gi;

	if ($('ul.form.manual #invite-email').val() == '' ||
		!$('ul.form.manual #invite-email').val().match(re)) {
		valid = false;
		$('.email-validation').show();
	}

	if ($('table.friends input.email[value!=]').length == 0) {
		valid = false;
		$('.emails-validation').show();
	} else {
		$.each($('table.friends input.email[value!=]'), function() {
			if (!$(this).val().match(re)) {
				valid = false;
				$('.emails-validation').show();
			}
		});
	}

	if (valid) {
		$.ajax({
			type: "POST",
			url: "/sendemail",
			data: formdata,
			success: function(data) {
				showInviteFormClose();
			}
		});

		self.attr('src', "/img/invite-friends-form/invite-friends.jpg");
		self.attr("disabled", "disabled");
	}

	return false;
};

function showInviteFormClose() {
	$('div.form-content').hide();
	$('div.close-form').show();
}

function anotherFriendClick() {
	var row = $('<tr class="last"><td><input type="text" class="txt" name="name" value="" />' +
		'</td><td><input type="text" class="txt email" name="email" value="" /><a href="" title="Delete" id="remove-friend">' +
		'<img src="/img/invite/delete.gif" alt="Delete" /></a></td></tr>');

	$('#manual-invite-container form#send-email table.friends tr:last a').remove();

	$('#manual-invite-container form#send-email table.friends').append(row);

	return false;
};

function removeFriendClick() {
	var deleteBtn = '<a href="" title="Delete" id="remove-friend">' +
		'<img src="/img/invite/delete.gif" alt="Delete" /></a>';

	$('#manual-invite-container form#send-email table.friends tr:last').remove();

	$('#manual-invite-container form#send-email table.friends tr:last td:last').append(deleteBtn);

	return false;
};
