/**
 * @author jonathan
 */
var countryList = [['AF', 'Afghanistan'], ['AL', 'Albania'], ['DZ', 'Algeria'], ['AS', 'American Samoa'], ['AD', 'Andorra'], ['AO', 'Angola'], ['AI', 'Anguilla'], ['AQ', 'Antarctica'], ['AG', 'Antigua and Barbuda'], ['AR', 'Argentina'], ['AM', 'Armenia'], ['AW', 'Aruba'], ['AU', 'Australia'], ['AT', 'Austria'], ['AZ', 'Azerbaijan'], ['BH', 'Bahrain'], ['BD', 'Bangladesh'], ['BB', 'Barbados'], ['BY', 'Belarus'], ['BE', 'Belgium'], ['BZ', 'Belize'], ['BJ', 'Benin'], ['BM', 'Bermuda'], ['BT', 'Bhutan'], ['BO', 'Bolivia'], ['BA', 'Bosnia and Herzegovina'], ['BW', 'Botswana'], ['BV', 'Bouvet Island'], ['BR', 'Brazil'], ['IO', 'British Indian Ocean Territory'], ['VG', 'British Virgin Islands'], ['BN', 'Brunei'], ['BG', 'Bulgaria'], ['BF', 'Burkina Faso'], ['MM', 'Burma'], ['BI', 'Burundi'], ['KH', 'Cambodia'], ['CM', 'Cameroon'], ['CA', 'Canada'], ['CV', 'Cape Verde'], ['KY', 'Cayman Islands'], ['CF', 'Central African Republic'], ['TD', 'Chad'], ['CL', 'Chile'], ['CN', 'China'], ['CX', 'Christmas Island'], ['CC', 'Cocos (Keeling) Islands'], ['CO', 'Colombia'], ['KM', 'Comoros'], ['CD', 'Congo Democratic Republic of the'], ['CG', 'Congo Republic of the'], ['CK', 'Cook Islands'], ['CR', 'Costa Rica'], ['CI', 'Coast Ivory'], ['HR', 'Croatia'], ['CU', 'Cuba'], ['CY', 'Cyprus'], ['CZ', 'Czech Republic'], ['DK', 'Denmark'], ['DJ', 'Djibouti'], ['DM', 'Dominica'], ['DO', 'Dominican Republic'], ['TL', 'East Timor'], ['EC', 'Ecuador'], ['EG', 'Egypt'], ['SV', 'El Salvador'], ['GQ', 'Equatorial Guinea'], ['ER', 'Eritrea'], ['EE', 'Estonia'], ['ET', 'Ethiopia'], ['FK', 'Falkland Islands (Islas Malvinas)'], ['FO', 'Faroe Islands'], ['FJ', 'Fiji'], ['FI', 'Finland'], ['FR', 'France'], ['GF', 'French Guiana'], ['PF', 'French Polynesia'], ['TF', 'French Southern and Antarctic Lands'], ['GA', 'Gabon'], ['GE', 'Georgia'], ['DE', 'Germany'], ['GH', 'Ghana'], ['GI', 'Gibraltar'], ['GR', 'Greece'], ['GL', 'Greenland'], ['GD', 'Grenada'], ['GP', 'Guadeloupe'], ['GU', 'Guam'], ['GT', 'Guatemala'], ['GN', 'Guinea'], ['GW', 'Guinea-Bissau'], ['GY', 'Guyana'], ['HT', 'Haiti'], ['HM', 'Heard Island and McDonald Islands'], ['VA', 'Holy See (Vatican City)'], ['HN', 'Honduras'], ['HK', 'Hong Kong (SAR)'], ['HU', 'Hungary'], ['IS', 'Iceland'], ['IN', 'India'], ['ID', 'Indonesia'], ['IR', 'Iran'], ['IQ', 'Iraq'], ['IE', 'Ireland'], ['IL', 'Israel'], ['IT', 'Italy'], ['JM', 'Jamaica'], ['JP', 'Japan'], ['JO', 'Jordan'], ['KZ', 'Kazakhstan'], ['KE', 'Kenya'], ['KI', 'Kiribati'], ['KP', 'Korea North'], ['KR', 'Korea South'], ['KW', 'Kuwait'], ['KG', 'Kyrgyzstan'], ['LA', 'Laos'], ['LV', 'Latvia'], ['LB', 'Lebanon'], ['LS', 'Lesotho'], ['LR', 'Liberia'], ['LY', 'Libya'], ['LI', 'Liechtenstein'], ['LT', 'Lithuania'], ['LU', 'Luxembourg'], ['MO', 'Macao'], ['MK', 'Macedonia The Former Yugoslav Republic of'], ['MG', 'Madagascar'], ['MW', 'Malawi'], ['MY', 'Malaysia'], ['MV', 'Maldives'], ['ML', 'Mali'], ['MT', 'Malta'], ['MH', 'Marshall Islands'], ['MQ', 'Martinique'], ['MR', 'Mauritania'], ['MU', 'Mauritius'], ['YT', 'Mayotte'], ['MX', 'Mexico'], ['FM', 'Micronesia Federated States of'], ['MD', 'Moldova'], ['MC', 'Monaco'], ['MN', 'Mongolia'], ['MS', 'Montserrat'], ['MA', 'Morocco'], ['MZ', 'Mozambique'], ['NA', 'Namibia'], ['NR', 'Nauru'], ['NP', 'Nepal'], ['NL', 'Netherlands'], ['AN', 'Netherlands Antilles'], ['NC', 'New Caledonia'], ['NZ', 'New Zealand'], ['NI', 'Nicaragua'], ['NE', 'Niger'], ['NG', 'Nigeria'], ['NU', 'Niue'], ['NF', 'Norfolk Island'], ['MP', 'Northern Mariana Islands'], ['NO', 'Norway'], ['OM', 'Oman'], ['PK', 'Pakistan'], ['PW', 'Palau'], ['PS', 'Palestinian Territory Occupied'], ['PA', 'Panama'], ['PG', 'Papua New Guinea'], ['PY', 'Paraguay'], ['PE', 'Peru'], ['PH', 'Philippines'], ['PN', 'Pitcairn Islands'], ['PL', 'Poland'], ['PT', 'Portugal'], ['PR', 'Puerto Rico'], ['QA', 'Qatar'], ['RE', 'Reunion'], ['RO', 'Romania'], ['RU', 'Russia'], ['RW', 'Rwanda'], ['SH', 'Saint Helena'], ['KN', 'Saint Kitts and Nevis'], ['LC', 'Saint Lucia'], ['PM', 'Saint Pierre and Miquelon'], ['VC', 'Saint Vincent and the Grenadines'], ['WS', 'Samoa'], ['SM', 'San Marino'], ['ST', 'Sao Tome and Principe'], ['SA', 'Saudi Arabia'], ['SN', 'Senegal'], ['SC', 'Seychelles'], ['SL', 'Sierra Leone'], ['SG', 'Singapore'], ['SK', 'Slovakia'], ['SI', 'Slovenia'], ['SB', 'Solomon Islands'], ['SO', 'Somalia'], ['ZA', 'South Africa'], ['GS', 'South Georgia and the South Sandwich Islands'], ['ES', 'Spain'], ['LK', 'Sri Lanka'], ['SD', 'Sudan'], ['SR', 'Suriname'], ['SJ', 'Svalbard'], ['SZ', 'Swaziland'], ['SE', 'Sweden'], ['CH', 'Switzerland'], ['SY', 'Syria'], ['TW', 'Taiwan'], ['TJ', 'Tajikistan'], ['TZ', 'Tanzania'], ['TH', 'Thailand'], ['BS', 'The Bahamas'], ['GM', 'The Gambia'], ['TG', 'Togo'], ['TK', 'Tokelau'], ['TO', 'Tonga'], ['TT', 'Trinidad and Tobago'], ['TN', 'Tunisia'], ['TR', 'Turkey'], ['TM', 'Turkmenistan'], ['TC', 'Turks and Caicos Islands'], ['TV', 'Tuvalu'], ['UG', 'Uganda'], ['UA', 'Ukraine'], ['AE', 'United Arab Emirates'], ['GB', 'United Kingdom'], ['UM', 'United States Minor Outlying Islands'], ['US', 'United States'], ['UY', 'Uruguay'], ['UZ', 'Uzbekistan'], ['VU', 'Vanuatu'], ['VE', 'Venezuela'], ['VN', 'Vietnam'], ['VI', 'Virgin Islands'], ['WF', 'Wallis and Futuna'], ['EH', 'Western Sahara'], ['YE', 'Yemen'], ['YU', 'Yugoslavia'], ['ZM', 'Zambia'], ['ZW', 'Zimbabwe']];
var StateList = [['AL', 'Alabama'], ['AK', 'Alaska'], ['AZ', 'Arizona'], ['AR', 'Arkansas'], ['CA', 'California'], ['CO', 'Colorado'], ['CT', 'Connecticut'], ['DE', 'Delaware'], ['DC', 'District of Columbia'], ['FL', 'Florida'], ['GA', 'Georgia'], ['HW', 'Hawaii'], ['ID', 'Idaho'], ['IL', 'Illinois'], ['IN', 'Indiana'], ['IA', 'Iowa'], ['KS', 'Kansas'], ['KY', 'Kentucky'], ['LA', 'Louisiana'], ['ME', 'Maine'], ['MD', 'Maryland'], ['MA', 'Massachusetts'], ['MI', 'Michigan'], ['MN', 'Minnesota'], ['MS', 'Mississippi'], ['MO', 'Missouri'], ['MT', 'Montana'], ['NE', 'Nebraska'], ['NV', 'Nevada'], ['NH', 'New Hampshire'], ['NJ', 'New Jersey'], ['NM', 'New Mexico'], ['NY', 'New York'], ['NC', 'North Carolina'], ['ND', 'North Dakota'], ['OH', 'Ohio'], ['OK', 'Oklahoma'], ['OR', 'Oregon'], ['PA', 'Pennsylvania'], ['RI', 'Rhode Island'], ['SC', 'South Carolina'], ['SD', 'South Dakota'], ['TN', 'Tennessee'], ['TX', 'Texas'], ['UT', 'Utah'], ['VE', 'Vermont'], ['VA', 'Virginia'], ['WA', 'Washington'], ['WV', 'West Virginia'], ['WI', 'Wisconsin'], ['WY', 'Wyoming'], ['PR', 'Puerto Rico']];

var profileApp = function(){
    var stores = {
        comments: new Ext.data.JsonStore({
            url: '/erequest/listAsJSON/comment',
            fields: ['erequestid', 'commentid', 'userid', 'user', 'commenttext', 'dateposted', 'postedby'],
            data: profile.comments,
			baseParams:{
				erequestid: profile.erequestid
			}
			
        }),
        signatures: new Ext.data.JsonStore({
            url: '/erequest/listAsJSON/signature',
            fields: ['erequestid', 'location', 'signatureid', 'name', 'emailaddress', 'city', 'state', 'country', 'dateposted'],
            data: profile.signatures,
			baseParams:{
				erequestid: profile.erequestid
			}
        })
    };
    
    var templates = {
        comments: new Ext.XTemplate('<tpl for="."><div class="comment4 {[ xindex%2===0 ? "odd" : "even"]}"><div class="commentinfo">Posted by <a href="/user/profile/{userid}">{postedby}</a> on {postdate}</div><div class="commenttext">{commenttext}</div></tpl>'),
        signatures: new Ext.XTemplate('<tpl for="."><div class="signature {[ xindex%2===0 ? "odd" : "even"]}">{emailaddress}</div></tpl>')
    };
    
    var forms = {
        comments: new Ext.form.FormPanel({
			title:'Post Comment',
			frame:true,
            id: 'commentsForm',
            renderTo: 'commentsFormContainer',
            url: '/erequest/save/comment',
            method: 'POST',
            labelAlign: 'top',
            items: [{
                xtype: 'hidden',
                name: 'erequestid',
                value: profile.erequestid
            }, {
                xtype: 'textarea',
                fieldLabel: 'Post your comment',
                name: 'commenttext',
                width: 350
            }],
            buttons: [{
				iconCls:'btn-apply',
                text: 'Post Comment',
                handler: function(){
                    this.ownerCt.form.submit({
                        waitMsg: 'Posting Comment',
                        success: function(){
                            var fc = Ext.get('commentsListContainer');
                            fc.radioClass('active');
                            Ext.getCmp('commentResults').store.load({
							});
                        },
						failure:function(a,b) {
							if(b.result.errors) {
								if(b.result.errors.message) {
									alert(b.result.errors.message)
								} else {
		                            var fc = Ext.get('commentsListContainer');
		                            fc.radioClass('active');									
								}
							}
						}
                    })
                }
            }]
        }),
        signature: new Ext.form.FormPanel({
			title:'Add eSignature',
            id: 'signatureForm',
            renderTo: 'signatureFormContainer',
            url: '/erequest/save/signature',
            method: 'POST',
			width:340,
			bodyStyle:'padding:5px',
			frame:true,
            labelAlign: 'top',
			defaults:{
				allowBlank:false,
				width:300,
				listWidth: 300
			},
            items: [{
                xtype: 'hidden',
                name: 'erequestid',
                value: profile.erequestid
            }, 
			{
				xtype:'textfield',
				fieldLabel:'First and Last Name',
				name:'name'
			},
			{
                xtype: 'textfield',
                fieldLabel: 'Email Address',
                emptyText: 'Will not be displayed on the site',
                name: 'emailaddress',
				vtype:'email'
            }, {
                xtype: 'combo',
                hiddenName: 'country',
                fieldLabel: 'Country',
                valueField: 'value',
                displayField: 'display',
                triggerAction: 'all',
                mode: 'local',
                store: new Ext.data.SimpleStore({
                    fields: ['value', 'display'],
                    data: countryList
                }),
                allowBlank: true,
                emptyText: 'Select One',
                listeners: {
                    select: function(a){
                        if (a.getValue() != 'US') {
                            Ext.getCmp('stateSelect').clearValue();
                            Ext.getCmp('stateSelect').store.loadData([]);
                        }
                        else {
                            Ext.getCmp('stateSelect').store.loadData(StateList);
                        }
                    }
                },
                value: 'US'
            }, {
                id: 'stateSelect',
                xtype: 'combo',
                hiddenName: 'state',
                fieldLabel: 'State',
                valueField: 'value',
                displayField: 'display',
                triggerAction: 'all',
				listWidth:300,
                mode: 'local',
                store: new Ext.data.SimpleStore({
                    fields: ['value', 'display'],
                    data: StateList
                }),
                allowBlank: true,
                emptyText: 'Select One if applicable'
            }, {
                xtype: 'textfield',
                name: 'city',
                allowBlank: false,
                fieldLabel: 'City',
                emptyText: ''
            }],
            buttons: [{
				iconCls:'btn-apply',
                text: 'Add eSignature',
                handler: function(){
                    this.ownerCt.form.submit({
                        waitMsg: 'Adding eSignature',
                        success: function(){
                            Ext.get('signatureListContainer').radioClass('active');
                            Ext.getCmp('signatureResults').store.load();
                        },
						failure: function(a,b) {
							if(b.result.errors) {
								var msg = '';
								for(var n in b.result.errors) {
									msg = msg + ' ' + b.result.errors[n];
								}
								alert(msg);
							}							
						}
                    })
                }
            }]
        })
    };
    
    
	var signatureTemplate = new Ext.XTemplate(
		'<tpl for="."><div class="signature {[ xindex%2===0 ? "odd" : "even"]}">',
		'<div class="sitem signature_name">{name}</div><div class="sitem signature_location">{city}, {state}&nbsp;{country}</div><div class="clear"></div>',
		'</div></tpl>'
	);    
    var signatureList = new Ext.DataView({
        id: 'signatureResults',
        renderTo: 'signatureListContainer',
        emptyText: '<h4>Click on Add eSignature to add your name to the petition.</h4>',
        loadingText: 'Loading Signatures...',
        store: stores.signatures,
        tpl: signatureTemplate,
        itemSelector: 'div.signature',
        singleSelect: true
    });
    
    
    var commentTemplate = new Ext.XTemplate('<tpl for=".">', '<div class="comment {[ xindex%2===0 ? "odd" : "even"]}"><div class="comment_text">{commenttext}</div><div class="postedby">Posted by <a href="/user/profile/{userid}">{postedby}</a> on {dateposted}</div></div>', '</tpl>');
    
    var commentList = new Ext.DataView({
        id: 'commentResults',
        renderTo: 'commentsListContainer',
        emptyText: '<h4>Buyadate.org members may post comments by clicking on Add Comment.</h4>',
        loadingText: 'Loading Comments...',
        store: stores.comments,
        tpl: commentTemplate,
        itemSelector: 'div.comment',
        singleSelect: true
    });
    
    
    return {
        addComment: function(){
            var fc = Ext.get('commentsFormContainer');
            fc.radioClass('active');
        },
        addSignature: function(){
            var fc = Ext.get('signatureFormContainer');
            fc.radioClass('active');
            
        },
        init: function(){
        
        }
    };
};

var pa;
Ext.onReady(function(){
    pa = profileApp();
    pa.init();
});
