GO.calendar.SelectDateDialog = function(config){

	if(!config)
	{
		config = {};
	}

	this.buildForm();

	var focusFirstField = function(){
		this.formPanel.items.items[0].focus();
	};

	config.layout='fit';
	config.modal=false;
	config.border=false;
	config.width=400;
	config.autoHeight=true;
	config.resizable=false;
	config.plain=true;
	config.shadow=false,
	config.title=GO.calendar.lang.copyEvent;
	config.closeAction='hide';
	config.items=this.formPanel;
	config.focus=focusFirstField.createDelegate(this);
	config.buttons=[{
		text:GO.lang['cmdOk'],
		handler: function()
		{
			this.beforeSubmit()
		},
		scope: this
	},{
		text:GO.lang['cmdClose'],
		handler: function()
		{
			this.hide()
		},
		scope: this
	}];

	GO.calendar.SelectDateDialog.superclass.constructor.call(this,config);

	this.addEvents({'updateEvent' : true});
}

Ext.extend(GO.calendar.SelectDateDialog, Ext.Window, {

	isCopy: false,
	event : null,
	repeats : false,
	offset : 0,
	view_id : 0,

	show : function(event, isCopy, repeats, view_id)
	{
		if(!this.rendered)
			this.render(Ext.getBody());

		this.event = event;

		this.isCopy = (isCopy) ? true : false;
		this.repeats = (repeats) ? true : false;
		this.view_id = (view_id) ? view_id : 0;

		var title = (this.isCopy) ? GO.calendar.lang.copyEvent : GO.calendar.lang.moveEvent;
		this.setTitle(title);

		this.datePicker.setValue(this.event.startDate.add(Date.DAY, 1));
		
		this.selectCalendar.setValue(this.event.calendar_id);
		this.selectCalendar.setRemoteText(this.event.calendar_name);

		GO.calendar.SelectDateDialog.superclass.show.call(this);
	},
	beforeSubmit : function()
	{
		this.offset = Math.ceil((this.datePicker.getValue() - this.event.startDate) / (86400000));

		var calendar_id = this.selectCalendar.getValue();
		var update_calendar_id = (calendar_id != this.event.calendar_id) ? calendar_id : 0;
		if(update_calendar_id)
		{
			this.event.calendar_id = this.formPanel.form.baseParams.update_calendar_id = update_calendar_id;
		}
		
		if(this.isCopy)
		{
			this.formPanel.form.baseParams.task = 'copy_event';
			this.formPanel.form.baseParams.event_id = this.event.event_id;
			this.formPanel.form.baseParams.offset = this.offset;
		}else
		{
			this.formPanel.form.baseParams.task = 'update_grid_event';
			this.formPanel.form.baseParams.update_event_id = this.event.event_id;
			this.formPanel.form.baseParams.offsetDays = this.offset;
			
			if(this.event.repeats && !this.repeats)
			{
				this.formPanel.form.baseParams.repeats = true;
				this.formPanel.form.baseParams.createException = true;
				this.formPanel.form.baseParams.exceptionDate = this.event.startDate.format(GO.calendar.daysGrid.dateTimeFormat);
			}
		}
		
		this.formPanel.form.baseParams.view_id = this.view_id;

		this.submitForm(update_calendar_id);
	},
	submitForm : function(update_calendar_id)
	{
		this.formPanel.form.submit(
		{
			waitMsg:GO.lang['waitMsgSave'],			
			success:function(form, action)
			{
				var new_event_id = (action.result.event_id) ? action.result.event_id : 0;

				var is_visible = (action.result.is_visible) ? action.result.is_visible : 0;


				this.fireEvent('updateEvent', this, new_event_id, is_visible);
				
				if(this.isCopy)
				{					
					delete(this.formPanel.form.baseParams.event_id);
					delete(this.formPanel.form.baseParams.offset);
				}else
				{
					delete(this.formPanel.form.baseParams.update_event_id);
					delete(this.formPanel.form.baseParams.offsetDays);

					if(this.formPanel.form.baseParams.repeats)
					{
						delete(this.formPanel.form.baseParams.repeats);
						delete(this.formPanel.form.baseParams.createException);
						delete(this.formPanel.form.baseParams.exceptionDate);
					}
				}
				if(update_calendar_id)
				{
					delete(this.formPanel.form.baseParams.update_calendar_id);					
				}				

				this.hide();
			},
			failure: function(form, action)
			{
				var error = '';
				if(action.failureType=='client')
				{
					error = GO.lang['strErrorsInForm'];
				}else
				{
					error = action.result.feedback;
				}
				if(error)
				{
					Ext.MessageBox.alert(GO.lang['strError'], error);
				}
			},
			scope:this
		});
	},
	buildForm : function ()
	{
		this.datePicker = new Ext.DatePicker({
	    		xtype:'datepicker',
	    		format: GO.settings.date_format,
	    		fieldLabel:GO.lang.strDate
	    	});
		
		this.formPanel = new Ext.form.FormPanel({
			url: GO.settings.modules.calendar.url+'action.php',
			baseParams:{},
			cls:'go-form-panel',
			labelWidth:75,
			waitMsgTarget:true,
			autoHeight:true,
			items:[
			{
				items:this.datePicker,
				width:220,
				style:'margin:auto;'
			},
			new GO.form.HtmlComponent({html:'<br />'}),
			this.selectCalendar = new GO.calendar.SelectCalendar({fieldLabel: GO.calendar.lang.calendar, anchor:'100%'})
			]
		});
	}
});