
var readUrl = "http://supernovum.ru/forum/read.php?";
var readxUrl = "http://supernovum.ru/forum/readx.php?";


var readMsgUrl = "read.php?";
var readxMsgUrl = "./readx.php?";

var newtag = "новое";
/*
var close_ico = '[x]',
    max_ico = '[+]',
    min_ico = '[-]';
*/
var close_ico = '<img src="./i/close_6a.gif" alt="[x]">',
    max_ico = '<img src="./i/plus_6a.gif" alt="[+]">',
    min_ico = '<img src="./i/minus_6a.gif" alt="[-]">';


function preview( urlParams, node )
{
  var elem = node.nextSibling;
  if ( elem )
    elem = elem.nextSibling;

  if ((elem )&&( elem.nodeName == 'SPAN'))
  {
    elem = elem.firstChild;
    var txt = (elem != null && elem.nodeValue != null) ? elem.nodeValue : "";
    if (txt == newtag) {
      elem.parentNode.removeChild(elem);
    }
  }

  document.getElementById( "goto_link" ).href = readMsgUrl + urlParams;
  document.getElementById( "preview_msg" ).src = readxMsgUrl + urlParams;


  ShowPreview(1);

  return false;
}

function getClientCenterX()
{   
    return parseInt(getClientWidth()/2)+getBodyScrollLeft();   
}   
  
function getClientCenterY()
{   
    return parseInt(getClientHeight()/2)+getBodyScrollTop();   
}  

var expanded = false;

function SetSizeButton( label )
{
  var btn = document.getElementById( "resize_lnk" );

  if (0) //isMoz) 
  {
    if ( btn.childNodes != null ) {
      var node = btn.childNodes[0];
      node.nodeValue = label;
    }

  } else { //  if (isIE)
    //btn.innerText = label;
    btn.innerHTML = label;
  }
}

function TogglePreviewSize()
{
  expanded = !expanded;

  if ( isChrome || isOpera )
  {
    var win_w = innerWidth;
    var win_h = innerHeight;
    var win_t = pageYOffset;

    //alert( "win_w = " + win_w + " win_h = " + win_h + " win_t = " + win_t );
  }
  else
  if (isMoz) {
    var win_w = document.width; //innerWidth;
    var win_h = innerHeight;
    var win_t = pageYOffset;
  } else {
    var body = document.body;
    var win_w = body.clientWidth;
    var win_h = body.clientHeight;
    var win_t = body.scrollTop;

    if ( document.compatMode )
    {
      win_t = document.documentElement.scrollTop;
    }
  }

  //alert( "win_w = " + win_w + " win_h = " + win_h + " win_t = " + win_t );

  var panel = document.getElementById( "preview_panel" );

  var msg_h = 0;

  if (expanded)
  {

    var w = win_w;
    var h = win_h;
    var left = 0 + "px";
    var top = win_t + "px";

    SetSizeButton( min_ico );

    msg_h = win_h - 30;

  } else {

    var w = parseInt(win_w * 0.9);        
    var h = 450; //parseInt(win_h * 0.9);        
    var left = parseInt((win_w - w)/2) + "px";
    var top = parseInt(win_t + 10) + "px"; 
    //var old = parseInt(win_t + ((win_h - h)/2)) + "px";  alert( top + ", " + old );


    SetSizeButton( max_ico );

    msg_h = h - 30;

  }

  if (isMoz || isChrome || isOpera) 
  {
    //alert( "Moz" );

    panel.setAttribute( "style", 
                        "position:absolute; z-order:2; display:block; "+
                        "width: " + w + "px; height: " + h + 
                        "px; left: " + parseInt((win_w - w)/2) + "px; " +
                        "top: " + parseInt(win_t + 10) + "px;"
                      );

  } else {

    panel.style.left = left;
    panel.style.top = top;
    panel.style.width = w;
    panel.style.height = h;

  }

  //alert( 'panel.style.width = ' + panel.style.width );


  document.getElementById( "preview_msg" ).height = msg_h + "px";

  return false;
}

function ShowPreview( mode )
{
  document.getElementById( "preview_panel" ).style.display = (mode)? 'block' : 'none';

  if (mode) {
    expanded = true;
    TogglePreviewSize();
  }

  return false;
}

function Setup_previews()
{
  SearchLinks( document.getElementById("thread_tree") );

  /*
  var p = '<div id="preview_panel" class="PhorumStdBlock" style="position:absolute; z-order:2; display:none;">'+
                '<table width=100% border=0>'+
                '<tr><td><table width=100% cellspacing=0 cellpadding=0><tr><td align=center><a href="" id="goto_link">Перейти к этому сообщению..</a></td>'+
                '<td align=right width=10px height=10px nowrap><nobr>'+
                  '<a href="" id="resize_lnk" title="Размер" onclick="return TogglePreviewSize();">[+]</a>'+
                  '<a href="" id="close_lnk" title="Закрыть" onclick="return ShowPreview(0);">[x]</a>'+
                  '</nobr></td></tr></table></td></tr>'+
                '<tr><td colspan=2><iframe width=100% height=400px id="preview_msg" src=""></iframe></td></tr>'+
                '</table></div>';
  */

  btn_align = ( isChrome ? 'left' : 'right' );


  var p = '<style>#icos a{margin: 0 0 0 5px;}</style>'+
          '<div id="preview_panel" class="PhorumStdBlock" style="position:absolute; z-order:2; display:none;">'+
                '<div style="padding: 5px;">'+
                  '<div style="height:22px;">'+

                    '<div id=icos style="float: ' + btn_align + '; width:45px; height:10px"><nobr>'+
                      '<a href="" id="resize_lnk" title="Размер" onclick="return TogglePreviewSize();">'+max_ico+'</a>'+
                      '<a href="" id="close_lnk" title="Закрыть" onclick="return ShowPreview(0);">'+close_ico+'</a>'+
                    '</nobr></div>'+

                    '<div style="text-align:center;"><a href="" id="goto_link">Перейти к этому сообщению..</a></div>'+

                  '</div>'+

                  '<iframe width=100% height=400px id="preview_msg" src=""></iframe>'+
                '</div></div>';

  document.write( p );

  var b = document.getElementById( 'preview_panel' );
  new Draggable(b, { handle : b });

}

function SearchLinks(node)
{
  if (node.tagName == 'A')
  {
    var url = node.getAttribute( "HREF" );

    if ( (pos = url.indexOf(readMsgUrl)) > -1)
    {
      var urlParams = url.substring( pos + readMsgUrl.length );  //alert( 'pos=' + pos + ': ' + urlParams );

      //var onclick_att = "return preview('" + urlParams + "',this);";
      var onclick_att = "preview('" + urlParams + "',this);return false;";

      var ico = document.createElement("img");

      var attr = document.createAttribute("src");
      attr.value = './images/snapshot.gif';
      ico.setAttributeNode(attr);

      var attr = document.createAttribute("ALT");
      attr.value = '';
      ico.setAttributeNode(attr);

      var attr = document.createAttribute("title");
      attr.value = 'смотреть'; // onclick_att;
      ico.setAttributeNode(attr);

      //

      if ( isIE || isChrome )
      {
        var elem = ico;

        elem.onclick = function() {
          return preview( urlParams,this );
        }

        elem.onmouseover = function() {
          this.style.cursor='hand';
        }
      } 
      else
      {
        var elem = document.createElement("A");

        var attr = document.createAttribute("HREF");
        attr.value = "javascript:void(0);";
        elem.setAttributeNode(attr);

        var attr = document.createAttribute("onclick");
        attr.value = onclick_att;
        elem.setAttributeNode(attr);

        elem.appendChild(ico);
      }

      node.parentNode.insertBefore(elem, node.nextSibling );

      return true;

    }
  }

  var skip_next = false;
  if ( node.childNodes != null )
    for (var i = 0; i < node.childNodes.length; i++)
    {
      skip_next = (skip_next) ? false : SearchLinks( node.childNodes[i] );
    }


  return false;
}


Setup_previews();


