Ext.namespace('Parafiada.Competition');

var data_window;

function rand(n){
    return (Math.floor(Math.random() * n + 1));
}

Parafiada.Competition.CreateWindow = function(user, gridId){
    data_window = Parafiada.Helper.CreateWindow("Szczegóły uczestnika: " + user['first_name'] + " " + user['last_name'], 580, 280, 'close', true);
    data_window.add(new Parafiada.Competition.Info(user, gridId, data_window));
    data_window.show();
}

Parafiada.Competition.Info = function(user, gridId, win){
    var dsWorkshop = new Ext.data.JsonStore({
        totalProperty: "total",
        root: "results",
        id: "dsWorkshop",
        url: '/iframe/workshop',
        fields: [{
            name: 'name',
            type: 'string'
        }],
        baseParams: {
            id: user['id']
        }
    });
    dsWorkshop.load();
    
    function onUploadSuccess(dialog, filename, resp_data, record){
        Ext.get('pic_pan').dom.src = Ext.get('pic_pan').dom.src + '/' + +new Date().getMilliseconds();
    };
    
    
    // ULOADER
    var dialog = new Ext.ux.UploadDialog.Dialog({
        url: '/iframe/upload',
        reset_on_hide: false,
        allow_close_on_upload: true,
        upload_autostart: true,
        permitted_extensions: ['jpg'],
        base_params: {
            id: user['id']
        }
    });
    dialog.on('uploadsuccess', onUploadSuccess);
    
    var button = new Ext.Button({
        text: 'dodaj swoje zdjęcie',
        handler: function(b, e){
            dialog.show('my-dlg');
        }
    });
    
    var empty_text = '<div class="user_info">' +
    '<img src="/iframe/show/id/' +
    user['id'] +
    '/height/100/width/100/r/' +
    new Date().getMilliseconds() +
    '" align="right" id="pic_pan"/>' +
    '<h1>' +
    user['first_name'] +
    ' ' +
    user['last_name'] +
    '</h1>' +
    '<h3>' +
    (user['type'] == 'organizator' ? 'komitet organizacyjny' : user['type']) +
	(user['type'] == 'organizator' ? "<br /> " + user['institution_name'] : '') +
    '</h3>' +
    '<hr />' +
    (user['type'] != 'organizator' ? '<h3>' + user['institution_name'] + '</h3>' : '') +
    '<h3>' +
    user['address_city'] +
    ', ' +
    user['country_name'] +
    '</h3>' +
    '</div>';
    
    var tpl = new Ext.XTemplate('<div class="user_info">' +
    '<img src="/iframe/show/id/' +
    user['id'] +
    '/height/100/width/100/r/' +
    new Date().getMilliseconds() +
    '" align="right" id="pic_pan"/>' +
    '<h1>' +
    user['first_name'] +
    ' ' +
    user['last_name'] +
    '</h1>' +
    '<h3>' +
    (user['type'] == 'organizator' ? 'komitet organizacyjny' : user['type']) +
	(user['type'] == 'organizator' ? "<br /> " + user['institution_name'] : '') +
    '</h3>' +
    '<hr />' +
    (user['type'] != 'organizator' ? '<h3>' + user['institution_name'] + '</h3>' : '') +
    '<h3>' +
    user['address_city'] +
    ', ' +
    user['country_name'] +
    '</h3>', '<br />', '<h4>Uczestniczył w następujących warsztatach teatralnych:</h4>', '<tpl for=".">', '<h4>• {name}</h4>', '</tpl>', '</div>');
    
    
    var panel = new Ext.Panel({
        layout: 'anchor',
        items: [new Ext.DataView({
            store: dsWorkshop,
            tpl: tpl,
            emptyText: empty_text
        }), button]
    });
    return panel;
}


// HELPER ---------------------------------------------------------------------
Ext.namespace('Parafiada.Helper');

Parafiada.Helper.CreateWindow = function(title, width, height, closeAction, modal){
    if ((title == null) || (title == 'undefined')) {
        title = '';
    }
    if ((width == null) || (width == 'undefined') || (width < 0)) {
        width = 640;
    }
    
    if ((height == null) || (height == 'undefined') || (height < 0)) {
        height = 480;
    }
    
    if ((closeAction == null) || (closeAction == 'undefined')) {
        closeAction = 'close';
    }
    
    if ((modal == null) || (modal == 'undefined')) {
        modal = true;
    }
    
    var helper = new Parafiada.Helper.WinLoader({});
    var win = new Ext.Window({
        title: title,
        layout: 'fit',
        width: width,
        height: height,
        closeAction: closeAction,
        plain: true,
        modal: modal,
        plugins: [helper]
    });
    return win;
}
Parafiada.Helper.WinLoader = function(config){
    Ext.apply(this, config, {
        init: function(win){
            this.win = win;
            this.waitWindow.show();
            win.on('show', this.hide, this);
            win.on('destroy', this.hide, this);
        },
        hide: function(){
            this.waitWindow.hide();
        }
    });
    
    if (!this.id) {
        this.id = Ext.id();
    }
    
    if (typeof(this.waitMsg) == 'undefined') {
        this.waitMsg = 'Czekaj...';
    }
    
    var waitMsg = '<table width="100%" height="100%" border="0"><tbody><tr><td align="center" valign="middle">' +
    this.waitMsg +
    '</td></tr></tbody></table>';
    
    this.waitWindow = new Ext.Window({
        closable: false,
        modal: true,
        resizable: false,
        plain: true,
        width: 200,
        height: 100,
        html: waitMsg
    });
};

