// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function setAction(id, newAction) {
	identity=document.getElementById(id);
	identity.action=newAction;
}

function setMethod(id, newMethod) {
	identity=document.getElementById(id);
	identity.method=newMethod;
}

// controls mouse out behavior to allow for child elements
HTMLElement.prototype.contains = function(node) {
	if (node == null) {
		return false;
	} else if (node == this) {
		return true;
	} else {
		return this.contains(node.parentNode);
	}
}

function navMenu(current) {	
	c = $(current + "_link")
	menu = $(current)
	Event.observe(c, 'mouseout', function(event) {
	  navMenuButtonOut(event);
	});
	Event.observe(menu, 'mouseout', function(event){
		navMenuOut(event, c, menu)
	})
	
	if (c.hasClassName('menuLink')) {
		c.removeClassName('menuLink');
	}
	c.addClassName('menuLinkCurrent')
	menu.show();	
	button_zindex = parseInt(c.getStyle('zIndex') || 2)
	if ((c.cumulativeOffset()[0] + menu.getWidth()) > (document.viewport.getWidth())) {
		x_position = c.positionedOffset()[0] - ((c.cumulativeOffset()[0] + menu.getWidth()) - (document.viewport.getWidth()));

	} else {
		x_position = c.positionedOffset()[0] + 'px';
	}	
	c.setStyle({zIndex: 200})
	menu.setStyle({zIndex: 199, left: x_position})
	
}

function navMenuOut(event, button, menu) {

	if (!menu.contains(event.relatedTarget || event.toElement)) {
		menu.hide();
		button.setStyle({zIndex: 0})
		menu.setStyle({zIndex: 1})

	if (button.hasClassName('menuLinkCurrent')) {
		button.removeClassName('menuLinkCurrent')
		button.addClassName('menuLink')

	}
	
}
}

function navMenuButtonOut(event) {
	button = Event.element(event);
	bottom = button.getHeight() + button.cumulativeOffset()[1]

	if (Event.pointerY(event) < bottom) {
		button.removeClassName('menuLinkCurrent');
		button.addClassName('menuLink')
		button.setStyle({zIndex: 0})		
		$(button.identify().gsub('_link', '')).hide();
	}
}

function navMenuClickOut(current) {
	c = $(current + "_link")	
	menu = $(current)

	c.setStyle({zIndex: 0})
	menu.setStyle({zIndex: 1})
	
	menu.hide();
	if (c.hasClassName('menuLinkCurrent')) {
		c.removeClassName('menuLinkCurrent');
	}
	c.addClassName('menuLink')
	
	
}



var active_menu = null
function navMenuClick(current) {
	if (active_menu && active_menu != current) {
		navMenuClick(active_menu)
		navMenuClick(current)
	} else {	
		c = $(current + "_link")
	
		menu = $(current)
	
		
		if (c.hasClassName('menuLink')) {
			active_menu = current
			Event.observe(document, 'click', closeNavMenuClick);
			c.removeClassName('menuLink');
			c.addClassName('menuLinkCurrent')
			menu.show();
			button_zindex = parseInt(c.getStyle('zIndex') || 2)
			if ((c.cumulativeOffset()[0] + menu.getWidth()) > document.viewport.getWidth()) {
				x_position = c.positionedOffset()[0] - ((c.cumulativeOffset()[0] + menu.getWidth()) - document.viewport.getWidth());

			} else {
				x_position = c.positionedOffset()[0] + 'px';
			}	
			// c.setStyle({zIndex: button_zindex})
			// menu.setStyle({zIndex: button_zindex - 1, left: x_position})
			c.setStyle({zIndex: 200})
			menu.setStyle({zIndex: 199, left: x_position})
		
		} else if (c.hasClassName('menuLinkCurrent')) {
			c.removeClassName('menuLinkCurrent');
			c.addClassName('menuLink')
			menu.hide()
			c.setStyle({zIndex: 0})
			menu.setStyle({zIndex: 1})
			
			active_menu = null
		}
	}
	
}

function closeNavMenuClick(event) {
	el = Event.element(event)
	if (active_menu) {
		if (el.inspect() != $(active_menu + "_link").inspect() && el.inspect() != $(active_menu).inspect()) {
			navMenuClick(active_menu)
			Event.stopObserving(document, 'click', closeNavMenuClick)
		}	
	}
}



function ResizeSpecificTextArea(ta, start_height, increase_interval)
{
	ta.extend
	
	// ta.setStyle({height: "100px"});
	// set the variables we need to keep track of
	if (start_height) {
		var start_height = start_height
	} else {
		var start_height = 100
	}
	if (increase_interval) {
		var increase_interval = increase_interval
	} else {
		var increase_interval = 50
	}
	var fontSize = ta.getStyle('fontSize').gsub(/px/, '');
	var lineHeight = ta.getStyle('lineHeight').gsub(/px/, '');
	var width = ta.getWidth();
	var simulatedColumnValue = Math.round(width/(fontSize/1.35));
	var stringValue = ta.getValue();
	var stringValueLength = stringValue.length;
	var softWraps = 0;
	var rowCount = 0;
	var returns = 1;
	var previousIndex = 0
	return_indexes = new Array(); 
	// first we flip through to see if we've got any linefeeds or returns
	// we'll add to our total returns number and push the index of the return 
	// into an array to later determine the total number of soft returns
	for (i=0;i<stringValueLength;i++) { 
		if (stringValue.charAt(i) == '\n' || stringValue.charAt(i) == '\r') { 
			return_indexes.push(i)
			returns += 1
		}
	}
	if (return_indexes.size() > 0) {
		return_indexes.each(function(index) { 
			subString = stringValue.substring(previousIndex, index)
			softWraps += Math.floor(subString.length/simulatedColumnValue)
			previousIndex = index
			
		});
		lastSubString = stringValue.substring(return_indexes.last(), stringValueLength)
		softWraps += Math.floor(lastSubString.length/simulatedColumnValue)
	} else {
		softWraps += Math.floor(stringValue.length/simulatedColumnValue)
	}
	rowCount = softWraps + returns
	new_height = (lineHeight * rowCount) + increase_interval;
	if (new_height > start_height) {
		// body = ta.ancestors().reverse()[1];
		// if (body.select('[class="sizer"]').size() == 0) {
		// 	body.insert({top: '<div class="sizer" style="display:none; height:100%"></div>'})
		// }
		// bg = body.select('[class="sizer"]').first()
		bg = $('inner_wrapper')
		container_height = bg.getHeight() - 30
		if (container_height > new_height) {
			ta.setStyle({height: new_height + "px"});
		} else {
			ta.setStyle({height: container_height + "px"});
		} 
	} else if (new_height < start_height) {
		ta.setStyle({height: start_height + "px"});
	}
	ta.writeAttribute('rows', value = rowCount);
	
	
	
}

function setValue(newValue, id) {
  identity=document.getElementById(id);
  identity.value=newValue;
}
