 var ScrollWhere;

 var isDOM=document.getElementById;
 var isIE=document.all && document.all.item;
 var isMZ=isDOM && (navigator.appName=="Netscape");
 var isO=window.opera && isDOM;

 // SGN(X) = -1, 0, 1
 function sign(x)
 { if (x > 0) return 1; if (x < 0) return -1; return 0; }

function travelA( Aname, quick, noplus )
 {
//   alert('travelA ' + Aname);
   if (isMZ) 
   { 
     var year = navigator.userAgent.substr(navigator.userAgent.indexOf("Gecko/")+6,4);
     if (year=="2002" || year=="2001") isMZ=false;
   } 
   var value=0;
   z = Aname;
   var x=0;
   var y=0;
   do 
   {
     x += parseInt(isNaN(parseInt(z.offsetLeft))?0:z.offsetLeft);
     y += parseInt(isNaN(parseInt(z.offsetTop))?0:z.offsetTop);
   } 
   while (z=z.offsetParent);
   travelTo( x,  y-value, quick );
   return true;
 }

 function travelTo(x, y, quick )
 {
   var d = document.body;
   if ((document.compatMode) && (document.compatMode == "CSS1Compat")) d = document.documentElement;
   do
     {
       ox = d.scrollLeft;
       oy = d.scrollTop;
       dx = (x - ox) / (quick?1:50);
       dx = sign(dx) * Math.ceil(Math.abs(dx));
       dy = (y - oy) / (quick?1:50);
       dy = sign(dy) * Math.ceil(Math.abs(dy));
       window.scrollBy(dx, dy);
       cx = d.scrollLeft;
       cy = d.scrollTop;
     }
   while (!quick && 
          (( (ox-cx) != 0 ) || ( (oy-cy) != 0 ))
         );
 }

function DL_GetElementLeft(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function DL_GetElementTop(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function existincookie(what) {

   var s = document.cookie;
   if (s) { return s.indexOf(what); } else { return -1; }

}

function addtocookie(what) {

     if (existincookie(what)==-1) {

       var s = document.cookie;
       s += " "+what;
       document.cookie = s;
//       alert(document.cookie)
 
     }
}

function deletefromcookie(what) {
     pressPos = existincookie(what);
     if (pressPos != -1) {

        var s = document.cookie;
        var ss = s.substring(0, pressPos)+s.substring(pressPos+what.length+1, s.length);

        document.cookie = ss;
//        alert(document.cookie)
 
     }
}



// now the real code starts

function closeAll() {

var x = document.getElementsByTagName('span');
for (var i=0;i<x.length;i++)
{
 if (x[i].getAttribute('collapsestate')) 
 {
     x[i].style['visibility'] = 'hidden';
     x[i].style['display'] = 'none';
	 x[i].state = 'hidden';
	 
	 deletefromcookie(x[i].id);
 }  
}

var x = document.getElementsByTagName('a');
for (var i=0;i<x.length;i++)
{
 if (x[i].getAttribute('connected')) 
 {
   x[i].onclick = mousedown2;
   x[i].style['background'] = x[i].parentNode.id == 'pageo' ? 'url(/i/down.gif) no-repeat 0px 0px' : 'url(/i/down.gif) no-repeat 0px 3px';
 }  
}

}

// -------------------------------------------------------------------------- 

function openAll() {

var SaveControl = document.cookie;
document.cookie = "";

var x = document.getElementsByTagName('span');
for (var i=0;i<x.length;i++)
{
 if (x[i].getAttribute('collapsestate')) 
 {
     x[i].style['visibility'] = 'visible';
     x[i].style['display'] = 'inline';
	 x[i].state = 'visible';
	 
	 //document.cookie += " "+x[i].id;
	 addtocookie(x[i].id);
	 //alert(document.cookie);
	 
 }  
}

var x = document.getElementsByTagName('a');
for (var i=0;i<x.length;i++)
{
 if (x[i].getAttribute('connected')) 
 {
   x[i].onclick = mousedown2;
//   x[i].style['background'] = 'url(/i/up.gif) no-repeat 0px 3px';
   x[i].style['background'] = x[i].parentNode.id == 'pageo' ? 'url(/i/up.gif) no-repeat 0px 0px' : 'url(/i/up.gif) no-repeat 0px 3px';

 }  
}

}

// -------------------------------------------------------------------------- 

// setStyleById: given an element id, style property and 
// value, apply the style.
// args:
//  i - element id
//  p - property
//  v - value
//
function setStyleById(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}

// -------------------------------------------------------------------------- 

function mousedown2() {

  var SaveControl = document.cookie;

  var PressId = this.getAttribute('connected');
  var pressControl = document.getElementById(PressId);
  var state = pressControl.state;
  state = (state=='hidden') || (!state) ? 'visible' : 'hidden';

  if (state=='hidden') {

     pressControl.style['visibility'] = 'hidden';
     pressControl.style['display'] = 'none';
//     this.style['background'] = 'url(/i/down.gif) no-repeat 0px 3px';
     this.style['background'] = this.parentNode.id == 'pageo' ? 'url(/i/down.gif) no-repeat 0px 0px' : 'url(/i/down.gif) no-repeat 0px 3px';
	 
/*	 var s = document.cookie;

     var pressPos = s.indexOf(PressId);
     if (pressPos != -1) {

        var ss = s.substring(0, pressPos)+s.substring(pressPos+PressId.length+1, s.length);

        document.cookie = ss;
 
     } */
	 deletefromcookie(PressId);


  } else {

     pressControl.style['visibility'] = 'visible';
     pressControl.style['display'] = 'inline';
//     this.style['background'] = 'url(/i/up.gif) no-repeat 0px 3px';
     this.style['background'] = this.parentNode.id == 'pageo' ? 'url(/i/up.gif) no-repeat 0px 0px' : 'url(/i/up.gif) no-repeat 0px 3px';

/*	 var s = document.cookie;
     if (s.indexOf(PressId)==-1) {

        document.cookie += " "+PressId;
 
     } */
	 addtocookie(PressId);


  }  
  pressControl.state = state; 

  return false;

}

// --------------------------------------------------------------------------

function mouseDownLocalLink() {
  var hr = this.getAttribute('href');
  var anchorid = hr.split("#")[1];
  var scrollWhere = document.getElementById(anchorid);

  window.location.replace( hr );

//  window.location.reload( false );
//  window.location.reload( true );

//  alert('local click for '+ anchorid + ',element : '+ scrollWhere);
  openNode(anchorid);
  travelA(scrollWhere, true);

  
  return false;

}

// -------------------------------------------------------------------------- 

function isOne(x, str){
var counter=0;
for (var i=0;i<x.length;i++)
{	
	if (x[i].getAttribute(str)) 
	{
		counter++
	}
}
return counter==1;
}

function openNode(node)
{
	var x = document.getElementById(node);
	if (x) {
		
		if (x.getAttribute('collapsestate')) {
			
			 x.style['visibility'] = 'visible';
			 x.style['display'] = 'inline';
			 x.state = 'visible';
			
		} //else {
			
			while (x = x.parentNode) {
		
				if ((x.nodeName=='SPAN') && (x.getAttribute('collapsestate'))) {
	
					 x.style['visibility'] = 'visible';
					 x.style['display'] = 'inline';
					 x.state = 'visible';
					 
						var y = document.getElementsByTagName('a');
						for (var i=0;i<y.length;i++) {
						 if (y[i].getAttribute('connected')) {
							 if (y[i].getAttribute('connected')==x.id ) {
								  // y[i].style['background'] = 'url(/i/up.gif) no-repeat 0px 3px';
  						           y[i].style['background'] = y[i].parentNode.id == 'pageo' ? 'url(/i/up.gif) no-repeat 0px 0px' : 'url(/i/up.gif) no-repeat 0px 3px';
								  
							}
						 }  
						}
					
				}		
				
			}
			
	//	}

        //deletefromcookie(x.id);

	}

// we need to clear all the cookies on this page

var x = document.getElementsByTagName('span');
	for (var i=0;i<x.length;i++)
		 if (x[i].getAttribute('collapsestate')) 
 			  deletefromcookie(x[i].id);

}

 function doOnLoad() {

     //alert('On load!');
     var savedvalue = document.cookie;
     var scrollWhere;
     var urlQueryId;

     // connecting the links to their hidden brothers

     var x = document.getElementsByTagName('a');
     var counter = 0;
     for (var i = 0; i < x.length; i++)
     {
         if (x[i].getAttribute('connected'))
         {
             counter++
         }
         if (x[i].getAttribute('connected'))
         {
             x[i].onclick = mousedown2;
             if (counter == 1 && isOne(x, 'connected'))
             {
                 x[i].state = 'visible';
                 x[i].style['background'] = x[i].parentNode.id == 'pageo' ? 'url(/i/up.gif) no-repeat 0px 0px' : 'url(/i/up.gif) no-repeat 0px 3px';
             }
         }
     }

     // hide all the sections

     var x = document.getElementsByTagName('span');
     var counter2 = 0;
     for (var i = 0; i < x.length; i++)
     {
         if (x[i].getAttribute('collapsestate'))
         {

             counter2++;
         }
         if (x[i].getAttribute('collapsestate') && counter2 == 1 && isOne(x, 'collapsestate'))
         {
             x[i].state = 'visible';
             continue;
         }

         if (x[i].getAttribute('collapsestate'))
         {
             //alert(isOne(x,'collapsestate'));
             //   x[i].className = 'hidden';
             x[i].style['visibility'] = 'hidden';
             x[i].style['display'] = 'none';
         }
         /* if(i=0 && x[i].getAttribute('collapsestate'))
         {
              x[i].style['display'] = 'inline';
        //     this.style['background'] = 'url(/i/up.gif) no-repeat 0px 3px';
         }*/
     }

     // check the #id thing in href
     // and reopen the corresponding section

     urlquery = location.href.split("#");

     //if (urlquery[1])
     //{
     //	var x = document.getElementsByTagName('span');
     //	for (var i=0;i<x.length;i++)
     //	{
     //		 if (x[i].getAttribute('collapsestate'))
     //		 {
     //			 if (x[i].id==urlquery[1])
     //			 {
     //				 x[i].style['visibility'] = 'visible';
     //				 x[i].style['display'] = 'inline';
     //				 x[i].state = 'visible';
     //
     //			 }
     //			 deletefromcookie(x[i].id);
     //
     //		 }
     //	}
     //
     //}

     // well, now we need to expand all it's parents as well?

     if (urlquery[1])
     {
//         alert(urlquery[1]);
         openNode(urlquery[1]);
         urlQueryId = urlquery[1];
     }


     // open all the closed sections that's stored in cookie

     var savedvalue = document.cookie;

     if (!(urlquery[1]) && (savedvalue.length > 0))
     {
         var x = document.getElementsByTagName('span');
         for (var i = 0; i < x.length; i++)
         {
             if (x[i].getAttribute('collapsestate'))
             {
                 if (savedvalue.indexOf(x[i].id) != -1)
                 {
                     x[i].style['visibility'] = 'visible';
                     x[i].style['display'] = 'inline';
                     x[i].state = 'visible';
                 }
             }
         }
     }

     // fixing the CSS style for those A's we just opened...

     if ((urlquery[1]) || (savedvalue.length > 0)) {
         var x = document.getElementsByTagName('a');
         for (var i = 0; i < x.length; i++)
         {
             if (x[i].getAttribute('connected'))
             {
                 if ((x[i].getAttribute('connected') == urlquery[1] ) || ( savedvalue.indexOf(x[i].getAttribute('connected')) != -1 )) {
                     //x[i].style['background'] = 'url(/i/up.gif) no-repeat 0px 3px';
                     x[i].style['background'] = x[i].parentNode.id == 'pageo' ? 'url(/i/up.gif) no-repeat 0px 0px' : 'url(/i/up.gif) no-repeat 0px 3px';

                 }

                 if (x[i].getAttribute('connected') == urlquery[1]) {
                     scrollWhere = x[i];
//                     setTimeout('travelA(scrollWhere, false);', 100);
                 }


             }
         }

     }

     // new functionality here
     // we need to check all the HREFs in the document on locality
     // and assign the special onclick handlers for them
     // so that local clicks will work once again

     var a = document.getElementsByTagName("a");
     for (var i = 0; i < a.length; i++)
         if (a[i].href.split("#")[0] == window.location.href.split("#")[0]) {


             if ((a[i].getAttribute('connected')) || (a[i].getAttribute('noreload'))) {
             }
             else {
                 a[i].onclick = mouseDownLocalLink;
             }

         }

         // eof
     if (scrollWhere) {
       travelA(scrollWhere, true);
     } else {
       if (urlQueryId)
         scrollWhere = document.getElementById(urlQueryId);
//       alert(scrollWhere);
       if (scrollWhere)
         travelA(scrollWhere, true);
     }


 }

 // --------------------------------------------------------------------------

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// -------------------------------------------------------------------------- 

addLoadEvent(doOnLoad);
/* addLoadEvent(function() { }); */

