(function($) {
	var _loc = document.location;
	var PROPNAME = 'safeview';
	var Safeview = {
		_build: function(attrs, params, options) {
			var ret = '<obj' + 'ect';
			$.each(attrs, function(name, value) { ret += ' ' + name + '="' + value + '"'; });
			ret += '>\n';
			var np = options.noplugin;
			if (np) {
				if (typeof (np) == "string")
					ret += np;
				else if ($.isFunction(np))
					ret += np();
				else if ($.isFunction(np.html))
					ret += np.html();
			}
			$.each(params, function(name, value) { ret += '<param name="' + name + '" value="' + value + '">\n'; });
			return ret + '</obj' + 'ect>';
		},
		_eachObj: function(jq, fn) {
			$(jq).each(function() {
				var obj = $(this).data(PROPNAME).object;
				if (obj)
					fn.apply(obj, arguments);
			});
		},
		_eachElem: function(jq, fn) {
			$(jq).each(function() {
				var obj = $(this).data(PROPNAME).elem;
				fn.apply(obj, arguments);
			});
		},
		create: function(options) {
			if (this.length) {
				var _opts = $.extend(true, {}, $.fn.safeview.defaults, options);
				if (!_opts.params.Server)
					_opts.params.Server = _opts.protocol + "//" + _loc.host;
				if (!_opts.params.Base)
					_opts.params.Base = _loc.protocol + "//" + _loc.host + _loc.pathname.replace(/[^\/]+$/, "");
				this.each(function() {
					var s = Safeview._build(_opts.attrs, _opts.params, _opts);
					$(this).html(s);
					var o = this.firstChild;
					$(this).data(PROPNAME, { elem: o, object: ("object" in o) ? o.object : o });
				});
			}
			return this;
		},
		obj: function() {
			return this.data(PROPNAME).object;
		},
		elem: function() {
			return this.data(PROPNAME).elem;
		},
		menu: function(arMenuItems, options) {
			var defaults = {
				label: function(item) { return item.label; },
				action: function(item) { return item.action; }
			};
			var opts = $.extend({}, defaults, options);
			var ar = $.map(arMenuItems, function(item, index) {
				return '<menuitem label="' + opts.label(item, index)
                        + '" action="' + opts.action(item, index) + '"/>';
			});
			var menuXml = '<menu>' + ar.join('') + '</menu>';
			Safeview._eachObj(this, function() { this.SetContextMenu(menuXml); });
			return this;
		},
		load: function() {
			Safeview._eachObj(this, function() { this.LoadDocument(); });
		},
		bind: function(eventType, handler) {
			Safeview._eachElem(this, function() {
				this.attachEvent(eventType, handler);
			});
			return this;
		},
		winlist: function(data) {
			Safeview._eachObj(this, function() { this.SetList(data); });
			return this;
		}
	};
	$.fn.safeview = function() {
		var args = Array.prototype.slice.call(arguments);
		var fname = args.shift();
		return Safeview[fname].apply(this, args);
	};
	$.fn.safeview.defaults = {
		protocol: _loc.protocol,
		attrs: {
			width: "100%",
			height: "100%",
			classid: "CLSID:3743E8B0-BE34-4652-9F11-7C4EB22F39B9",
			codebase: "NetisUtils/install/safeview.cab"
			//type: "application/x-safeview"	// causes error in IE
		},
		params: {
			ViewType: "Document",
			LoadNow: "Yes"
		}
	};

})(jQuery);
