var menus_collection = {};

function getOffsetLeft (el) {
  var ol = el.offsetLeft;
  while ((el = el.offsetParent) != null) ol += el.offsetLeft;
  return ol; }

function getOffsetTop (el) {
  var ot = el.offsetTop;
  while((el = el.offsetParent) != null) ot += el.offsetTop;
  return ot; }

function getElementById( id ) {
  itm = null;

  if (document.getElementById) itm = document.getElementById(id);
    else if (document.all)  itm = document.all[id];
      else if (document.layers) itm = document.layers[id];

  return itm;
}

function getCookie( name ) {
  cname = name + '=';
  cpos  = document.cookie.indexOf( cname );
  if( cpos != -1 ) {
    cstart = cpos + cname.length;
    cend   = document.cookie.indexOf(";", cstart);
    if( cend == -1 )
      cend = document.cookie.length;
    return unescape( document.cookie.substring(cstart, cend) );
  }
  return null;
}

function setCookie( name, value, sticky ) {
  expire = "";
  domain = "";
  path   = "/";
  if ( sticky )
    expire = "; expires=Wed, 1 Jan 2020 00:00:00 GMT";
  document.cookie = name + "=" + value + "; " + expire +  ';';
}


// Новое всплывающее меню

function popupmenu_CollectNew( Id, iWidth, sTitle, stItems, image ) {
	if( menus_collection[ Id ] != '' && menus_collection[ Id ] != null ) {
		// alert( 'Popup menu with id "' + Id + '" always exists in current collection.\nCreate process was terminated.' );
		return;
	}

	if( !stItems || stItems == false ) {
	 	// alert( 'Can not collect popup menu from empty.\nCreate process was terminated.' );
	 	return;
	}

	if( !image || image == null ) image = './images/menu_item.gif';

	menus_collection[ Id ] = 'gang_popupmenu_' + Id;
	var wasId = Id;
	Id = 'gang_popupmenu_' + Id;
	var iFontSize = "8.0";

	var htmWindow = '<table class="popupmenu_canvas" id="' + Id + '" name="' + Id + '" style="width: width: 100%; visibility: hidden; display: none; position: absolute;" cellpadding="0" border="0">';
	var i = 0;
	var htmTitle = '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td><div style="width: 100%;" class="popupmenu_title" >' + sTitle + '</div></td></tr></table>';
	htmWindow += '<tr><td>' + htmTitle + '</td></tr>';
	for( i in stItems ) {
	 	if( i.substr( 0, 1 ) == '#' ) htmWindow += '<tr><td><div style="width: ' + iWidth + 'px;" class="popupmenu_title" >' + i.substr( 1, i.length ) + '</div></td></tr>';
	 	else if( i.substr( 0, 1 ) == '-' ) htmWindow += '<tr><td><div style="width: ' + iWidth + 'px;" class="popupmenu_divider">&nbsp;</div></td></tr>';
	 	else if( i.substr( 0, 1 ) != '-' )
	 	{
	 		iFontSize = i.substr( 0, 6 );
	 		a = i;
	 		if( iFontSize.substr( 2, 1 ) != '.' ) iFontSize = "8.0pt";
	 		else a = i.substr( 6, i.length );
	 		htmWindow += '<tr><td><div style="width: ' + iWidth + 'px; font-size: ' + iFontSize + ';" class="popupmenu_item" onMouseOver="javascript: this.className=\'popupmenu_item_hover\';" onMouseOut="javascript: this.className=\'popupmenu_item\';" onClick="javascript: ' + stItems[ i ] + '; popupmenu_HideAllCollection();"><img src="' + image + '" border="0" valign="absmiddle" /><span style="font-size: 8.0pt">&nbsp;&nbsp;</span>' + a + '</div></td></tr>';
	 	}
	}
	htmWindow += '</table>';

	document.write( htmWindow );
}

// Показывает меню по Id. Всплывает у родительского элемента oParent

function popupmenu_ShowCollected( Id, oParent, Align ) {
	if( !menus_collection[ Id ] || menus_collection[ Id ] == null ) {
		alert( 'Popup menu with id "' + Id + '" is not exists in current collection.' );
		return;
	}
	var oMenu    = getElementById( menus_collection[ Id ] );
	if( !oMenu || oMenu == null ) {
		alert( 'Selected popup menu was not collected.\nYou must collect popup menu before showing it as collected item.' );
		return;
	}

	if( !Align || Align == null ) Align = 'left';
	oMenu.style.display    = "inline";
	var iLeftPos = getOffsetLeft( oParent ) + "px";
	if( Align != 'left' ) iLeftPos = getOffsetLeft( oParent ) - oMenu.offsetWidth + oParent.offsetWidth + "px";
	var iTopPos  = getOffsetTop( oParent ) + oParent.offsetHeight + "px";
	oMenu.style.left = iLeftPos;
	oMenu.style.top  = iTopPos;
	oMenu.style.visibility = "visible";
}

// Скрывает меню по Id.

function popupmenu_HideCollected( Id ) {
	if( !menus_collection[ Id ] || menus_collection[ Id ] == null ) {
		alert( 'Popup menu with id "' + Id + '" is not exists in current collection.' );
		return;
	}
	var oMenu    = getElementById( menus_collection[ Id ] );
	if( !oMenu || oMenu == null ) {
		alert( 'Selected popup menu was not collected.\nYou must collect popup menu before showing it as collected item.' );
		return;
	}

	oMenu.style.visibility = "hidden";
	oMenu.style.display    = "none";
}

// Уничтожает запись о меню в коллекции по Id

function popupmenu_DestroyCollected( Id ) {
	if( !menus_collection[ Id ] || menus_collection[ Id ] == null ) {
		alert( 'Popup menu with id "' + Id + '" is not exists in current collection.' );
		return;
	}
	var oMenu    = getElementById( menus_collection[ Id ] );
	if( !oMenu || oMenu == null ) {
		alert( 'Selected popup menu was not collected.\nYou must collect popup menu before destroing it as collected item.' );
		return;
	}

	oMenu.id   = '';
	oMenu.name = '';
	menus_collection[ Id ] = '';
}

// Скрывает всю коллекцию меню

function popupmenu_HideAllCollection() {
	var i = 0;
	for( i in menus_collection ) popupmenu_HideCollected( i );
}


// Создает цветовое меню "матричного" вида
// iWidth - в отличие от функции popupmenu_CollectNew() определяет ширину одного элемента матрицы

function popupmenu_CollectColorMatrix( Id, iWidth, sTitle, stItems )
{
	if( menus_collection[ Id ] != '' && menus_collection[ Id ] != null ) {
		// alert( 'Popup menu with id "' + Id + '" always exists in current collection.\nCreate process was terminated.' );
		return;
	}

	if( !stItems || stItems == false ) {
	 	// alert( 'Can not collect popup menu from empty.\nCreate process was terminated.' );
	 	return;
	}

	menus_collection[ Id ] = 'gang_popupmenu_' + Id;
	var wasId = Id;
	Id = 'gang_popupmenu_' + Id;
	var iFontSize = "8.0pt";

	var htmWindow = '<table class="popupmenu_canvas" id="' + Id + '" name="' + Id + '" style="width: width: 100%; visibility: hidden; display: none; position: absolute;" cellpadding="0" cellspacing="0" border="0">';
	var i = 0;
	var htmTitle = '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td><div style="width: 100%;" class="popupmenu_title" >' + sTitle + '</div></td></tr></table>';
	htmWindow += '<tr><td>' + htmTitle + '</td></tr>';

for( var j in stItems )
{
	htmWindow += '<tr><td style="padding: 0;"><table cellpadding="0" cellspacing="0" border="0"><tr>';
	for( var i in stItems[ j ] )
	{
 		htmWindow += '<td title="Вставить цвет: ' + stItems[ j ][ i ] + '" bgcolor="' + stItems[ j ][ i ] + '" style="width: ' + iWidth + 'px; font-size: 8pt;" ';
 		htmWindow += 'class="popupmenu_citem" onMouseOver="javascript: this.className=\'popupmenu_citem_hover\';" ';
 		htmWindow += 'onMouseOut="javascript: this.className=\'popupmenu_citem\';" ';
 		htmWindow += 'onClick="javascript: java_editor.insert_color_tag( \'' + stItems[ j ][ i ] + '\'); popupmenu_HideCollected( \'' + wasId + '\' );">';
 		htmWindow += '&nbsp;</td>';
	}
	htmWindow += '</td></table></td></tr>';
}

	htmWindow += '</table>';

	document.write( htmWindow );
}


// Создает меню колобков "матричного" вида

function popupmenu_CollectEmoticonMatrix( Id, sTitle, iMainWidth, stItems, EmoticonsPath )
{
	if( menus_collection[ Id ] != '' && menus_collection[ Id ] != null ) {
		// alert( 'Popup menu with id "' + Id + '" always exists in current collection.\nCreate process was terminated.' );
		return;
	}

	if( !stItems || stItems == null ) {
	 	// alert( 'Can not collect popup menu from empty.\nCreate process was terminated.' );
	 	return;
	}

	menus_collection[ Id ] = 'gang_popupmenu_' + Id;
	var wasId = Id;
	Id = 'gang_popupmenu_' + Id;
	var iFontSize = "8.0pt";

	var htmWindow = '<table class="popupmenu_canvas" id="' + Id + '" name="' + Id + '" style="width: ' + iMainWidth + '; visibility: hidden; display: none; position: absolute;" cellpadding="0" cellspacing="0" border="0">';
	var i = 0;
	var htmTitle = '<table border="0" cellpadding="0" cellspacing="0" style="width: 100%;"><tr><td class="popupmenu_title">' + sTitle + '</td></tr></table>';
	// Вычисляем ширину ячеек в процентах
	var count = 0;
	for( var j in stItems )
	{
		for( var i in stItems[ j ] )
			count++;
		break;
	}
	var iWidth = parseInt( parseInt( iMainWidth ) / count ) + "px";

	htmWindow += '<tr><td style="width: 100%;">' + htmTitle + '</td></tr>';
	htmWindow += '<tr><td style="width: 100%;" style="padding: 0;"><table style="width: 100%;" cellpadding="0" cellspacing="0" border="0">';
for( var j in stItems )
{
	htmWindow += '<tr>';
	for( var i in stItems[ j ] )
	{
 		htmWindow += '<td title="' + i + '" style="width: ' + iWidth + '; height: 50px;" ';
 		htmWindow += 'class="popupmenu_citem" onMouseOver="javascript: this.className=\'popupmenu_citem_hover\';" ';
 		htmWindow += 'onMouseOut="javascript: this.className=\'popupmenu_citem\';" ';
 		htmWindow += 'onClick="javascript: java_editor.insert_emoticon( \'' + i + '\'); popupmenu_HideCollected( \'' + wasId + '\' );">';
 		htmWindow += '<div align="center"><img valign="middle" src="' + EmoticonsPath + stItems[ j ][ i ] + '" border="0" /></div></td>';
	}
	htmWindow += '</tr>';
}

	htmWindow += '</table></td></tr>';
	htmWindow += '</table>';

	document.write( htmWindow );
}

function create_menu_bar( index, title, items ) {
 var html = '<div id="popup_menu_'+index+'" name="popup_menu_'+index+'" class="borderwrapm" style="width:250px; visibility: hidden; border-color: #777777; display: none;">';
 html += '<table cellpadding="0" cellspacing="0" class="ipbtable">';
 html += '<tr><td class="popupmenu-category" style="border:0;padding-top:6px;text-align:left;font-weight:bold;font-size:10px;color:#000000;">'+title+'</td></tr>';
 for( var i in items ) {
  if( items[i].substr(0, 3) == "id=" ) continue;
  html += '<tr><td class="popupmenu" style="border:0; border-top:#777777 1px solid;font-size:10px;"><img src="./forums/style_images/light-beige/menu_item.gif" border="0" alt="*">&nbsp;&nbsp;<a href="http://www.city-of-masters.ru/download.php?id='+items[i]+'" target="_blank" alt="Click to download" title="Click to download">'+i+"</a></td></tr>";
 }
 html += "</table></div>";
 return html;
}

function adm_menu_bar( index, title, items ) {
 var html = '<div id="popup_menu_'+index+'" name="popup_menu_'+index+'" class="borderwrapm" style="width:250px; padding:0; visibility: hidden; display: none;">';
 html += '<table cellpadding="0" cellspacing="0" class="ipbtable" style="width:100%;border:1px solid;">';
 if( title != "" ) html += '<tr><td class="titlemedium" style="border-right:1px solid;padding-left:5px;text-align:left;font-weight:bold;font-size:12px;color:#000000;">'+title+'</td></tr>';
 for( var i in items ) {
  html += '<tr><td class="tablerow1" style="border:0px solid;border-bottom:#ffffff 1px solid;text-align:left;text-indent:10px;font-size:11px;line-height:135%;"><a href="'+items[i]+'">'+i+"</a></td></tr>";
 }
 html += "</table></div>";
 return html;
}

function new_menu_bar( index, title, items ) {
 var html = '<div id="popup_menu_'+index+'" name="popup_menu_'+index+'" class="borderwrapm" style="width:300px; border:0; padding:0; visibility: hidden; display: none;">';
 html += '<table cellpadding="0" cellspacing="0" class="ipbtable" style="width:100%;border:1px solid; border-top:0;">';
 if( title != "" ) html += '<tr><td class="popupmenu-category" style="padding-left:5px;text-align:left;font-weight:bold;font-size:10px;color:#000000;">'+title+'</td></tr>';
 for( var i in items ) {
  html += '<tr><td class="popupmenu" style="border:0px solid;border-bottom:#ffffff 1px solid;text-align:left;text-indent:10px;font-size:11px;line-height:135%;"><img src="./forums/style_images/light-beige/menu_item.gif" border="0" alt="*">&nbsp;&nbsp;<a href="'+items[i]+'">'+i+"</a></td></tr>";
 }
 html += "</table></div>";
 return html;
}

function popup_menu( index, command ) {

 // find menu by index
 var menu = document.getElementById( "popup_menu_"+index );

 // set need position
 var iLeftPos = ( getOffsetLeft( command ) + 9 ) + "px";
 var iTopPos  = ( getOffsetTop( command ) + ( command.offsetHeight ) - 9 ) + "px";

 menu.style.position = "absolute";
 menu.style.left     = iLeftPos;
 menu.style.top      = iTopPos;

 if( menu.style.display == "none" ) {
  // popup menu
  menu.style.visibility = "visible";
  menu.style.display    = "inline";
  // set image disabled
  command.src   = "./portal/images/next_N.gif";
  command.title = "Hide menu";
  command.alt   = "Hide menu";
 } else {
  // hide menu
  menu.style.visibility = "hidden";
  menu.style.display    = "none";
  // set image enabled
  command.src   = "./portal/images/next_R.gif";
  command.title = "Quick download";
  command.alt   = "Quick download";
 }

 return;
}

function adm_popup_menu( index, command, angle_align ) {

 // find menu by index
 var menu = document.getElementById( "popup_menu_"+index );

 // set need position
 var iLeftPos = 0;
 var iTopPos  = 0;

 if( angle_align == 'left' ) {
  iLeftPos = ( ( getOffsetLeft( command ) + command.offsetWidth ) + 9 ) + "px";
  iTopPos  = ( ( getOffsetTop( command ) + command.offsetHeight ) - 9 ) + "px";
 }

 if( angle_align == 'right' ) {
  iLeftPos = ( getOffsetLeft( command ) - 250 ) + "px";
  iTopPos  = ( ( getOffsetTop( command ) + command.offsetHeight ) - 9 ) + "px";
 }

 menu.style.position = "absolute";
 menu.style.left     = iLeftPos;
 menu.style.top      = iTopPos;

 if( menu.style.display == "none" ) {
  // popup menu
  menu.style.visibility = "visible";
  menu.style.display    = "inline";

  command.src="../portal/images/indexdowntoolbar.gif";
 } else {
  // hide menu
  menu.style.visibility = "hidden";
  menu.style.display    = "none";

  command.src="../portal/images/indextoolbar.gif";
 }

 return;
}

function _popup( index, command, angle_align ) {

 // find menu by index
 var menu = document.getElementById( "popup_menu_"+index );

 // set need position
 var iLeftPos = 0;
 var iTopPos  = 0;

 if( angle_align == 'left' ) {
  iLeftPos = ( ( getOffsetLeft( command ) + command.offsetWidth ) + 9 ) + "px";
  iTopPos  = ( ( getOffsetTop( command ) + command.offsetHeight ) - 9 ) + "px";
 }

 if( angle_align == 'right' ) {
  iLeftPos = ( getOffsetLeft( command ) - 300 ) + "px";
  iTopPos  = ( ( getOffsetTop( command ) + command.offsetHeight ) - 9 ) + "px";
 }

 menu.style.position = "absolute";
 menu.style.left     = iLeftPos;
 menu.style.top      = iTopPos;

 if( menu.style.display == "none" ) {
  // popup menu
  menu.style.visibility = "visible";
  menu.style.display    = "inline";
 } else {
  // hide menu
  menu.style.visibility = "hidden";
  menu.style.display    = "none";
 }

 return;
}

