/* Ext JS Library 2.0.1
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
 
/*
  <property_list>
    <prop_id>AB33</prop_id>
    <community_id>AB</community_id>
    <prop_lot>33</prop_lot>
    <prop_neighborhood>Stratford</prop_neighborhood>
    <prop_size>.28 acre</prop_size>
    <prop_long>0</prop_long>
    <prop_lat>0</prop_lat>
    <prop_city>Apex</prop_city>
    <prop_state>NC</prop_state>
    <prop_county>Wake</prop_county>
    <community_name>Abbington</community_name>
    <web_folder>abbington</web_folder>
    <location>Off of Highway 64 in Apex, NC 27502</location>
  </property_list>
*/
 
Ext.BLANK_IMAGE_URL = '/wrweb/scripts/resources/images/default/s.gif';

function loadLotGrid(gridArea) {
	
	var dataFile = 'data/lots.xml';
	var gridRows = 0;

// create the Data Store
    var store1 = 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: 'property_list'
           }, [ {	name: 'community_name'},
           		{	name: 'web_folder'},
           		{	name: 'prop_id'},
           		{	name: 'prop_neighborhood'},
           		{	name: 'prop_lot', type: 'float'},
			   	{	name: 'prop_size', type: 'float'},
				{	name: 'prop_city'},
				{	name: 'prop_state'},
				{	name: 'prop_county'},
				{	name: 'location'}
           ])
    });

	store1.on("load", function(e,r,t) {
		if(e.data.length <=4)
			grid1.setWidth(755);
		gridRows = store1.getCount();
		if(gridRows == 0)
			$('lots').dispose();
		else
			grid1.setHeight(gridRows * 25 + 25);
	}, this, true);
	

	function imageInsert(val) {
		if(val == "true")
			return '<img src="/wrweb/grid/images/check.gif" />';
	}

	function thumbInsert(val, p, record) {
		var thumb = val + '/images/grid_thumb.jpg';
	
		return '<a href="' + val + '"><img src="' + thumb + '" 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 = "TBD";
        	
        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 = "TBD";
        	
        return result;
	}

    // create the grid
    var grid1 = new Ext.grid.GridPanel({
        store: store1,
        columns: [
            {header: "Lot", width: 38, dataIndex: 'prop_lot', sortable: true},
            {header: "Size (acres)", width: 75, dataIndex: 'prop_size', sortable: true, renderer: formatComma},
            {header: "City/Town", width: 150, dataIndex: 'prop_city', sortable: true},
            {header: "Community", width: 245, dataIndex: 'community_name', sortable: true},
            {header: "Neighborhood", width: 245, dataIndex: 'prop_neighborhood', sortable: true}
        ],
        renderTo:'lot-grid',
		border: false,
		loadMask: true,
		enableHdMenu: false,
		autoExpand: 'community_name',
		stripeRows: true,
        width: 755,
			listeners: {
            	scope: this,
            	render: function() {
                	store1.load();
            	}
        	}
    });
}