/**
 * Used for stylizing upload file field.
 *
 * @author mdiordienko
 */

var Stylize = {};
        Stylize.UploadBox = Class.create({
                initialize: function(id, options) {
                    this.options = Object.extend({
                        //default options
                        wrapperHeight: '24px',
                        iconsPath: '/images/uploadBox/icons.png'

                    }, options || {});
                    this.upload_box_wrapper = $(id);

                    try {
                        this.file_input = $$('#' + id + ' > input[type=file]').reduce();
                        this.file_name = document.createElement('div');
                        this.file_name.style.display = 'none';
                        this.file_name.style.background = 'url(images/'+ this.options.iconsPath +')';
                        this.stylize();
                    } catch(e) {
                        console.log(e.toString());
                        this.stylized = false;
                    }
                },
                stylize: function() {                    
                        var activeButton = document.createElement('div');
                        var bb = document.createElement('div');
                        var bl = document.createElement('div');
                        this.file_name.setAttribute('id','FileName');
                        activeButton.setAttribute('id','activeBrowseButton');
                        this.init_input();
                        Event.observe(this.file_name, 'click', function(e) { this.clear_file(); }.bind(this));
                        this.file_input.className = 'customFile';
                        bl.className = 'blocker';
                        bb.className = 'fakeButton';
                        activeButton.className = 'fakeButton';
                        this.upload_box_wrapper.appendChild(bb);
                        this.upload_box_wrapper.appendChild(bl);
                        this.upload_box_wrapper.appendChild(activeButton);
                        this.upload_box_wrapper.appendChild(this.file_name);
                        //alert(this.options.wrapperHeight);
                        this.upload_box_wrapper.setStyle({
                            height: this.options.wrapperHeight
                        });
                        this.stylized = true;
                    return this;

                },
                clear_file: function()
                {
                    if (this.stylized) {
                        var oldFileInputName = this.file_input.readAttribute('name');
                        var newFileInput = new Element('input', {'name': oldFileInputName,'type': 'file', 'value': '', 'class':'customFile','id':'fileInput2'})
                        Element.replace(this.file_input, newFileInput);
                        //this.file_input.replace();
                        this.file_input = newFileInput;
                        //alert(typeof(newFileInput));
                        //this.file_input.replace(newFileInput);
                        this.init_input();
                        this.file_name.style.background = '';
                        this.file_name.update('');
                        //this.file_input.value = 'sss.dll';
                    }
                },
                init_input: function()
                {
                    this.file_input.value = '';
                        var hd = function () {
                            file = this.file_input.value;
                            //alert(arguments.toString());
                            reWin = /.*\\(.*)/;
                            var fileTitle = file.replace(reWin, "$1"); //�������� �������� �����
                            reUnix = /.*\/(.*)/;
                            fileTitle = fileTitle.replace(reUnix, "$1"); //�������� �������� �����

                            var close_link = new Element('a', {'id':'removeUploadFile'});
                            //document.createElement('a');
                            //close_link.setAttribute('id', 'removeUploadFile');
                            close_link.update('[X]');
                            this.file_name.innerHTML = fileTitle;
                            this.file_name.appendChild(close_link);

                            //Event.observe(this.close_link, 'click', f4.bindAsEventListener(this));
                            //close_link.observe('click', function() {this.close_file()}.bindAsEventListener(this));

                            //Event.observe('removeUploadFile', 'click', function(e) { elt = $(Event.element(event)).up(); elt.hide();});
                            //this.file_name.observe('click', function() {alert('ssss')});
                            //Event.observe(this.close_link, 'click', function() { alert('sss'); });

                            //this.file_name.appendChild(close_link);
                            var RegExExt =/.*\.(.*)/;
                            var ext = fileTitle.replace(RegExExt, "$1");//� ��� ����������
                            var pos;
                            if (ext){
                            switch (ext.toLowerCase())
                            {
                            case 'doc': pos = '0'; break;
                            case 'bmp': pos = '16'; break;
                            case 'jpg': pos = '32'; break;
                            case 'jpeg': pos = '32'; break;
                            case 'png': pos = '48'; break;
                            case 'gif': pos = '64'; break;
                            case 'psd': pos = '80'; break;
                            case 'mp3': pos = '96'; break;
                            case 'wav': pos = '96'; break;
                            case 'ogg': pos = '96'; break;
                            case 'avi': pos = '112'; break;
                            case 'wmv': pos = '112'; break;
                            case 'flv': pos = '112'; break;
                            case 'pdf': pos = '128'; break;
                            case 'exe': pos = '144'; break;
                            case 'txt': pos = '160'; break;
                            default: pos = '176'; break;
                            };
                            this.file_name.style.display = 'block';
                            this.file_name.style.background = 'url('+ this.options.iconsPath +') no-repeat 0 -'+pos+'px';
                            };
                        }
                        Event.observe(this.file_input, 'change', hd.bindAsEventListener(this));
                }


            });