/**
 * Some DHTML JavaScript to show a contextual help div
 * using the wz_tooltip library from
 * 
 * @version	$Id: functions_submission.js 4445 2008-09-17 18:32:10Z florian $
 */

function toggleElement(id) 
{
	// store the current display setting of the element
	var display = document.getElementById(id).style.display;
	
	// check wether the element should be displayed or hidden
	if (display == 'none')
	{
		document.getElementById(id).style.display = 'block';
	}	
	else
	{
		document.getElementById(id).style.display = 'none';
	}
}

/**
 * reregisterDraggables takes an array of class names and registers all elements with this class as draggable
 * 
 */
  function reregisterDraggables(classNames) {
  	if (1 < arguments.length) {
		var dragOptions = arguments[1];
	}
  	for (var elIndex = 0; elIndex < classNames.length; elIndex++) {
	  	drags = document.getElementsByClassName(classNames[elIndex]);
	  	for (var i = 0; i < drags.length; i++){
	  		new Draggable(drags[i].id, dragOptions);
	  	}
  	}
  }

  
/**
 * fillInstituteAddress fills the form fields of the address fieldset
 * 
 * @author  Florian Beyerlein <f.beyerlein@congree.de>
 * @author	Patrick Rodacker <patrick.rodacker@the-reflection.de>
 */
function fillInstituteAddress()
{
	var inst_sel = document.getElementById('address_existing');
	var sel_index = inst_sel.value;
	var addrd = institutes[sel_index];
	var el_arr = ['ins1', 'ins2', 'ins3', 'street', 'zip', 'city', 'county', 'cntid'];
	for (var i = 0; i < el_arr.length; i++){
		var field = el_arr[i].toUpperCase();
		var el = document.getElementById('address_' + field);
		if (!el){ continue;}
		el.value = addrd[el_arr[i]];
		if (el_arr[i] == 'cntid') {
			for (var j = 0; j < el.options.length; j++) {
				if (el.options[j].value == addrd[el_arr[i]]){
					el.selectedIndex = j;
					break;
				}
			}
		}
	}
}

/**
 * updateAbstractCharCount counts the overall characters 
 * in the different content fields
 * 
 * @author  Florian Beyerlein <f.beyerlein@congree.de>
 * @author	Patrick Rodacker <patrick.rodacker@the-reflection.de>
 * @see		buildFakeString()
 */	
function updateAbstractCharCount(countClass, updateId, total, imageLength, tableLength, referenceLength) {
	//var els = document.getElementsByClassName(countClass);
	var els = $$('.'+countClass);
	var count = 0;
	var IMAGE_LENGTH = imageLength;
	imageFakeString = buildFakeString(IMAGE_LENGTH);
	var TABLE_LENGTH = tableLength;
	tableFakeString = buildFakeString(TABLE_LENGTH);
	var REFERENCE_LENGTH = referenceLength;
	referenceFakeString = buildFakeString(REFERENCE_LENGTH);
	
	els.each(function(el){
		var input = el.value;
		input = input.replace(/#IMG:\S*#/g, imageFakeString);
		input = input.replace(/#TBL:\S*#/g, tableFakeString);
		input = input.replace(/#REF:[a-zA-Z0-9]*#/g, referenceFakeString);
		input = input.replace(/#CHR:[a-zA-Z0-9_]*#/g, " ");
		// strip tags
		input = input.replace(/(<([^>^ ]+)>)/ig, '');
		input = input.replace(/\n/g, "");
		input = input.replace(/\r/g, "");
		count += input.length;
	});
	left = total - count;
	$(updateId).innerHTML = left;
}

/**
 * updateAbstractCharCount counts the overall characters 
 * in the different content fields
 * 
 * @author  Florian Beyerlein <f.beyerlein@congree.de>
 * @see		updateAbstractCharCount
 */	
function buildFakeString (count) {
	var s = '';
	for(var i = 0; i < count; i++) 
		s += ' ';
	return s;
}

var SidebarStatic = {
	id : "sidebar-static",
	element : null,
	previousElement : null,
	MINTOP: 20, // in px
	
	initialize : function() {
		this.element = $(this.id);
		if (!this.element) return;
		
		Element.makePositioned(this.element);
		this.previousElement = this.element.previous();
	},
	
	position : function() {
		if (!this.element || this.element == 'undefined') return;
		var viewPortOffset = Element.viewportOffset(this.element);
		this.previousElement = this.element.previous();
		if (!this.previousElement || this.previousElement == 'undefined') return;
		
		var previousDimensions = this.previousElement.getDimensions();
		var	previousTopOffset = Element.viewportOffset(this.previousElement).top;
		if (previousTopOffset + previousDimensions.height < 0){
			var topOffset = Math.abs(previousTopOffset + previousDimensions.height) + this.MINTOP;
			topOffset = topOffset < 0 ? 0 : topOffset;
		}
		else {
			topOffset = 0;
		}
		new Effect.Move (
					this.element, 
					{ 
						x: 0, 
						y: topOffset, 
						mode: 'absolute',
						duration: 0.1,
						queue: 'end' 
					} 
		);
	}
}
function initSidebarStatic() { 
	SidebarStatic.initialize(); 
	Event.observe(window, 'scroll', SidebarStatic.position.bindAsEventListener(SidebarStatic), true);
	}
Event.observe(window, 'load', initSidebarStatic, false);

