var inviteLetterText = '';

$(document).ready(function() {

    loadVideo('winner', '/flv', 1, 640, 384);
    loadVideo('winner2', '/flv', 2, 400, 324);
    loadVideo('winner3', '/flv', 3, 400, 324);


    /*    var numberVideo = $('body').find('div.video-over-center>div>div').attr('id');
    var videoName = $('body').find('div.video-over-center>div>div div').attr('id');
    */

    $(".vote-btn").livequery(function() { $(this).click(chooseGirl); });
    $('#another-friend').click(anotherFriendClick);
    $('#remove-friend').livequery(function() {
        $(this).click(removeFriendClick);
    });

    inviteLetterText = $("textarea.letter").val();

    $("input#sendemail-manualy").click(sendEmailManClick);


    /*	$(".girls-pages .next a").click(function() {
    if ($('.voted').length <= 0) {
    $('#choose-girl-message').show();
    return false;
    }
    });
    */
    $('#choose-girl-message .close').click(function() {
        $('#choose-girl-message').hide();
        return false;
    });

    $("#choose-girl-message .overlay").click(function() {
        $('#choose-girl-message').hide();
        return false;
    });

    // winner's video overlay
    $(".winner img.winner-video").click(function() {
        $('#winner-video').show();
        return false;
    });

    $("#winner-video .overlay").click(function() {
        $('#winner-video').hide();
        return false;
    });

    $(".first-runner-up img.winner-video").click(function() {
        $('#video-second').show();
        return false;
    });

    $("#video-second .overlay").click(function() {
        $('#video-second').hide();
        return false;
    });

    $(".second-runner-up img.winner-video").click(function() {
        $('#video-third').show();
        return false;
    });

    $("#video-third .overlay").click(function() {
        $('#video-third').hide();
        return false;
    });


    /* Invite popup */
    $('.girl-profile .invite a').click(function() {
        showInvite($(this).attr('href'));
        return false;
    });

    $('#top-links .invite a').click(function() {
        showInvite($(this).attr('href'));
        return false;
    });

    $('#choose-girl-message a.invite').click(function() {
        showInvite($(this).attr('href'));
        return false;
    });

    $('#pages a.invite').click(function() {
        showInvite($(this).attr('href'));
        return false;
    });

    $('.photo-link').click(function() {
        var girlNumber = $(this).attr('value');
        $('.girl-profile-overlay').hide();
        $('#profile-' + girlNumber).show();
        return false;
    });

    $('.girl-profile .close a').click(function() {
        $('.girl-profile-overlay').hide();
        return false;
    });

    $('.girl-profile-overlay .overlay').click(function() {
        $('.girl-profile-overlay').hide();
        return false;
    });

    $('input#select-all-contacts').change(function() {
        $('input.checkbox-friend')
			.attr('checked', $(this).attr('checked'));
    });

    $('.close-form .close a').click(function() {
        window.close();
    });

    $('.girl-profile .photos .photo-1 a').click(function() {
        $(this).parents('.girl-profile-overlay').find('.photo').css('background-image', 'url(' + $(this).find('img').attr('src').replace('1-prev.jpg', '1.jpg') + ')');
        return false;
    });

    $('.girl-profile .photos .photo-2 a').click(function() {
        $(this).parents('.girl-profile-overlay').find('.photo').css('background-image', 'url(' + $(this).find('img').attr('src').replace('2-prev.jpg', '2.jpg') + ')');
        return false;
    });

    $('.girl-profile .photos .photo-3 a').click(function() {
        $(this).parents('.girl-profile-overlay').find('.photo').css('background-image', 'url(' + $(this).find('img').attr('src').replace('3-prev.jpg', '3.jpg') + ')');
        return false;
    });


    $("input#invite-name").change(inviteLetterChange)
		.keyup(inviteLetterChange);
});

function inviteLetterChange() {
	$('textarea.letter').val(inviteLetterText.replace("%%user's name%%", $(this).val()));
};

function showInvite(href) {
	wd = 600;
	hg = 600;
	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 chooseGirl() {

if ($(this).hasClass('voted')) 
return false;

var girlNumber = ($(this).attr('value') - 1) % 5 + 1;

var vote = $('#profile-' + girlNumber + ' .vote-btn, ' +
'#girl-' + girlNumber + ' .vote-btn');
if (vote.hasClass('voted')) return false;
vote.hide();


var spinner = $('#girl-' + girlNumber + ' .loading, #profile-' + 
girlNumber + ' .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 = $('.form-content form').serialize();
	var self = $(this);
	var valid = true;

	if ($('ul.form #invite-name').attr('value') == '') {
		valid = false;
		$('.name-validation').show();
	}

	var re = /\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b/gi;

	if ($('ul.form #invite-email').val() == '' ||
		!$('ul.form #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();
			}
		});
	}

	return false;
};

function showInviteFormClose() {
	$('.form-content').hide();
	$('.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-icon.gif" alt="Delete" /></a></td></tr>');

	$('table.friends tr:last a').remove();

	$('table.friends').append(row);

	return false;
};

function removeFriendClick() {
	var deleteBtn = '<a href="" title="Delete" id="remove-friend">' +
		'<img src="/img/invite/delete-icon.gif" alt="Delete" /></a>';

	$('table.friends tr:last').remove();

	$('table.friends tr:last td:last').append(deleteBtn);

	return false;
};

function loadVideo(filename, path, elementId, videoWidth, videoHeight) {    
    swfobject.embedSWF(
		'/flv/player.swf', elementId, videoWidth, videoHeight, '9', false, {
		    'image': path + '/' + filename + '.jpg',
		    'file': path + '/' + filename + '.flv'
		}, {
		    'allowfullscreen': 'true',
		    'allowscriptaccess': 'always',
		    'wmode': 'opaque'
		}
	)
};

