
var inviteBlock;

$(document).ready(function() {
	
	animateLeafes();
	
	inviteBlock = $('.terminator');

	$("a.vote-btn").livequery(function() { $(this).click(chooseGirl); });
	$("a.vote").livequery(function() { $(this).click(chooseGirl); });
	$('#another-friend').click(anotherFriendClick);
	$('#remove-friend').livequery(function() {
		$(this).click(removeFriendClick);
	});
	$("input.but-inv").click(sendEmailManClick);

	$("#choose-girl-message a").click(function() {
		$('#choose-girl-message').hide();
		return false;
	});


	$('a.invite').click(function() {
		$('.profile').hide();
		$('#top-invate1', inviteBlock).hide();
		$('.overlay').show();
		inviteBlock.show();
		return false;
	});
	$('#invite_nocount').click(function() {
		$('.profile').hide();
		$('#top-invate1', inviteBlock).hide();
		$('.overlay').show();
		inviteBlock.show();
		return false;
	});
//	$('.invite-skip-btn').click(function() {
//		$('.terminator').hide();
//		$('.overlay').hide();
//		return false;
//	});


	$('a.show-invite').click(function() {
		showInvite($(this).attr('href'));
		return false;
	});

	$('a.photos.mainpage').click(function() {
		var girlNumber = $(this).attr('value');
		$('.girl-profile').hide();
		$('.terminator').hide();
		$('#profile-girl-' + girlNumber).show();
		$('.overlay').show();
		return false;
	});

	$('ol.photo li a').click(function() {
		var id = $(this).attr('value').split('-')[0];
		var num = $(this).attr('value').split('-')[1];
		$('#big-img-' + id).attr('src', 'img/profile/' + id + '/300x460-' + num + '.jpg');
		return false;
	});

	$('div.close a').click(function() {
		$('.profile').hide();
		$('.overlay').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() {
	$('.profile').hide();
	$('.overlay').show();
	$('#top-invate1', inviteBlock).show();
	inviteBlock.show();

	var girlNumber = $(this).attr('value');
	
	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-girl-' + 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 spanVoted = $('span.voted');
			spanVoted.before($('<a href="" class="vote-btn" value="' + spanVoted.attr('value') + '"></a>'));
			spanVoted.remove();

			vote.before($('<span class="voted" value="' + vote.attr('value') + '" />'));
			vote.remove();

			spinner.css('display', 'none');
		}
	});

	return false;
}

function sendEmailManClick() {
	$('.name-validation').hide();
	$('.email-validation').hide();
	var form = $('div.terminator form');
	var formdata = form.serialize();
	var self = $(this);
	var valid = true;

	if ($('#invite-name', form).attr('value') == '') {
		valid = false;
		$('.name-validation').show();
	}

	var re = /\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b/gi;

	if ($('#invite-email', form).val() == '' ||
		!$('#invite-email', form).val().match(re)) {
		valid = false;
		$('.email-validation').show();
	}

	if ($('table.friend input[name=email][value!=]', form).length == 0) {
		valid = false;
		$('.emails-validation').show();
	} else {
		$.each($('table.friend input[name=email][value!=]', form), 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() {
	$('.terminator').hide();
	$('.invite-success').show();
}

function anotherFriendClick() {
	if ($('table.friend tr').length >= 4) {
		$('#another-friend').hide();
	}
	var row = $('<tr><td><input type="text" name="name" /></td><td class="align"><input type="text" name="email" /></td></tr>');
	$('table.friend tr:last').after(row);
	$('#remove-friend').show();
	return false;
};

function removeFriendClick() {
	$('table.friend tr:last').remove();
	$('#another-friend').show();
	if ($('table.friend tr').length < 2)
		$('#remove-friend').hide();

	return false;
};
