/*
 * Ext JS Library 2.0.1
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
Ext.BLANK_IMAGE_URL = '/wrweb/scripts/resources/images/default/s.gif';

function loadLotGrid(communityData) {
	
	var dataFile = 'data/lots.xml';

// create the Data Store
    var store = new Ext.data.Store({
        // load using HTTP
        url: dataFile,
        // the return will be XML, so lets set up a reader
        reader: new Ext.data.XmlReader({
        	record: 'Master_Lots'
           }, [ {	name: 'web_folder'},
			   	{	name: 'community_name'},
				{	name: 'prop_lot'},
				{	name: 'prop_size'},
				{	name: 'prop_city'},
				{	name: 'prop_county'}
           ])
    });

	store.on("load", function(e,r,t) {
		if(e.data.length <=4)
			grid.setWidth(723);
	}, this, true);
	

	function imageInsert(val) {
		if(val == "1")
			return '<img src="../../../grid/images/check.gif" />';
	}

	function thumbInsert(val, p, record) {
		if(val == null | val == '')
			val = '../../../grid/images/empty_thumb.gif';
		else
			val = 'thumbs/' + val;
		return '<a href="' + record.data.Link + '"><img src="' + val + '" width="100px" height="66px" /></a>';
	}
	
	function formatComma( num ) {
        var isNegative = false;
   
        if( isNaN( num ) ) {
          num = "0";
        }
        if ( num < 0 ) {
          num = Math.abs( num );
          isNegative = true;
        }

        for ( i = 0; i < Math.floor( ( num.length - ( 1 + i ) ) / 3 ); i++) {
          num = num.substring( 0 ,num.length - ( 4 * i + 3 ) ) + ',' + num.substring( num.length - ( 4 * i + 3 ) );
        }

        var result = num;
        if ( isNegative ) {
          result = "-" + result;
        }
        
        if ( num == 0 )
        	result = "N/A";
        	
        return result;
	}

	function formatCurrency( num ) {
        var isNegative = false;
        num = num.toString().replace(/\\$|\\,/g,'');

        if( isNaN( num ) ) {
          num = "0";
        }
        if ( num < 0 ) {
          num = Math.abs( num );
          isNegative = true;
        }
        cents = Math.floor( ( num * 100 + 0.5 ) % 100 );
        num = Math.floor( ( num * 100 + 0.5 ) / 100 ).toString();
        if ( cents < 10 ) {
          cents = "0" + cents;
        }
        for ( i = 0; i < Math.floor( ( num.length - ( 1 + i ) ) / 3 ); i++) {
          num = num.substring( 0 ,num.length - ( 4 * i + 3 ) ) + ',' + num.substring( num.length - ( 4 * i + 3 ) );
        }

        var result = '$' + num // + '.' + cents;
        if ( isNegative ) {
          result = "-" + result;
        }
        
        if ( num == 0 )
        	result = "N/A";
        	
        return result;
	}

    // create the grid
    var grid = new Ext.grid.GridPanel({
        store: store,
        columns: [
            {header: "Community", width: 170, dataIndex: 'community_name', sortable: true},
            {header: "Lot", width: 45, dataIndex: 'prop_lot', sortable: false},
            {header: "Size", width: 80, dataIndex: 'prop_size', sortable: true},
            {header: "City", width: 110, dataIndex: 'prop_city', sortable: true},
            {header: "County", width: 75, align: 'center', dataIndex: 'prop_county', sortable: true}
        ],
        renderTo:'lot-grid',
		border: false,
		loadMask: true,
		enableHdMenu: false,
		autoExpand: 'Community',
		stripeRows: true,
        width: 500,
		height: 297
    });

	store.load();
}