/* -------- HELPER FUNCTIONS -------- */

function ListAppend(l,v) {
	if ( l.blank() )  return v;
	var _a = l.split(",");
	_a.push(v);
	return _a.join(",");
}
function ListRemove(l,v) {
	if ( l == v )  return "";
	return l.split(",").without(v).join(",");
}

function parseRecord(tr) {
	if ( !tr ||  !Object.isString(tr.id) )  return null;
	
	var qs = tr.id.replace( /_/, "?" ).replace( /:/g, "=" ).replace( /__/g, "&" );
	
	var record = qs.parseQuery();
	var n = qs.indexOf('?');
	record.table = ( n > 0 ? $(qs.substring(0,n)) : tr.up("table") );
	record.tr = tr;
	
	return record;
}

function openWindow( url, winName, width, height, left, top, scrollbars, resizable, location, toolbar, menubar, status )
{
	//alert( $A(arguments).inspect() );
	var opts = new Array();
	if ( width )  opts.push( "width=" + width );
	if ( height )  opts.push( "height=" + height );
	if ( left )  opts.push( "left=" + left );
	if ( top )  opts.push( "top=" + top );
	
	opts.push( "scrollbars=" + ( scrollbars ? "yes" : "no" ) );
	opts.push( "resizable=" + ( resizable ? "yes" : "no" ) );
	opts.push( "location=" + ( location ? "yes" : "no" ) );
	opts.push( "toolbar=" + ( toolbar ? "yes" : "no" ) );
	opts.push( "status=" + ( status ? "yes" : "no" ) );
	opts.push( "menubar=" + ( menubar ? "yes" : "no" ) );
	
	w = window.open( url, winName, opts.join(", ") );
	w.focus();
	
	return w;
}
function focusWindow( winName ) {
	w = window.open( "", winName );
	w.focus();
}

function reloadWindow() { window.location.href = window.location.href; }
function reloadOpener() { window.opener.location.href = window.opener.location.href; }


/* -------- DATE FORMAT PROTOTYPE -------- */

var gsMonthNames = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
var gsDayNames = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
Date.prototype.format = function(mask) {
	if (!this.valueOf())  return "";
	var d = this;
	return mask.replace( /(yyyy|yy|mmmm|mmm|mm|dddd|ddd|dd|hh|HH|nn|ss|tt|t|TT|T)/g,
		function(token) {
			switch ( token ) {
				case 'yyyy': return d.getFullYear();
				case 'yy':   return d.getFullYear().toString().substr(2, 2);
				case 'mmmm': return gsMonthNames[d.getMonth()];
				case 'mmm':  return gsMonthNames[d.getMonth()].substr(0, 3);
				case 'mm':   return (d.getMonth() + 1).toPaddedString(2);
				case 'dddd': return gsDayNames[d.getDay()];
				case 'ddd':  return gsDayNames[d.getDay()].substr(0, 3);
				case 'dd':   return d.getDate().toPaddedString(2);
				case 'hh':   return ((h = d.getHours() % 12) ? h : 12).toPaddedString(2);
				case 'HH':   return d.getHours().toPaddedString(2);
				case 'nn':   return d.getMinutes().toPaddedString(2);
				case 'ss':   return d.getSeconds().toPaddedString(2);
				case 'tt':   return d.getHours() < 12 ? 'am' : 'pm';
				case 't':    return d.getHours() < 12 ? 'a' : 'p';
				case 'TT':   return d.getHours() < 12 ? 'AM' : 'PM';
				case 'T':    return d.getHours() < 12 ? 'A' : 'P';
			}
		}
	);
}


/* -------- CUSTOM METHODS -------- */

Form.Methods.getSelectedValue = function( form, name ) {
	form = $(form);
	var inputs = form.getInputs( "checkbox", name );
	if ( inputs.size() == 0 )
		inputs = form.getInputs( "radio", name );
	
	var selInput = inputs.find( function(el) { return el.checked; });
	if ( selInput )  return selInput.value;
}
Element.addMethods();  // methodize() and add to forms


/* -------- MENU CLASS & SUBNAV -------- */

var Menu = Class.create({
	initialize: function( obj ) {
		obj = $(obj);
		this.menu = obj;
		obj.self = this;
	},
	setPosition: function( top, left ) {
		this.menu.setStyle({ top: top + 'px', left: left + 'px' });
	},
	show: function() {
		if ( document.CurrentMenu && document.CurrentMenu != this )
			document.CurrentMenu.hide();
		
		document.CurrentMenu = this;
		this.menu.show();
	},
	hide: function() {
		this.menu.hide();
		
		if ( document.CurrentMenu == this )  document.CurrentMenu = null;
	}
});
document.CurrentMenu = null;

function hideMenu(css,e) {
	if ( !document.CurrentMenu )  return;
	try {
		var el = e.element();
		if ( el.match(css) || el.up(css) ) { e.stop(); return; }
	}
	catch(err) { }
	document.CurrentMenu.hide();
}
document.observe( "mousedown", hideMenu.curry(".menu") );


var NavMenu = Class.create( Menu, {
	initialize: function( $super, obj, objLink ) {
		$super(obj);
		this.caller = objLink;
		
		var offset = $(objLink).positionedOffset();
		this.setPosition( offset.top + $(objLink).offsetHeight, offset.left );
		
		$(objLink).observe( "mouseover", this.show.bindAsEventListener(this) );
		$(obj,objLink).invoke( "observe", "mouseout", this.onNavOut.bindAsEventListener(this) );
		$(obj).observe( "mouseover", this.cancelHide.bindAsEventListener(this) );
	},
	
	hide: function( $super ) {
		this.tmr_hide = null;
		this.caller.removeClassName("selected");
		$super();
	},
	
	show: function( $super, e ) {
		this.cancelHide(e);
		this.caller.addClassName("selected");
		$super();
	},
	
	onNavOut: function(e) {
		var el = $(e.relatedTarget);
		if ( el.up(".subnav") || el.hasClassName("subnav") )  return;
		
		if ( Prototype.Browser.IE )
			this.tmr_hide = this.hide.bind(this).delay(0.25);
		else
			this.tmr_hide = (function() { this.fx_hide = new Effect.Fade( this.menu, { duration: 0.15, afterFinish: this.hide.bind(this) }); }).bind(this).delay(0.25);
	},
	
	cancelHide: function(e) {
		window.clearTimeout(this.tmr_hide);
		this.tmr_hide = null;
		
		if ( this.fx_hide ) {
			this.fx_hide.cancel();
			this.menu.setStyle({ opacity: "" });
		}
	}
});


/* -------- DOM LOAD -------- */

document.observe( "dom:loaded", function(e) {
	$$('.linkbox').invoke( "observe", "click", function(e) {
		var a = this.down("a");
		window.open( a.href, a.target || "_self" );
		e.stop();
	});
	
	$$('.subnav').each( function(el) { new NavMenu( el, $( el.id.replace(/^sub/,"") )); });
});
