﻿var previewImgID = 0;

/////////////////////////////
var goodsList = new Array();
var goodsListLen = 0;

function check_IsInNotepadGV(goodsId) {
	IMPOST.FrontOffice.Services.NotepadService.IsInNotepad(goodsId,
		OnCheckIsInNotepadSuccessGV,
		OnOperation_Failed);
}

function check_IsInNotepadA() {
	IMPOST.FrontOffice.Services.NotepadService.IsInNotepadA(goodsList,
		OnCheckIsInNotepadSuccessA,
		OnOperation_Failed);
}

function RegisterGoodsInList(goodsId) {
	goodsList[goodsListLen] = new Array();
	goodsList[goodsListLen][0] = goodsId;
	goodsList[goodsListLen][1] = false; //focus on goods
	goodsList[goodsListLen][2] = false; //is in notepad
	goodsListLen++;
}

function EndRegisterGoodsInList() {
	if (goodsListLen > 0) {
		check_IsInNotepadA();
	}
}

function set_mouseOverGoods(goodsId) {
	if (goodsListLen == 0)
		return;

	for (var i = 0; i < goodsListLen; i++) {
		if ((goodsList[i][0] == goodsId) && (goodsList[i][2] == false)) {
			goodsList[i][1] = true;
			show_AddToNotepadButton(goodsId);
			break;
		}
	}
}

function set_mouseOutGoods(goodsId) {
	if (goodsListLen == 0)
		return;

	for (var i = 0; i < goodsListLen; i++) {
		if (goodsList[i][0] == goodsId) {
			goodsList[i][1] = false;
			break;
		}
	}
}

function show_AddToNotepadButton(goodsId) {
	if (previewImgID != 0 && goodsId != previewImgID) {
		hide_AddToNotepadButton(previewImgID);
	}

	setTimeout("showAddToNotepadButtonAfterDelay(" + goodsId + ")", 500);

	previewImgID = goodsId;
}

function showAddToNotepadButtonAfterDelay(goodsId) {
	if (goodsListLen == 0)
		return;

	for (var i = 0; i < goodsListLen; i++) {
		if ((goodsList[i][0] == goodsId) && (goodsList[i][2] == false)) {
			if (goodsList[i][1] == true) {
				var btn = document.getElementById("addToNote_" + goodsId);
				btn.style.display = "block";
			}
			break;
		}
	}
}

function hide_AddToNotepadButton(goodsId) {
	for (var i = 0; i < goodsListLen; i++) {
		if (goodsList[i][0] == goodsId) {
			goodsList[i][1] = false;
			break;
		}
	}

	var btn = document.getElementById("addToNote_" + goodsId);
	btn.style.display = "none";
}

function OnCheckIsInNotepadSuccess(result, userContext, methodName) {
	if (goodsListLen == 0)
		return;

	for (var count = 0; count < goodsListLen; count++) {
		if (goodsList[count][0] == userContext) {
			goodsList[count][2] = result;

			if (result == true) {
				var btn = document.getElementById("addToNote_" + userContext);
				btn.className = "save_inm";
				btn.style.display = "block";
			}
		}
	}
}

function OnCheckIsInNotepadSuccessA(result, userContext, methodName) {
	goodsList = result;

	for (var count = 0; count < goodsListLen; count++) {
		if (goodsList[count][2] == true) {
			var btn = document.getElementById("addToNote_" + goodsList[count][0]);
			btn.className = "save_inm";
			btn.style.display = "block";
		}
	}
}

function mark_AsSavedInNotepad(goodsId) {
	previewImgID = 0;

	for (var count = 0; count < goodsListLen; count++) {
		if (goodsList[count][0] == goodsId) {
			goodsList[count][2] = true;
			var btn = document.getElementById("addToNote_" + goodsId);
			btn.className = "save_inm";
			btn.style.display = "block";
		}
	}
}

function OnCheckIsInNotepadSuccessGV(result, userContext, methodName) {
	if (result == true) {
		mark_AsSavedInNotepadGV();
	}
	else {
		mark_AsNonSavedInNotepadGV();
	}
}

function mark_AsSavedInNotepadGV() {
	var btn = document.getElementById("addToNoteGV");
	btn.className = "save_horizontal_inm";
	btn.style.display = "";
}

function mark_AsNonSavedInNotepadGV() {
	var btn = document.getElementById("addToNoteGV");
	btn.className = "save_horizontal";
	btn.style.display = "";
}

function addToNotepad(goodsId, goodsView) {
	var context = new Array();
	context[0] = goodsId;
	context[1] = goodsView;

	IMPOST.FrontOffice.Services.NotepadService.AddGoodsToNotepad(goodsId,
		OnAddToNotepad_Success,
		OnOperation_Failed,
		context);
}

function getGoodsCountInNotepad() {
	IMPOST.FrontOffice.Services.NotepadService.GetGoodsCount(
		OnGetGoodsCount_Success,
		OnOperation_Failed);
}

function removeGoodsFromNotepad(goodsId) {
	IMPOST.FrontOffice.Services.NotepadService.RemoveGoodsFromNotepad(goodsId,
		OnRemoveFromNotepad_Success,
		OnOperation_Failed);
}

function OnAddToNotepad_Success(result, userContext, methodName) {
	getGoodsCountInNotepad();
	if (userContext[1] == false) {
		mark_AsSavedInNotepad(userContext[0]);
	}
	else {
		mark_AsSavedInNotepadGV(userContext[0]);
	}
}

function OnRemoveFromNotepad_Success(result, userContext, methodsName) {
	//!
}

function OnGetGoodsCount_Success(result, userContext, methodName) {
	var obj = document.getElementById("idRememberAll");
	if (obj !== "undefine") {
		if (result != 0) {
			obj.innerHTML = result.toString();
			obj.href = notepadUrl;
		}
	}
}

function OnOperation_Failed(error) {
	//Ошибка
}