//Ext.BLANK_IMAGE_URL = '../../Includes/images/ext/default/s.gif';
ContactYourSenatorDialog = Ext.extend(Ext.Window, {
    infoHtml: 'Use the contact information below to let one of your two state senators know how you feel about your tax spending.<br/><br/><div style="text-align:center;"><b>{0},&nbsp;{1}&nbsp;&ndash;&nbsp;({2}&nbsp;&ndash;&nbsp;{3})</b><br/>{4}<br/><br/>OR<br/><br/><b>{5},&nbsp;{6}&nbsp;&ndash;&nbsp;({7}&nbsp;&ndash;&nbsp;{8})</b><br/>{9}</div>',
    
    emptyText: 'Please choose your state from the dropdown menu above. This will display the contact information for the 2 Senators in your state, so that you can let them know your opinions on your how your tax dollars are being spent.',

    infoPanel: null,
    store: null,
    senatorsStore: null,


    initComponent: function() {
        ContactYourSenatorDialog.superclass.initComponent.apply(this, arguments);

        this.store = new Ext.data.SimpleStore({
            fields: ['abbr', 'state', 'nick'],
            data: Ext.exampledata.states
        });

        Ext.applyIf(this, {
            title: 'Contact Your Senator',
            resizable: false,
            layout: 'border',
            width: 340,
            height: 240,
            modal: false,
            shadow: false,
            defaults: {
                border: false
            }
        });

        this.infoPanel = new Ext.Panel({
            border: false,
            html: this.emptyText
        });

        this.add(new Ext.Panel({
            region: 'center',
            bodyStyle: 'padding: 10px;',
            layout: 'form',
            labelWidth: 120,
            items: [
            {
                xtype: 'combo',
                fieldLabel: 'Choose your state',
                store: this.store,
                displayField: 'state',
                typeAhead: true,
                mode: 'local',
                triggerAction: 'all',
                emptyText: 'Select a state...',
                selectOnFocus: true,
                scope: this,
                listeners: {
                    select: function(combo, record, index)
                    {
                        var html = '';
                        var items = this.senatorsStore.query('state', record.get('abbr'));
                        if (items.getCount() == 2)
                        {
                            html = String.format(this.infoHtml, 
                            	items.first().get('last_name'),
                            	items.first().get('first_name'),
                            	items.first().get('party'),
                            	items.first().get('state'),
                            	this.getContactLink(items.first().get('email')),
                            	items.last().get('last_name'),
                            	items.last().get('first_name'),
                            	items.last().get('party'),
                            	items.last().get('state'),
                            	this.getContactLink(items.last().get('email')));
                        }

                        this.infoPanel.body.update(html);
                    },
                    scope: this
                }
            },
            this.infoPanel
            ]
        }));

        this.senatorsStore = new Ext.data.Store({
            autoLoad: true,
            url: 'senators.xml',
            reader: new Ext.data.XmlReader({
                record: 'member'
            },
            [
            {
                name: 'last_name'
            },
            {
                name: 'first_name'
            },
            {
                name: 'party'
            },
            {
                name: 'state'
            },
            {
                name: 'email'
            }
            ])
        });

        this.senatorsStore.on('load',
        function(st) {
            this.show();
        },
        this);
    },
    
    getContactLink: function(str)
    {
    	if(str.indexOf('http://') != -1)
    	{
    		return String.format('<a href="{0}" target="_blank">Contact Form</a>', str);
    	}
    	
    	if(str.indexOf('mailto:') != -1)
    	{
    		return String.format('Email address: <a href="{0}">{1}</a>', str, str.replace('mailto:', ''));
    	}
    	
    	if(str.trim().length == 0)
    	{
    		return 'No Contact Information Available';
    	}
    	
    	return str;
    }
});