var block = 0;

function tab(x, y, c, t) { // x - id вкладки, y - название вкладки, с - класс вкладки, t - массив елемента
	if (block == 0) {
		block++;
		var allTab = document.getElementsByTagName("td");
		var allDivTab = document.getElementsByTagName("div");
		var idTab = x; idTab = idTab.replace("Tab", "");
		
		for (i = 0; i < allTab.length; i++) {
			if (allTab[i].className == "tabActive") {
				allTab[i].className = "tabNoActive";
				break;
			}
		}
		t.className = "tabActive";
		
		for (i = 0; i < allDivTab.length; i++) {
			if (allDivTab[i].id.indexOf("divTab") != -1 && allDivTab[i].style.display != "none") {
				$("#" + allDivTab[i].id).fadeOut("fast", function() {
					$("#divTab" + idTab).fadeIn("fast", function() {block=0;});
				});
			} else {
				block=0;
			}
		}
	}
}

function pizda(i) { alert(i); }

var openMymenu = 0;
function showMyMenu() {
	if (openMymenu == 0) {
		openMymenu++;
		if ($("#myMenuDiv").is(":hidden")) {
			$("#myMenuLink").removeClass();
			$("#myMenuLink").addClass("myMenuLink2");
			$("#myMenuDiv").slideDown("normal", function() {
				openMymenu--;
			});
		} else {
			$("#myMenuLink").removeClass();
			$("#myMenuLink").addClass("myMenuLink1");
			$("#myMenuDiv").slideUp("slow", function() {
				openMymenu--;
			});
		}
	}
}

$("#loadblock").ajaxStart(function() {
    $(this).show();
});

$("#loadblock").ajaxStop(function() {
    $(this).hide();
});

function successful(data) {
	if (data.slice(0, 5) == "error") {
		var exitLink = data.replace("error", "");
		window.location.href = exitLink;
	} 
	else return false;
}

function fadeToBody(id) {
    //$("#allContentBody").fadeTo(1, 0.5, function()
    $("#allContentBody").block({
        message: null,
        overlayCSS: {
            backgroundColor: '#926A50'
        }
    });
    
    $("#" + id).fadeIn("slow");
}

function unFadeToBody(id) {
    document.getElementById(id).style.display = 'none';
    $('#allContentBody').unblock();
}

function showHide(id) {
    if (block == 0) {
        block++;
        if ($.browser.msie) {
            if ($("#" + id).is(":hidden")) $("#" + id).show();
            else $("#" + id).hide();
            block = 0;
        } else {
            if ($("#" + id).is(":hidden")) $("#" + id).show("normal", function() { block = 0; });
            else $("#" + id).hide("fast", function() { block = 0; });
        }
    }
}

function findElement(idElement, nameElement, numElement, valElement) {
    var xElement = document.getElementById(idElement);
    if (xElement) {
        yElements = xElement.getElementsByTagName(nameElement);
        if (yElements.length > 0) {
            if (valElement == 'val') {
                return yElements[numElement].value;
            } else if (valElement && typeof numElement == "number") {
                if (yElements[numElement].value != "undefined") {
                    yElements[numElement].value = valElement;
                }
            }
        }
    }
}

function stringToArray(str) {
    var someStr = new String(str);
    var someArr = someStr.split(",");
    var moneyStr = new String();
    for (i = 0; i < someArr.length; i++) {
        if (i == 0 && someArr[i] != 0) moneyStr += someArr[i] + " зол. ";
        if (i == 1 && someArr[i] != 0) moneyStr += someArr[i] + " кам. ";
        if (i == 2 && someArr[i] != 0) moneyStr += someArr[i] + " жел. ";
        if (i == 3 && someArr[i] != 0) moneyStr += someArr[i] + " дер. ";
        if (i == 4 && someArr[i] != 0) moneyStr += someArr[i] + " зер. ";
        if (i == 5 && someArr[i] != 0) moneyStr += someArr[i] + " неф. ";
    }
    return moneyStr;
}

function getMoney(z, k, zh, d, zr, n) { //золото,камень,железо,дерево,зерно,нефть
	money = '<img border="0" src="http://'+window.location.hostname+'/images/res/money.gif" width="17" height="17" > <sup class="resurs">' + z + '</sup> ' +
        '<img border="0" src="http://'+window.location.hostname+'/images/res/stone.gif" width="17" height="17" > <sup class="resurs">' + k + '</sup> ' +
        '<img border="0" src="http://'+window.location.hostname+'/images/res/iron.gif" width="17" height="17" > <sup class="resurs">' + zh + '</sup> ' +
        '<img border="0" src="http://'+window.location.hostname+'/images/res/arbor.gif" width="17" height="17" > <sup class="resurs">' + d + '</sup> ' +
        '<img border="0" src="http://'+window.location.hostname+'/images/res/grain.gif" width="17" height="17" > <sup class="resurs">' + zr + '</sup> ' +
        '<img border="0" src="http://'+window.location.hostname+'/images/res/oil.gif" width="17" height="17" > <sup class="resurs">' + n + '</sup> ';
    $('#resourses').html(money);
}

// функции ассамблеи
var assamTimer = 0;

function getMessagesAssam(idses) {
	$("#loadingBarAssam").html('<img src="http://'+window.location.hostname+'/new/images/loading.gif" border="0" align="absmiddle" /> Обновление...');
	$.get('http://'+window.location.hostname+'/chat.php?' + idses, function(data) {
		if (data == '<div style="color: red;" align="center">Ошибка авторизации. Нажмите "обновить" в браузере.</div>') {
			x = location.href; x = x.replace(location.search, ""); location.href = x;
		} else {
			$("#assammessages").html(data);
			$("#loadingBarAssam").html('');
			assamTimer = 30;
			refreshAssam(idses);
		}
	});
}

function postMessagesAssam(idses, mes) {
	$("#mchatMsgF").val('');
	$("#loadingBarAssam").html('<img src="http://'+window.location.hostname+'/new/images/loading.gif" border="0" align="absmiddle" /> Отправка...');
	$.post('http://'+window.location.hostname+'/chat.php?' + idses + '&msg=' + mes, function(data) {
		$("#assammessages").html(data);
		$("#loadingBarAssam").html('');
		if (assamTimer == 0) {
			refreshAssam(idses);
		}
		assamTimer = 30;
	});
}

function refreshAssam(idses, x) {
	if (x == 'stop') {
		$('#loadingBarAssam').stopTime('assamT');
		$('#loadingBarAssam').html(assamTimer + ' [<a href="javascript://" onclick="refreshAssam(\'' + idses + '\', \'start\')" style="color: green;" title="Возобновить обновление">V</a>]');
		assamTimer = 0;
	} else if (x == 'start') {
		refreshAssam(idses);
		$("#loadingBarAssam").html(assamTimer + ' [<a href="javascript://" onclick="refreshAssam(\'' + idses + '\', \'stop\')" style="color: red;" title="Остановить обновление">X</a>]');
	} else {
		$("#loadingBarAssam").oneTime("1s", "assamT", function() {
  			if (assamTimer <= 0) {
  				if ($("#mchatMsgF").val().length == 0) getMessagesAssam(idses);
  				else {
  					assamTimer = 0;
  					$(this).html('<a style="color: red; cursor: pointer;" title="Автообновление остановлено! Отправьте сообщение!">Обн. остановлено!</a>');
  				}
  			}
  			else {
  				assamTimer--;
  				$(this).html(assamTimer + ' [<a href="javascript://" onclick="refreshAssam(\'' + idses + '\', \'stop\')" style="color: red;" title="Остановить обновление">X</a>]');
  				refreshAssam(idses);
  			}
		});
	}
}

function checkStrAssam(i) {
	if (i <= 0) $("#mchatBtn").val("Обновить");
	else $("#mchatBtn").val("Написать");
}
