function goTo(sel) {
  p= sel.options[sel.selectedIndex].value;
  if (p==-1) return;
  p= p.split('@');
  window.location= 'page.php?obj='+p[0]+'&path='+p[1];
}

function openC(url,w,h,features) {
  var x= (screen.width-w)/2, y= (screen.height-h)/2;
  if (features==null || features=='') // NS optimization
    features= 'screenX='+x+',screenY='+y+',width='+w+',height='+h;
  else
    features= features +',screenX='+x+',screenY='+y+',width='+w+',height='+h;
  w= window.open(url,'',features);
  w.moveTo(x,y);
  // return w;
}

function showPict(objID,w,h,center) {
  //  var pw,loc ='../../../../public/picture.php?obj='+objID;
  var pw,loc ='../proxy.php?target=picture&obj='+objID;

  if (w==null) w= 640;
  if (h==null) h= 480;
  if (center==null) { //do not center
	  pw= window.open(loc,'pic','scrollbars,resizable,width='+w+',height='+h+',screenX=100,screenY=100');
	  pw.moveTo(100,100);
	} else
    openC(loc,w,h,'scrollbars,resizable');
}

function reloadHead() {
  if (window.parent.head) {
    window.parent.head.location.reload();
  }
}

function showEditorFor(oID) {
  editor= window.open('','publisher');
  if (editor.JoustFrameset) {
    url= window.system_root+'intern/tshow.php?obj='+oID+'&ignoreP=no';
    //alert('editor active; loading '+url);
    editor.work.location.href= url;
  } else {
    url= window.system_root+'intern/index.php?page=%3D'+oID;
    //alert('editor not active; loading '+url);
    editor.location.href= url;
  }
  editor.focus();
}

function defOnError(msg, url, lno) {
  //return true;  // *** for now ***

	if (msg.search('Window.status')!=-1)
    return false;
  else {
		alert(
      'A JavaScript error has occurred on this page!  Please note down the '
     +'following information and pass it on to the Webmaster.\n\n'
     +'Error: ' +msg +'\nPage: ' +url +'\nLine: ' +lno +'\nBrowser: ' +navigator.userAgent
    );
		return true;
	}
}

function myMessage(aMessage) {
  if (Components) {
    var consoleService= Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
    consoleService.logStringMessage("My extension: " + aMessage);
  } else
    alert(aMessage);
}
/*
function browserInfo() {
	this.code = 'unknown';
	this.version = 0;
	this.platform = 'Win';
	var ua = navigator.userAgent;
	var i = ua.indexOf('WebTV');
	if (i >= 0) {
		this.code = 'WebTV';
		i += 6;
	} else {
		i = ua.indexOf('Opera');
		if (i >= 0) {
			this.code = 'OP';
			i = ua.indexOf(') ') + 2;
		} else {
			i = ua.indexOf('MSIE');
			if (i >= 0) {
				this.code = 'MSIE';
				i += 5;
			} else {
				i = ua.indexOf('Mozilla/');
				if (i >= 0) {
					this.code = 'NS';
					i += 8;
				}
			}
		}
	}
	this.version = parseFloat(ua.substring(i, i+4));
	if (ua.indexOf('Mac') >= 0) {this.platform = 'Mac';}
	if (ua.indexOf('OS/2') >= 0) {this.platform = 'OS/2';}
	if (ua.indexOf('X11') >= 0) {this.platform = 'UNIX';}
	var v = this.version;
	var p = this.platform;
	var NS = (this.code == 'NS');
	var IE = (this.code == 'MSIE');
	var WTV = (this.code == 'WebTV');
	var OP = (this.code == 'OP');
	var OP32up = (OP && (v >= 3.2));
	var OP5up = (OP && (v >= 5));
	var IE4up = (IE && (v >= 4));
	var NS3up = (NS && (v >= 3));
	var NS6up = (NS && (v >= 5));
	this.canCache = NS3up || IE4up || OP32up || WTV;
	this.canOnMouseOut = this.canCache;
	this.canOnError = NS3up || IE4up || OP32up;
	this.canJSVoid = !((NS && !NS3up) || (IE && !IE4up) || (OP && (v < 3.5)));
	this.lineByLine = (v < 4);
	this.mustMoveAfterLoad = NS3up || (IE4up && (p != 'Mac')) || WTV;
	if (NS6up == true) {
		this.reloadMethod = 'reload';
	} else {
		if (NS3up || IE4up || WTV || OP5up) {
			this.reloadMethod = 'replace';
		} else {
			this.reloadMethod = (NS && (v == 2.01) && (p != 'Win')) ? 'timeout' : 'href';
		}
	}
	this.needsMenuSearch = (OP && !OP5up);
	this.canFloat = NS || (IE && !((p == 'Mac') && (v >= 4) && (v < 5)));
	this.hasDHTML = ((NS || IE) && (v >= 4)) && !(IE && (p == 'Mac') && (v < 4.5));
	this.slowDHTML = IE4up || NS6up;
	this.hasW3CDOM = (document.getElementById) ? true : false;
	this.needLM = (!this.hasW3CDOM && NS) || (IE && (p == 'Mac') && (v >= 4.5));
	this.DHTMLRange = IE ? '.all' : '';
	this.DHTMLStyleObj = IE ? '.style' : '';
	this.DHTMLDivHeight = IE ? '.offsetHeight' : '.clip.height';
}

*
var theBrowser = new browserInfo;
if (theBrowser.canOnError) {
  window.onerror= defOnError;
  alert('errh set.');
} else
  alert('-bcoe');
*/

function blinkField(field,color,n) {
  var fld, interval =0.5;
  
  if (n == undefined) n= 1;
  
  if (n > 0)
    field.style.backgroundColor= color;
  else
    field.style.backgroundColor= 'white';

  if (n > 0)
    n= -n;
  else  
    n= Math.abs(n)-1;
    
  if (Math.abs(n) > 0) {
    fld= 'document.'+field.form.name+'.'+field.name;
    window.setTimeout('blinkField('+fld+',\''+color+'\','+n+')',interval*1000);
  } else
    field.select();
}

/* Smooth scrolling
   Changes links that link to other parts of this page to scroll
   smoothly to those links rather than jump to them directly, which
   can be a little disorienting.

   sil, http://www.kryogenix.org/

   v1.0 2003-11-11
   v1.1 2005-06-16 wrap it up in an object
   
   usage: put
    <script src="smoothscroll.js" type="text/javascript"></script>
   into HEAD area.
*/

var ss = {
  fixAllLinks: function() {
    // Get a list of all links in the page
    var allLinks = document.getElementsByTagName('a');
    // Walk through the list
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if ((lnk.href && lnk.href.indexOf('#') != -1) &&
          ( (lnk.pathname == location.pathname) ||
            ('/'+lnk.pathname == location.pathname) ) &&
          (lnk.search == location.search)) {
        // If the link is internal to the page (begins in #)
        // then attach the smoothScroll function as an onclick
        // event handler
        ss.addEvent(lnk,'click',ss.smoothScroll);
      }
    }
  },

  smoothScroll: function(e) {
    // This is an event handler; get the clicked on element,
    // in a cross-browser fashion
    if (window.event) {
      target = window.event.srcElement;
    } else if (e) {
      target = e.target;
    } else return;

    // Make sure that the target is an element, not a text node
    // within an element
    if (target.nodeType == 3) {
      target = target.parentNode;
    }

    // Paranoia; check this is an A tag
    if (target.nodeName.toLowerCase() != 'a') return;

    // Find the <a name> tag corresponding to this href
    // First strip off the hash (first character)
    anchor = target.hash.substr(1);
    // Now loop all A tags until we find one with that name
    var allLinks = document.getElementsByTagName('a');
    var destinationLink = null;
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if (lnk.name && (lnk.name == anchor)) {
        destinationLink = lnk;
        break;
      }
    }

    // If we didn't find a destination, give up and let the browser do
    // its thing
    if (!destinationLink) return true;

    // Find the destination's position
    var destx = destinationLink.offsetLeft;
    var desty = destinationLink.offsetTop;
    var thisNode = destinationLink;
    while (thisNode.offsetParent &&
          (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }

    // Stop any current scrolling
    clearInterval(ss.INTERVAL);

    cypos = ss.getCurrentYPos();

    ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
    ss.INTERVAL = setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);

    // And stop the actual click happening
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
    if (e && e.preventDefault && e.stopPropagation) {
      e.preventDefault();
      e.stopPropagation();
    }
  },

  scrollWindow: function(scramount,dest,anchor) {
    wascypos = ss.getCurrentYPos();
    isAbove = (wascypos < dest);
    window.scrollTo(0,wascypos + scramount);
    iscypos = ss.getCurrentYPos();
    isAboveNow = (iscypos < dest);
    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
      // if we've just scrolled past the destination, or
      // we haven't moved from the last scroll (i.e., we're at the
      // bottom of the page) then scroll exactly to the link
      window.scrollTo(0,dest);
      // cancel the repeating timer
      clearInterval(ss.INTERVAL);
      // and jump to the link directly so the URL's right
      location.hash = anchor;
    }
  },

  getCurrentYPos: function() {
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
  },

  addEvent: function(elm, evType, fn, useCapture) {
    // addEvent and removeEvent
    // cross-browser event handling for IE5+,  NS6 and Mozilla
    // By Scott Andrew
    if (elm.addEventListener){
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent){
      var r = elm.attachEvent("on"+evType, fn);
      return r;
    } else {
      alert("Handler could not be removed");
    }
  }
}

function $() {  // copied from lib/prototype.js
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

function _show(object,flag) {
  if ($(object)==undefined) return;
  
  if (flag)
    $(object).style.display= 'inline';
  else
    $(object).style.display= 'none';
}

ss.STEPS = 25;
ss.addEvent(window,"load",ss.fixAllLinks);
