/**
 * @author schiesser
 */
Ext.ns('de.meinkabinett.view');

de.meinkabinett.view.VoteCelebrityView = Ext.extend(Ext.DataView, {
    constructor: function(config){
        var that = this;
		
        that.filter = new Ext.form.TextField({
            selectOnFocus: true,
            enableKeyEvents: true,
            id: 'celebrities-search',
			emptyText: 'Promi nach Namen filtern (z.B. Angela Merkel)'
        });
        
        de.meinkabinett.view.VoteCelebrityView.superclass.constructor.call(this, Ext.apply({
            store: de.meinkabinett.Model.celebrities,
            tpl: util.TemplateLoader.getTemplate('/templates/celebrity.tpl'),
            overClass: 'item-over',
            itemSelector: 'li.celebrity',
            id: 'celebrities'
        }, config));
        
        that.on('click', function(thisDataView, index, node){
            var celebrityId = thisDataView.getRecord(node).get('id');
            var secretaryId = de.meinkabinett.Model.selectedSecretaryId;
            Ext.Ajax.request({
                url: "/secretaries/" + secretaryId + "/votings",
                success: function(response){
					de.meinkabinett.Model.secretaries.reload();
					Ext.fly('modal_content').update('<iframe src="/ads/ad_336x280.html" style="border: none; width: 500px; height: 300px;" scrolling="no"/>');
					Ext.fly('modal_head').update('<h3>Danke</h3><p>Vielen Dank für Deine Stimme. Bitte empfehle uns Deinen Freunden weiter.</p>');
                },
                failure: function(response){
					Ext.fly('modal_content').update('');
					Ext.fly('modal_head').update('<h3>Du hast für diesen Posten schon abgestimmt!</h3><p>Du kannst aber noch für die anderen Posten abstimmen.</p>');
                },
                headers: {
                    'Accept': 'application/json'
                },
                method: 'POST',
                params: {
                    celebrity_id: celebrityId
                }
            });
        });
        
        that.filter.on('keyup', function(){
            de.meinkabinett.Model.celebrities.filter('name', that.filter.getValue(), true);
        });
        
		Ext.fly('modal_closebutton').on('click', function() {
			that.closeWindow();
		});
		
		var name = de.meinkabinett.Model.selectedSecretaryName;
		Ext.fly('modal_head').update('<h3>' + name + ' wählen</h3>' +
                            '<p>Wähle den Promi, den Du gerne als ' + name + ' sehen würdest.</p>');
    },
	showWindow: function() {
		this.filter.render('modal_content');
        this.render('modal_content');
		Ext.fly('modal_window').show();
	},
	closeWindow: function() {
		Ext.fly('modal_content').update('');
		Ext.fly('modal_window').hide();
	}
});

