$(function () {
    $('#funcmenu li.send-page a').click(function () {
        sendPage();
        return false;
    });
});

function sendPage() {
    if (!$('#sendToFriend').length) {
        var stf = $('<div>').attr('id','sendToFriend').hide();
        $('#pageContent').prepend(stf);
        stf.load(LANGROOT + '/send-page/',function () {
            stf.fadeIn();
            $('#sendToFriendForm').submit(function () {
                //zmena value buttonu
                var v1 = $('#sendToFriendFormSubmit').val();
                var v2 = $('#sendToFriendFormSubmit').attr('rel');
                $('#sendToFriendFormSubmit').attr('disabled','disabled').val(v2).attr('rel',v1);
                $('#sendToFriendFormUrl').val(window.location.href);
                $.post(this.action,$(this).formSerialize(),function (response) {
                    
                    //response ako JSON
                    eval('var r = '+response+';');
                    
                    //odstranenie reportov
                    $('div.report',stf).remove();
                    
                    if (r.status == 'OK') {
                        stf.prepend('<div class="report"><span class="ico ico_INFO">&nbsp;</span>'+r.message+'</div>');
                        $('#sendToFriendFormSubmit').val(v1).attr('rel',v2);
                        window.setTimeout(function () {
                            stf.fadeOut(function () {
                                $(this).remove();
                            });
                        },1500);
                    } else {
                        stf.prepend('<div class="report"><span class="ico ico_ERROR">&nbsp;</span>'+r.message+'</div>');
                        $('#sendToFriendFormSubmit').removeAttr('disabled').val(v1).attr('rel',v2);
                    }
                });
                return false;
            });
        });
    } else {
        $('#sendToFriend').fadeOut(function () {
            $(this).remove();
        });
    }
    // $.get(LANGROOT + '/send-to-friend/',function () {
    //     
    // });
}
