/* File Created: June 23, 2014 */


function unSelectAll() {


	$("#message-selected").hide();
	$('#selected-messages').hide();
	$('.message-selected-item').remove();
	$('.messenger-dummy').show();
	$("#message-view-bar").hide();
	$("#message-move-bar").hide();
	$("#message-nav").show();

	$(".messenger-item-check input:checkbox:checked").prop('checked', false);

	$(".messenger-item").removeClass("checked");
	
}

function removeSelected() {
	var items = $(".messenger-item.checked");
	items.each(function () {
		$(this).remove();
	});

	items = $(".messenger-item.selected");
	items.each(function () {
		$(this).remove();
	});

	var itemsLeft = $(".messenger-item").toArray();
	var moreButton = $(".more-wrapper").toArray();
	if (itemsLeft.length == 0 && moreButton.length == 0) {
		$(".messenger-empty").show();
		
	}
	else if (itemsLeft.length == 0) {
		$(".messenger-more")[0].trigger('click');	// AX-24898.24919.2 $(".messenger-more")[0].click();
	}


	try {
		refreshAfterDelete();
	} catch (e) {
		// handle an exception here if refreshAfterDelete doesn't exist or throws an exception
	}
}


function showMessageView() {

	if ($(".messenger-container-d").length == 0) { // no container-d means it's mobile and we have to change the nav bar
		$("#message-selected").hide();
		$("#message-view-bar").show();
		$("#message-nav").hide();
	}

	$("#messengerDummy").hide();
	$("#messageView").show();
	
}

function hideMessageView() {

	if ($(".messenger-container-d").length == 0) { // no container-d means it's mobile and we have to change the nav bar
		$("#message-selected").hide();
		$("#message-view-bar").hide();
		$("#message-nav").show();
	}
	$(".messenger-item").removeClass("selected");
	$("#messageView").hide();
	$("#messengerDummy").show();
}

// AX-1385
function closeComposeShowPrevious(messageAction, textSent) {
    $('#msg-compose-div').empty();
    $('#msg-compose-div').hide();

    $(".mobile-top-nav").show();
    $(".messenger-top-nav").show();
    if (messageAction != "Compose")
        showMessageView();
    if (textSent != "") {
        // AX-1385 removeStringInUrl();
        setSentConfirmationText(textSent);
        showDeleteConfirmation();
    }
}

// AX-1385
function hideMessageViewForCompose() {

    if ($(".messenger-container-d").length == 0) { // no container-d means it's mobile and we have to change the nav bar
        $("#message-selected").hide();
        $("#message-view-bar").hide();
    }
    $(".mobile-top-nav").hide();
    $(".messenger-top-nav").hide();
    $(".messenger-item").removeClass("selected");
    $("#messageView").hide();
    $("#messengerDummy").show();
}

// AX-1385
function setSentConfirmationText(textSent) {
    var div = $('.confirmation-content').first();

    div.html(
			'<span class="fa fa-paper-plane-o"></span>&nbsp;' + textSent
		);

}


function showMoveBar() {
	$("#message-selected").hide();
	$("#message-view-bar").hide();
	$("#message-nav").hide();

	if ($(".messenger-container-d").length == 0) { // means we are in mobile mode
		$("#message-move-bar").show();
	}
}


function showCc() {

	$("#ccBtn").hide();
	$("#hideCcBtn").show();

	$(".cc-line").show();
	$("#recipients").removeClass("one-line");
}

function hideCc() {
	$("#hideCcBtn").hide();
	$("#ccBtn").show();
	$(".cc-line").hide();
	$("#recipients").addClass("one-line");
}





// bind events to the dialog buttons
function dialogDelete(messageIds, folderType) {

	$("#alert-no-btn").off();
	$("#alert-ok-btn").off();
	$("#alert-yes-btn").off();

	$("#alert-no-btn").on("click", function () {
		hideAlertDialog();
	});

	$("#alert-ok-btn").on("click", function () {
		hideAlertDialog();
	});

	$("#alert-yes-btn").on("click", function () {


		deleteSelectedMessages(messageIds, folderType);

		hideAlertDialog();
		
	});
}



function verifyMoveMessages(folderId) {
	var result;

	var messageIds = $("#messenger-folders-move").attr("data-message-ids").split(",");
	var folderType = $.urlParam('folderType');

	$.ajax({
		url: '/Messenger/VerifyMoveMessages?messageIds=' + messageIds + '&folderId=' + folderId + '&folderType=' + folderType, 
		type: 'POST',
		async: false,
		dataType: 'json',
		success: function (data) {

			result = data;
		}

	});

	return result;
}

function moveSelectedMessages(folderId, folderType) {

	var messageIds = $("#messenger-folders-move").attr("data-message-ids").split(",");

	
	$.ajax({
		url: '/Messenger/MoveMessages?messageIds=' + messageIds + '&folderId=' + folderId + '&folderType=' + folderType,
		type: 'POST',
		dataType: 'json',
		success: function (data) {

			if (data == true) {
				$("#messenger-folders-move").hide();
				removeSelected();
				unSelectAll();
				hideMessageView();
				showDeleteConfirmation();
				try {
					hideFoldersMove();
				} catch (e) {
					// handle an exception here if hideFoldersMove doesn't exist or throws an exception
				}
			}
		}
	});
}



function verifyDeleteMessages(messageIds, folderType) {
	var result;

	$.ajax({
		url: '/Messenger/VerifyDeleteMessages?messageIds=' + messageIds + '&folderType=' + folderType,
		type: 'POST',
		async: false,
		dataType: 'json',
		success: function (data) {

			result = data;
		}

	});

	return result;
}


function deleteSelectedMessages(messageIds, folderType) {
	$.ajax({
		url: '/Messenger/DeleteMessages?messageIds=' + messageIds + '&folderType=' + folderType,
		type: 'POST',
		dataType: 'json',
		success: function (data) {

			if (data == true) {

				removeSelected();
				unSelectAll();
				hideMessageView();
				showDeleteConfirmation();
			}
		}
	});
}

function showDeleteConfirmation() {
	$("#messengerConfirmation").show().delay(3000).fadeOut();
}

function selectedView(messageId, folderType) {
	$.ajax({
		url: '/Messenger/MessageSelectedDetail?messageId=' + messageId + '&folderType=' + folderType,
		dataType: 'html',
		success: function (data) {
			$(data).appendTo('#selected-messages-list');

			var $wrapper = $('#selected-messages-list');

			$wrapper.find('.message-selected-item').sort(function (a, b) {
				if ($(b).data("messagedate") != $(a).data("messagedate")) {
					return $(a).data("messagedate") - $(b).data("messagedate");
				}

				return $(b).data("message-id") - $(a).data("message-id");
			})
			.appendTo($wrapper);
		}
	});
}


function grayViewMessage() {

	if ($("#messageView").is(':visible'))
	{
		var id; 

		$(".message-view-subject").map(function () {
			id = ($(this).data("message-view-id"));
		});

		$('.messenger-item[data-message-id="' + id + '"]').addClass("selected");
	}

}

function pushMessageState(folder, folderType, message) {
    var url = "Messenger?folderId=" + folder + "&folderType=" + folderType + "&messageId=" + message;
    if (history.state === null || history.state.popping != 1)
        history.pushState({ message: message, popping: 0 }, "", url);
    else {
        history.state.popping = 0;
        history.replaceState({ message: message, popping: 0 }, "", url);
    }
        
    
}

