/**
 * @author mcmichae
 */
	var ourSearchForm = null;
	var ourResultsGrid = null;
	
	
 	function displaySearchForm()
	{
		Ext.BLANK_IMAGE_URL = '../ext-3.0.3/resources/images/default/s.gif';
		Ext.QuickTips.init();
    	//Ext.form.Field.prototype.msgTarget = 'side';
    	var bd = Ext.get('formSearchDiv');

		if (ourSearchForm == null) 
		{
			ourSearchForm = new Ext.FormPanel(
			{
				labelWidth: 150, // label settings here cascade unless overridden
				//draggable:false,
				id: 'ourSearchForm',
				
				
				//url:'foo.php',
				//method: 'POST',
				//monitorValid: true,
				frame: true,
				buttonAlign: 'left',
				//bodyStyle: 'padding:5px 5px 0',
				
				defaults: {
					width: 250
				},
				defaultType: 'textfield',
				
				items: [{
					fieldLabel: ' <strong> Certification Search </strong> ',
					name: 'contentSearch'
					//,allowBlank: false
				
				}],
				
				buttons: [{
					text: 'Search',
					formBind: true,
					handler: function()
							{
								//Ext.Msg.alert('Values : ' + Ext.getCmp('ourContactForm').getForm().getValues().first);
								//alert('---' + Ext.getCmp('ourContactForm').getForm().getValues().requestComment);
								xajax_CertificationSearch.searchDatabase(Ext.getCmp('ourSearchForm').getForm().getValues());
							}

				}]
			});
			

			
			
			ourSearchForm.render(document.getElementById('formSearchDiv'));
		}
	}
	function displayGridResults(ourData)
	{
		//alert("This is ourData : " + ourData[0].ContentTitle);
		var myData = new Array();
		for(i=0;i < ourData.length;i++)
		{
			myData[i] = new Array();
			myData[i][0] = parseInt(ourData[i].Content_id + '');
			
			if(ourData[i].ContentTitle == null)
				myData[i][1] = '';
			else
				myData[i][1] = ourData[i].ContentTitle;
				
			if(ourData[i].Content == null)
				myData[i][2] = '';
			else
				myData[i][2] = ourData[i].Content;
			
			if(ourData[i].ContentType == null)
				myData[i][3] = '';
			else
				myData[i][3] = ourData[i].ContentType;
				
			if(ourData[i].file00 == null)
				myData[i][4] = '';
			else
				myData[i][4] = ourData[i].file00;	
			
			if(ourData[i].active == null)
				myData[i][5] = 'INACTIVE';
			else
			{
				if(ourData[i].active == 1)
					myData[i][5] = 'ACTIVE';
				else
					myData[i][5] = 'INACTIVE';
					
			}
		}
		//alert("Parsed data Content_id: " + myData[0][0]);
		//alert("Parsed data Content Title: " + myData[0][1]);
		//alert("Parsed data Content: " + myData[0][2]);
		//alert("Parsed data ContentType: " + myData[0][3]);
		//alert("Parsed data file00: " + myData[0][4]);
		if(ourResultsGrid != null)
		{
			Ext.destroy(ourResultsGrid);
			ourResultGrid = null;
		}
		
		
			function renderPDFLink(value, p, record)
			{
				if(value == null || value.replace(' ','') == '')
					return String.format('<a href="https://secure.edapia.com/docs/Certifications/{1}/{0}" target="_blank"> </a>',value,record.data.Content_id);
				else
					return String.format('<a href="https://secure.edapia.com/docs/Certifications/{1}/{0}" target="_blank"> <img src="/images/pdf.gif"> </a>',value,record.data.Content_id);
				
			}
			
			function renderHTMLContent(value, p, record)
			{
				return String.format('<div style="height:49px;overflow:hidden;" > {0} <BR><BR><BR> </div>',value);
					
			}

			    var store = new Ext.data.Store({
        			proxy: new Ext.ux.data.PagingMemoryProxy(myData),
        			remoteSort:true,
        			sortInfo: {field:'Content_id', direction:'DESC'},
        			reader: new Ext.data.ArrayReader({
            		fields: [
								{name: 'Content_id',type:'int'},
								{name: 'ContentTitle', type: 'string'},
               					{name: 'Content', type: 'string'},
               					{name: 'ContentType', type: 'string'},
               					{name: 'file00', type:'string'},
               					{name: 'active', type:'string'}
               					
								//{name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}
            				]
        			})
    			});
			
			    Ext.ToolTip.prototype.onTargetOver =
			    	Ext.ToolTip.prototype.onTargetOver.createInterceptor(function(e) {
			    		this.baseTarget = e.getTarget();
			    	});
			    Ext.ToolTip.prototype.onMouseMove =
			    	Ext.ToolTip.prototype.onMouseMove.createInterceptor(function(e) {
			    		if (!e.within(this.baseTarget)) {
			    			this.onTargetOver(e);
			    			return false;
			    		}
			    	});

			    
				ourResultsGrid = new Ext.grid.GridPanel({
        		store: store,
        		columns: [
            		//{id:'Cert_ID',header: 'Certification ID', width: 100, sortable: true, dataIndex: 'Content_id'},
            		{header: 'Title', width: 150, sortable: true, dataIndex: 'ContentTitle'},
            		{header: 'Status',width: 75,sortable: true, dataIndex: 'active'},
            		{id:'HTMLContent',header: 'Content', width: 150, sortable: false, dataIndex: 'Content',renderer: renderHTMLContent},
            		//{header: 'Content Type', width: 75, sortable: true, dataIndex: 'ContentType'},
            		{id:'fileName',header: 'PDF Link', width: 50, sortable: true, dataIndex: 'file00',renderer: renderPDFLink}
        				],
        		stripeRows: true,
        		autoExpandColumn: 'HTMLContent',
       	 		height: 300,
				autoHeight:true,
        		autoWidth:true,
                onRender: function() {
                	Ext.grid.GridPanel.prototype.onRender.apply(this, arguments);
                	this.addEvents("beforetooltipshow");
        	        this.tooltip = new Ext.ToolTip({
        	        	renderTo: Ext.getBody(),
        	        	target: this.view.mainBody,
        	        	listeners: {
        	        		beforeshow: function(qt) {
        	        			var v = this.getView();
        			            var row = v.findRowIndex(qt.baseTarget);
        			            var cell = v.findCellIndex(qt.baseTarget);
        			            return this.fireEvent("beforetooltipshow", this, row, cell);
        	        		},
        	        		scope: this
        	        	}
        	        });
                },
        		listeners: {
        			render: function(g) {
        				g.on("beforetooltipshow", function(grid, row, col) {
                                                // return false here if you don't want to show the tooltip
        					if(col != 2)
        						return(false);
        					
        					//grid.tooltip.body.update("Tooltip for (" + row + ", " + col + ")");
        					grid.tooltip.body.update(grid.getStore().getAt(row).data.ContentTitle + " : " + grid.getStore().getAt(row).data.Content);
        				});
        			}},

				//width: 600,
        		//title: 'Array Grid',
				plugins: new Ext.ux.PanelResizer({minHeight: 100}),
				bbar: new Ext.PagingToolbar({
            									pageSize: 5,
            									store: store,
            									displayInfo: true,
												 plugins: new Ext.ux.ProgressBarPager()}),
        		// config options for stateful behavior
        		stateful: true,
        		stateId: 'grid'});
				
				ourResultsGrid.render(document.getElementById('gridResultsDiv'));
				store.load({params:{start:0, limit:5}});
			
		
		
	}
	Ext.onReady(function(){displaySearchForm()});