/* class */ function SplayProgressBar( in_form ) {
    if( !this.isSupported( ) ) {
        in_form.onsubmit = chainHandlers(
              in_form.onsubmit
            , bind( this, 'degrade', in_form )
       );
        return;
    }
    
    this.setFUC(
        new FileUploadController( in_form, function( ) { } )
    );
    
    var fuc     = this.getFUC( );
    var form    = this.getForm( );
    
    fuc.setFrameOnloadHandler( bind( this, 'frameLoad' ) );
    
    form.onsubmit = chainHandlers(
          form.onsubmit
        , bind( this, 'submit' )
    );
}

extend( SplayProgressBar.prototype, {
        
            _fuc                : null
        ,   getFUC              : function( ) {
                return this._fuc;
            }
        ,   setFUC              : function( v ) {
                this._fuc = v; return this;
            }
        
        ,   getForm             : function( ) {
                return this.getFUC( ).getForm( );
            }
        
        ,   isSupported         : function( ) {
//                return false;
                
                if( isIE( ) ) {
                    return ( document.cloneNode );
                }
                else {
                    return ( document.importNode );
                }
            }
        
        ,   getFrame            : function( )   {
                return this.getFUC( ).getFrame( );
            }
        
        ,   submit              : function( ) {
                var fuc = this.getFUC( );
                
                if( fuc.isAvailable( ) ) {
                    this.getFUC( ).getLitebar( ).setAutoDispose( false );
                }
            }
        
        ,   frameLoad           : function( ) {
                response = getIFrameHTML( this.getFrame( ) );
                
                // Most likely melee_trigger( ) has occurred...
                if( !response.getElementById( 'uvInterior' ) ) {
                    transferNode(
                          document.getElementsByTagName( 'head' )[0]
                        , response.getElementsByTagName( 'head' )[0]
                    );
                    
                    transferNode(
                          document.getElementsByTagName( 'body' )[0]
                        , response.getElementsByTagName( 'body' )[0]
                    );
                    
                    return;
                }
                
                transferNode(
                      getObj( 'uvInterior' )
                    , response.getElementById('uvInterior')
                );
                
                this.tabulaRasa( );
                return;
            }
        
        ,   tabulaRasa          : function( ) {
                form = getObj( this.getForm( ).id );
                
                window.scrollTo( 0, 0 );
                this.getFUC( ).getLitebar( ).dispose( );
                
                if( form ) {
                    new SplayProgressBar( form );
                }
            }
        
        ,   degrade             : function( ) {
                content = getObj( 'uvPageContent' );
                
                content.className += ' sending';
                content.appendChild(
                    content_wait = document.createElement( 'p' )
                ).id = 'degradedProgressMeter';
                
                content_wait.innerHTML =
                     'Your Maine Memory is being processed. Please be '
                    +'patient, as this may take several minutes.'
                    ;
                
                return true;
            }
} );



/* class */ function Terra( in_country, in_province ) {
	this._country   = in_country;
	this._province  = in_province;
}

extend( Terra.prototype, {
		_country			: null
	,	_province			: null
	,   _cache              : new Object( )
	
	,	isAvailable			: function( ) {
			return this._country && this._province;
		}
    
    ,   isCached            : function( id ) {
            return !!this._cache[ id ];
        }
    ,   getFromCache        : function( id ) {
            return this._cache[ id ];
        }
    ,   putInCache          : function( id, obj ) {
            this._cache[ id ] = obj;
        }
	
	,	bindCountry			: function( in_countryName ) {
			this._country = getObj( in_countryName );
		}
	
	,	bindProvince		: function( in_provinceName ) {
			this._province = getObj( in_provinceName );
		}

	,	attach				: function( ) {
			if( this.isAvailable( ) ) {
				this._country.onchange = chainHandlers(
					  this._country.onchange
					, bind( this, 'ajax' )
				);
				return this;
			}
			else {
			    return false;
			}
		}

	,	ajax				: function( ) {
			if( this.isAvailable( ) ) {
			    this._province.disabled = true;
			    
				provinceValue = '';
				if( this._province.type == 'text' ) {
					provinceValue = this._province.value;
				}
				
				uri =
				     'resource/splay/'
                    +'?terra'
                    +'&country_value='  + this._country.value
                    +'&province_value=' + provinceValue
                    +'&province_name='  + this._province.name
                    ;
                
                if( this.isCached( uri ) ) {
                    this.assemble( this.getFromCache( uri ), uri );
                    return;
                }
				
				XMLReq.pullURI(
					  auri( uri )
					, bind(
					      this
					    , function( xml ) {
                            this.putInCache( uri, xml );
                            this.assemble( xml, uri );
                          }
                      )
				);
			}
		}
	
	,	assemble			: function( xml, uri ) {
			// May present a problem if the visage markup changes
			this._province.parentNode.className =
			    this._province.parentNode.className.replace(
			          this._province.type
			        , xml.className
			    );
			
			this._province.parentNode.innerHTML = xml.provinceMenu;
			this.bindProvince( this._province.name );
			
			this._province.disabled = false;
		}
} );

function bootstrap_splay( ) {
	var box		= getObj( 'sp_p_same_as_m' );
	var fields	= [ 'address', 'address2', 'city', 'state', 'postal' ];
	if( box ) {
		box.onclick = bind(
			  box
			, function ( ) {
				if( this.checked ) {
					var field;
					for( var i = 0; i < fields.length; i++ ) {
						field = 'sp_p'+fields[ i ];
						$( field ).value = $( 'sp_m'+fields[ i ] ).value;
						$( field ).highlight( );
					}
				}
			  }
		);
		
		for( var i = 0; i < fields.length; i++ ) {
			field = 'sp_p'+fields[ i ];
			$( field ).addEvent( 'change', function( ) {
				box.checked = false;
			} );
		}
	}
}

document.onload = chainHandlers( document.onload, bootstrap_splay );

var SplayTabCollections = { };
function getSplayTabCollection( key ) {
	if( SplayTabCollections[ key ] ) {
		return SplayTabCollections[ key ];
	}
	return new SplayTabCollection( key );
}

var SplayTabCollection = new Class( {
	  tabView: null
	, labelsView: null
	
	, tabs: []
	, labels: []
	, labelsWidth: 0
	, current: null
	
	, initialize: function( id ) {
		this.tabView = new Element(
			'div', { 'id': id, 'class': 'viTabGroup' }
		);
		this.tabView.grab(
			this.labelsView = new Element( 'ul', { 'class': 'labels' } )
		);
		
		SplayTabCollections[ id ] = this;
	  }
	
	, attach: function( where, how ) {
		this.tabView.inject( where, how );
	  }
	
	, add: function( splayTab ) {
		if( !this.current ) {
			this.current = splayTab;
			splayTab.focus( );
			this.labelsView.addClass( 'isFirstActive' );
			this.labelsView.addClass( 'isLastActive' );
		}
		else {
			this.getFirst( ).tab.removeClass( 'isLastActive' );
		}
		
		this.tabView.grab( splayTab.tab );
		
		this.tabs.push( splayTab );
		this.labels.push( splayTab.rawLabel );
		
		this.labelsView.grab( splayTab.label );
		
		splayTab.label.onclick = bind(
			  this
			, function( newTab ) {
				this.toggle( this.current, newTab );
			  }
			, splayTab
		);
		
		splayTab.label.onmouseover
			= splayTab.label.onmouseout
			= bind(
			  this
			, function( tab ) {
				this.hover( tab )
			  }
			, splayTab
		);
		
		// Account for ie6 including padding in a box's width
		if( !isIE6( ) ) {
			// Set width explicitly (ie compat)
			var padding =
				  parseInt( splayTab.label.getStyle( 'padding-left' ) )
				+ parseInt( splayTab.label.getStyle( 'padding-right' ) );
			
			splayTab.label.setStyle(
				 'width'
				, splayTab.label.getSize( ).x - padding
			);
		}
		
		this.labelsWidth +=
			  splayTab.label.getSize( ).x
			+ parseInt( splayTab.label.getStyle( 'margin-left' ) );
		
		// Here too
		if( isIE6( ) ) {
			this.labelsWidth +=
				  parseInt( this.labelsView.getStyle( 'padding-left' ) )
				+ parseInt( this.labelsView.getStyle( 'padding-right' ) );
		}
		
		this.labelsView.setStyle( 'width', this.labelsWidth );
	  }
	
	, has: function( ) {
		return !!this.getFirst( );
	  }
	
	, getTab: function( label ) {
		return this.tabs[ this.labels.indexOf( label ) ];
	  }
	
	, getFirst: function( ) {
		for( var i = 0; i < this.tabs.length; i++ ) {
			if( this.tabs[ i ].isVisible( ) ) {
				return this.tabs[ i ];
			}
		}
	  }
	
	, getNext: function( tab ) {
		for( var i = this.tabs.indexOf( tab ); i < this.tabs.length; i++ ) {
			if( this.tabs[ i ].isVisible( ) ) {
				return this.tabs[ i ];
			}
		}
	  }
	
	, getPrev: function( tab ) {
		for( var i = this.tabs.indexOf( tab ); i >= 0; i-- ) {
			if( this.tabs[ i ].isVisible( ) ) {
				return this.tabs[ i ];
			}
		}
	  }
	
	, getLast: function( ) {
		for( var i = this.tabs.length - 1; i >= 0; i-- ) {
			if( this.tabs[ i ].isVisible( ) ) {
				return this.tabs[ i ];
			}
		}
	  }
	
	, show: function( label ) {
		this.showLabel( label );
		this.showTab( this.getTab( label ) );
	  }
	
	, hide: function( label ) {
		this.hideLabel( label );
		this.hideTab( this.getTab( label ) );
	  }
	
	, showLabel: function( label ) {
		var tab = this.getTab( label );
		tab.showLabel( );
		
		if( !this.current ) {
			this.showTab( tab );
		}
	  }
	
	, hideLabel: function( label ) {
		var tab = this.getTab( label );
		tab.hideLabel( );
		
		if( this.has( ) ) {
			if( this.current == tab ) {
				var nextTab = this.getNext( tab );
				
				if( !nextTab ) {
					nextTab = this.getPrev( tab );
				}
				
				this.toggle( tab, nextTab );
			}
		}
		else {
			this.hideTab( tab );
		}
		
		// Do this last so this tab is visible for comparisons in above if/else
	  }
	
	, hover: function( tab ) {
		if( tab == this.current ) {
			return;
		}
		
		if( tab == this.getFirst( ) ) {
			this.labelsView.toggleClass( 'isFirstHover' );
		}
		
		if( tab == this.getLast( ) ) {
			this.labelsView.toggleClass( 'isLastHover' );
		}
	  }
	
	, showTab: function( tab ) {
		tab.focus( );
		this.current = tab;
		
		if( tab == this.getFirst( ) ) {
			this.labelsView.addClass( 'isFirstActive' );
			this.labelsView.removeClass( 'isFirstHover' );
		}
		else {
			this.labelsView.removeClass( 'isFirstActive' );
		}
		
		if( tab == this.getLast( ) ) {
			this.labelsView.addClass( 'isLastActive' );
			this.labelsView.removeClass( 'isLastHover' );
		}
		else {
			this.labelsView.removeClass( 'isLastActive' );
		}
	  }
	
	, hideTab: function( tab ) {
		tab.blur( );
		if( tab == this.current ) {
			this.current = null;
		}
	  }
	
	, toggle: function( oldTab, newTab ) {
		if( newTab != oldTab ) {
			this.hideTab( oldTab );
			this.showTab( newTab );
		}
	  }
} );

var SplayTab = new Class( {
	  label: null
	, tab: null
	
	, rawLabel: ''
	
	, initialize: function( label, tab ) {
		
		// Remove single fieldset children
/*		var fieldsets = tab.getElements( 'fieldset:only-child' );	
		if( fieldsets.length == 1 ) {
			tab.grab( tab.getElement( 'div' ) );
			fieldsets[0].dispose( );
		}*/
		
	  	this.tab = tab;
	  	this.rawLabel = label;
	  	
		// First fades won't work if opacity isn't set inline
		this.tab.fade( 'hide' );
		
		this.label = new Element( 'li', { 'html': label } );
	  }
	
	, hideLabel: function( ) {
		this.label.addClass( 'hide' );
	  }
	
	, showLabel: function( ) {
		this.label.removeClass( 'hide' );
	  }
	
	, isVisible: function( ) {
		return !this.label.hasClass( 'hide' );
	  }
	
	, focus: function( ) {
		this.label.addClass( 'current' );
		this.tab.addClass( 'current' );
		this.tab.fade( 'in' );
	  }
	
	, blur: function( ) {
		this.label.removeClass( 'current' );
		this.tab.fade( 'out' );
		this.tab.removeClass( 'current' );
	  }
} );