// ========================================================================
// Функции отрисовки контекстного меню
// ========================================================================

var LastContextMenu = 0;

function AddD(evnt) {
	var D = document.createElement('DIV');
	D.style.width = 170;
	D.style.border = '1px #CCCCCC solid';
	D.style.backgroundColor = '#FFFFFF';
	D.style.filter="progid:DXImageTransform.Microsoft.dropshadow(OffX=3, OffY=3, Color='#999999', Positive='true')";
	D.id = 'ContextMenu'+LastContextMenu;
	D.style.position = 'absolute';
	
	var pos = getPos(evnt.srcElement);
	D.style.top = pos.y+11;
	var dw = parseInt(document.body.clientWidth);
	if ((dw>0) && (pos.x+10+180>dw)) {
		D.style.left=dw-180;
	} else {
		D.style.left=pos.x+10;
	}
	
	D.style.zIndex = 100;
	D.style.padding = '2px';
	document.body.appendChild(D);
	D.ondeactivate = function(){window.setTimeout('HideContextMenu('+LastContextMenu+')', 500);};
	D.setActive();
	
	var D2 = document.createElement('DIV');
	D2.style.width = 170;
	D2.style.fontSize = '1px';
	D2.style.backgroundColor = '#CCCCCC';
	D2.innerHTML = '&nbsp;';
	D.appendChild(D2);
	
	var D3 = document.createElement('DIV');
	D3.style.width = 170;
	D3.style.fontSize = '1px';
	D3.innerHTML = '&nbsp;';
	D.appendChild(D3);
	
	var D4 = document.createElement('DIV');
	D4.style.width = 170;
	D4.style.fontSize = '1px';
	D4.style.backgroundColor = '#CCCCCC';
	D4.innerHTML = '&nbsp;';
	D.appendChild(D4);
	
	var D5 = document.createElement('DIV');
	D5.style.width = 170;
	D5.style.fontSize = '3px';
	D5.innerHTML = '&nbsp;';
	D.appendChild(D5);
	
	return D;
}

function AddCMs(evnt, CMs) {
	if (!evnt) {evnt=event;}
	HideContextMenu(LastContextMenu);
	LastContextMenu++;
	var D = AddD(evnt);
	
	for(var i=0; i<CMs.length; i++) {
		var CM = document.createElement('DIV');
		CM.style.width = 170;
		var MName = CMs[i].Name.toString();
		if (MName == '-') {
			CM.innerHTML = '<hr width="100%" noshade color="#999999" size="1">';
		} else {
			if (MName.substr(0, 1) == '&') {
				CM.style.fontSize = '11px';
				CM.style.color = '#CCCCCC';
				CM.style.cursor = 'pointer';
				CM.style.backgroundImage = "url('"+CMs[i].Image+"')";
				CM.style.backgroundRepeat = 'no-repeat';
				CM.style.backgroundPositionY = 'center';
				CM.style.backgroundPositionX = '3px';
				CM.style.paddingTop = '5px';
				CM.style.paddingBottom = '5px';
				CM.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+MName.substr(1, 255);
			} else {
				CM.style.fontSize = '11px';
				CM.style.cursor = 'pointer';
				CM.style.backgroundImage = "url('"+CMs[i].Image+"')";
				CM.style.backgroundRepeat = 'no-repeat';
				CM.style.backgroundPositionY = 'center';
				CM.style.backgroundPositionX = '3px';
				CM.style.paddingTop = '5px';
				CM.style.paddingBottom = '5px';
				CM.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+MName;
				CM.onmouseover = function(){event.srcElement.style.backgroundColor='#F3F2F2';};
				CM.onmouseout = function(){event.srcElement.style.backgroundColor='';};
				CM.onclick2 = CMs[i].onclick;
				CM.onclick = CMonclick;
			}
		}
		D.appendChild(CM);
	}
}

function HideContextMenu(LCM) {
	if (LCM>0) {
		var LD = document.getElementById('ContextMenu'+LCM);
		if (LD) {
			document.body.removeChild(LD);
		}
	}
}

function CMonclick() {
	if (event) {
		eval(event.srcElement.onclick2);
		window.setTimeout('HideContextMenu('+LastContextMenu+')', 500);
	}
}



// ========================================================================
// Функции контекстных меню сайта
// Первый ОБЯЗАТЕЛЬНЫЙ параметр каждой функции - evnt
// Остальные параметры в каждой функции свои
// ========================================================================

// Контекстное меню пользователя
function ShowUserMenu(evnt, UserID, OrgID) {
	var CMs = new Array();
	CMs[0] = {Name: "Открыть профиль", Image: "/images/context/user.gif", onclick: "OpenWin('/private/auth/profile.php?ID="+UserID+"', 'Profile"+UserID+"', 600)"};
	if (OrgID>0) {
		CMs[1] = {Name: "Профиль организации", Image: "/images/context/users.gif", onclick: "OpenWin('/private/auth/orgprofile.php?ID="+OrgID+"', 'OrgProfile"+OrgID+"', 600)"};
		CMs[2] = {Name: "Написать сообщение", Image: "/images/context/mail.gif", onclick: "OpenWin2('/private/site/reply.php?to="+UserID+"', 'Reply"+UserID+"', 550, 550);"};
		CMs[3] = {Name: "История общения", Image: "", onclick: "OpenWin('/private/site/sendmessage.php?to="+UserID+"', 'History"+UserID+"', 600);"};
		CMs[4] = {Name: "Позвонить", Image: "/images/context/talk.gif", onclick: "CallUser('"+UserID+"', fow.top);"};
	} else {
		CMs[1] = {Name: "Написать сообщение", Image: "/images/context/mail.gif", onclick: "OpenWin2('/private/site/reply.php?to="+UserID+"', 'Reply"+UserID+"', 550, 550);"};
		CMs[2] = {Name: "История общения", Image: "", onclick: "OpenWin('/private/site/sendmessage.php?to="+UserID+"', 'History"+UserID+"', 600);"};
		CMs[3] = {Name: "Позвонить", Image: "/images/context/talk.gif", onclick: "CallUser('"+UserID+"', fow.top);"};
	}
	AddCMs(evnt, CMs);
}

// Админское контекстное меню пользователя
function ShowAdminMenu(evnt, UserID, OrgID) {
	var CMs = new Array();
	CMs[0] = {Name: "Открыть профиль", Image: "/images/context/user.gif", onclick: "OpenWin('/private/auth/profile.php?ID="+UserID+"', 'Profile"+UserID+"', 600)"};
	if (OrgID>0) {
		CMs[1] = {Name: "Профиль организации", Image: "/images/context/users.gif", onclick: "OpenWin('/private/auth/orgprofile.php?ID="+OrgID+"', 'OrgProfile"+OrgID+"', 600)"};
		CMs[2] = {Name: "Написать сообщение", Image: "/images/context/mail.gif", onclick: "OpenWin2('/private/site/reply.php?to="+UserID+"', 'Reply"+UserID+"', 550, 550);"};
		CMs[3] = {Name: "История общения", Image: "", onclick: "OpenWin('/private/site/sendmessage.php?to="+UserID+"', 'History"+UserID+"', 600);"};
		CMs[4] = {Name: "Позвонить", Image: "/images/context/talk.gif", onclick: "CallUser('"+UserID+"', fow.top);"};
		CMs[5] = {Name: "-", Image: "", onclick: ""};
		CMs[6] = {Name: "Изменить профиль", Image: "/images/context/edit.gif", onclick: "OpenWin('/private/admin/users/edit.php?id="+UserID+"', 'EditProfile"+UserID+"', 650)"};
		CMs[7] = {Name: "Начислить бонус", Image: "/images/context/money.gif", onclick: "OpenWin('/private/admin/givebonus.php?id="+UserID+"', 'GiveBonus"+UserID+"', 600)"};
		CMs[8] = {Name: "Смотреть расходы", Image: "/images/context/money.gif", onclick: "OpenWin('/private/admin/users/balanseout_full.php?id="+UserID+"', 'Balance"+UserID+"', 600)"};
	} else {
		CMs[1] = {Name: "Написать сообщение", Image: "/images/context/mail.gif", onclick: "OpenWin2('/private/site/reply.php?to="+UserID+"', 'Reply"+UserID+"', 550, 550);"};
		CMs[2] = {Name: "История общения", Image: "", onclick: "OpenWin('/private/site/sendmessage.php?to="+UserID+"', 'History"+UserID+"', 600);"};
		CMs[3] = {Name: "Позвонить", Image: "/images/context/talk.gif", onclick: "CallUser('"+UserID+"', fow.top);"};
		CMs[4] = {Name: "-", Image: "", onclick: ""};
		CMs[5] = {Name: "Изменить профиль", Image: "/images/context/edit.gif", onclick: "OpenWin('/private/admin/users/edit.php?id="+UserID+"', 'EditProfile"+UserID+"', 650)"};
		CMs[6] = {Name: "Начислить бонус", Image: "/images/context/money.gif", onclick: "OpenWin('/private/admin/givebonus.php?id="+UserID+"', 'GiveBonus"+UserID+"', 600)"};
		CMs[7] = {Name: "Смотреть расходы", Image: "/images/context/money.gif", onclick: "OpenWin('/private/admin/users/balanseout_full.php?id="+UserID+"', 'Balance"+UserID+"', 600)"};
	}
	AddCMs(evnt, CMs);
}

// Контекстное меню лота(заявки) на торговой площадке (НЕ для владельца лота(заявки))
function ShowTorgAllMenu(evnt, TorgAllID, UserID, OrgID) {
	var CMs = new Array();
	CMs[0] = {Name: "Детали лота", Image: "/images/context/torg_app.gif", onclick: "OpenDetail("+TorgAllID+");"};
	CMs[1] = {Name: "Оптимальные пары", Image: "/images/context/torg_opt.gif", onclick: "document.location.href='/private/work/torg_optimal.php?ID="+TorgAllID+"';"};
	CMs[2] = {Name: "Торги по лоту", Image: "/images/context/torg_torg.gif", onclick: "OpenWin('/private/work/torg_torg.php?ID="+TorgAllID+"', 'Torg"+TorgAllID+"', 600);"};
	if (UserID>0) {
		CMs[3] = {Name: "Контактная информация", Image: "/images/context/user.gif", onclick: "OpenWin('/private/auth/profile.php?ID="+UserID+"', 'Profile"+UserID+"', 600)"};
		CMs[4] = {Name: "Написать сообщение", Image: "/images/context/mail.gif", onclick: "OpenWin2('/private/site/reply.php?to="+UserID+"', 'Reply"+UserID+"', 550, 550);"};
		if (OrgID>0) {
			CMs[5] = {Name: "Профиль организации", Image: "/images/context/users.gif", onclick: "OpenWin('/private/auth/orgprofile.php?ID="+OrgID+"', 'OrgProfile"+OrgID+"', 600)"};
		}
//	} else {
//		CMs[3] = {Name: "Контактная информация", Image: "/images/context/user.gif", onclick: "alert('Ваш тарифный план не позволяет просматривать контакты участников торгов до момента заключения сделки.');"};
//		CMs[4] = {Name: "&Написать сообщение", Image: "/images/context/mail.gif", onclick: "alert('Ваш тарифный план не позволяет просматривать контакты участников торгов до момента заключения сделки.');"};
	}
	AddCMs(evnt, CMs);
}

// Контекстное меню лота(заявки) на торговой площадке (ТОЛЬКО для владельца лота(заявки) и АДМИНИСТРАТОРОВ)
function ShowTorgAllOwnerMenu(evnt, TorgAllID, UserID, OrgID) {
	var CMs = new Array();
	CMs[0] = {Name: "Детали лота", Image: "/images/context/torg_app.gif", onclick: "OpenDetail("+TorgAllID+");"};
	CMs[1] = {Name: "Оптимальные пары", Image: "/images/context/torg_opt.gif", onclick: "document.location.href='/private/work/torg_optimal.php?ID="+TorgAllID+"';"};
	CMs[2] = {Name: "Торги по лоту", Image: "/images/context/torg_torg.gif", onclick: "OpenWin('/private/work/torg_torg.php?ID="+TorgAllID+"', 'Torg"+TorgAllID+"', 600);"};
	CMs[3] = {Name: "-", Image: "", onclick: ""};
	CMs[4] = {Name: "Изменить лот", Image: "/images/context/edit.gif", onclick: "document.location.href='/private/work/torg_edit.php?id="+TorgAllID+"';"};
	CMs[5] = {Name: "Удалить лот", Image: "/images/context/delete.gif", onclick: "if(confirm('Вы действительно хотите удалить лот?')){OpenWin('/private/work/torg_detail.php?ID="+TorgAllID+"&act=del', 'Torg"+TorgAllID+"', 600);}"};
	CMs[6] = {Name: "-", Image: "", onclick: ""};
	if (UserID>0) {
		CMs[7] = {Name: "Контактная информация", Image: "/images/context/user.gif", onclick: "OpenWin('/private/auth/profile.php?ID="+UserID+"', 'Profile"+UserID+"', 600)"};
		CMs[8] = {Name: "Написать сообщение", Image: "/images/context/mail.gif", onclick: "OpenWin2('/private/site/reply.php?to="+UserID+"', 'Reply"+UserID+"', 550, 550);"};
		if (OrgID>0) {
			CMs[9] = {Name: "Профиль организации", Image: "/images/context/users.gif", onclick: "OpenWin('/private/auth/orgprofile.php?ID="+OrgID+"', 'OrgProfile"+OrgID+"', 600)"};
		}
	}
	AddCMs(evnt, CMs);
}

// Контекстное меню новости
function ShowNewsMenu(evnt, NewsID, LastNewsID, NextNewsID) {
	if (evnt && evnt.srcElement) {
		var tag = evnt.srcElement.tagName;
		if (tag=='A' || tag=='IMG' || tag=='TD') {
			return true;
		}
	}
	
	if (document.selection && document.selection.type!='None') {
		return true;
	}
	
	var CMs = new Array();
	if (NextNewsID>0) {
		CMs[0] = {Name: "К следующей новости", Image: "/images/context/arrow_right.gif", onclick: "document.location.href='/private/news/index.php?id="+NextNewsID+"';"};
	} else {
		CMs[0] = {Name: "&К следующей новости", Image: "/images/context/arrow_right_gray.gif", onclick: "document.location.href='/private/news/index.php?id="+NextNewsID+"';"};
	}
	if (LastNewsID>0) {
		CMs[1] = {Name: "К предыдущей новости", Image: "/images/context/arrow_left.gif", onclick: "document.location.href='/private/news/index.php?id="+LastNewsID+"';"};
	} else {
		CMs[1] = {Name: "&К предыдущей новости", Image: "/images/context/arrow_left_gray.gif", onclick: "document.location.href='/private/news/index.php?id="+LastNewsID+"';"};
	}
	CMs[2] = {Name: "-", Image: "", onclick: ""};
	CMs[3] = {Name: "Копировать ссылку", Image: "", onclick: "clipboardData.setData('Text', 'http://"+DomainName+"/news/"+NewsID+"');"};
	CMs[4] = {Name: "Версия для печати", Image: "/images/context/print.gif", onclick: "window.open('/private/news/index.php?id="+NewsID+"&prn=1', 'News"+NewsID+"Prn');"};
	CMs[5] = {Name: "Отправить новость другу", Image: "/images/context/mail.gif", onclick: "if(email=prompt(\'Введите e-mail получателя\', \'\')){document.location.href='/private/news/index.php?id="+NewsID+"&email='+email}"};
	CMs[6] = {Name: "-", Image: "", onclick: ""};
	CMs[7] = {Name: "Обновить страницу", Image: "", onclick: "document.location.href=document.location.href;"};
	AddCMs(evnt, CMs);
	return false;
}

// Админское контекстное меню новости
function ShowNewsAdminMenu(evnt, NewsID, LastNewsID, NextNewsID) {
	if (evnt && evnt.srcElement) {
		var tag = evnt.srcElement.tagName;
		if (tag=='A' || tag=='IMG' || tag=='TD') {
			return true;
		}
	}
	
	if (document.selection && document.selection.type!='None') {
		return true;
	}
	
	var CMs = new Array();
	if (NextNewsID>0) {
		CMs[0] = {Name: "К следующей новости", Image: "/images/context/arrow_right.gif", onclick: "document.location.href='/private/news/index.php?id="+NextNewsID+"';"};
	} else {
		CMs[0] = {Name: "&К следующей новости", Image: "/images/context/arrow_right_gray.gif", onclick: "document.location.href='/private/news/index.php?id="+NextNewsID+"';"};
	}
	if (LastNewsID>0) {
		CMs[1] = {Name: "К предыдущей новости", Image: "/images/context/arrow_left.gif", onclick: "document.location.href='/private/news/index.php?id="+LastNewsID+"';"};
	} else {
		CMs[1] = {Name: "&К предыдущей новости", Image: "/images/context/arrow_left_gray.gif", onclick: "document.location.href='/private/news/index.php?id="+LastNewsID+"';"};
	}
	CMs[2] = {Name: "-", Image: "", onclick: ""};
	CMs[3] = {Name: "Копировать ссылку", Image: "", onclick: "clipboardData.setData('Text', 'http://"+DomainName+"/news/"+NewsID+"');"};
	CMs[4] = {Name: "Версия для печати", Image: "/images/context/print.gif", onclick: "window.open('/private/news/index.php?id="+NewsID+"&prn=1', 'News"+NewsID+"Prn');"};
	CMs[5] = {Name: "Отправить новость другу", Image: "/images/context/mail.gif", onclick: "if(email=prompt(\'Введите e-mail получателя\', \'\')){document.location.href='/private/news/index.php?id="+NewsID+"&email='+email}"};
	CMs[6] = {Name: "-", Image: "", onclick: ""};
	CMs[7] = {Name: "Обновить страницу", Image: "", onclick: "document.location.href=document.location.href;"};
	CMs[8] = {Name: "-", Image: "", onclick: ""};
	CMs[9] = {Name: "Изменить новость", Image: "/images/context/edit.gif", onclick: "document.location.href='/private/admin/news.php?id="+NewsID+"';"};
	AddCMs(evnt, CMs);
	return false;
}



//=======================================================================
// Красявости
//=======================================================================

var rr = null;
if(!rr) try { rr = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e){}
if(!rr) try { rr = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e){}
if(!rr) try { rr = new XMLHttpRequest(); } catch (e){}

var tmpevnt = null;
var tmpadmins = null;

// Контекстное меню со списком авторизованных
function ShowOnlineUsers(evnt, admins) {
	if(!rr) return false;
	
	tmpevnt = evnt;
	tmpadmins = admins;
	
	var URL = "/private/site/users_online.php";
	if (admins==1) {
		URL += "?admin=1";
	}
	
	rr.abort();
	rr.open("GET", URL, false);
	rr.onreadystatechange=function(evnt) {
			if (rr.readyState==4 && rr.status==200) {
				ShowOnlineUsers2(rr.responseText);
			}
		}
	rr.send("");
}

function ShowOnlineUsers2(text) {
	if (!tmpevnt) return false;
	
	var arr = text.toString().split("\n");
	if (arr.length<1) {
		return false;
	}
	
	var CMs = new Array();
	
	if (tmpadmins==1) {
		CMs[0] = {Name: "Администрация", Image: "/images/context/users.gif", onclick: "OpenWin('/private/users/admins.php', 'Admins', 600)"};
	} else {
		CMs[0] = {Name: "Пользователи на сайте", Image: "/images/context/users.gif", onclick: "OpenWin('/private/site/online.php', 'UsersOnline', 600)"};
	}
	CMs[1] = {Name: "-", Image: "", onclick: ""};
	var n = 2;
	for (i in arr) {
		arr2 = arr[i].toString().split("	");
		if (arr2[1]) {
			CMs[n] = {Name: arr2[1], Image: "/images/status/online.gif", onclick: "OpenWin('/private/auth/profile.php?ID="+arr2[0]+"', 'Profile"+arr2[0]+"', 600)"};
			n++;
		}
	}
	
	AddCMs(tmpevnt, CMs);
}


