﻿/*
Requires: Prototype library
*/

var MapItemDialog = Class.create(
{
	initialize: function(container, options)
	{
		this.container = $(container);

		if (!options)
			options = {};

		this.objectName = 'mapItemDialog';
		if (options.objectName)
			this.objectName = options.objectName;

		this.basePath = '/';
		if (options.basePath)
			this.basePath = options.basePath;

		if (options.onUpdate)
			this.onUpdate = options.onUpdate;

		if (options.onMerge)
			this.onMerge = options.onMerge;

		this.options = options;

		this.dialog = new Dialog(
		{
			dialogElement: this.container.id,
			cancelButtons: $$('#' + this.container.id + ' .close'),
			minTop: 20,
			overlayCancel: false,
			// fixed: false,
			fade: false
		});
	},

	_open: function(args)
	{
		var fr = $$('#' + this.container.id + ' iframe').first();
		if (fr)
		{
			fr.style.visibility = 'hidden';
			fr.src = "about:blank";

			this.dialog.show();

			var src = this.basePath + 'MapItemEdit.aspx?parentObject=' + this.objectName + args;
			setTimeout(function()
			{
				fr.src = src;
			}, 100);
			setTimeout(function()
			{
				fr.style.visibility = 'visible';
			}, 200);
		}
	},

	show: function(id, userid, ratingid, admin)
	{
		if (id)
		{
			var args = '&id=' + id;
			if (userid)
				args += '&userid=' + userid;
			if(ratingid)
				args += '&ratingid=' + ratingid;
			if(admin)
				args += '&admin=' + admin;
					
			this._open(args);
		}
	},

	add: function(opts)
	{
		var args = '&ownerClass=' + escape(opts.ownerClass) + '&ownerId=' + escape(opts.ownerId);
		if (opts.ownerUrl)
			args += '&ownerUrl=' + encodeURI(opts.ownerUrl);
		if (opts.language)
			args += '&language=' + escape(opts.language);
		if (opts.category)
			args += '&category=' + escape(opts.category);
		if (opts.cityCode)
			args += '&cityCode=' + escape(opts.cityCode);
		if (typeof opts.lat == 'number' && typeof opts.lng == 'number')
			args += '&lat=' + opts.lat + '&lng=' + opts.lng;
		if (typeof opts.address == 'string')
			args += '&address=' + encodeURI(opts.address);
		if (typeof opts.name == 'string')
			args += '&name=' + encodeURI(opts.name);
		this._open(args);
	},

	close: function()
	{
		this.dialog.hide();
	},

	update: function(mapitemid)
	{
		this.dialog.hide();
		if (this.onUpdate)
			this.onUpdate(mapitemid);
	},

	closeAndOpenItem: function(id)
	{
		this.dialog.hide();
		openMapItem(id)
	},

	merge: function()
	{
		if (this.onMerge)
			this.onMerge();
	}
});