﻿function initVideoPlayer(playerSWF, controlsSWF) 
{
    $(".VideoPlayer").flowplayer(
		playerSWF,
		{
		    screen:
		    {
		        bottom: 0	// make the video take all the height
		    },
		    plugins:
		    {
		        controls:
		        {
		            // tooltips configuration
		            tooltips:
			        {
			            // enable english tooltips on all buttons
			            buttons: true,

			            // customized texts for buttons
			            mute: 'Без звука',
			            unmute: 'Включить звук',
			            play: 'Играть',
			            pause: 'Пауза',
			            fullscreen: 'В полный экран',
			            exitfullscreen: 'В оконный режим'
			        },

		            url: controlsSWF,

		            backgroundColor: "transparent",
		            backgroundGradient: "none",
		            sliderColor: '#FFFFFF',
		            sliderBorder: '1.5px solid rgba(160,160,160,0.7)',
		            volumeSliderColor: '#FFFFFF',
		            volumeBorder: '1.5px solid rgba(160,160,160,0.7)',

		            timeColor: '#ffffff',
		            durationColor: '#535353',
		            // background color for all tooltips
		            tooltipColor: '#112233',
		            // text color
		            tooltipTextColor: '#8899ff'
		        }
		    },
		    clip:
	        {
	            autoPlay: false,
	            autoBuffering: true
	        }
		}
	    );
}

function changeUserFileDescription(fileID, description, callback) {
    $.ajax({
        type: "post",
        url: "/File/mv",
        dataType: "text",
        data: { 'fileID': fileID, 'description': description },
        success: function(data) {
            callback(fileID, description);
        }
    });
}

function deleteUserFile(fileID, callback) {
    $.ajax({
        type: "post",
        url: "/File/rm",
        dataType: "text",
        data: { 'fileID': fileID },
        success: function(data) {
            callback(fileID);
        }
    });
}

function encode64(input) {
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;

    while (i < input.length) {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }

        output += keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
    }

    return output.toString();
}

function decode64(input) {
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;

    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    while (i < input.length) {
        enc1 = keyStr.indexOf(input.charAt(i++));
        enc2 = keyStr.indexOf(input.charAt(i++));
        enc3 = keyStr.indexOf(input.charAt(i++));
        enc4 = keyStr.indexOf(input.charAt(i++));

        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        output += String.fromCharCode(chr1);

        if (enc3 != 64) {
            output += String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            output += String.fromCharCode(chr3);
        }
    }

    return output.toString();
}

function insertSmile(target, emoticon) {
    $("#" + target).insertAtCaret(' ' + decode64(emoticon) + ' ');
    $(".SmileList").hide();
}

function refresh(data) {
    window.location.reload(true);
}

function ctrlEnter(event, formElem) {
    if ((event.ctrlKey) && ((event.keyCode == 0xA) || (event.keyCode == 0xD))) {
        formElem.ajaxSubmit();
    }
}

function PrepareToEditComment(formElem) {
    initButtons();
    var txtId = $(formElem.get_data()).children(".FormEditComment").children(".newCommentContent").attr("id");
    $("#" + txtId).focus();
}

function initButtons() {
    $(".button").button();
}

function initColapseblePanels() {
    $(".ContainerPanelHeader").click(function() {
        $(this).next(".ContainerPanelContent").slideToggle(300)
        return false;
    });
}


function selectOption(select_id, option_val) {
    $('#' + select_id + ' > option:selected').removeAttr('selected');
    $('#' + select_id + ' > option[value=' + option_val + ']').attr('selected', 'selected');
    $("#" + select_id).trigger('change');
}

function deleteEdu(id, callback) {
    $.ajax({
        type: "post",
        url: "/Profile/DeleteUserEdu",
        data: {
            'id': id
        },
        dataType: "text",
        success: function(data) {
            if (data == "OK")
                callback(id);
        }
    });    // End ajax method
}

function initStatusText() {
    $("#status_text.clickable_editable").editable("/Profile/SaveStatus", {
        type: "text",
        loadurl: "/Profile/GetStatus",
        cssclass: "editable_clickable",
        style: "display: inline",
        tooltip: "Кликните чтобы изменить",
        indicator: "Изменяем...",
        onsubmit: function(settings, original) {
            if (original.firstChild.value != '') {
                $("#status_date").attr('display', 'block');
            }
            else {
                $("#status_date").attr('display', 'none');
            }
        }
    });
}

function UndoJoinGroup(gid, callback) {
    $.ajax({
        type: "post",
        url: "/Groups/RemoveJoinApplication",
        data: {
            'gid': gid
        },
        dataType: "text",
        success: function(data) {
            if (data == "OK")
                callback(gid);
        }
    });    // End ajax method
}

function InviteUsersToGroup(uidList, gid, callback) {
    $.ajax({
        type: "post",
        url: "/Groups/Invite",
        data: {
            'uid_list' : uidList,
            'gid': gid
        },
        dataType: "text",
        success: function(data) {
            if (data == "OK")
                callback();
        }
    });    // End ajax method
}

function JoinGroup(gid, callback) {
    $.ajax({
        type: "post",
        url: "/Groups/Join",
        data: {
            'gid': gid
        },
        dataType: "text",
        success: function(data) {
            if (data == "OK")
                callback(gid);
        }
    });    // End ajax method
}

function LeaveGroup(gid, callback) {
    $.ajax({
        type: "post",
        url: "/Groups/Leave",
        data: {
            'gid': gid
        },
        dataType: "text",
        success: function(data) {
            if (data == "OK")
                callback(gid);
        }
    });    // End ajax method
}

function acceptInviteToGroup(fromUser, group, callback) {
    $.ajax({
        type: "post",
        url: "/Groups/InviteAccept",
        dataType: "text",
        data: {
            'fromUser': fromUser,
            'gid' : group
        },
        success: function(data) {
            callback(fromUser, group, data);
        }
    });
}

function rejectInviteToGroup(fromUser, group, callback) {
    $.ajax({
        type: "post",
        url: "/Groups/InviteReject",
        dataType: "text",
        data: {
            'fromUser': fromUser,
            'gid' : group
        },
        success: function(data) {
            callback(fromUser, group, data);
        }
    });
}

function acceptInvite(fromUser, callback) {
    $.ajax({
        type: "post",
        url: "/Profile/acceptInvite",
        dataType: "text",
        data: {
            'fromUser': fromUser
        },
        success: function(data) {
            callback(fromUser, data);
        }
    });
}

function rejectInvite(fromUser, callback) {
    $.ajax({
        type: "post",
        url: "/Profile/rejectInvite",
        dataType: "text",
        data: {
            'fromUser': fromUser
        },
        success: function(data) {
            callback(fromUser, data);
        }
    });
}


function reportAboutObject(id, type, author, desc, callback) 
{
    $.ajax({
        type: "post",
        url: "/Report",
        data: {
            'id': id,
            'typeID': type,
            'AuthorID': author,
            'description': desc
        },
        dataType: "text",
        success: function() {
                callback(id);
        }
    });    // End ajax method
}

function deleteComment(commentID, callback) {
    $.ajax({
        type: "post",
        url: "/Comments/Delete",
        data: {
            'id': commentID
        },
        dataType: "text",
        success: function() {
            callback(commentID);
        }
    });    // End ajax method
}

function unDeleteComment(commentID, callback) {
    $.ajax({
        type: "post",
        url: "/Comments/Undelete",
        data: {
            'id': commentID
        },
        dataType: "text",
        success: function() {
            callback(commentID);
        }
    });    // End ajax method
}

function deleteMessage(fromUser, toUser, msgID, isOutbox, listItemID, callback) {
    $.ajax({
        type: "post",
        url: "/Messages/DeleteMessage",
        data: {
            'fromUser': fromUser,
            'toUser': toUser,
            'msgID': msgID,
            'isOutbox': isOutbox
        },
        dataType: "text",
        success: function(data) {
            if (data = "OK") {
                callback(listItemID);
            }
        }
    });     // End ajax method
}

function UndeleteMessage(fromUser, toUser, msgID, isOutbox, listItemID, callback) {
    $.ajax({
        type: "post",
        url: "/Messages/UnDeleteMessage",
        data: {
            'fromUser': fromUser,
            'toUser': toUser,
            'msgID': msgID,
            'isOutbox': isOutbox
        },
        dataType: "text",
        success: function(data) {
            if (data = "OK") {
                callback(listItemID);
            }
        }
    });     // End ajax method
}

function MarkUnread(fromUser, toUser, msgID, isOutbox, callback) {
    $.ajax({
        type: "post",
        url: "/Messages/MarkUnread",
        data: {
            'fromUser': fromUser,
            'toUser': toUser,
            'msgID': msgID,
            'isOutbox': isOutbox
        },
        success: function(data) {
            if (data = "OK") {
                callback();
            }
        }
    });      // End ajax method
}

function removeFriend(friendID, callback)
{
    $.ajax({
        type: "post",
        url: "/Profile/RemoveFriend",
        dataType: "text",
        data: {
            'id': friendID
        },
        success: function(data) {
            callback(data);
        }
    });    // End ajax method

}

function inviteFriend(friendID, txt, callback) {
	$.ajax({
		type: "post",
		url: "/Profile/InviteFriend",
		dataType: "text",
		data: {
			'id': friendID,
			'msg': txt
        },
        success: function(data) {
           callback(data);
        }
	}); // End ajax method

}
