/**
 * @extends SyqComponent
 */
function SyqComponent_fileupload()
{
    this.select = new SyqEvent;
    this.complete = new SyqEvent;
    this.filterNotMatch = new SyqEvent;

    this.saveImgSrc = null;

    this.init = function()
    {
        var button = this.q('#form .button');
        this.q('#fname').width(this.q('#form').width() - (button.length ? button.width() : 0) - 10);
    }

    this.reset = function()
    {
        this.q('#fname').html('');
        this.q('#fileName').val('');
        this.q('#tempFileName').val('');
        this.q('#form').removeClass('uploading');
        this.q('#form').removeClass('uploaded');
    };

    this.form_mousemove = function(e)
    {
        if (!this.q('#enable').val()) return;

        var form = this.q('#form');
        this.q('#file').css({
            top: e.pageY-form.offset().top+'px',
            left: e.pageX-form.offset().left+'px'
        });

        this.img_mouseover();
    };

    this.img_mouseover = function()
    {
        if (!this.q('#enable').val()) return;

        if (!this.q('#img').is(':visible')) return;

        this.q('#img').css('opacity',0.2);
        this.q('#imgText')
            .css('width', this.q('#form').width()+"px")
            .css('height', this.q('#form').height()+"px")
            .html('Кликни,<br />чтобы<br />сменить!')
            .show();
    };
    this.form_mouseleave = function()
    {
        if (!this.q('#img').is(':visible')) return;
        this.q('#img').css('opacity',1);
        this.q('#imgText').hide();
    };

    this.file_change = function(e)
    {
        this.reset();
        this.q('#fname').html(this.q('#file').val());

        var filter = this.q('#filter').val();
        if (filter!='')
        {
            var re = new RegExp(filter);
            if (!re.test(this.q('#file').val()))
            {
                this.filterNotMatch.call();
                this.reset();
                return false;
            }
        }

        if (!this.select.call(this.q('#file').val())) return false;

        this.q('#form').addClass('uploading');

        this.q('#form img').animate({opacity: "-0.1"}, 100);


        this.q('#imgText').html('<img src="/img/uploadWait.gif" />');
        this.q('#enable').val('');

        this.q('#form').submit();

        var self = this;

        this.q('#form img').unbind().load(function() {
            self.q('#imgText').hide();
            self.q('#img').css('opacity',1);
            self.q('#enable').val('1');
        });

        var frame = this.q('#frame');
        frame.unbind().load(function() {
            var response = $(frame[0].contentWindow.document.body).text();
            self.q('#form').removeClass('uploading');
            self.q('#form').addClass('uploaded');
            self.q('#fileName').val(self.q('#file').val());
            self.q('#tempFileName').val(response);

            if (self.complete.call(response)===false)
            {
                self.q('#imgText').hide();
                self.q('#img').css('opacity',1);
                self.q('#enable').val('1');
            }

            self.q('#file').show();
        });

        this.q('#file').hide();
    };

    this.setImage = function(url)
    {
        if (url) this.q('#form img').attr('src',url).fadeIn();
        else this.q('#form img').fadeOut();
    };

    this.enable = function(isEnable)
    {
        this.q('#enable').val(isEnable ? '1':'');
    }
};
SyqComponent_fileupload.prototype = new SyqComponent;
