// 
var dtWin;
var csViewport = null;
Ext.BLANK_IMAGE_URL='./ext/resources/images/default/s.gif'; 

Ext.onReady(function(){
	// 
	if (! isOkWebBrowser() ) {
		confirm('Notice: ConfShare version 2 is tested only Safari 3.x and Firefox 3.x. We are afraid that ConfShare could not run properly on other web browsers. Sorry for inconvenience. In our opinion, Safari web browser is better for this service.');
	}

	//
	loadDataSrc();

	csViewport = new Ext.Viewport({
		layout:'border', 
		items:[
			new Ext.BoxComponent({
				region: 'north',	
				el: 'north',
				height: 56
			}),
			{
				region: 'center',	
				layout: 'fit',
				border: false,
				bodyborder: false,
				items: 
					new Ext.Panel({
						layout:'border',
						border: false,
						bodyborder: false,
						items:[{
							region: 'north',
							title: 'Map',
							height: 288,
							collapsible: true,
							contentEl: 'gmap'
						},
						new Ext.TabPanel({
							region:'center',
							deferredRender: false,
							border: false,
							bodyborder: false,
							activeTab: 0,
							defaults: { autoScroll: true },
							items:[
								{
									id: 'ConfTab',
									title: 'Conferences',
									contentEl: 'confCt',
								}, 
								{
									title: 'Submission deadlines',
									contentEl: 'sbdlCt'
								},
								{
									title: 'Let us know',
									listeners: { activate: activateLetUsKnowTab },
									items: [
										{
											border: false,
											html: '<h2 style="text-align:center;">Post Unlisted Conference Information</h2>',
										},
										{
											xtype: 'form',
											labelAlign: 'left', 
											labelWidth: 168, 
											// title: 'Submit Unlisted Conference Information',
											// border: false,
											bodyStyle: 'padding:5px 5px',
											// buttonAlign: 'right',
											defaults: { width: 360, grow: true, growMin: 360 },
											defaultType: 'textfield',
											onSubmit: Ext.emptyFn,

											items: [{
													fieldLabel: 'Your name',
													name: 'name',
													allowBlank: false
												},{
													fieldLabel: 'Your E-mail address',
													name: 'email',
													vtype: 'email',
													allowBlank: true
												},{
													fieldLabel: 'URL of Conference web page',
													name: 'url',
													vtype: 'url',
													emptyText: 'Enter a URL of conference web page',
													allowBlank: false
												}
											],
											buttons: [{
													text: 'Submit',
													handler: function () {
														var tgt=this.findParentByType('form');
														var bf=tgt.getForm()
														if (bf.isValid()) {
															bf.submit({
																url:'/~zetaka/cgi-bin/ConfShare/nagekomi.cgi', 
																method: 'POST', 
																waitMsg: 'Wait a moment...',
																success: function(form, action){
																	Ext.MessageBox.alert('Thanks!!', 'Thank you for your cooperation'); 
																},
																failure: function(form, action){
																	Ext.MessageBox.alert('', 'failed to submit information: '+action.failureType+', '+action.result.toSource()); 
																	
																}
															});
														} else {
															Ext.MessageBox.alert('Error', 'Wrong data! You enter all necessary fields of the form correctly.');
														}
													}
												},{
													text: 'Reset',
													handler: function(){ 
														var tgt=this.findParentByType('form');
														tgt.getForm().reset(); 
													},
											}]
										},
										{
											border: false,
											html: '<div>Your name and a URL of conference Web page are needed to submit a conference information to ConfShare.<br />Enter your E-mail address if you need a reply E-mail whether or not posted information is registered.</div>'
										},
									]
								}
							]
						})
						]
					})
			},
			{
				region: 'west',	
				title: 'Filters',
				layout: 'fit',
				width: 300,
				split: true,
				collapsible: true,
				collapsed: false,
				tbar: [
					{ 
						xtype: 'textfield',
						id: 'kwdVal',
						value: '',
						enableKeyEvents: true,
						emptyText: 'Input keyword',
						width: 188,
						listeners: { keyup: keywordSearchInAttrBrowser },
					},
					{
						xtype:'button' ,
						type:'button' ,
						text:'clear',
						name:'clear',
						handler: clearKeywordSearchInAttrBrowser,
					},
					{
						xtype:'button' ,
						type:'button' ,
						text:'reset',
						name:'reset',
						handler: resetAllFilters2,
					},
				],
				items: 
					new Ext.TabPanel({
						activeTab: 0,
						plain: true,
						// autoScroll: true,
						defaults: { autoScroll: true },
						items:[
						/*{
								title: 'Periods',
								contentEl: 'pdCt'
							},*/
							{
								title: 'Shortnames',
								contentEl: 'snFt'
							},{
								title: 'Locations',
								contentEl: 'ccCt',
							},{
								title: 'Keywords',
								contentEl: 'kwCt',
							}]
					})
			},
			{
				region: 'east',	
				title: 'Date',
				layout: 'fit',
				width: 120,
				split: true,
				autoScroll: true,
				collapsible: true,
				contentEl: 'dtBx'
			},
			{
				region: 'south',	
				contentEl: 'south',
				height: 20,
			}
		]
	});

	function activateLetUsKnowTab(panel){
		// console.log('Activate Let us Know tab');
	}

	csViewport.syncSize();

	dtWin = new Ext.Window({
		modal: true,
		plain: true,
		title: 'Conference detail',
		minWidth: 480,
		// autoWidth: true,
		// autoHeight: true,
		autoScroll: true,
		closeAction: 'hide',
		defaultButton: 0,
		contentEl: 'cfDetail',
		listeners: { 
			show: updateWinSizeAndPos,
			hide: hideConfWin
		},
		buttons: [{
			text: 'close',
			handler: function(){
				dtWin.hide();
			}
		}]
	});

	// setTimeout( function(){ csViewport.items.itemAt(2).collapse(true); }, 3000);
	// setTimeout( function(){ csViewport.items.itemAt(3).collapse(true); }, 3000);
	setTimeout( toggleBothSidePanes, 3000);
});

// 
function toggleBothSidePanes(){
	var rpnl=csViewport.items.itemAt(2);
	var lpnl=csViewport.items.itemAt(3);

	if ( rpnl.isVisible() ) {
		rpnl.collapse(true);
		lpnl.collapse(true);
	} else {
		rpnl.expand(true);
		lpnl.expand(true);
	}

	// console.log( csViewport.items.itemAt(3).getXType() );
}

// ---
function updateWinSizeAndPos(evt) {
	console.log('test object is '+this.getXTypes());
	this.doLayout();
	// this.syncSize();
	this.setWidth(window.innerWidth * 0.90);
	console.log('InnerHeight: '+this.getInnerHeight());
	if ((this.getFrameHeight()+this.getInnerHeight()) > (window.innerHeight)) {
		this.setHeight(window.innerHeight * 0.95);
		console.log('Change height: '+this.getInnerHeight());
	}
	console.log('InnerHeight 2: '+this.getInnerHeight());
}

// ---
function hideConfWin(p) {
	// re-initialize(clear) target dom node
	setTimeout(function(){
		var tg=Ext.get('cfInnerDetail');
		tg.dom.innerHTML='';
	}, 0);
}

// ---
function openDetailWin(evt) {
	var aryIdx=findArrayIndexByConfID(this.cfID); 

	var tg=Ext.get('cfInnerDetail');
	var x = setupConferenceDetailInfo(axObj.cfs[aryIdx]);
	tg.dom.appendChild( x );

	Ext.get('wuhArea').load({
		url: 'http://www.netaro.info/~zetaka/cgi-bin/ConfShare/uhList.cgi',
		params: 'id='+this.cfID,
		method: 'GET',
		scripts: false,
		discardUrl: true,
		timeout: 12,
		callback: displayWUH,
	});

	dtWin.show();
	dtWin.center();
	dtWin.focus();

	showPos('vmap', axObj.cfs[aryIdx].kdo, axObj.cfs[aryIdx].ido, 5);
}

// ----
function isOkWebBrowser() {
	if ( Ext.isSafari ) { return true; }
	if ( Ext.isGecko )  { return true; }
	if ( Ext.isChrome )  { return true; }
	return false;
}

//
// ends here
//

