Ext.BLANK_IMAGE_URL = "/medias/img/pix.gif";

//var articles = function() {
//    return {
//        init: function() {
Ext.onReady (function () {
	Ext.QuickTips.init();

	Ext.example = function(){
		var msgCt;

		function createBox(t, s){
			return ['<div class="msg">',
					'<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
					'<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', t, '</h3>', s, '</div></div></div>',
					'<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
					'</div>'].join('');
		}
		return {
			msg : function(title, format){
				if(!msgCt){
					msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
				}
				msgCt.alignTo(document, 'c');
				var s = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
				var m = Ext.DomHelper.append(msgCt, {html:createBox(title, s)}, true);
				m.slideIn('t').pause(1).ghost("t", {remove:true});
			},

			init : function(){
				var t = Ext.get('exttheme');
				if(!t){ // run locally?
					return;
				}
				var theme = Cookies.get('exttheme') || 'aero';
				if(theme){
					t.dom.value = theme;
					Ext.getBody().addClass('x-'+theme);
				}
				t.on('change', function(){
					Cookies.set('exttheme', t.getValue());
					setTimeout(function(){
						window.location.reload();
					}, 250);
				});

				var lb = Ext.get('lib-bar');
				if(lb){
					lb.show();
				}
			}
		};
	}();

	var store = new Ext.data.JsonStore ({
		/*url: '/main/recherche_article_resultats.process.php?strQueryLucene=' + (typeof(strQueryLucene) != "undefined" ? strQueryLucene : ""),*/
		url: '/main/vente_agence_resultats.process.php?strQueryLucene=',
		totalProperty: 'totals',
		root: 'articles',
		fields:['line','marque','code_modele', 'model', 'year', 'km','immat', 'color', 'center', {name:'price',name:'price',type: 'int'}],
		remoteSort: true
	});
	store.on ('loadexception', function () {
		if (store.getCount() == 0)
			Ext.example.msg ('No result', '');
	});

	var columns = [ { header: 'Marca', sortable: false, dataIndex: 'marque', width: 200, resizable: false, css: 'white-space:normal;' },
		{ header: 'Modello', sortable: false, dataIndex: 'model', width: 180, resizable: false, css: 'white-space:normal;' },
		{ header: 'Anno', sortable: false, dataIndex: 'year', width: 80, resizable: false },
		{ header: 'Chilometraggio', sortable: false, dataIndex: 'km', width: 80, resizable: false, renderer: formatKilo },
		{ header: 'Targa', sortable: false, dataIndex: 'immat', width: 80, resizable: false },
		{ header: 'Colore', sortable: false, dataIndex: 'color', width: 70, resizable: false },
		{ header: 'Agenzia', sortable: false, dataIndex: 'center', width: 110, resizable: false },
		{ header: 'Prezzo', sortable: false, dataIndex: 'price', width: 76, resizable: false, renderer: formatPrice }
	];

	var articleGrid = new Ext.grid.GridPanel({
		store: store,
		el: 'resultPanel',
		width: 890,
		height: 470,
		loadMask: true,
		stripeRows:false,
		frame: true,
		columns:columns,
		sm: new Ext.grid.RowSelectionModel(),
		bbar: new Ext.PagingToolbar({
			pageSize: 20,
			store: store,
			emptyMsg: 'There\'s no result to your search'
		})
	});
	articleGrid.on('rowclick', viewArticle );
	store.load.defer (20, store);

	function formatPrice (value, p, record) {
		return value + " &euro;";
	}

	function formatKilo (value, p, record) {
		return value + " km";
	}

	var textField = new Ext.form.TextField ({
		id: 'textFieldID',
		name: 'name',
		width: 100,
		allowBlank: false
	});

	function viewArticle (grid, row, event) {
		var store = grid.getStore();
		var record = store.getAt( row );

		if (record.get('center') != "")
			new Ext.Window({
				title    : 'Hertz Rent2Buy',
				closable : true,
				width    : 550,
				height   : 450,
				border : false,
				plain    : true,
				layout   : 'fit',
				html: '<iframe src="../main/fiche_detail.php?line=' + record.get('line') + '" style="overflow: hidden;width: 550px; height: 450px; border: 0;padding:0;margin:0;"/>'
			}).show();
	}

	function getArticle (id) {
		Ext.Ajax.request({
			url: '../main/fiche_detail.php?line='+ id,
			method: 'GET',
			success: function(objServerResponse) {
				viewArticleDetail(objServerResponse.responseText);
			}
		});
	}

	function viewArticleDetail(textHtml){
		var win;
		var win = new Ext.Window({
			title    : 'Hertz Rent2Buy',
			closable : true,
			width    : 550,
			height   : 450,
			border : false,
			plain    : true,
			layout   : 'fit',
			items:[{
				html: textHtml
			}]
		});
		win.show();
	}

	function renderTitle (value, p, record) {
		return String.format('<p style="width: 500px; font-weight: bold;cursor:hand">{0}</p><p style="cursor:hand">{1}</p>', value, record.data['description'] );
	}

	articleGrid.render();

	var view = articleGrid.getView();
	view.showPreview = true;
//	view.refresh();
//	}
//    };
});
//Ext.EventManager.onDocumentReady( articles.init, articles, true );

