(function(){

	if (typeof(NOTIFY) == 'undefined') { NOTIFY = {}; }
	if (typeof(NOTIFY.affiliate) == 'undefined') { NOTIFY.affiliate = {}; }

	NOTIFY.affiliate.Button = function() { 
            this.init(); 
        };

	NOTIFY.affiliate.Button.prototype = {
		sources: [],
		return_to: null,
		id: null,
                _affiliate: null,
		_image: 'nm_2_100x35',
		init: function() {
		},
		writeHTML: function() {
			document.write( this.getHTML() );
		},
		getHTML: function() {
			var url;
                        var euc = encodeURIComponent;
			if (this.sources.length > 0) {
                            url = this.sources.join(',');
                        } else {
                            url = window.location.href;
                        }
                        var aid = this.getAffiliate();
			var args = "url=" + euc(url);

			if (this.return_to) { args += '&amp;return_to=' + euc( this.return_to ); }
                        if (aid) { args += '&amp;aid=' + euc(aid); }
			return '<a href="http://notify.me/affiliate/source/referer?'+args+'"><img src="'+this.getImageURL()+'" alt="notify.me" /></a>';
		},
		addSourceURL: function(url) { this.sources.push(url); },
		setImage: function(img) { this._image = img; },
		getImageURL: function() {
				return 'http://static.notify.me/images/badges/'+this._image+'.png';
		},
		returnToURL: function(url) { this.return_to = url; },
                setAffiliate: function(a) { this._affiliate = a; },
                getAffiliate: function() { return this._affiliate; }
	};
})();
