//alert( 'my_scripts.js BEGIN' );

//var SiteRoot = 'http://me/simatika/';

var navstr = navigator.userAgent.toLowerCase();
var isMoz = 0;
var isIE = 0;

isIE = (navstr.indexOf("ie") > -1);
isChrome = (navstr.indexOf("chrome") > -1);
if (!isIE && !isChrome)
  isMoz = (navstr.indexOf("mozilla") > -1);

isOpera = (navstr.indexOf("opera") > -1);

// elements access util

function el( id )
{
  return document.getElementById(id);
}

function sw_d(dd)
{
  var d = el(dd);
  if ( d )
    d.style.display = (d.style.display == 'block' ? 'none' : 'block' );

  return false;
}

function clr( id )
{
  el( id ).innerHTML = '';
  return false;
}

function ib_sw( id, e )
{
  var o = el(id);
  if ( o )
  {
    if ( e )
    {
      e.className = (e.className == 'ib_collapse' ? 'ib_expand' : 'ib_collapse' );
    }

    var ss = o.style.display;
    var s = o.style.display = ( o.style.display == 'none' ? 'block' : 'none' );

    //alert( ss + ' -> ' + s );

    var url = SiteRoot + 'index.php?module=content&section=ib&id=' + id + '&style=' + s;

    //alert( url );

    //*
    JsHttpRequest.query(

      url,

      { 'q' : 0 },

      function(result, text) 
      {
        alert( text );
      }

    );
    //*/
  }


  return false;
}


// (re)load node in central column

function c_node( id )
{
  l = '';

  el('central_tree').innerHTML = '<div id="branch_' + l + '_' + id + '" class="sub_tree_visible"></div>';
  s = el( 'branch_' + l + '_' + id );
  expand( s, id, l );

  return false;
}

// switch 2 blocks
function sw( h, s )
{
  el( h ).style.display = 'none';
  el( s ).style.display = 'block';
}

function toggle_display( id )
{
  var d = el( id );
  if ( d )
  {
    d.style.display = ( d.style.display == 'none' ? 'block' : 'none' );
  }
  return false;
}

// expand or collapse node in left tree
// newly expanded node is appended by its subtree

function sw_node( id, l )
{
  //alert( 'sw_node(' ); // + id + ', ' + l + ')' );

  o = el( 'expand_' + l + '_' + id );  //alert( o );
  s = el( 'branch_' + l + '_' + id );  //alert( s );

  if ( o.className == 'expand' )
  {
    o.className = 'collapse';
    s.className = 'sub_tree_visible';

    //alert( 's.innerHTML = ' + s.innerHTML );

    if ( s.innerHTML == '' )
    {
      expand( s, id, l );
    }
  }
  else
  {
    o.className = 'expand';
    s.className = 'sub_tree_hidden';
  }

  return false;
}

// expand/load node block

function expand( s, id, l )
{
  //alert( 'expand( s, '+id+', '+l+' )' );

  s.innerHTML = '<img src="'+SiteRoot+'img/loader.gif">';

  var url = SiteRoot + 'index.php?module=content&section=main&topic=branch&view=rq&id=' + id + '&left=' + l;

  JsHttpRequest.query(

    url,

    { 'q' : 0 },

    function(result, text) 
    {
      if ( el( 'branch_' + l + '_' + id ) )
        el( 'branch_' + l + '_' + id ).innerHTML = text;
    }

  );

}

// load node images list (used in gallery for article)

function show_node_images( l, id, topic )
{
  var o = el( l );

  if ( o.innerHTML == '' )
  {
    o.innerHTML = '<img src="./img/loader.gif">';

    var url = SiteRoot + 'index.php?module=structure&topic='+topic+'&view=rq&id=' + id;
    //alert( url );

    JsHttpRequest.query(

      url,

      { 'q' : 0 },

      function(result, text) 
      {
        if ( el( l ) )
          el( l ).innerHTML = text;
        window.location = window.location + '#gallery';
      }

    );

  }

}

// doesn't work

function copyToClipboard(content) 
{
	if (window.clipboardData && window.clipboardData.setData) {
		window.clipboardData.setData("Text", content);
		return true;
	}
	else 
  {
		try { 
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
		} 
		catch (e) { 
			return false;
		}
		
		var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].getService(); 
		if (clipboard) { 
			clipboard = clipboard.QueryInterface(Components.interfaces.nsIClipboard); 
		} 
		
		var transferable = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(); 
		if (transferable) { 
			transferable = transferable.QueryInterface(Components.interfaces.nsITransferable); 
		}
		
		if (clipboard && transferable) {
			var textObj = new Object(); 
			var textObj = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); 
			if (textObj) { 
				textObj.data = content; 
				transferable.setTransferData("text/unicode", textObj, content.length*2); 
				var clipid=Components.interfaces.nsIClipboard; 
				clipboard.setData(transferable,null,clipid.kGlobalClipboard); 
				
				return true;
			} 
		}
		
		return false;
	}
}

//alert( 1 );

function getElementPosition(elemId)
{
    var elem = document.getElementById(elemId);
	
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;
	
    var l = 0;
    var t = 0;
	
    while (elem)
    {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return {"left":l, "top":t, "width": w, "height":h};
}

// ---------------------------

var k_prev = '';
var keywords_input = '';
var keywords_list_div = '';

function FindKeyword( keywords_i, keywords_list )
{
  keywords_input = keywords_i;
  keywords_list_div = keywords_list;

  var k_i = el( keywords_i );
  var k_l = el( keywords_list );

  k_l.innerHTML = '<img src="'+SiteRoot+'img/loader.gif">';

  var url = SiteRoot + 'index.php?module=content&section=find_keywords&view=rq';

  //alert( url );

  JsHttpRequest.query(

    url,

    { 'keywords_i' : k_i.value },

    function(result, text) 
    {
      k_l.innerHTML = text;

      //
      //alert( result );

      k_prev = result["k_prev"];  //alert( 'k_prev = ' + k_prev );
    }

  );


  return false;
}

function SetKeyword( keyw )
{
  //alert( keywords_input );
  //alert( k_prev );

  el( keywords_input ).value = (k_prev ? k_prev + ' ' : '' ) + keyw;
  el( keywords_list_div ).innerHTML = '';

  return false;
}

// ---------------------------

/*
function ShowAddFile()
{
  el('add_file_btn').style.display = 'none';
  el('add_file_frm').style.display = 'block';
}
function HideAddFile()
{
  el('add_file_btn').style.display = 'block';
  el('add_file_frm').style.display = 'none';
}
*/

function a_reload( op, key, cnf )
{
  if (cnf) {
    if (!confirm(cnf)) return;
  }

  document.attach.key.value = key;
  document.attach.op.value = op;
  document.attach.submit();

}


var cur_i = 0;

function show_i( p1, p2, id )
{
  //alert( 'show_i( '+p1+', '+p2+', '+id+' )' );

  if ( cur_i )
  {
    cur_i.style.display='none';
  }

  //alert( p1 + id );

  var hlp = el(p1+id); //alert( hlp );

  if (hlp) 
  {
    cur_i = hlp;

    //alert( p2 + id );

    var tip_c = getElementPosition( p2 + id); //alert( tip_c );

    if ( isIE )
    {
      hlp.style.top = tip_c.top + 30;
      hlp.style.left = tip_c.left + 0;
      hlp.style.display='block';
    }
    else
    {
      hlp.setAttribute( "style", 
                        "top:" + (tip_c.top + 30) + "px; " +
                        "left:" + (tip_c.left + 0) + "px; " +
                        "display:block"
                      );
    }
  }
}

function hide_i( prefix, id )
{
  var hlp = el(prefix+id);
  if (hlp) hlp.style.display='none';
}

var tstat = 0;
var hide_delay = 10;

function HideMenu( prefix, id )
{
  timer1 = setTimeout("hide_i( '"+prefix+"', "+id+" )", hide_delay );
  tstat = 1;
  return 1;
}

function CancelHide()
{
 if (tstat==1) 
 {
   clearTimeout( timer1 );
   tstat = 0;
 }

 return 1;
}


//alert( 'my_scripts.js END' );



