/******************************************************************************
 ** Internaional addresses
 *****************************************************************************/
(function ($) { // protect the queen!

$.widget( "ui.rubyInternational", {
    _init: function () {
	var self = this;

	this.country    = $( '.rtg-country select', this.element );
	this.usState    = $( '.rtg-us_state', this.element );
	this.caState    = $( '.rtg-ca_state', this.element );
	this.gbState    = $( '.rtg-gb_state', this.element );
	this.otherState = $( '.rtg-other_state', this.element );

	this.country.bind( 'change.rubyInternational', function (ev) {
	    self._change.call( self, ev );
	} );
	
	self._change();

	return;
    }, // _init

    destroy: function () {
	this.country.unbind( '.rubyInternational' );

	this.country    = null;
	this.usState    = null;
	this.caState    = null; 
	this.gbState    = null;
	this.otherState = null;

	$.widget.prototype.destroy.apply( this, arguments );
    }, // destroy

    refresh: function () {
	// Get the chosen country
	var countryCode = this.country.val();

	// Hide all states
	this.usState.hide();
	this.caState.hide();
	this.gbState.hide();
	this.otherState.hide();

	// Show appropriate state
	switch( countryCode ) {
	    case 'US':
  	        this.usState.show();
	        break;
	    case 'CA':
  	        this.caState.show();
	        break;
	    case 'GB':
  	        this.gbState.show();
	        break;
   	    default:
	        this.otherState.show();
	        break;
	}
	
	return;
    }, // refresh

    _change: function () {

	this.refresh();
    } // _change

}); // $.widget

$.extend( $.ui.rubyInternational, {
    version: "1.0",
    defaults: {
	
    }
});

})(jQuery); // function ($)
