


function getElementByClass( Node, Clas )
	{
	var Pattern = new RegExp( "(^|\\s)" + Clas + "(\\s|$)" );
	var ChildrenNum = Node.childNodes.length;
	for (i = 0; i < ChildrenNum; i++)
		{
		if (Pattern.test( Node.childNodes[i].className ) )
			return Node.childNodes[i]
		}
	return null
	}


function getElementsByClass( searchClass, node, tag)
// http://www.dustindiaz.com/getelementsbyclass/
	{
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp( "(^|\\s)"+searchClass+"(\\s|$)" );
	for (i = 0, j = 0; i < elsLen; i++)
		{
		if (pattern.test( els[i].className ))
			{
			classElements[j] = els[i];
			j++;
			}
		}
	return classElements;
	}


function getElementByClassName_off( needle )
	{
	var xpathResult = document.evaluate('//*[@class = needle]', document, null, 0, null);
	var outArray = new Array();
	var item;
	while (item = xpathResult.iterateNext())
		outArray[outArray.length] = item;
	return outArray;
	}


function pop_up( Event, Link )
	{
	// ToDo: measure size of parent and set size here to 80%.
	var Url = Link.getAttribute( 'href' )
	var WindowName = Link.text // or innerHTML
	var Window = window.open( Url, WindowName, 'menubar=yes, toolbar=no, location=no, status=yes, resizable=yes, scrollbars=yes, height=700, width=800' )
	if (window.focus)
		Window.focus()
	YAHOO.util.Event.preventDefault( Event )
	return true
	}


function uri_input_check_off( Event, Input )
	{
	if (Input.value.length == 0 && Input.type != 'file')
		{
		Input.type = 'file'
		}
	else if (Input.value.search(/^http:/) != -1)
		{
		var Value = Input.value
		Input.type = 'text'
		Input.value = Value
		}
	// If the user tabbed out, keyboard focus will be before
	// the first link.  So set the focus to this element.
	Input.focus()
	}


function trickleup_init( Y )
	{
	var PopUps = Y.all( '.popup' );
	for (var i = 0; i < PopUps.length; i++)
		{
		var PopUp = PopUps.item( i );
		PopUp.on( "click", pop_up, PopUp );
		}
	/*
	var UriInputs = YAHOO.util.Dom.getElementsByClassName( 'uri_input' )
	for (var i = 0; i < UriInputs.length; i++)
		{
		YAHOO.util.Event.addListener( UriInputs[i], "change", uri_input_check, UriInputs[i] )
		YAHOO.util.Event.addListener( UriInputs[i], "keypress", uri_input_check, UriInputs[i] )
		}
	*/

	// first text input gets focus
	initFocus( Y );

	form_option_list_init( Y );
	form_label_width_init( Y );
	}


/**
 * Set focus to first input field.  If a page contains multiple forms,
 * only call this for one of them.
 * from jafl gallery-formmgr
 */
function initFocus( Y )
	{
	if (Y.config.doc.forms.length == 0)
		return;
	var Form = Y.config.doc.forms[0];
	for (var i = 0; i < Form.elements.length; i++)
		{
		var el = Form.elements[i];
		if (el.disabled || el.offsetHeight === 0)
			{
			continue;
			}

		var name = el.tagName.toLowerCase();
		var type = (el.type ? el.type.toLowerCase() : null);

		// What are we omitting?
		if ( (name == 'input' && (type == 'file' || type == 'password' || type == 'text'))
		  || (name == 'textarea')
		  || (name == 'select') )
			{
			try	{
				el.focus();
				}
			catch (Ex)
				{
				// no way to determine in IE if this will fail
				}
			if (name != 'select')
				{
				el.select();
				}
			break;
			}
		}
	}


function form_option_pop_image( Ev, Target )
	{
	var AltSrc = Ev.target.get( 'value' ).split( '^' );
	Target.alt.set( 'value', AltSrc[0] );
	Target.src.set( 'value', AltSrc[1] );
	}


function form_option_pop_summary( Ev, Target )
	{
	var FullStr = new Array();
	var Others = Target.Others.all( 'input' );
	for (var i = 0; i < Others.size(); i++)
		{
		var Other = Others.item( i );
		if (Other.get( 'checked' ))
			{
			FullStr.push( "<p>\n" + Other.get( 'value' ) + "\n</p>" );
			}
		}
	Target.textarea.set( 'value', FullStr.join( "\n" ) );
	}


/**
Set a callback for each input in the List to Func.
*/
function form_option_add_listener( List, Func, Target )
	{
//	var Inputs = YAHOO.util.Dom.getElementsBy( form_option_test, 'input', List );
	var Inputs = List.all( 'input' );
	for (var i = 0; i < Inputs.size(); i++)
		{
		var Input = Inputs.item( i );
//		YAHOO.util.Event.addListener( Input, 'click', Func, Target );
		Input.on( 'click', Func, null, Target );
		}
	}


function form_option_list_init( Y )
	{
	// ToDo: Handle Prefixes.
	var ImageTarget =
		{ alt: Y.one( 'input[name=ImageAlt]' )
		, src: Y.one( 'input[name=ImageDataURL]' )
		};
	var SummaryTarget = 
		{ textarea: Y.one( 'textarea[name=Summary]' )
		, Others: null
		};

	var Lists = Y.all( '.form_option_list' );
	for (var i = 0; i < Lists.size(); i++)
		{
		var List = Lists.item( i );
		if (List.hasClass( 'fol_image' ))
			form_option_add_listener( List, form_option_pop_image, ImageTarget );
		else if (List.hasClass( 'fol_summary' ))
			{
			SummaryTarget.Others = List;
			form_option_add_listener( List, form_option_pop_summary, SummaryTarget );
			}
		}
	}


function removePx( Str )
	{
	return Number( Str.substring( 0, Str.length - 2) );
	}


/**
offsetWidth includes border, clientWidth does not
*/
function form_label_width_init( Y )
	{
	var Forms = Y.all( '.form_style' );
	for (var fo = 0; fo < Forms.size(); fo++)
		{
		var Form = Forms.item( fo );

		// ToDo: Number().
		// ToDo: Review this in cases where box-sizing is border-box.
//		var BorderLeft = removePx( Form.getComputedStyle( 'border-left-width' ) );
//		var BorderRight = removePx( Form.getComputedStyle( 'border-right-width' ) );
//		var PadLeft = removePx( Form.getComputedStyle( 'padding-left' ) );
//		var PadRight = removePx( Form.getComputedStyle( 'padding-right' ) );
		var FormWidth = removePx( Form.getComputedStyle( 'width' ) );
//		FormWidth = FormWidth - BorderLeft - BorderRight - PadLeft - PadRight;
		FormWidth = FormWidth - 2 - 2 - 6 - 6;

		var Fields = Form.all( '> fieldset > .form_field' );
		var MaxLabelWidth = 0;
		for (var fi = 0; fi < Fields.size(); fi++)
			{
			var Field = Fields.item( fi );
			var Label = Field.one( 'label' );
			var LabelWidth = Label.getComputedStyle( 'width' );
			if (LabelWidth == 'auto')
				{
				continue;
				}
			LabelWidth = removePx( LabelWidth );
			if (MaxLabelWidth < LabelWidth)
				{
				MaxLabelWidth = LabelWidth;
				}
			}

		FormWidth = Number( FormWidth );
		for (var fi = 0; fi < Fields.size(); fi++)
			{
			var Field = Fields.item( fi );
			var Label = Field.one( 'label' );
			Label.setStyle( 'width',  MaxLabelWidth + "px" );
			var Value = Field.one( 'div' );
			Value.setStyle( 'width', (FormWidth - MaxLabelWidth - 32) + "px" );
			}
		}
	}


var Y = YUI().use( '*', function( Y )
	{
//	var used = [];
//	Y.each( Y.Env._attached, function( v, k )
//		{
//		used[used.length] = k;
//		} );
//	alert( 'Modules Loaded: ' + used.sort().join(', '));

	Y.on( "domready", trickleup_init, Y, Y );
	} );

