!function ($) {
	"use strict";

	$.WaAdriverClasses = {
		INITIALIZED: 'wa-adriver_initialized'
	};

	var WaAdriver = function (elem, options) {
		
	};

	$.extend(WaAdriver.prototype, {
		_newAdriver: function(target, options) {
			if (!this._isInitialized(target)) {
				var settings = $.extend(
					$(target).data('ad'),
					options || {});

				++this.uuid;

				target.id = 'wa_adriver_' + this.uuid;

				if (settings.sz == undefined) settings.sz = this._getSiteZone();
				if (settings.keyword == undefined) settings.keyword = window.ar_bn1;
				$(target).removeAttr('data-ad');
				this._markAsInitialized(target);
				new adriver(target.id, settings);
			}
		},

		_getSiteZone: function() {
			var l = window.location, a = l.hostname.split('.');
			a.splice(a.length-2, 2);

			return (a.length ? '/' + a.join('/') : '') + l.pathname + escape(l.search);
		},

		_isInitialized: function(target) {
			return $(target).is('.' + $.WaAdriverClasses.INITIALIZED);
		},

		_markAsInitialized: function(target) {
			return $(target).addClass($.WaAdriverClasses.INITIALIZED);
		}
	});

	$.fn.waAdriver = function (options) {
		return this.each(function(){
			$.waadriver._newAdriver(this, options);
		});
	}

	$.waadriver = new WaAdriver(); // singleton instance
	$.waadriver.uuid = (new Date()).getTime();

	
}(window.jQuery);