/*
 * Ext JS Library 3.0 Pre-alpha
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
Ext.ns('GS');
Ext.BLANK_IMAGE_URL = '/ext-2.2/resources/images/default/s.gif';

    var primaryKey='ID'; 
	var tableName='Listing';
	
	 
	var tpl = new Ext.Template(
        '{qtip}'
	);
    tpl.compile();
	
	
	var bookTplMarkup = [
		'{ListingDesc}'
	];
	var bookTpl = new Ext.Template(bookTplMarkup);

    var detailsText = '../admin/showcategory.php';
	
	   function renderTopic(value, p, record){
        return String.format( 
                '<table width="100%" border="0" cellspacing="2" cellpadding="2"><tr><td colspan="2"><span class="mainhead3">{1}</span><br/><br/><span class="basictextnormal">{2}</span></td><td width="100" rowspan="2"><img src="/admin/uploads/{3}" alt="{1}" name="{1}" width="100" height="100" id="Image" /></td></tr><tr><td width="296" class="sidebar2">Manufacture : {5} </td><td width="894" class="sidebar2">Category : {4}</td></tr></table>',
                value, record.data.Title, record.data.SubTitle, record.data.LargeImage, record.data.Category, record.data.Manufacture);
    }

 GS.Grid = Ext.extend(Ext.grid.GridPanel, {
 initComponent:function() {
 var config = {
 store:new Ext.data.JsonStore({
 id:primaryKey
 ,totalProperty:'total'
 ,root:'results'
 ,url:'../admin/data.php'
 ,fields:[
 {name: 'Title', mapping: 'Title', sortDir: 'ASC', sortType: 'asUCString'},
			{name: 'LTID', type: 'int'},
			{name: 'SubTitle'},
			{name: 'ListingDesc'},
			{name: 'ShortDesc'},
			{name: 'BasicRate', type: 'int'},
			{name: 'SpecialRate', type: 'int'},
			{name: 'ResellerRate', type: 'int'},
			{name: 'Category'},
			{name: 'Manufacture'},
			{name: 'TaxID'},
			{name: 'DelID'},
			{name: 'SmallImage'},
			{name: 'LargeImage'},
			{name: 'Status', type: 'int'},
			{name: 'SortOrder' , type: 'int' }
 ]
 ,baseParams:{table: tableName, task: "products"}
 })
 ,sm: new Ext.grid.RowSelectionModel({singleSelect: true,
									 listeners: { 
									 	'rowselect' : function(sm, rowIdx, r) {
										//	alert("2");
											var detailPanel = Ext.getCmp('sp-panel');
											bookTpl.overwrite(detailPanel.body, r.data);
											detailPanel.expand();
											}
										}
									})
 ,autoExpandColumn : 1
 ,columns:[{
            id: 'Manufacture', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: "Manufacture",
            dataIndex: 'Manufacture',
            width: 120,
			hidden:true,
         //   renderer: renderTopic,
            sortable: true
        },{
            id: 'Category', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: "Category",
            dataIndex: 'Category',
            width: 120,
			hidden:true,
         //   renderer: renderTopic,
            sortable: true
        },{
            id: 'Title', // id assigned so we can apply custom css (e.g. .x-grid-col-topic b { color:#333 })
            header: "Title",
            dataIndex: 'Title',
            width: 'auto',
            renderer: renderTopic,
            sortable: true
        }]
 ,viewConfig:{
	 forceFit:true,
            enableRowBody:true
		//	,
           // showPreview:false,
           // getRowClass : function(record, rowIndex, p, store){
            //    if(this.showPreview){
             //       p.body = '<p>'+record.data.ShortDesc+'</p>';
              //      return 'x-grid3-row-expanded';
              //  }
             //   return 'x-grid3-row-collapsed';
          //  }
			}
 }; // eo config object
  
 // apply config
 Ext.apply(this, Ext.apply(this.initialConfig, config));
  
 this.bbar = new Ext.PagingToolbar({
pageSize: 25,
        store: this.store,
        displayInfo: true,
        displayMsg: 'Displaying topics {0} - {1} of {2}',
        emptyMsg: "No topics to display",
        
        items:[
            '-', {
            pressed: false,
           // enableToggle:true,
            text: 'Click on refresh button to show all products.',
            cls: 'x-btn-text-icon details'
			//,
           // onclick: function(btn, pressed){
             //   var view = this.grid.getView();
               // view.showPreview = pressed;
              //  view.refresh();
			//	view.reload();
		//	 var el1 = Ext.getCmp('gsgrid');
			//					el1.store.clearFilter(); 
          //  }
        }]
		
});

 
 // call parent
 GS.Grid.superclass.initComponent.apply(this, arguments);
 } // eo function initComponent
  
 ,onRender:function() {
  
 // call parent
 GS.Grid.superclass.onRender.apply(this, arguments);
  
 // load the store
 this.store.load({params:{start:0, limit:25,table: tableName, task: "products"}});
  
 } // eo function onRender
 
 
 
 });
 Ext.reg('gsgrid', GS.Grid);





 Ext.onReady(function() {
  
 Ext.QuickTips.init();
  
 // create viewport with border layout
 var viewport = new Ext.Viewport({
 layout:'border'
 ,id:'vp'
 ,items:[{
 region:'north'
// ,title:'Kiran Sales'
 ,height:118
 ,html:'<table width="100%" border="0" cellspacing="0" cellpadding="0" height=100%><tr><td background="/images/top_bg.jpg" colspan=5><img src="/images/top.gif"></td></tr></table>'
 //,contentEl:'adsense-north'
 },
  {
 region:'center'
 ,xtype:'tabpanel'
 ,defaults:{layout:'fit'}
 ,activeItem:0
 ,items: [{
 title:'Home'
 ,id:'firsttab'
// ,iconCls:'icon-ok'
 ,bodyStyle:'padding:5px'
  ,contentEl:'homepage'
 //,html:'Welcome to Kiran Sales'
 }, {
 id:'gsgrid'
 ,layout:'fit'
 ,bodyStyle:'padding:5px'
 ,xtype:'gsgrid'
 ,title:'Products'
 },{
 title:'About Us'
 ,id:'abouttab'
 //,iconCls:'icon-ok'
 ,bodyStyle:'padding:5px'
 ,contentEl:'aboutus'
//,html:'<table width="90%" border="0" cellspacing="3" cellpadding="2"><tr><td class="mainhead3">:: About Us ::</td></tr><tr><td class="text style5" style="line-height:20px"><p><br>Our firm was established in the year 1964 by Shri Anil sheth (Radio Officer). He had laid the foundation of this organization which has today grown into big firm having good reputation for quality material. </p><p>We have very good qualified technical staff to take care of your after sales service.</p><p>Our technically qualified staff can even take care of your needs by understanding them and supplying you the best possible parts suitable to you.</p><p> At our place its quality &amp; customer satisfaction that comes first, every thing else is second for us. Our assets are our good relationship with all our clients and their blind faith on us for genuine quality material.</p></td></tr></table>'
 },{
 title:'Contact Us'
 ,id:'contacttab'
 //,iconCls:'icon-ok'
 ,bodyStyle:'padding:5px'
 ,contentEl:'contactus'
 //,html:'Contact'
 },{
 title:'Client List'
 ,id:'clienttab'
 //,iconCls:'icon-ok'
 ,bodyStyle:'padding:5px'
 ,contentEl:'clientlist'
 //,html:'Client'
 }]
 }
 
,{
 //region:'west'
 title:'Categories',
 //,id:'ss-panel'
 //,width:220
 //,split:true
 //,collapsible:true
 
 
            xtype: 'treepanel',
            id: 'tree-panel',
            region: 'west',
            autoScroll: true,
	        rootVisible: false,
	     
        	root: {
            	nodeType: 'async',
            	text: 'Categories',
            	draggable:false,
            	id:'data'
        	},   
            // Our custom TreeLoader:
	        dataUrl: '../admin/getcategorytree.php',
            width: 200,
         	 height: 600,
			 			bodyStyle: "background-color: #ffffff",

	        listeners: {
	            'render': function(tp){
                    tp.getSelectionModel().on('selectionchange', function(tree, node){
								 var el1 = Ext.getCmp('gsgrid');
							//	el1.store.filter('Category', node.text); 
							//	el1.store.reload();
								el1.store.load({params:{start:0, limit:25,table: tableName, task: "products", categoryID: node.id, category:node.text}});										  
																		  
                    //    var el1 = Ext.getCmp('ss-panel');
						var el2 = Ext.getCmp('sp-panel').body;
					//	var grid2 = Ext.getCmp('myGrid');

						if(node.leaf){
	                   		tpl.overwrite(el2, node.attributes);
						//	  el1.load();
				//			el1.load({ url : "../admin/showcategory.php?ID="+ node.id + "&Category=" + node.Category, discardUrl: false,
					//			nocache: true,
					//			text: "Loading...",
					//			method : 'POST',
					//			timeout: 30, scripts : true });
						//	   el2.load({ url : "../admin/showcategory.php?ID="+ node.id + "&Category=" + node.text, discardUrl: false,
						//		nocache: true,
						//		text: "Loading...",
						//		method : 'POST',
							//	timeout: 30, scripts : true });
							  
							//  grid2.load({params:{start:25, limit:25,table: tableName, task: "products"}});
							//var t1 =  Ext.getCmp('ss-panel').layout.setActiveItem(node.id);
							//var pantalla = Ext.get(node.id);
							//pantalla.load({params:{start:0, limit:25,table: tableName, task: "products"}});

							  //grid2.reload();
	                    }else{
                          //  el2.load({ url : "../admin/showcategory.php?Category=" + node.id, discardUrl: false,
						//		nocache: true,
						//		text: "Loading...",
						//		timeout: 30, scripts : true });
						//	tpl.overwrite(el, node.attributes);
                        }
						
                    })
	            }
	        }
        
		
		
 },{
 region:'south'
 ,title:'Product Detail'
  ,titleCollapse : false
 ,id: 'sp-panel'
// ,height:280
 ,height: 300
 ,minSize: 75
 ,maxSize: 450
 ,bodyStyle: 'padding:20px;overflow:auto'
 ,split:true
 ,collapsible:true
 ,collapsed:true
 ,autoload: { url : detailsText, scripts : true }
 }]
 });
  
 
  
  
 }); // eo function onReady
  


