var ___browser = 'Unknown';
if(navigator.userAgent.search(/MSIE/i) >= 0)
  ___browser = 'MSIE';
else if(navigator.userAgent.search(/Opera/i) >= 0)
  ___browser = 'Opera';
else if(navigator.userAgent.search(/Firefox/i) >= 0)
  ___browser = 'Firefox';

var origHtml = new Array();
function UpdateFpText(vi){
	origHtml[vi] = document.getElementById('fptext'+vi).innerHTML;
	
	var re = /[ ]*[\r]?[\n]?<br>[ ]*[\r]?[\n]?[ ]*/gi;
	origHtmltmp = origHtml[vi].replace(re, "\r\n");
	re = /<[\/]?b>/gi;
	origHtmltmp = origHtmltmp.replace(re, "");
	document.getElementById('fptext'+vi).innerHTML='\
	<table cellspacing="0" cellpadding="0" border="0">\
<tr>\
	<td colspan="2"><textarea cols="" rows="5" name="text" onKeyUp="check_length(this)" id="text'+vi+'" style="width:169px"></textarea></td>\
</tr>\
<tr>\
	<td><input type="button" name="reset" value="Отменить" onClick="resetform(\''+vi+'\');"></td>\
	<td align="right"><input type="button" name="save" value="Сохранить" onClick="saveform(\''+vi+'\')"></td>\
</tr>\
</table>';

	document.getElementById('text'+vi).value = inpscr(origHtmltmp).replace(/<\/?a[^>]*>/ig,'');
	document.getElementById('chtextB'+vi).style.visibility = 'hidden';
	document.getElementById('chtextB'+vi).style.height = '1px';
	document.getElementById('chtextB'+vi).style.overflow = 'hidden';
	document.getElementById('text'+vi).focus();

}

function resetform(vi){
	document.getElementById('fptext'+vi).innerHTML = origHtml[vi];
	document.getElementById('chtextB'+vi).style.visibility = 'visible';
	document.getElementById('chtextB'+vi).style.height = 'auto';
}

function saveform(vi){
	txt = document.getElementById('text'+vi).value;
	xajax_FpTextSave(txt, vi);
}

function check_length(message){
  var maxLen = 50;
  if (message.value.length > maxLen)
    {
    alert('Слишком длинный текст');
    message.value = message.value.substring(0, maxLen);
    }
}



var player=null; // Об]ект RTPlayer
var mg_lock=0;
var cleans = new Array();
var mg_ufpID = null;
var nomg_contentDiv = null;
var mg_container = null;
var mg_timer=null;
var fp_addedWorksCount     = -1;
var fp_maxWorks            = 3;
var mg_prevLeftCatherHtml  = '';
var mg_prevRightCatherHtml = '';
var mg_debug = 1;
var mg_all = null;
var mg_isEdit=0;
var mg_wait=0;
var mg_closeLock=0;


var MG_BORDER             = 1;
var MG_PLAYINTERVAL       = 2;
var MG_ABSOLUTETOP        = -20 - MG_BORDER;
var MG_ABSOLUTELEFT       = -15 - MG_BORDER;
var MG_ZINDEX             = 20;
var MG_BGCOLOR            = '#ffffff';
var MG_TABLEWIDTH         = 955;
var MG_LEFTBLOCKWIDTH     = 265;
var MG_TABLEBORDER        = MG_BORDER+'px solid #c0c0c0';
var MG_SNAKECOLOR         = '#9f9f9f';
var MG_SNAKEWIDTH         = 1;
var MG_SHADOWOPACITY      = 0.13;
var MG_SHADOWOFFSET       = 2;
var MG_SHADOWBGCOLOR      = 'black';
var MG_GLOOMOPACITY       = 0.14;
var MG_GLOOMBGCOLOR       = 'black';



function mg_onError(msg)
{
	if(mg_debug)
		alert(msg);
}


var lockInterval=null;
var closeTimerID=null;
var closeTimeout=450;

function mg_onGloomOver()
{
	mg_closeLock=0;
	if(!lockInterval)
	  lockInterval = window.setInterval('if(!mg_lock) { window.clearInterval(lockInterval); lockInterval=null; if(!closeTimerID) closeTimerID=window.setTimeout("mg_Close()", closeTimeout) }', 100);
}

function mg_onGloomClick()
{
	mg_closeLock=0;
	mg_Close();
}

function mg_onContainerOver()
{
	mg_closeLock=1;
	if(closeTimerID) {
		window.clearTimeout(closeTimerID);
		closeTimerID = null;
	}
}

var gloomOff;
function mg_Close(force)
{ try {
	

//	alert(mg_closeLock+','+(!force)+','+closeTimerID);
  if(mg_closeLock && !force) {
		if(closeTimerID) {
			window.clearTimeout(closeTimerID);
			closeTimerID = null;
		}
		return;
	}

  if(mg_lock)
    return;

  if(player && player.status!='Stopped')
    return;

	var uf;
	if(uf=document.getElementById('fupload'))
	  uf.blur();

  mg_timer=null;
  fp_addedWorksCount = -1;

  if(nomg_contentDiv) {
    nomg_contentDiv.style.visibility="visible";
  	nomg_contentDiv = null;
	}

  mg_prevLeftCatherHtml  = '';
  mg_prevRightCatherHtml = '';

  if(cleans) {
    var o;
    while(o=cleans.pop())
      o.parentNode.removeChild(o);
  }

  player=null;
	var _chtextB
	if(_chtextB = document.getElementById("chtextB"+mg_ufpID))
    _chtextB.style.visibility = 'visible';

  mg_ufpID = null;
  if(mg_container)
    mg_container.parentNode.removeChild(mg_container);

  mg_container=null;

  if(!mg_all)
		return;

	mg_all.fo_gloom.object.parentNode.removeChild(mg_all.fo_gloom.object);
	mg_all=null;

	if(closeTimerID) {
		window.clearTimeout(closeTimerID);
		closeTimerID = null;
	}

	mg_isEdit=0;


//  mg_all.fo_gloom.cur_top = 0;
//  mg_all.fo_gloom.cur_left = 0;
//  mg_all.fo_gloom.cur_width = mg_all.fo_gloom.object.offsetWidth;
//  mg_all.fo_gloom.cur_height = mg_all.fo_gloom.object.offsetHeight;
//	gloomOff = new RTPlayer('gloomOff',
//	new Function(
//	'if(mg_all.fo_gloom.cur_width - 200<=0 && mg_all.fo_gloom.cur_height - 280<=0) {'+
//	'  mg_all.fo_gloom.object.parentNode.removeChild(mg_all.fo_gloom.object);'+
//	'  mg_all=null; gloomOff=null;return false;'+
//	'}'+
//	' mg_all.fo_gloom.cur_height -= 300; if(mg_all.fo_gloom.cur_height<=0) mg_all.fo_gloom.cur_height=0;'+
//	' mg_all.fo_gloom.cur_width -= 200; if(mg_all.fo_gloom.cur_width<=0) mg_all.fo_gloom.cur_width=0;'+
//	' mg_all.fo_gloom.cur_top  += 180;'+
//	' mg_all.fo_gloom.cur_left  += 100;'+
//	' mg_all.fo_gloom.update({ width: mg_all.fo_gloom.cur_width+"px", left: mg_all.fo_gloom.cur_left+"px", top: mg_all.fo_gloom.cur_top+"px", height: mg_all.fo_gloom.cur_height+"px" }); return true'), 2)
//	gloomOff.play();

} catch(e) { mg_onError('mg_Close: '+e.message) } }


function mg_Create()
{
  mg_container
  = createHTMLElement(
    'DIV',
    { display: 'none', position: 'absolute' },
    { 
     innerHTML:
     '<table border="0" cellpadding="0" cellspacing="0" width="' + MG_TABLEWIDTH + '" style="border-collapse:separate">'+ 
       '<col style="width:' + MG_LEFTBLOCKWIDTH + 'px"/>'+
       '<col style="width:' + ( MG_TABLEWIDTH - MG_LEFTBLOCKWIDTH ) + 'px"/>'+
       '<tr valign="top">'+
         '<td onmouseover="mg_onContainerOver()" style="padding-right:8px">'+
           //'<a href="javascript:void(0)"><div id="mg_leftContent" style="overflow:hidden;visibility:hidden;padding-bottom:10px;padding-top:' + ( -(MG_ABSOLUTETOP + MG_BORDER) ) + 'px;padding-left:' + ( -(MG_ABSOLUTELEFT + MG_BORDER) ) + 'px">'+
           //'</div></a>'+
           '<div id="mg_leftContent" style="overflow:hidden;visibility:hidden;padding-bottom:10px;padding-top:' + ( -(MG_ABSOLUTETOP + MG_BORDER) ) + 'px;padding-left:' + ( -(MG_ABSOLUTELEFT + MG_BORDER) ) + 'px">'+
           '</div>'+
         '</td>'+
         '<td onmouseover="mg_onContainerOver()" rowspan="2" nowrap>'+
           '<div id="mg_rightContent" style="overflow:hidden;visibility:hidden">'+
           '</div>'+
         '</td>'+
       '</tr>'+
       '<tr valign="top">'+
         '<td>&nbsp;</td>'+
       '</tr>'+
     '</table>'
    });
}

function mg_Update()
{
  var mg_table     = mg_container.childNodes[0];
  var mg_lTCell    = mg_table.rows[0].cells[0];
  var mg_rCell     = mg_table.rows[0].cells[1];
  var mg_lBCell    = mg_table.rows[1].cells[0];
  var mg_lTDiv     = mg_lTCell.childNodes[0];
  var mg_rDiv      = mg_rCell.childNodes[0];
  var mg_lBRow     = mg_table.rows[1];

//	mg_lBCell.onmouseover = mg_isEdit ? null : mg_onGloomOver;
//	mg_all.fo_gloom.object.onmouseover = mg_isEdit ? null : mg_onGloomOver;
	mg_lBCell.onclick = mg_isEdit ? null : mg_onGloomClick;
	mg_all.fo_gloom.object.onclick = mg_isEdit ? null : mg_onGloomClick;

//  alert(mg_lTDiv.innerHTML);
  mg_rDiv.style.height = 'auto';
  mg_lTDiv.style.height = 'auto';


  mg_lBRow.style.height = (Math.abs(mg_rDiv.offsetHeight - mg_lTDiv.offsetHeight))+'px';
  if(mg_lTDiv.offsetHeight > mg_rDiv.offsetHeight) {
    mg_lTCell.rowSpan = 2;
    mg_rCell.rowSpan = 1;
    mg_lBCell.style.borderRight = 0;
    mg_lBCell.style.borderLeft = MG_TABLEBORDER;
  }
  else {
    mg_lTCell.rowSpan = 1;
    mg_rCell.rowSpan = 2;
    mg_lBCell.style.borderRight = MG_TABLEBORDER;
    mg_lBCell.style.borderLeft = 0;
  }

  mg_all.fo_rShadow.update({ marginTop:  (MG_ABSOLUTETOP + MG_SHADOWOFFSET + MG_BORDER)+'px',
                         marginLeft: (mg_container.offsetWidth + MG_ABSOLUTELEFT)+'px',
                         height: (mg_rCell.offsetHeight - MG_BORDER)+'px'
                       });

  mg_all.fo_bShadow.update({ marginTop:  (mg_rCell.offsetHeight + MG_ABSOLUTETOP)+'px',
                         marginLeft: (MG_ABSOLUTELEFT + MG_SHADOWOFFSET + mg_lTCell.offsetWidth)+'px',
                         width: (mg_rCell.offsetWidth - MG_SHADOWOFFSET)+'px'
                       });
	
	mg_all.fo_gloom.update({height: document.body.scrollHeight+'px'});
}

var gCmpl=hCmpl=rCmpl=false;

function mg_onBeforePlay(ufp_id, wait, mod, is_edit)
{ try {
	
  if(mg_lock)
    return;

  if(wait > 0 && !mg_timer) {
    mg_timer = window.setTimeout("mg_onBeforePlay("+ufp_id+", 0,"+mod+","+(is_edit ? 1 : 0)+")", wait);
    return;
  }

	mg_lock = 1;
	mg_timer = null;
  mg_Create();
  document.getElementById('mg_leftContent').innerHTML = document.getElementById('mgContent'+ufp_id
   ).innerHTML.replace(/(id=\"?)(fptext)/, '$1$2-').replace(/(id=\"?)(chtextB)/, '$1$2-');
	if(!is_edit)
    xajax_FpShowInfo(ufp_id, mod);

} catch(e) { mg_onError('mg_onBeforePlay: '+e.message) } }


function mg_firstFrame(ufp_id)
{ try {
	
  if(!ufp_id)
    return false;
	
  var nomg_contentCapsuleDiv = document.getElementById('mgCapsule' + ufp_id);
  nomg_contentDiv            = document.getElementById('mgContent' + ufp_id);


  mg_all = new Object();

  mg_container
  = createHTMLElement(
    'DIV',
    { position: 'absolute', display: 'block', marginTop: MG_ABSOLUTETOP+'px', marginLeft: MG_ABSOLUTELEFT+'px', zIndex: MG_ZINDEX },
    null,
    mg_container,
    nomg_contentCapsuleDiv,
    nomg_contentDiv);

  var mg_table     = mg_container.childNodes[0];
  var mg_lTCell    = mg_table.rows[0].cells[0];
  var mg_rCell     = mg_table.rows[0].cells[1];
  var mg_lBCell    = mg_table.rows[1].cells[0];
  var mg_lTDiv     = mg_lTCell.childNodes[0]; // !!!
  var mg_rDiv      = mg_rCell.childNodes[0];
  var mg_lBRow     = mg_table.rows[1];


  var mg_fo_gloom = mg_all.fo_gloom = new frameObject({ opacity: 0, zIndex: MG_ZINDEX - 1,
                                      top: '0px', left: '0px',
                                      background: MG_GLOOMBGCOLOR,
                                      //visibility: 'hidden',
                                      width: '100%',
                                      height: 2000+'px',
                                      position: 'absolute' }, null, null, null);

//	mg_lBCell.onmouseover = mg_isEdit ? null : mg_onGloomOver;
//	mg_all.fo_gloom.object.onmouseover = mg_isEdit ? null : mg_onGloomOver;
	mg_lBCell.onclick = mg_isEdit ? null : mg_onGloomClick;
	mg_all.fo_gloom.object.onclick = mg_isEdit ? null : mg_onGloomClick;

  //cleans.push(mg_fo_gloom.object);

  mg_lBRow.style.height = (mg_rDiv.offsetHeight - mg_lTDiv.offsetHeight + MG_BORDER)+'px';

	mg_all.table = mg_table;

  var mg_fo_lTCell = mg_all.fo_lTCell = new frameObject(null,null,null, mg_lTCell);
  var mg_fo_rCell  = mg_all.fo_rCell  = new frameObject(null, null,null, mg_rCell);
  var mg_fo_lBCell = mg_all.fo_lBCell = new frameObject(null,null,null, mg_lBCell);
  var mg_fo_lTDiv  = mg_all.fo_lTDiv  = new frameObject(null,null,null, mg_lTDiv);
  var mg_fo_rDiv   = mg_all.fo_rDiv   = new frameObject(null,null,null, mg_rDiv);


  var mg_snakeT1Step = 7;
  var mg_fo_line1H = Math.round(mg_lTCell.offsetHeight/2) + mg_snakeT1Step - (Math.round(mg_lTCell.offsetHeight/2) % mg_snakeT1Step);
  var mg_fo_line1 = mg_all.fo_line1 = new frameObject({ position: 'absolute',
                                     marginLeft: '0px',
                                     marginTop:  MG_BORDER+'px',
                                     height: mg_fo_line1H+'px',
                                     width: '0px', zIndex: MG_ZINDEX+1,
                                     background: MG_BGCOLOR
                                   },
                                   null, null, null,
                                   mg_container, mg_table);

	mg_fo_line1.cur_height = mg_fo_line1H;

  var mg_fo_snake = mg_all.fo_snake = new frameObject({ background: MG_SNAKECOLOR, zIndex: MG_ZINDEX + 100,
                                      position: 'absolute', width: '0px', height: '0px', overflow:'hidden' },
                                    '&nbsp;', null, null, mg_container, mg_table);

  var mg_fo_rShadow = mg_all.fo_rShadow = new frameObject(
                              { background: MG_SHADOWBGCOLOR,
                                opacity:    MG_SHADOWOPACITY,
                                position:   'absolute',
                                marginTop:  (MG_ABSOLUTETOP + MG_SHADOWOFFSET + MG_BORDER)+'px',
                                marginLeft: (mg_table.offsetWidth + MG_ABSOLUTELEFT)+'px',
                                width:      MG_SHADOWOFFSET+'px',
                                height:     '0px',
                                zIndex:     MG_ZINDEX
                              },
                              null, null, null,
                              nomg_contentCapsuleDiv, mg_container);

  cleans.push(mg_fo_rShadow.object);


  var bsh_cur_left = (mg_table.offsetWidth + MG_ABSOLUTELEFT);
  var bsh_cur_top = (mg_table.offsetHeight + MG_ABSOLUTETOP);
  var mg_fo_bShadow = mg_all.fo_bShadow = new frameObject(
                              { background: MG_SHADOWBGCOLOR,
                                opacity:    MG_SHADOWOPACITY,
                                position:   'absolute',
                                marginTop:  bsh_cur_top+'px',
                                marginLeft: bsh_cur_left+'px',
                                width:      '0px',
                                height:     MG_SHADOWOFFSET+'px',
                                zIndex:     MG_ZINDEX
                              },
                              null, null, null,
                              nomg_contentCapsuleDiv, mg_container);

  cleans.push(mg_fo_bShadow.object);

	mg_fo_bShadow.cur_left  = bsh_cur_left;
	mg_fo_bShadow.cur_top   = bsh_cur_top;
  mg_fo_bShadow.cur_width = 0;

  mg_fo_snake.direct = 'T1';
  mg_fo_snake.cur_top = mg_fo_line1H;
  mg_fo_snake.cur_height = 0;
  mg_fo_snake.cur_left = 0;
  mg_fo_snake.cur_width = MG_SNAKEWIDTH;

  var mg_tablePerimeter = mg_table.offsetWidth*2 + mg_table.offsetHeight*2;
	var mg_ffnorm = 1;
	if(___browser=='Firefox')
		mg_ffnorm = 1.04;

  var mg_norm = 2624 * mg_ffnorm;

  mg_fo_snake.stepT1 = (mg_snakeT1Step / (150 * mg_ffnorm)) * mg_lTCell.offsetHeight * (mg_tablePerimeter / mg_norm);
  mg_fo_snake.stepR  = (81 / (955 * mg_ffnorm)) * mg_table.offsetWidth * (mg_tablePerimeter / mg_norm);
  mg_fo_snake.stepB  = (64 / (320 * mg_ffnorm)) * mg_table.offsetHeight * (mg_tablePerimeter / mg_norm);
  mg_fo_snake.stepL1 = (74 / (635 * mg_ffnorm)) * mg_rCell.offsetWidth * (mg_tablePerimeter / mg_norm);
  mg_fo_snake.stepT2 = (30 / (195 * mg_ffnorm)) * (mg_table.offsetHeight - mg_lTCell.offsetHeight) * (mg_tablePerimeter / mg_norm);
  mg_fo_snake.stepL2 = (50 / (320 * mg_ffnorm)) * mg_lTCell.offsetWidth * (mg_tablePerimeter / mg_norm);
  mg_fo_snake.stepT3 = ((2*mg_snakeT1Step-1.5)  / (150 * mg_ffnorm)) * mg_lTCell.offsetHeight * (mg_tablePerimeter / mg_norm);

  mg_fo_gloom.cur_opacity = 0;
  mg_fo_gloom.opa_step    = MG_GLOOMOPACITY;
  gCmpl=hCmpl=rCmpl=false;

  return true;

} catch(e) { mg_onError('mg_firstFrame: '+e.message); return false; } }



function mg_Play(ufp_id)
{ try {
	
  if(player)
    return;

  if(!ufp_id)
    return;

  if(!mg_firstFrame(ufp_id))
    return;

  player = new RTPlayer('player', mg_onPlay, MG_PLAYINTERVAL);
  player.play();
} catch(e) { mg_onError('mg_Play: '+e.message) } }


function mg_noPlay(ufp_id)
{ try {
	
  if(player)
    return;

  if(!ufp_id)
    return;

  if(!mg_firstFrame(ufp_id))
    return;
  
	mg_lastFrame();
  mg_all.fo_lTCell.update({ borderTop: MG_TABLEBORDER, borderLeft: MG_TABLEBORDER, borderBottom: MG_TABLEBORDER });
  mg_all.fo_lBCell.update({ borderRight: MG_TABLEBORDER });
  mg_all.fo_rCell.update({ borderTop: MG_TABLEBORDER, borderRight: MG_TABLEBORDER, borderBottom: MG_TABLEBORDER });
  mg_all.fo_rShadow.update({ marginTop:  (MG_ABSOLUTETOP + MG_SHADOWOFFSET + MG_BORDER)+'px',
                         marginLeft: (mg_container.offsetWidth + MG_ABSOLUTELEFT)+'px',
                         height: (mg_all.fo_rCell.object.offsetHeight - MG_BORDER)+'px'
                       });

  mg_all.fo_bShadow.update({ marginTop:  (mg_all.fo_rCell.object.offsetHeight + MG_ABSOLUTETOP)+'px',
                         marginLeft: (MG_ABSOLUTELEFT + MG_SHADOWOFFSET + mg_all.fo_lTCell.object.offsetWidth)+'px',
                         width: (mg_all.fo_rCell.object.offsetWidth - MG_SHADOWOFFSET)+'px'
                       });
	mg_lock=0;

} catch(e) { mg_onError('mg_noPlay: '+e.message) } }


function mg_lastFrame(ffWaiting) { try
{
	mg_all.fo_gloom.update({ opacity: MG_GLOOMOPACITY, height: document.body.scrollHeight+'px' });
//	var x = mg_all.fo_lTDiv.object.cloneNode(true);
//	mg_all.fo_lTCell.object.removeChild(mg_all.fo_lTCell.object.childNodes[0]);
//	mg_all.fo_lTDiv.object = x;
//	mg_all.fo_lTCell.object.appendChild(mg_all.fo_lTDiv.object);
  mg_all.fo_lTDiv.update({ background: MG_BGCOLOR, visibility: 'visible' });
  mg_all.fo_rDiv.update({ background: MG_BGCOLOR, visibility: 'visible' });
  mg_all.fo_lTCell.update({ background: MG_BGCOLOR });
  mg_all.fo_rCell.update({ background: MG_BGCOLOR });
  mg_all.fo_line1.object.parentNode.removeChild(mg_all.fo_line1.object);
  mg_all.fo_snake.object.parentNode.removeChild(mg_all.fo_snake.object);

	
} catch(e) { mg_onError('mg_lastFrame: '+e.message) } }


function mg_onPlay() { try
{
  var mg_lTCell = mg_all.fo_lTCell.object;
  var mg_rCell  = mg_all.fo_rCell.object;
  var mg_table  = mg_all.table;

	var	fo_lTCell = mg_all.fo_lTCell;
	var	fo_lBCell = mg_all.fo_lBCell;
	var	fo_lTDiv  = mg_all.fo_lTDiv;
  var	fo_rDiv   = mg_all.fo_rDiv;
  var	fo_rCell  = mg_all.fo_rCell;
  var	fo_line1  = mg_all.fo_line1;
  var	fo_snake  = mg_all.fo_snake;
  var	fo_gloom  = mg_all.fo_gloom;
  var fo_rShadow = mg_all.fo_rShadow;
  var fo_bShadow = mg_all.fo_bShadow;


  if(rCmpl) {
    nomg_contentDiv.style.visibility="hidden";
		mg_lastFrame();
    gCmpl = true;
  }


  if(gCmpl && rCmpl) {
		mg_lock=0;
		return false;
	}

  if(rCmpl)	return true;


  if(fo_snake.direct=='T1') {
    if(fo_snake.cur_top - fo_snake.stepT1 <= 0 ) {
      fo_line1.update({ borderLeft: MG_TABLEBORDER });
      fo_snake.cur_left = 0;
      fo_snake.cur_top = 0;
      fo_snake.cur_height = MG_SNAKEWIDTH;
      fo_snake.cur_width = 0;
      fo_snake.direct = 'R';
    }
    else {
      fo_snake.cur_height += fo_snake.stepT1;
      fo_snake.cur_top -= fo_snake.stepT1;
      fo_snake.update({ marginTop: fo_snake.cur_top+'px', marginLeft: fo_snake.cur_left+'px',  width: fo_snake.cur_width+'px', height: fo_snake.cur_height+'px' });
    }
  }

  if(fo_snake.direct=='R') {
    if(fo_snake.cur_width + fo_snake.stepR >= mg_table.offsetWidth ) {
      fo_lTCell.update({ borderTop: MG_TABLEBORDER });
      fo_rCell.update({ borderTop: MG_TABLEBORDER });
      fo_snake.cur_width = MG_SNAKEWIDTH;
      fo_snake.cur_left = mg_table.offsetWidth - MG_SNAKEWIDTH;
      fo_snake.direct = 'B';
    }
    else {
      fo_snake.cur_width += fo_snake.stepR;
      fo_snake.update({ marginTop: fo_snake.cur_top+'px', marginLeft: fo_snake.cur_left+'px',  width: fo_snake.cur_width+'px', height: fo_snake.cur_height+'px' });
    }
  }


  if(fo_snake.direct=='B') {
    if(fo_snake.cur_height + fo_snake.stepB >= mg_table.offsetHeight ) {
      fo_rCell.update({ borderRight: MG_TABLEBORDER });
      fo_rShadow.update({ height: (mg_table.offsetHeight - MG_BORDER)+'px' });
      fo_bShadow.update({ marginTop: (MG_ABSOLUTETOP + mg_table.offsetHeight) + 'px', marginLeft: fo_bShadow.cur_left+'px' });
      fo_snake.cur_height = MG_SNAKEWIDTH;
			fo_bShadow.cur_left = mg_table.offsetWidth + MG_ABSOLUTELEFT;
      fo_snake.cur_top = mg_table.offsetHeight - MG_SNAKEWIDTH;
      fo_snake.direct = 'L1';
    }
    else {
      fo_snake.cur_height += fo_snake.stepB;
      fo_snake.update({ marginTop: fo_snake.cur_top+'px', marginLeft: fo_snake.cur_left+'px',  width: fo_snake.cur_width+'px', height: fo_snake.cur_height+'px' });
      fo_rShadow.update({ height: fo_snake.cur_height+'px' });
    }
  }


  if(fo_snake.direct=='L1') {
    if(fo_snake.cur_left - fo_snake.stepL1 <= mg_lTCell.offsetWidth ) {
      fo_rCell.update({ borderBottom: MG_TABLEBORDER });
      fo_rShadow.update({ height: (mg_table.offsetHeight - MG_BORDER)+'px' });
      fo_bShadow.update({ marginTop: (MG_ABSOLUTETOP + mg_table.offsetHeight) + 'px', marginLeft: (MG_ABSOLUTELEFT + MG_SHADOWOFFSET + mg_lTCell.offsetWidth)+'px', width: (mg_rCell.offsetWidth - MG_SHADOWOFFSET)+'px' });
      fo_snake.cur_left = mg_lTCell.offsetWidth - MG_SNAKEWIDTH;
      fo_snake.cur_top = mg_table.offsetHeight - MG_SNAKEWIDTH;
      fo_snake.cur_width = MG_SNAKEWIDTH;
      fo_snake.direct = 'T2';
    }
    else {
      fo_snake.cur_width += fo_snake.stepL1;
      fo_snake.cur_left -= fo_snake.stepL1;
      fo_bShadow.cur_width += fo_snake.stepL1;
      fo_bShadow.cur_left -= fo_snake.stepL1;
      fo_snake.update({ marginTop: fo_snake.cur_top+'px', marginLeft: fo_snake.cur_left+'px',  width: fo_snake.cur_width+'px', height: fo_snake.cur_height+'px' });
      fo_bShadow.update({ marginLeft: fo_bShadow.cur_left+'px', width: fo_bShadow.cur_width+'px' });
    }
  }


  if(fo_snake.direct=='T2') {
    if(fo_snake.cur_top - fo_snake.stepT2 <= mg_lTCell.offsetHeight ) {
      fo_lBCell.update({ borderRight: MG_TABLEBORDER });
      fo_snake.cur_left = mg_lTCell.offsetWidth - MG_SNAKEWIDTH;
      fo_snake.cur_top = mg_lTCell.offsetHeight;
      fo_snake.cur_height = MG_SNAKEWIDTH;
      fo_snake.direct = 'L2';
    }
    else {
      fo_snake.cur_height += fo_snake.stepT2;
      fo_snake.cur_top -= fo_snake.stepT2;
      fo_snake.update({ marginTop: fo_snake.cur_top+'px', marginLeft: fo_snake.cur_left+'px',  width: fo_snake.cur_width+'px', height: fo_snake.cur_height+'px' });
    }

  }


  if(fo_snake.direct=='L2') {
    if(fo_snake.cur_left - fo_snake.stepL2 <= 0 ) {
      fo_lTCell.update({ borderBottom: MG_TABLEBORDER });
      fo_bShadow.update({ marginTop: (MG_ABSOLUTETOP + mg_table.offsetHeight) + 'px' });
      fo_rShadow.update({ height: (mg_table.offsetHeight - MG_BORDER)+'px' });
      fo_snake.cur_left = 0;
      fo_snake.cur_width = MG_SNAKEWIDTH;
      fo_snake.cur_top = mg_lTCell.offsetHeight - MG_SNAKEWIDTH;
      fo_snake.direct = 'T3';
    }
    else {
      fo_snake.cur_width += fo_snake.stepL2;
      fo_snake.cur_left -= fo_snake.stepL2;
      fo_snake.update({ marginTop: fo_snake.cur_top+'px', marginLeft: fo_snake.cur_left+'px',  width: fo_snake.cur_width+'px', height: fo_snake.cur_height+'px' });
    }
  }


  if(fo_snake.direct=='T3') {
    if(fo_snake.cur_top - fo_snake.stepT3 <= fo_line1.cur_height ) {
      fo_snake.update({ marginTop: fo_snake.cur_top+'px', marginLeft: fo_snake.cur_left+'px',  width: fo_snake.cur_width+'px', height: fo_snake.cur_height+'px' });
      fo_lTCell.update({ borderLeft: MG_TABLEBORDER });
      fo_snake.cur_top = 0;
      fo_snake.cur_height = 0;
      fo_snake.cur_width = 0;
      rCmpl=true;
    }
    else {
      fo_snake.cur_height += fo_snake.stepT3;
      fo_snake.cur_top -= fo_snake.stepT3;
      fo_snake.update({ marginTop: fo_snake.cur_top+'px', marginLeft: fo_snake.cur_left+'px',  width: fo_snake.cur_width+'px', height: fo_snake.cur_height+'px' });
    }
  }

    
  if(player.frameNumber>300) { alert('!'); return false; }

  
	return true;

} catch(e) { mg_onError('mg_onPlay: '+e.message); return false; } }


function fp_setAddedWorksCount()
{
  if(fp_addedWorksCount>=0)
    return fp_addedWorksCount;

  fp_addedWorksCount=0;
  var row = document.getElementById('idTrW');
  var i,cells = row.cells;
  for(i=0; i<cells.length; i+=2)
    if(cells[i].getAttribute('pf_id'))
      fp_addedWorksCount++
}


function fp_changeWorkPos(_this, direct)
{
  var cell, n=0;
  while(n++<10 && (cell=_this.parentNode) && cell.tagName.toLowerCase()!='td');
  if(n>=10 || !cell)
    return;

  var row = cell.parentNode;
  var a = row.cells[cell.cellIndex + direct];
  var b = row.cells[cell.cellIndex - direct];
  if(!a || !b)
    return;

  var aHtml = a.innerHTML;
  var aID = a.id;
  var aPfID = a.getAttribute('pf_id');
  a.innerHTML = b.innerHTML;
  a.setAttribute('id', b.id);
  a.setAttribute('pf_id', b.getAttribute('pf_id'));
  b.innerHTML = aHtml;
  b.setAttribute('id', aID);
  b.setAttribute('pf_id', aPfID);
}


function fp_addWork(checkbox, id, pict, prev, name, user)
{
	// Тут вообще name не нужен. Убить !!!
 	// name = name.replace(/\"/g,'&quot;');
	var pfDiv;
	if((pfDiv=document.getElementById('idPfDiv'+id))) {
		fp_clearWork(pfDiv, id);
		return;
	}

  fp_setAddedWorksCount();

  var on_arrows = "<img src='/images/ico_right.gif' vspace='2' style='width:9px;height:9px' border='0' onclick='fp_changeWorkPos(this, 1)'/><br/><img src='/images/ico_left.gif' style='width:9px;height:9px' border='0' onclick='fp_changeWorkPos(this, 1)'/>";
  
  if(fp_addedWorksCount >= fp_maxWorks) {
    if(mg_debug)
      alert(fp_addedWorksCount);
    return;
  }

	if(!id)
    id=pict; 

  var prevCell, cell = document.getElementById('idTdW'+fp_addedWorksCount);

	if(checkbox) {
//    checkbox.disabled=true;
    checkbox.checked=true;
//  	document.getElementById('idAW'+id).style.display="none";
//  	document.getElementById('idSpanW'+id).style.display="inline";
	}
	else
	  id = pict;

  cell.setAttribute('pf_id', id);

	var href;
	if(checkbox)
		href = '/users/'+user+'/viewproj.php?prjid='+id;
	else
		href = ___WDCPREFIX+'/users/'+user+'/upload/'+pict;
  if(prev) {
    cell.innerHTML =
    '<div id="idPfDiv'+id+'" style="width:202px">'+
    '<a href="'+href+'" target="_blank" class="blue">'+
    '<div align="left"><img src="'+___WDCPREFIX+'/users/'+user+'/upload/' + prev + '" alt="" border="0"></div>'+
    '</a>'+
    '<div style="margin-top:6px; font-size:100%;">'+
    '<a href="javascript:void(0)" onclick="fp_clearWork(this.parentNode.parentNode, \''+id+'\');">'+
    '<img src="/images/ico_close.gif" alt="удалить" width="9" height="9" border="0" style="margin-right: 6px" />Удалить</a></div>'+
    '</div>';
		//alert(cell.innerHTML);
  }
  else {
    cell.innerHTML =
    '<div id="idPfDiv'+id+'" style="width:202px">'+
    '<div align="left">'+
    '<a href="'+href+'" target="_blank" class="blue">' + pict + '</a>'+
    '<img src="/images/ico_dowload.gif" alt="загрузить" width="14" height="18" border="0" style="vertical-align: middle;padding-left:4px;">'+
    '</div>'+
    '<div style="margin-top:6px; font-size:100%;">'+
    '<a href="javascript:void(0)" onclick="fp_clearWork(this.parentNode.parentNode, \''+id+'\');">'+
    '<img src="/images/ico_close.gif" alt="удалить" width="9" height="9" border="0" style="margin-right: 6px" />Удалить</a></div>'+
    '</div>';
  }


  if(cell.cellIndex > 0 && (prevCell = cell.parentNode.cells[cell.cellIndex-1]))
    prevCell.innerHTML = on_arrows;

  if(++fp_addedWorksCount >= fp_maxWorks) {
    document.getElementById('exceededMaxWork').style.display='block';
		var vl=document.getElementById('workList');
		if(vl) vl.style.display='none';
    document.getElementById('uploadBox').style.display='none';
    mg_Update();
  }

}

function fp_clearWork(pfDiv, id)
{
  fp_setAddedWorksCount();

  var off_arrows = "<img src='/images/ico_right0.gif' vspace='2' style='width:9px;height:9px' border='0'/><br/><img src='/images/ico_left0.gif' style='width:9px;height:9px' border='0'/>";
  var pic_blank = "<div style='height:200px;width:200px;border:1px solid #c0c0c0;background: #dfdfdf;'>&nbsp;</div><br/>";
  
  var pf = pfDiv;

  var blankCell,posCell, nextCell=null;
  var cell = pf.parentNode;
  var row = cell.parentNode;

  if(nextCell = row.cells[cell.cellIndex+1])
    row.deleteCell(nextCell.cellIndex);

  row.deleteCell(cell.cellIndex);

  var index = row.cells.length;
  if(nextCell) {
    posCell = row.insertCell(index++);
    posCell.style.textAlign = 'center';
  }

  blankCell = row.insertCell(index);
  blankCell.innerHTML = pic_blank;

  var row = blankCell.parentNode;

	if(id) {
  	var checkbox = document.getElementById('idCbW'+id);
  	if(checkbox) {
//  	  checkbox.disabled = false;
  	  checkbox.checked = false;
//  	  document.getElementById('idAW'+id).style.display="inline";
//  	  document.getElementById('idSpanW'+id).style.display="none";
  	}
	}

  if(fp_addedWorksCount == fp_maxWorks) {
    document.getElementById('exceededMaxWork').style.display='none';
		var vl=document.getElementById('workList');
		if(vl) vl.style.display='block';
    document.getElementById('uploadBox').style.display='block';
    mg_Update();
  }

  fp_addedWorksCount--;

  var i, j, cells = row.cells;
  for(i=j=0; i<cells.length; i+=2,j++) {
    cells[i].id = 'idTdW'+j;
    if(i && !cells[i].getAttribute('pf_id'))
      cells[i-1].innerHTML = off_arrows;
  }

}

function fp_getSelProfs()
{
  var row = document.getElementById('idTrW');
  var i,cells = row.cells;
  var pid, selProfs = '';
  for(i=0; i<cells.length; i+=2)
    if(pid = cells[i].getAttribute('pf_id'))
      selProfs += (!i?'':',') + pid;

  return selProfs;
}

function fp_changeProf(user_id, prof_id)
{
  xajax_FpChangeProf(user_id, prof_id, fp_getSelProfs());
}

function fp_saveInfo(ufp_id, mod)
{
	var uf;
	if(uf=document.getElementById('fupload'))
	  uf.blur();
  var title = document.getElementById('idFpTitle');
  var descr = document.getElementById('idFpDescr');
  var fullDescr = document.getElementById('idFpFullDescr');
  xajax_FpSaveInfo(ufp_id, mod, title.value, descr.value, fullDescr.value, fp_getSelProfs());

}


function mg_onOver(ufp_id, mod)
{
  if(!mg_lock && !mg_container && !mg_timer) {
		mg_ufpID = ufp_id;
 	  mg_onBeforePlay(ufp_id, mg_wait, mod);
	}
}

function mg_onClick(ufp_id, mod)
{
  if(!mg_lock && !mg_container && !mg_timer) {
		mg_ufpID = ufp_id;
 	  mg_onBeforePlay(ufp_id, mg_wait, mod);
	}
	else
		mg_Close(1);
}

function mg_onLeave()
{
  if(mg_timer) {
    window.clearTimeout(mg_timer);
  	mg_timer=null;
  }
}

function mg_onChangeClick(ufp_id, mod)
{
  if(!mg_lock) {
		var is_showed=1;
		mg_ufpID = ufp_id;
    if(mg_container) {
      mg_prevLeftCatherHtml  = document.getElementById('mg_leftContent').innerHTML;
      mg_prevRightCatherHtml = document.getElementById('mg_rightContent').innerHTML;
    }
    else {
			is_showed=0;
      mg_onBeforePlay(ufp_id, 0, mod, 1)
		}

    fp_addedWorksCount=-1;
	  mg_isEdit=1;
    xajax_FpShowInfo(ufp_id, mod, 1, is_showed);
  }
  return false;
}

function mg_onChangeCancel()
{
	var uf;
	if(uf=document.getElementById('fupload'))
	  uf.blur();

	mg_isEdit=0;

	if(!mg_prevLeftCatherHtml)
  	mg_Close(1);
	else {
  	document.getElementById('mg_leftContent').innerHTML = mg_prevLeftCatherHtml;
  	document.getElementById('mg_rightContent').innerHTML = mg_prevRightCatherHtml;
  	mg_Update();
	}
}
  
