/* 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( 'uvInteriorContent' ) ) {
                    transferNode(
                          document.getElementsByTagName( 'head' )[0]
                        , response.getElementsByTagName( 'head' )[0]
                    );
                    
                    transferNode(
                          document.getElementsByTagName( 'body' )[0]
                        , response.getElementsByTagName( 'body' )[0]
                    );
                    
                    return;
                }
                
                transferNode(
                      getObj( 'uvInteriorContent' )
                    , response.getElementById('uvInteriorContent')
                );
                
                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;
		}
} );
