/*
 * 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 loadHomeGrid(homeType) {
	var dataFile = "";
	debugger;
	if(homeType == "townhomes") {
		baseLink = "/wrweb/avail_homes/townhomes/";
		dataFile = "data/homes.xml";
	}
	else {
		baseLink = "/wrweb/avail_homes/homes/";
		dataFile = 'data/homes.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_homes'
           }, [ {	name: 'community_name'},
           		{	name: 'web_folder'},
           		{	name: 'prop_id'},
           		{	name: 'prop_lot'},
			   	{	name: 'sqft'},
				{	name: 'bed'},
				{	name: 'bath'},
				{	name: 'garage'},
				{	name: 'master_location'},
				{   name: 'prop_city'},
				{	name: 'price',type: 'float'},
				{	name: 'plans'},
				{	name: 'tour'},
				{	name: 'prop_id'},
				{	name: 'date_contract'}
           ])
    });

	store.on("load", function(e,r,t) {
		if(e.data.length <=5)
			grid.setWidth(868);
	}, this, true);
	

	function imageInsert(val) {
		if(val == "true")
			return '<img src="/wrweb/grid/images/check.gif" />';
	}

	function thumbInsert(val, p, record) {

		var linkLocation = baseLink + record.data.web_folder + '/' + val;
		var imageLocation = linkLocation + '/images/grid_thumb.jpg';
		if(record.data.date_contract != "") {
			return '<div style="background: url(' + imageLocation + ') no-repeat; height: 66px; width: 100px"><a href="' + linkLocation + '"><img src="/wrweb/styles/images/under_contract.png" /></div></a>';
		}
		else
			return '<a href="' + linkLocation + '"><img src="' + imageLocation + '" 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;
	}
	
	function communityLink(val, p, record) {
		
		var link = '<a href="' + baseLink + record.data.web_folder + '" title="Visit '+ val + '">' + val + '</a>';
		return link;
	}
	
    // create the grid
		var grid = new Ext.grid.GridPanel({
			store: store,
			columns: [
				{header: "", width: 110, dataIndex: 'prop_id', sortable: false, renderer: thumbInsert},
				{header: "Community", width: 127, dataIndex: 'community_name', sortable: true, renderer: communityLink},
				{header: "City/Town", width: 118, dataIndex: 'prop_city', sortable: true},
				{header: "Lot", width: 42, dataIndex: 'prop_lot', sortable: true},
				{header: "SqFt", width: 48, 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: 58, align: 'center', dataIndex: 'garage', sortable: true},
				{header: "Master", width: 65, align: 'center', dataIndex: 'master_location', sortable: true},
				{header: "Price", width: 70, align: 'right', dataIndex: 'price', sortable: true, renderer: formatCurrency},
				{header: "Plans", align: 'center', width: 54, dataIndex: 'plans', sortable: true, renderer: imageInsert},
				{header: "Tour", align: 'center', width: 54, dataIndex: 'tour', sortable: true, renderer: imageInsert}
			],
			renderTo:'home-grid',
			border: false,
			loadMask: true,
			enableHdMenu: false,
			autoExpand: "community_name",
			stripeRows: true,
			width: 885,
			height: 395,
			
			listeners: {
            	scope: this,
            	render: function() {
                	store.load();
            	}
        	}
		});
}