// Initilaze the eventhub
var eventHub = new Roo.data.EventHub();
var urlHandler = new Roo.data.UrlHandler();

// Configure ext
Ext.BLANK_IMAGE_URL = './extjs/resources/images/default/s.gif';
Ext.useShims = true;

/**
 * The general PlansStore that contains the plans that are being searched.
 *
 * TODO create an object that contains all stores and assign al stores to it
 */
var plansStore = new Ext.data.Store({
	remoteSort: false
	,proxy: new Ext.ux.data.DwrProxy({
		api: {read: SearchService.findPlannenBySearchCriteria}
		,listeners: {
			'beforeload': function(dataProxy, params) {
				if (params.query) {
					if(params.query.gemeenteCbsCode && params.query.gemeenteCbsCode.length==3) {
						params.query.gemeenteCbsCode = '0' + params.query.gemeenteCbsCode;
					}
					if(params.query.planType) {
						params.query.planType = {type: params.query.planType, titel: params.query.planType};
					}
					if(params.query.planStatus) {
						params.query.planStatus = {naam: params.query.planStatus, titel: params.query.planStatus};
					}

					// Set the current tab filter
					var filter = (params.query.tabFilter=="NONE" ? null : Roo.currentTabFilter.name);
					params[dataProxy.loadArgsKey] = [params.query, filter];
				}
			}
			,'load': function(dataProxy, data, arg) { }
		}
	})
	,reader: new Ext.data.JsonReader({}, Ext.data.Record.create([
		{name: 'id', mapping:'identificatie' }
		,{name: 'name', mapping:'naam' }
		,{name: 'type', mapping: 'typePlan'}
		,{name: 'datum', mapping: 'datum'}
		,{name: 'box', mapping: 'boundingBox'}
	]))
	,listeners: {
		scope: this
		,'datachanged' : function(store) {
			if(store.getCount() == 1 && store.getAt(0).json.planTooMuch == true) {
				store.removeAll();
				Ext.Msg.alert(messages.msg.titleWarning(), messages.application.msg.toMuchPlans());
				return false;
			}
		}
		,'load' : function(store, records, options) {
			store.sort('datum', 'DESC');
		}
	}
});

eventHub.on({
	scope: this
	,'changedFilter': function(parameters) {
		console.debug('Application.on(changedFilter): clear search results');
		if (plansStore && plansStore.getTotalCount() > 0) {
			plansStore.removeAll();
		}
	}
});

Ext.onReady(function() {
	// Initialize ExtJS Quicktips for tabtips
	Ext.QuickTips.init();

	var viewport = new Ext.Viewport({
		layout:'border'
		,border: false
		,defaults: {
			border:false
		}
		,items:[{
			region:'north'
			,id:'headerBar'
			,layout: 'row-fit'
			,height: 90
			,items: [{
				id: 'headerImg'
				,xtype: 'box'
				,el: 'header'
				,height: 50
			},{
				xtype: 'headerTabs'
			}]
		},{
			region:'center'
			,id:'main'
			,layout: 'fit'
			,border: false
			,items: [{xtype: 'applicationPanel'}]
		}]
	});

	eventHub.initialize();

	if (urlHandler.parametersPresent()) {
		Ext.getCmp('headerTabs').activate('juridisch');
	}

	// remove loading mask
	viewport.doLayout();
	setTimeout(function(){
        Ext.get('loading').remove();
        Ext.get('loading-mask').fadeOut({remove:true});
    }, 250);

});


/**
 * Callback after initialization of Flamingo
 */

function onFlamingoInitialized() {
	Roo.flamingoInitialized = true;
	console.log('Called: onRoo.flamingoInitialized');
	Ext.getCmp('headerTabs').fireEvent('changedFilter', {'filter': Roo.currentTabFilter.name, 'lastFilterName':'NONE', 'panelTitle': Roo.currentTabTitle});
	urlHandler.initialize();
}

function onInit() {

}

function onIdentify(identifyResult) {
	Ext.getCmp('infoPanel').fireEvent('receivedIdentifyResult', identifyResult);
}

function onMaptipFeatures(maptipResult) {
	Ext.getCmp('planGridPanel').highlightEntries(maptipResult);
}

function openFilterWindow() {
	if (Roo.currentTabFilter.name === "JURIDISCH" || Roo.currentTabFilter.name === "ACTUEEL") {
		var win = new Roo.gui.GebruiksmogelijkhedenWindow();
		win.show();
	}
	else {
		return false;
	}
}
function closeFilterWindow() {
	if(Ext.fly('gebruiksmogelijkhedenWindow')) {
		Ext.getCmp('gebruiksmogelijkhedenWindow').close();
	}
}
function openPlanTreeWindow() {
	if ((Roo.currentTabFilter.name === "VISIE" || Roo.currentTabFilter.name === "INSTRUCTIE") && Roo.isVormVrijPlan(Roo.currentPlan) ) {
		console.debug('opening plantreeWindow');
		var win = new Roo.gui.PlantreeWindow({plan:Roo.currentPlan});
		win.show();
	} else {
		return false;
	}
}
function closePlanTreeWindow() {
	console.log('in closePlanTreeWindow()');

	if(Ext.fly('plantreeWindow')) {
		Ext.getCmp('plantreeWindow').close();
	}
}
