// check all checkboxes
function checkall(obj, i_name) {
   c_name = obj.name;
   f = obj.form;   
   for (var i=0;i<f.elements.length;i++) {
      var e = f.elements[i];
      if ( (e.name != c_name) && e.name == i_name && (e.type=='checkbox') && (!e.disabled) ) {
        e.checked = f.check_all.checked;
      }
   }
}

//insert cursor at current position in textarea
function insertAtCursor(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
						+ myValue
						+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}


function showVideo(url, container, options){
  
  url = location.protocol +'//'+ location.host + url;
  player_url = location.protocol +'//'+ location.host + '/flvplay.swf';
  skin_url = location.protocol +'//'+ location.host + '/flvskin';
  width = options.width;
  height = options.height;
  $(container).innerHTML= '<center><object width="' + width + '" height="' + height + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"><param name="salign" value="lt"><param name="quality" value="high"><param name="scale" value="noscale"><param name="wmode" value="transparent"><param name="true" value="allowfullscreen"><param name="movie" value="'+player_url+'"><param name="FlashVars" value="&file=' +url+ '&skinName=' + skin_url + '&autoPlay=false&autoRewind=true"> <embed width="' + width + '" height="' + height + '" flashvars="&file=' +url+ '&autoPlay=false&allowfullscreen=true&autoRewind=true&skinName=' +skin_url+ '" quality="high" scale="noscale" salign="LT" allowfullscreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' +player_url+ '" wmode="transparent"></embed></object></center>';
  
}


function showSWF(url, container, options){
  
  url = location.protocol +'//'+ location.host + url;
  width = options.width;
  height = options.height;
  $(container).innerHTML= '<center><object width="' + width + '" height="' + height + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"><param name="salign" value="lt"><param name="quality" value="high"><param name="scale" value="noscale"><param name="wmode" value="transparent"><param name="movie" value="'+url+'"> <embed width="' + width + '" height="' + height + '" quality="high" scale="noscale" salign="LT" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' +url+ '" wmode="transparent"></embed></object></center>';
  
}

 
//asks if images and documents should be copied too
function askOnCopy(link, question1, question2){  
  if (confirm(question1)){ 
    var f = document.createElement('form');
    link.parentNode.appendChild(f);
    
    if(!confirm(question2)){
      var h = document.createElement('input');
      f.appendChild(h);
      h.type = "hidden";
      h.name = "only";
      h.value = "text";
    }
        
    f.method = 'POST';
    f.action = link.href;
    f.submit();
  };
  return false;  
}

// Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
//
// See scriptaculous.js for full license.

var Engine = {
  detect: function() {
    var UA = navigator.userAgent;
    this.isKHTML = /Konqueror|Safari|KHTML/.test(UA);
    this.isGecko = (/Gecko/.test(UA) && !this.isKHTML);
    this.isOpera = /Opera/.test(UA);
    this.isMSIE  = (/MSIE/.test(UA) && !this.isOpera);
    this.isMSIE7 = this.isMSIE && !(/MSIE 6\./.test(UA) && !this.isOpera);
    this.isMSIE6 = this.isMSIE && !this.isMSIE7;
  }
};
Engine.detect();

var Builder = {
  NODEMAP: {
    AREA: 'map',
    CAPTION: 'table',
    COL: 'table',
    COLGROUP: 'table',
    LEGEND: 'fieldset',
    OPTGROUP: 'select',
    OPTION: 'select',
    PARAM: 'object',
    TBODY: 'table',
    TD: 'table',
    TFOOT: 'table',
    TH: 'table',
    THEAD: 'table',
    TR: 'table'
  },
  // note: For Firefox < 1.5, OPTION and OPTGROUP tags are currently broken,
  //       due to a Firefox bug
  node: function(elementName) {
    elementName = elementName.toUpperCase();
    
    // try innerHTML approach
    var parentTag = this.NODEMAP[elementName] || 'div';
    var parentElement = document.createElement(parentTag);
    try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
      parentElement.innerHTML = "<" + elementName + "></" + elementName + ">";
    } catch(e) {}
    var element = parentElement.firstChild || null;
      
    // see if browser added wrapping tags
    if(element && (element.tagName != elementName))
      element = element.getElementsByTagName(elementName)[0];
    
    // fallback to createElement approach
    if(!element) element = document.createElement(elementName);
    
    // abort if nothing could be created
    if(!element) return;

    // attributes (or text)
    if(arguments[1])
      if(this._isStringOrNumber(arguments[1]) ||
        (arguments[1] instanceof Array)) {
          this._children(element, arguments[1]);
        } else {
          var attrs = this._attributes(arguments[1]);
          if(attrs.length) {
            try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
              parentElement.innerHTML = "<" +elementName + " " +
                attrs + "></" + elementName + ">";
            } catch(e) {}
            element = parentElement.firstChild || null;
            // workaround firefox 1.0.X bug
            if(!element) {
              element = document.createElement(elementName);
              for(attr in arguments[1]) 
                element[attr == 'class' ? 'className' : attr] = arguments[1][attr];
            }
            if(element.tagName != elementName)
              element = parentElement.getElementsByTagName(elementName)[0];
            }
        } 

    // text, or array of children
    if(arguments[2])
      this._children(element, arguments[2]);

     return element;
  },
  _text: function(text) {
     return document.createTextNode(text);
  },
  _attributes: function(attributes) {
    var attrs = [];
    for(attribute in attributes)
      attrs.push((attribute=='className' ? 'class' : attribute) +
          '="' + attributes[attribute].toString().escapeHTML() + '"');
    return attrs.join(" ");
  },
  _children: function(element, children) {
    if(typeof children=='object') { // array can hold nodes and text
      children.flatten().each( function(e) {
        if(typeof e=='object')
          element.appendChild(e);
        else
          if(Builder._isStringOrNumber(e))
            element.appendChild(Builder._text(e));
      });
    } else
      if(Builder._isStringOrNumber(children)) 
         element.appendChild(Builder._text(children));
  },
  _isStringOrNumber: function(param) {
    return(typeof param=='string' || typeof param=='number');
  }
};

Effect.BackgroundScroll = Class.create();
Object.extend(Object.extend(Effect.BackgroundScroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    if(!this.element.currentBackgroundPosition)
      this.element.currentBackgroundPosition = [0, 0];
    this.offsets = arguments[1];
    this.start(arguments[2] || {});
  },
  setup: function() {
    this.current = this.element.currentBackgroundPosition;
    this.delta = [
      this.offsets[0] - this.current[0],
      this.offsets[1] - this.current[1]];
  },
  update: function(position) {
    this.element.currentBackgroundPosition = [
      Math.round(this.current[0] + (this.delta[0]*position)),
      Math.round(this.current[1] + (this.delta[1]*position)) ];
    this.element.style.backgroundPosition = 
      this.element.currentBackgroundPosition[0] + 'px ' +
      this.element.currentBackgroundPosition[1] + 'px';
  }
});

Effect.ScrollTo = Class.create();
Object.extend(Object.extend(Effect.ScrollTo.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    this.start(arguments[1] || {});
  },
  setup: function() {
    Position.prepare();
    var offsets = Position.cumulativeOffset(this.element);
    if(this.options.offset) offsets[1] += this.options.offset;
    var max = window.innerHeight ? 
      window.height - window.innerHeight :
      document.body.scrollHeight - 
        (document.documentElement.clientHeight ? 
          document.documentElement.clientHeight : document.body.clientHeight);
    this.scrollStart = Position.deltaY;
    this.delta = (offsets[1] > max ? max : offsets[1]) - this.scrollStart;
  },
  update: function(position) {
    Position.prepare();
    var offset = this.scrollStart + (position*this.delta);
    if(Engine.isMSIE6) {
      document.body.scrollTop = offset;
    }
    else
      window.scrollTo(Position.deltaX, offset);
  }
});

var X_REF = -431, Y_REF = -207;

function ref(element, x, y, title, url, linktitle) {
  Element.setOpacity('reference-title',0);
  if(url) {
    $('reference-title').innerHTML=title+"<a href=\""+url+"\" target=\"_new\">"+linktitle+"</a>";
  } else {
    $('reference-title').innerHTML=title;
  }
  new Effect.Opacity('reference-title',{from:0,to:1});
  new Effect.BackgroundScroll('panel', [X_REF*x, Y_REF*y], { duration: 1.5 });
  Element.addClassName(element, 'visited');
}



function toggleVisibility(name){
	var a = $(name);
	if(a.style.display != "none"){
		a.style.display = "none";
	}else{
		a.style.display = "inline";
	}
	console.log(a);
}