(function ($) {
    $.changecountry = function (el, options) {
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;

        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;

        // Add a reverse reference to the DOM object
        base.$el.data("changecountry", base);	

        base.init = function () {
            base.options = $.extend({}, $.changecountry.defaultOptions, options);
            
	    // Put your initialization code here
	    var currentcountry = base.$el.find(base.options.countryField).data();
	    var currentstate = base.$el.find(base.options.stateField).data();
	    var zipcodeHasValue = false;
	    if(base.$el.find(base.options.zipcodeField).val() == '!ZIPCODE!'){
		zipcodeHasValue = false;
	    } else if(base.$el.find(base.options.zipcodeField).val() != ''){
	    	zipcodeHasValue = true;
	    }
	    setTimeout(function(){ base.setCountry(currentcountry.value) }, 1);
            base.$el.find(base.options.countryField).live('change', function () {
                if(!zipcodeHasValue){
			base.clearFields();
		}
                base.setZipcodeClass($(this).val());
                base.loadCountry(function(){
			base.setState(currentstate.value);
		});
		zipcodeHasValue = false;
            });
        };

        // Sample Function, Uncomment to use
        // base.functionName = function(paramaters){
        //
        // };
	base.setCountry = function(country){
	    if(country == '' || country == '!COUNTRY!'){
		base.$el.find(base.options.countryField).val('US').trigger('change');
  	    } else {
		base.$el.find(base.options.countryField).val(country).trigger('change');
	    }
	}
	base.setState = function(state){
	    if(state != '!STATE!' || state != ''){
		base.$el.find(base.options.stateField).val(state);
  	    }
	}
        base.clearFields = function () {
           base.$el.find(base.options.zipcodeField).val('');
        }
        base.setZipcodeClass = function (country) {
            if (country === "US") {
                base.$el.find(base.options.zipcodeField).removeClass('postalcode').addClass('zipcode');
            } else if (country === 'AR' || country === 'BM' || country === 'BN' || country === 'CA' || country === 'MT' || country === 'NL' || country === 'GB' || country === 'VE') {
                base.$el.find(base.options.zipcodeField).removeClass('zipcode').addClass('postalcode');
            }
        }
        base.loadCountry = function(callback) {
            $.getJSON('Fetch.aspx?CN=2EF841540163&COUNTRYCODE=' + base.$el.find(base.options.countryField).val(), function (data) {
                base.$el.find(base.options.stateLabel).text(data.STATELABEL);
                base.$el.find(base.options.stateField).html(data.STATEOPTIONLIST2);
                base.$el.find(base.options.zipcodeLabel).text(data.ZIPCODELABEL);
		callback();
            });
        }

        // Run initializer
        base.init();
    };

    $.changecountry.defaultOptions = {
        countryField: '#tbCOUNTRY',
        stateField: '#tbSTATE',
        stateLabel: 'label[for=tbSTATE]',
        zipcodeField: "#tbZIPCODE",
        zipcodeLabel: 'label[for=tbZIPCODE]'
    };

    $.fn.changecountry = function (options) {
        return this.each(function () {
            (new $.changecountry(this, options));

            // HAVE YOUR PLUGIN DO STUFF HERE
            // END DOING STUFF
        });
    };

})(jQuery);
