var stringEditor = stdClass.extend({
	//constructor
	constructor: function(el, settings) {
		this.base();
		// initialize settings
		Object.extend(this.s, {
			moduleName: 'ContentString',
			moduleID: null,
			showDebugs: ['kramer'], // See Base.js stdClass for docs on these.
			showErrors: ['kramer'], // See Base.js stdClass for docs on these.
			exemplarSelector: 'div.rSelectBox'
			/* put extensions to collections here */
		});

		// initialize nodes
		Object.extend(this.n, {
			el: el,
			moduleEl: null,
			json: document.createElement('div')
			/* put extensions to nodes here */
		});

		// initialize collections
		Object.extend(this.c, {
			/* put extensions to collections here */
		});
		this._findModuleEl();
		this._attachEvents();
	},
	elOnChange: function(e) {
		if(this.n.el.options[this.n.el.selectedIndex].value != ""){
			new FormDialog({
				'+zones': {
					 heading: 'Loading...',
					 text: 'Loading...'
				},
				position: {
					 exemplarAnchor: 'bottom left',
					 selfAnchor: 'top left',
					 exemplar: document.getElementsBySelector(this.s.exemplarSelector, this.n.el.parentNode)[0]
				},
				'+classNames': ['stringEditorDialog'],
				groupId: 'StringEditor',
				groupLimit: 1,
				startHidden: true,
				moduleName: this.s.moduleName,
				parentThis: this,
				recordID: this.n.el.options[this.n.el.selectedIndex].value,
				moduleID: this.s.moduleID,
				onClose: this.updateModule.bind(this)
			});
			/*new kDialog2({
				'+zones': {
					heading: 'Loading...',
					text: 'Loading...'
				},
				position: {
					exemplarAnchor: 'bottom right',
					selfAnchor: 'top right',
					exemplar: this.n.el,
					offsetY: 0,
					offsetX: 0
				},
				groupId: 'InviteDialogs',
				groupLimit: 1,
				startHidden: false
			});*/
		}
	},
	updateModule: function(result) {
		this.n.json.innerHTML = result.data.html;
		var boxNodes = document.getElementsBySelector('.module', this.n.json);
		if (boxNodes.length) {
			this.n.moduleEl.innerHTML = boxNodes[0].innerHTML;
			this._initScripts();
			this._fixStuffInModule();
		} else {
			this.error('No module found in returned HTML: \n\n'+result.data.html);
		}
	},
	_findModuleEl: function() {
		var moduleEl = this.n.el;
		while(!Element.hasClassName(moduleEl, 'module') && moduleEl.parentNode && moduleEl.tagName != 'BODY'){
			moduleEl = moduleEl.parentNode;
		}
		if(Element.hasClassName(moduleEl, 'module')){
			this.n.moduleEl = moduleEl;
			var sp = String(this.n.moduleEl.id).split("_");
			if(sp[1]){
				this.s.moduleID = sp[1];
			}
		}
	},
	_initScripts: function() {
		//EventSelectors.apply();
		//return;
		//init scripts here
		//this._fixStuffInModule();
	},
	_fixStuffInModule: function() {
		//debugger;
		var rSelects = document.getElementsBySelector('select.rSelect', this.n.moduleEl);
		for(var x=0; x<rSelects.length; x++){
			new rSelectClass (rSelects[x], {
			});
		}
		var stringEditors = document.getElementsBySelector('select.adminstrings', this.n.moduleEl);
		for(var x=0; x<stringEditors.length; x++){
			new stringEditor (stringEditors[x], {
			});
		}
	},
	_attachEvents: function() {
		this.eObserve(this.n.el, 'change', this.elOnChange.bind(this));
	}
});
EventSelectors.register({
	'select.adminstrings': function(el, index) {
		new stringEditor(el);
	}
}, true);

var aAccordion = stdClass.extend({
	//constructor
	constructor: function(el, settings, nodes, collections) {
		this.base();
		// initialize settings
		Object.extend(this.s, {
			selHeading: 'h3',
			startCollapsed: true,
			slideTime: 400,
			canOpenAll: true, /* only one open at a time */
			canCloseAll: true, /* can't close them all at the same time */
			openClass: 'aAccordionOpen'
			/* put extensions to collections here */
		});
		Object.extendProperties(this.s, settings);
		
		// initialize nodes
		Object.extend(this.n, {
			topEl: el,
			headers: []
			/* put extensions to nodes here */
		});
		Object.extendProperties(this.n, nodes);
		
		// initialize collections
		Object.extend(this.c, {
			/* put extensions to collections here */
		});
		Object.extendProperties(this.c, collections);
		this._findHeaders();
	},
	headerOnClick: function(header){
		var oneOpen = false;
		if(!this.s.canOpenAll || !this.s.canCloseAll){
			for(var x=0; x<this.n.headers.length; x++){
				if(this.n.headers[x].childEl != header.childEl){
					if(Element.visible(this.n.headers[x].childEl)){
						if(!this.s.canOpenAll){
							this.hideEl(this.n.headers[x].childEl, this.n.headers[x]);
						}
						if(!this.s.canCloseAll && this.s.canOpenAll){
							oneOpen = true;
						}
					}
				}
			}
		}
		if(Element.visible(header.childEl)){
			if(this.s.canCloseAll || oneOpen){
				//Element.hide(header.child);
				this.hideEl(header.childEl, header);
			}
		}else{
			//Element.show(header.childEl);
			this.showEl(header.childEl, header);
		}
	},
	hideEl: function(el, header) {
		if(header && header.el){
			Element.removeClassName(header.el, this.s.openClass);
		}
		Element.removeClassName(el, this.s.openClass);
		Effect.BlindUp(el,{
			duration: (this.s.slideTime/1000),
			afterFinishInternal: function(effect) {
				effect.element.undoClipping();
				effect.element.style.height = '';
				Element.hide(el);
			}
		});
	},
	showEl: function(el, header) {
		if(header && header.el){
			Element.addClassName(header.el, this.s.openClass);
		}
		Element.addClassName(el, this.s.openClass);
		Effect.BlindDown(el,{
			duration: (this.s.slideTime/1000),
			afterFinishInternal: function(effect) {
				effect.element.undoClipping();
				effect.element.style.height = '';
			}
		});
	},
	_findHeaders: function(){
		var headers = document.getElementsBySelector(this.s.selHeading, this.n.topEl);
		for(var x=0; x<headers.length; x++){
			var child = headers[x];
			while(child = child.nextSibling){
				if(child.tagName){
					break;
				}
			}
			if(String(child.tagName).toLowerCase() == 'fieldset'){
				newdiv = document.createElement('span');
				Element.addClassName(newdiv, 'aAccordionFix');
				var displayAttribute = child.style.display;
				child.style.display = 'block';
				newdiv.style.display = displayAttribute;
				child.parentNode.insertBefore(newdiv, child);			
				newdiv.appendChild(child);
				child = newdiv;
			}
			var header = {
				el: headers[x],
				childEl: child
			}
			if(Element.visible(header.childEl)){
				Element.addClassName(header.el, this.s.openClass);
			}
			
			this.eObserve(header.el, 'click', this.headerOnClick.bind(this, header));
			this.n.headers.push(header);
		}
	}
});
EventSelectors.register({
	'.aAccordion': function(el, index) {
		new aAccordion(el, {
			selHeading: 'h3',
			openClass: 'selected'
		}, {
			topEl: el
		});
	}
}, true);
/*EventSelectors.addLoadEvent(function() {
	new kAccordion({
		elRoot: document.getElementsByTagName('form')[0],
		selHeading: 'h3'
	});
});*/
/**
 * rSelect
 *
 * @version 1.5
 * @author Ryan Brill
 *
 * @requires prototype.js
 * @requires prototype_ss.js
 * @requires DOM.js
 * @requires EventSelectors.js
 **/

var rSelectClass = Class.create();
rSelectClass.prototype = {

	//Constructor
	initialize: function(el, s) {
		this.items = [];									// array of list items
		this.el = el;										// <select class="rSelect">
		this.label = document.createElement('div');			// the label of our dropdown
		this.labelText = document.createElement('span');	// the text node inside our label
		this.container = document.createElement('div');		// the container element
		this.ulcontainer = document.createElement('div');	// Generated UL mirroring the <select> box
		this.imask = document.createElement('iframe');		// iframe fix for IE
		this.actualCols = 0;								// Actual number of columns that get created
		this.s = {
			fixedWidth: true,					// set the width to the size of the original <select> box
			className: 'rSelectBox',			// Class name to apply to the <div> tag
			selectedClass: 'selected',			// Class name for the seleted item in the select box
			hoverClass: 'hover',				// Class name for the hover of the items in the list
			toggleClass: 'toggle',				// Class name for the toggle all items in the list
			isMultiSelect: false,				// whether or not this is a multiple select box. It will look for the attribute multiple="multiple" on the select box
			defaultLabel: 'No Values Selected',	// Default value for when no options are selected in a multi-select box
			pxPerChar: 9,						// Number of pixels to allow for each character in the label
			maxHeight: 300,						// Maximum height of the dropdown for multi selects
			maxCols: 1,							// Maximum number of columns - will be fewer colums if fewer fit in maxHeight
			pxPerRow: 22,						// Number of pixels to allow for each row
      processed: 'rSelectReplaced'        // Flag to tell whether a select has been processed yet
		};
		for (var key in s) {
			this.s[key] = s[key];
		}
		if(this.el.hasClassName(this.s.processed))
			return 0;
			
		Element.addClassName(this.el, this.s.processed);
		
		var multi = (this.el.attributes['multiple']) ? this.el.attributes['multiple'].value : this.el.getAttribute('multiple');
		if (multi && (multi.toLowerCase() == "multiple" || multi.toLowerCase() == "true")){
			this.s.isMultiSelect = true;
		}

		Element.addClassName(this.container, this.s.className);
		if(this.el.className != ''){
			Element.addClassName(this.container, this.el.className);
		}

		/* Event observers */
		Event.observe(this.ulcontainer, 'click', this.onClick.bind(this));
		Event.observe(this.ulcontainer, 'mouseover', this.onMouseOver.bind(this));
		Event.observe(this.label, 'click', this.clickLabel.bind(this));

		this.findElements();
		this.createContainers();
		Element.hide(this.el);
		this.initializeOnBlur();
		
	},
	//Methods
	findElements: function() {
		var alleles = this.el.childNodes;
		var eles = new Array();
		var totalEles = 0;
		var makeSelected = false;
		var currentRow = 0;
		var ul = document.createElement('ul');
		
		// add top level <option> and <optgroups> to eles, and count all rows (totalEles) that will get outputted
		for (var i=0; i<alleles.length; i++) {
			switch (String(alleles[i].tagName).toLowerCase()) {
				case 'optgroup':
					eles.push(alleles[i]);
					var opts = alleles[i].getElementsByTagName('option');
					if (alleles[i].label) {
						totalEles++;
					}
					totalEles = totalEles + opts.length;
				case 'option':
					if (alleles[i].childNodes.length == 1) {
						eles.push(alleles[i]);
						totalEles++;
					}
			}
		}
		
		// Check how many rows we are expecting
		var expectedCols = (Math.ceil(totalEles * this.s.pxPerRow / this.s.maxHeight) > this.s.maxCols ? this.s.maxCols : Math.ceil(totalEles * this.s.pxPerRow / this.s.maxHeight));
		
		for (var i=0; i<eles.length; i++) {
			
			// Close our current UL and start a new one
			if (currentRow > Math.ceil(totalEles / expectedCols) - 1) {
				Element.addClassName(ul, 'col');
				this.ulcontainer.appendChild(ul);
				ul = document.createElement('ul');
				currentRow = 0;
				this.actualCols++;
			}
						
			// Fix disabled options in IE
			if(eles[i].selected && eles[i].disabled){
				makeSelected = true;
			}
			if(makeSelected && !eles[i].disabled && String(eles[i].tagName).toLowerCase() == "option"){
				makeSelected = false;
				eles[i].selected = true;
			}
			
			switch (String(eles[i].tagName).toLowerCase()) {
				case 'optgroup':
					var li = document.createElement('li');
					var optgroupUl = document.createElement('ul');
					
					if (eles[i].label) {
						Element.addClassName(li, 'optlabel');
						if (eles[i].disabled) {
							Element.addClassName(li, 'disabled');
							Element.addClassName(optgroupUl, 'disabled');
						}
						li.innerHTML = eles[i].label;
						ul.appendChild(li);
						currentRow++;
					}
					
					var opts = eles[i].getElementsByTagName('option');
					currentRow = currentRow + opts.length;

					for (var j=0; j<opts.length; j++) {
						// Fix disabled options in IE
						if(opts[j].selected && (eles[i].disabled || opts[j].disabled)){
							makeSelected = true;
						}
						if(makeSelected && !eles[i].disabled && !opts[j].disabled){
							makeSelected = false;
							opts[j].selected = true;
						}
						
						this.createElement(opts[j], eles[i].disabled, optgroupUl);
					}
					ul.appendChild(optgroupUl);
					break;
				case 'option':
					if (eles[i].childNodes.length == 1) {
						this.createElement(eles[i], eles[i].disabled, ul);
						currentRow++;
					}
					break;
			}
		}
		this.actualCols++;
		if (this.actualCols > 1) {
			Element.addClassName(ul, 'col');
		}
		this.ulcontainer.appendChild(ul);
		
	},
	createElement: function(el, state, parent) {
		// Fix disabled options in IE
		var elSelected = (el.selected && (!el.disabled && (!el.parentNode || !el.parentNode.disabled))) ? true : false;
		
		var itemvar = {};
		itemvar.value = el.value;
		itemvar.el = el;
		itemvar.selected = elSelected;
		
		itemvar.disabled = (el.disabled || el.parentNode.disabled) ? true : false;
		itemvar.text = el.text;
		itemvar.li = document.createElement('li');
		if(itemvar.el.className != ''){
			Element.addClassName(itemvar.li, itemvar.el.className);
		}
		if (state == true) {
			itemvar.li.innerHTML = el.text;
			Element.addClassName(itemvar.li, 'disabled');
		}
		else {
			itemvar.a = document.createElement('a');
			itemvar.a.href = 'javascript:void(0);';
			itemvar.text = el.text;
			if(this.s.isMultiSelect){
				itemvar.a.innerHTML = '<input type="checkbox" ' + ((itemvar.selected)? ' checked="checked"' : '' ) + '> ' + el.text;
			}else{
				itemvar.a.innerHTML = el.text;
			}
			itemvar.li.appendChild(itemvar.a);
		}
		parent.appendChild(itemvar.li);
		this.items.push(itemvar);
		if (elSelected) { // create our label element
			this.setLabel(el.text);
		}
	},
	createContainers: function() {
		
		Element.addClassName(this.label, 'label');
		
		var icon = document.createElement('span');
		Element.addClassName(icon, 'icon');
		this.label.appendChild(this.labelText);
		this.label.appendChild(icon);
		
		this.container.appendChild(this.label);
		this.container.appendChild(this.ulcontainer);
		DOM.insertAfter(this.container, this.el);
		Element.addClassName(this.ulcontainer, 'ulcontainer');
		
		var dims = Element.getDimensions(this.el);
		var origDims = dims.width;
		
		var uldims = Element.getDimensions(this.ulcontainer);
		
		// container width
		if (this.s.fixedWidth == true && origDims) {
			this.container.style.width = origDims + 'px'; // set the width of the original <select>
		}
		else if (this.s.fixedWidth == true) {
			this.container.style.width = dims.width + ((this.s.isMultiSelect == true) ? 30 : 0) + ((!isNaN(this.s.maxHeight) && uldims.height > this.s.maxHeight) ? 17 : 0) + 'px'; // set the width of the container
		}
		
		var uls = this.ulcontainer.childNodes;
		var ulswidth = 0;
		for (i=0; i<uls.length; i++) {
			if (uls[i].tagName.toLowerCase() == 'ul') {
				ulswidth += uls[i].offsetWidth;
			}
		}
		
		// dropdown width
		if (this.actualCols > 1) {
			// multiple columns
			this.ulcontainer.style.width = ulswidth + ((!isNaN(this.s.maxHeight) && uldims.height > this.s.maxHeight) ? 17 : 0) + 'px'; // set the width of the UL to the width of the original <select>
			//alert ('1');
		}
		else if (ulswidth > dims.width && ulswidth.width > parseInt(this.container.style.width)) {
			
			//-------------------------
			// IE sometimes needs ulswidth and sometimes uldims.width
			//-------------------------
			
			// options longer than select
			this.ulcontainer.style.width = uldims.width + ((!isNaN(this.s.maxHeight) && uldims.height > this.s.maxHeight) ? 17 : 0) + 'px';
			//alert ('2');
		}
		else if (this.s.fixedWidth == true && origDims) {
			// set the width of the original <select>
			this.ulcontainer.style.width = origDims + 'px';
			this.ulcontainer.style.width = origDims + (origDims - this.ulcontainer.offsetWidth) + 'px';
			//alert ('3');
		}
		else {
			// normal width
			this.ulcontainer.style.width = dims.width + 'px'; // set the width of the UL container to the width of the original <select>
			this.ulcontainer.style.width = dims.width + (dims.width - this.ulcontainer.offsetWidth) + ((this.s.isMultiSelect == true) ? 30 : 0) + ((!isNaN(this.s.maxHeight) && uldims.height > this.s.maxHeight) ? 17 : 0) + 'px'; // reset the width of the UL, calculating border / padding
			//alert ('4');
		}
		
		if (!isNaN(this.s.maxHeight) && uldims.height > this.s.maxHeight) {
			this.ulcontainer.style.height = this.s.maxHeight + 'px';
			this.ulcontainer.style.overflow = 'auto';
		}
		
		var left = String(Position.cumulativeOffset(this.ulcontainer));
		left = Number(left.substring(0, left.indexOf(',')));
		left += Element.getDimensions(this.ulcontainer).width;
		if(Element.getDimensions(document.getElementsByTagName('body')[0]).width < left){
			this.ulcontainer.style.right = "0px";
		}
				
		var uldims = Element.getDimensions(this.ulcontainer);
		
		if (document.all) { 
			/* create an iFrame to fix IE bug */
			this.imask.scrolling = 'no';
			this.imask.frameborder = '0';
			this.imask.style.display = 'none';
			this.imask.style.position = 'absolute';
			this.imask.style.marginTop = '-1';
			//this.imask.style.zIndex = 10;
			this.imask.style.zIndex = -1;
			this.imask.allowTransparency = "true";
			this.ulcontainer.style.zIndex = 11;
			this.imask.style.width = uldims.width + 'px';
			this.imask.style.height = uldims.height + 'px';
			this.container.appendChild(this.imask);
			this.imask.style.top = this.ulcontainer.style.top;
			if(Element.getDimensions(document.getElementsByTagName('body')[0]).width < left){
				this.imask.style.right = "0px";
			}
		}
		this.updateLabel();
		this.hide();
	},	
	clickLabel: function(e) {
		this.toggle();
		for (var i=0; i<this.items.length; i++) {
			Element.removeClassName(this.items[i].li, this.s.hoverClass);
			Element.removeClassName(this.items[i].li, this.s.selectedClass);
			if (this.items[i].selected == true) {
				Element.addClassName(this.items[i].li, this.s.hoverClass);
				Element.addClassName(this.items[i].li, this.s.selectedClass);
			}
		}
		if (typeof(this.el.onclick) == 'function') {
			this.el.onclick();
		}
		if(!Element.visible(this.ulcontainer)){
			this.hide();
			if (typeof(this.el.onclose) == 'function') {
				this.el.onclose();
			}
			if (typeof(Event.fire) == 'function') {
				Event.fire(this.el, "close");
			}
		}
	},
	toggle: function() {
		if(Element.visible(this.ulcontainer)){
			this.hide();
		}else{
			this.show();
		}
	},
	hide: function() {
		Element.hide(this.ulcontainer);
		if (document.all) { 
			Element.hide(this.imask);
		}
	},
	show: function() {
		Element.show(this.ulcontainer);
		if (document.all) { 
			Element.show(this.imask);
		}
		var left = String(Position.cumulativeOffset(this.ulcontainer));
		left = Number(left.substring(0, left.indexOf(',')));
		left += Element.getDimensions(this.ulcontainer).width;
		if(Element.getDimensions(document.getElementsByTagName('body')[0]).width < left){
			this.ulcontainer.style.right = "0px";
		}
	},
	checkItem: function(itemvar, index){
		itemvar.selected = true;
		itemvar.el.selected = true;
		if(itemvar.li.getElementsByTagName('input')[0]){
			itemvar.li.getElementsByTagName('input')[0].checked = true;
		}
		Element.addClassName(itemvar.li, this.s.selectedClass);
		if(index){
			this.el.selectedIndex = index;
		}
	},
	uncheckItem: function(itemvar){
		itemvar.selected = false;
		itemvar.el.selected = false;
		if(itemvar.li.getElementsByTagName('input')[0]){
			itemvar.li.getElementsByTagName('input')[0].checked = false;
		}
		Element.removeClassName(itemvar.li, this.s.selectedClass);
	},
	onClick: function(e) {
		var ele = Event.element(e);
		while(ele && ele != this.container){
			if(ele.tagName.toLowerCase() == 'a'){
				break;
			}
			ele = ele.parentNode;
		}
		if(ele.tagName.toLowerCase() != 'a'){
			return;
		}
		if(this.s.isMultiSelect){
			for (var i=0; i<this.items.length; i++) {
				if (this.items[i].a == ele) {
					if(this.items[i].selected == true){
						this.uncheckItem(this.items[i]);
					}else{
						this.checkItem(this.items[i]);
					}
					break;
				}
			}
		}else{
			for (var i=0; i<this.items.length; i++) {
				if (this.items[i].a == ele) {
					this.checkItem(this.items[i], i);
				} else {
					this.uncheckItem(this.items[i]);
				}
			}
			this.hide();
		}
		var allchecked = true;
		for(var x=0; x<this.items.length; x++){
			if(this.items[x].a == ele){
				var itemvar = this.items[x];
			}
			if(this.items[x].selected == false && !Element.hasClassName(this.items[x].li, this.s.toggleClass) && !this.items[x].disabled){
				allchecked = false;
			}
		}
		if(Element.hasClassName(itemvar.li, this.s.toggleClass)){
			for(var x=0; x<this.items.length; x++){
				if(allchecked){
					//uncheck all
					this.uncheckItem(this.items[x]);
				}else{
					//check all
					if (!this.items[x].disabled) {
						this.checkItem(this.items[x]);
					}
				}
			}
		}else if(allchecked){
			for(var x=0; x<this.items.length; x++){
				if(Element.hasClassName(this.items[x].li, this.s.toggleClass)){
					//alert ('ran');
					this.checkItem(this.items[x]);
				}
			}
		}else{
			for(var x=0; x<this.items.length; x++){
				if(Element.hasClassName(this.items[x].li, this.s.toggleClass)){
					this.uncheckItem(this.items[x]);
				}
			}
		}
		this.updateLabel();
		if (typeof(this.el.onclick) == 'function') {
			this.el.onclick();
		}
		if (typeof(this.el.onchange) == 'function') {
			this.el.onchange();
		}
		if (typeof(Event.fire) == 'function') {
			Event.fire(this.el, "change");
		}
	},
	onMouseOver: function (e) {
		var ele = Event.element(e);
		if(ele.tagName.toLowerCase() != 'a' && ele.tagName.toLowerCase() != 'li'){
			return;
		}
		for (var i=0; i<this.items.length; i++) {
			Element.removeClassName(this.items[i].li, this.s.hoverClass);
			if (this.items[i].a == ele) {
				Element.addClassName(this.items[i].li, this.s.hoverClass);
			}
		}
	},
	initializeOnBlur: function() {
		Event.observe(document, 'click', this.onBlur.bind(this), false);
		Event.observe(window, 'blur', this.onBlur.bind(this), false);
	},
	onBlur: function (e) {
		var el = Event.element(e);
		var found = false;
		do {
			if (el == null || el == window || el == document.body) break;
			if(el == this.container){
				found = true;
				break;
			}
		} while(el = el.parentNode);
		if(!found && Element.visible(this.ulcontainer)){
			this.hide();
			if (typeof(this.el.onclose) == 'function') {
				this.el.onclose();
			}
			if (typeof(Event.fire) == 'function') {
				Event.fire(this.el, "close");
			}
		}
	},
	updateLabel: function() {
		var selectedItems = [];
		for(var x=0; x<this.items.length; x++){
			if(this.items[x].selected && !Element.hasClassName(this.items[x].li, this.s.toggleClass)){
				selectedItems.push(this.items[x].text);
			}
		}
		text = selectedItems.join(', ');
		if(text == ''){
			text = this.s.defaultLabel;
		}
		var dims = Element.getDimensions(this.label);
		var shorttext = text.substring(0, Math.round(dims.width/this.s.pxPerChar));
		if(shorttext != text){
			text = shorttext + '\u2026'; // truncate text and add ellipsis
		}
		this.setLabel(text);
	},
	setLabel: function(text) {
		//this.labelText.innerHTML = text; // This brakes in IE
		var text = document.createTextNode(text);
		this.labelText.innerHTML = '';
		this.labelText.insertBefore(text, this.labelText.firstChild);
	}
}

//instantiate and use the object
EventSelectors.register({
	'select.rSelect' : function(el) {
		new rSelectClass (el, {
		});
	},
	'select.rSelectNetwork' : function(el) {
		new rSelectClass (el, {
			defaultLabel: 'My Network'
		});
	},
	'select.rSelectFriends' : function(el) { 
		new rSelectClass (el, {
		   defaultLabel: 'No Friends Selected' 
		}); 
	},
	'select.rSelectContent' : function(el) {
		new rSelectClass (el, {
			defaultLabel: 'No Content Selected'
		});
	}
}, true);

var LocalizedFormData = stdClass.extend({
	//constructor
	constructor: function(el, settings) {
		this.base();
		// initialize settings
		Object.extend(this.s, {
			showLanguageTimeout: null,
			showLanguageSlideTime: 400,
			headerTag: 'h3'
		});

		// initialize nodes
		Object.extend(this.n, {
			header: null,
			container: el.parentNode,
			selects: [],
			fieldsets: []
		});

		// initialize collections
		Object.extend(this.c, {
			/* put extensions to collections here */
		});
		
		if($("stringtype"))
		{
			this.changeEditor();
			this._findSelects();
			this._findFieldsets();
			this._findHeader();
		
			this.showLanguage(this.n.selects[0].el.options[this.n.selects[0].el.selectedIndex].value, false);
		}
	},
	selectOnChange: function(sel) {
		this.changeSelected(sel.el.options[sel.el.selectedIndex].value);
	},
	showLanguage: function(lang, effect) {
		if(this.s.showLanguageTimeout){
			window.clearTimeout(this.s.showLanguageTimeout);
		}
		for(var x=0; x<this.n.fieldsets.length; x++){
			if(this.n.fieldsets[x].lang === lang){
				if(effect == false){
					Element.show(this.n.fieldsets[x].el);
				}else{
					this.n.fieldsets[x].el.style.marginTop = "10px"; //IE Hack
					Effect.BlindDown(this.n.fieldsets[x].el,{
						duration: (this.s.showLanguageSlideTime/1000),
						afterFinishInternal: function(effect) {
							effect.element.undoClipping();
							effect.element.style.height = '';
						}
					});
				}
			}else if(Element.visible(this.n.fieldsets[x].el)){
				this.n.fieldsets[x].el.style.marginTop = "0"; //IE Hack
				Element.hide(this.n.fieldsets[x].el);
			}
		}
	},
	changeEditor: function()
	{
		if($("stringtype").value=="EditableHTML")
		{
			$A(document.getElementsByClassName("localValueTextArea")).each(function (ta){ta.hide(); ta.value="";});
			$A(document.getElementsByTagName("iframe")).each(function(i){if(/fckeditor/.test(i.src+"")) i.show()});
		}
		else
		{
			$A(document.getElementsByClassName("localValueTextArea")).each(function (ta){ta.show()});
			$A(document.getElementsByTagName("iframe")).each(function(i){if(/fckeditor/.test(i.src+"")) i.hide()});
		}
		
	},

	changeSelected: function(lang) {
		var found = false;
		for(var x=0; x<this.n.fieldsets.length; x++){
			if(Element.visible(this.n.fieldsets[x].el)){
				found = true;
				Effect.BlindUp(this.n.fieldsets[x].el, {duration: (this.s.showLanguageSlideTime/1000)});
				this.s.showLanguageTimeout = window.setTimeout(this.showLanguage.bind(this, lang), this.s.showLanguageSlideTime);
			}
		}
		if(!found){
			this.showLanguage(lang);
		}
	},
	fieldsetOnChange: function(fieldset){
		fieldset.changed = true;
		this.setHeader();
	},
	setHeader: function(){
		var changedLangs = [];
		for(var x=0; x<this.n.fieldsets.length; x++){
			if(this.n.fieldsets[x].changed){
				for(var i=0; i<this.n.selects[0].el.options.length; i++){
					if(this.n.selects[0].el.options[i].value == this.n.fieldsets[x].lang){
						changedLangs.push(this.n.selects[0].el.options[i].innerHTML);
						break;
					}
				}
			}
		}
		if(this.n.header){
			this.n.header.el.innerHTML = this.n.header.originalText + " [Edited: " + changedLangs.join(", ") + "]";
		}
	},
	_findSelects: function() {
		var selects = document.getElementsBySelector('select.languages', this.n.container);
		selects.each(function(object, index) {
			var sel = {
				el: object
			}
			this.eObserve(sel.el, 'change', this.selectOnChange.bind(this, sel));
			this.n.selects.push(sel);
		}.bind(this));

		this.eObserve($("stringtype"), 'change', this.changeEditor);
	},
	_findFieldsets: function() {
		var fieldsets = document.getElementsBySelector('fieldset', this.n.container);
		fieldsets.each(function(object, index) {
			var div = document.createElement('div'); // This line previously read: var div = document.createElement('span'); -- why were we calling our span a div? I'm sure there was a reason for changing the element to a span, but I don't know what it was, and it was causing browser errors because contents of this element want to be block-level. (-Kramer)
			object.parentNode.insertBefore(div, object);
			div.appendChild(object);
			if(!Element.visible(object)){
				Element.show(object);
				Element.hide(div);
			}
			var fieldset = {
				el: div,
				fieldset: object,
				lang: object.attributes['lang'].value,
				changed: false
			}

			var formFields = document.getElementsBySelector('input', fieldset.el);
			formFields.push(document.getElementsBySelector('textarea', fieldset.el));
			formFields.push(document.getElementsBySelector('select'));
			

			for(var x=0; x<formFields.length; x++){
				if(formFields[x][0]){
					for (var y=0;y<formFields[x].length;y++){
						this.eObserve(formFields[x][y], 'change', this.fieldsetOnChange.bind(this, fieldset));
					}
				}else{
					this.eObserve(formFields[x], 'change', this.fieldsetOnChange.bind(this, fieldset));
				}
			}

			var fckEditors = document.getElementsBySelector('textarea.FCKeditor', fieldset.el);
			for(var x=0; x<fckEditors.length; x++){
				var oFCKeditor = new FCKeditor(fckEditors[x].name);
				oFCKeditor.ReplaceTextarea();
			}

			//this.eObserve(fieldset.el, 'change', this.fieldsetOnChange.bind(this, fieldset));
			this.n.fieldsets.push(fieldset);
		}.bind(this));
	},
	_findHeader: function() {
		var el = this.n.container;
		do{
			if(String(el.tagName).toLowerCase() == this.s.headerTag){
				this.n.header = {
					el: el,
					originalText: el.innerHTML
				}
				break;
			}
		} while(el = el.previousSibling);
	}
});

EventSelectors.register({
	'select.languages': function(el, index) {
		new LocalizedFormData(el);
	}
}, true);


var LimitedTextArea = Class.create();
Object.extend(LimitedTextArea.prototype, {
	counter: null,
	softlimit: 160,
	hardlimit: 200,
	infocontainer: '',
	stoptyping: true,
	
	initialize: function(options){
		Object.extend(this, options);
		
		if(this.id)
		{
			if($(this.id).value.length > 0)
			{
				$(this.infocontainer).innerHTML = '<i>Characters remaining:</i> '+ (this.softlimit - $(this.id).value.length);
			}
		}
		if(this.id != null){ this.monitor(); }
	},
	monitor: function(){
		new Form.Element.Observer($(this.id), .5, function(elem){
			text = elem.value;
			if(text.length > this.hardlimit)
			{
				//over the limit, disable more typing if enabled:
				if(this.stoptyping) {
					elem.value = elem.value.substring(0, this.hardlimit);
				} else {
					//update limit:
					$(this.infocontainer).innerHTML = '<i>Characters remaining:</i> '+ (this.softlimit - text.length);
					if($(this.infocontainer).hasClassName('gettingclose'))
					{
						$(this.infocontainer).removeClassName('gettingclose');
						elem.removeClassName('gettingclose');
					}
					if(!$(this.infocontainer).hasClassName('overlimit'))
					{
						$(this.infocontainer).addClassName('overlimit');
						elem.addClassName('overlimit');
					}
				}
			}
			else if(text.length >= this.softlimit)
			{
				//warn the user
				$(this.infocontainer).innerHTML = '<i>Characters remaining:</i> '+ (this.softlimit - text.length);
				if($(this.infocontainer).hasClassName('gettingclose'))
				{
					$(this.infocontainer).removeClassName('gettingclose');
					elem.removeClassName('gettingclose');
				}
				$(this.infocontainer).addClassName('overlimit');
				elem.addClassName('overlimit');
			}
			else if((this.softlimit - text.length) < 15)
			{
				//getting close to the end of their field.
				$(this.infocontainer).innerHTML = '<i>Characters remaining:</i> '+ (this.softlimit - text.length);
				if($(this.infocontainer).hasClassName('overlimit'))
				{
					$(this.infocontainer).removeClassName('overlimit');
					elem.removeClassName('overlimit');
				}
				$(this.infocontainer).addClassName('gettingclose');
				elem.addClassName('gettingclose');
			} 
			else
			{
				//under the limit, reset classes if needed.
				$(this.infocontainer).innerHTML = '<i>Characters remaining:</i> '+ (this.softlimit - text.length);
				if($(this.infocontainer).hasClassName('overlimit'))
				{
					$(this.infocontainer).removeClassName('overlimit');
					elem.removeClassName('overlimit');
				}
				if($(this.infocontainer).hasClassName('gettingclose'))
				{
					$(this.infocontainer).removeClassName('gettingclose');
					elem.removeClassName('gettingclose');
				}
			}
		}.bind(this));
	}
});

function initLimitedTextArea() { new LimitedTextArea(); }
Event.observe(window, 'load', initLimitedTextArea, false);

var MouseOverHelp = {
	timeout: null,
	theelement: null,
	showPopuphelp: function(theelement) {
		clearTimeout(this.timeout);
		if($(theelement).style.display == 'none') {
			this.timeout = setTimeout(function(){new Effect.BlindDown(theelement, {duration:.3, fps:40})},400);
			//$(theelement).setStyle({left: '400px'});
		}
	},
	hidePopuphelp: function(theelement){
        if($(theelement).style.display == 'none'){
            clearTimeout(this.timeout);
        }else{
            this.timeout = setTimeout(function(){new Effect.BlindUp(theelement, {duration:.3, fps:40})},300);
        }
    }
}    

