/*
 * 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 loadGrid(accordionSection) {
	
	var fDate = new Date();
	var dataFile = 'data/homes.xml?timestamp=' + fDate.getTime();

// 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: 'Available_Homes'
           }, [ {	name: 'community_name'},
           		{	name: 'web_folder'},
           		{	name: 'prop_id'},
           		{	name: 'prop_neighborhood'},
           		{	name: 'prop_lot', type: 'float'},
			   	{	name: 'sqft'},
				{	name: 'bed'},
				{	name: 'bath'},
				{	name: 'garage'},
				{	name: 'master_location'},
				{	name: 'special_features'},
				{	name: 'price',type: 'float'},
				{	name: 'plans'},
				{	name: 'tour'}
           ])
    });

	store.on("load", function(e,r,t) {
		gridRows = store.getCount();
		if(gridRows > 0) {
			if(e.data.length <=5)
				grid.setWidth(792);
			if(e.data.length <= 4)
				grid.setHeight((e.data.length * 85) + 15);
		}
		else
		{
			$('homes').dispose();
			accordionSection.display(1);
		}
	}, 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 + '"><div style="background: url(' + thumb + ') no-repeat; height: 66px; width: 100px"></div></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 grid = new Ext.grid.GridPanel({
        store: store,
        columns: [
            {header: "", width: 110, dataIndex: 'prop_id', sortable: false, renderer: thumbInsert},
            {header: "Lot", width: 38, dataIndex: 'prop_lot', sortable: true},
            {header: "SqFt", width: 45, dataIndex: 'sqft', sortable: true, renderer: formatComma},
            {header: "Beds", width: 60, align: 'center', dataIndex: 'bed', sortable: true},
            {header: "Baths", width: 60, align: 'center', dataIndex: 'bath', sortable: true},
            {header: "Garage", width: 61, align: 'center', dataIndex: 'garage', sortable: true},
            {header: "Master", width: 65, align: 'center', dataIndex: 'master_location', sortable: true},
            {header: "Special Features", width: 163, dataIndex: 'special_features', sortable: false},
            {header: "Price", width: 65, align: 'right', dataIndex: 'price', sortable: true, renderer: formatCurrency},
            {header: "Plans", align: 'center', width: 62, dataIndex: 'plans', sortable: true, renderer: imageInsert},
            {header: "Tour", align: 'center', width: 60, dataIndex: 'tour', sortable: true, renderer: imageInsert}
        ],
        renderTo:'home-grid',
		border: false,
		loadMask: true,
		enableHdMenu: false,
		autoExpand: 'Special_Features',
		stripeRows: true,
        width: 808,
		height: 400,
		listeners: {
           	scope: this,
           	render: function() {
               	store.load();
           	}
       	}
    });
}