/* --- js/imgblock.js -- start ------------------------------------- *
 * 
 * $Id: imgblock.js 485 2011-06-23 21:29:44Z vugluskr $
 *
 * Require:
 *   jquery.js
 *   jquery.json.js
 */

function imgblock_dialog_edit() {
    imgblock_dialog_edit.superclass.constructor.apply(this, arguments);
    this.fetch_data = {'pzl': this.master.bind};
}
extend(imgblock_dialog_edit, ui_dialog);
mixin(imgblock_dialog_edit.prototype, {
    'alt_action': null,

    'jqm_args': {'modal': true},
    'fetch_url': '/pzl/ajax/form/imgblock.html',

    'data_extend': function() {
        this.data['pzl'] = this.master.bind; 
    },

    'btn_remove': function(btn) {
        this.alt_action = 'remove';
        this.input_disable();

        var dz = this._get_dynzone();
        dz.empty();
        this._form.attr('action', btn.getAttribute('set_action'));

        var ent;
        ent = document.createElement('p');
        ent.setAttribute('class', 'text_center');
        ent.appendChild(document.createTextNode('Вы действительно хотите удалить этот пазл?'));
        dz.append(ent);

        var btn_yes, bnt_no;
        ent = document.createElement('p');
        ent.setAttribute('class', 'text_center');
        btn_yes = document.createElement('button');
        btn_yes.appendChild(document.createTextNode('Да'));
        btn_no = document.createElement('button');
        btn_no.appendChild(document.createTextNode('Нет'));
        ent.appendChild(btn_yes);
        ent.appendChild(btn_no);

        dz.append(ent);
        var self = this;
        $(btn_yes).click(function() { self.commit(); });
        this.wrap.jqmAddClose(btn_no);
    }});

function imgblock_dialog_edit_listing() {
    imgblock_dialog_edit_listing.superclass.constructor.apply(this, arguments);
}
extend(imgblock_dialog_edit_listing, imgblock_dialog_edit);
mixin(imgblock_dialog_edit_listing.prototype, {
    'data_extend': function() {
        imgblock_dialog_edit_listing.superclass.data_extend.apply(this, arguments);
        this.data['from_listing'] = 'yes';
    },
    'reply_ok': function(r, stat, xhr) {
        imgblock_dialog_edit_listing.superclass.reply_ok.apply(this, arguments);
        if (this.alt_action == 'remove') {
            var ent = this.master.root;
            while (ent.length && ! ent.hasClass('imgblock_wrap'))
                ent = ent.parent();
            if (! ent.length)
                throw Error('Unable to find .imglbock_wrap element under .imgblock element');
            ent.remove();
        }
    }});

function imgblock(root) {
    imgblock.superclass.constructor.call(this, root);
    this.swbtn_tooltip_tout = null;

    root = this.root;
    root.get(0).hndl = this;
    this.bind = root.attr('bind');
    root.removeAttr('bind');

    this.btns_wrap = root.find('*[bind="btn_wrap"]');
    this.assemble_url_ent = root.find('a[bind="url_process"]');

    this.assemble_modes = new Array({
        url: '/' + this.bind + '/',
        tooltip_html: 'Сложность: <b>простая</b> - переворот кусочков отключен.'
        }, {
        url: '/' + this.bind + '/hard/',
        tooltip_html: 'Сложность: <b>тяжелая</b> - включен переворот кусочков.'});
    this.assemble_mode_idx = 0;

    var self = this;
    var ent;
    
    // Открытие малого блока модерации.
    ent = root.find('*[bind="moderation_ctrl"]');
    if (ent.length) {
        var ctrl = ent;
        root.find('*[bind="moderation"]').click(function(){ ctrl.slideToggle('fast'); });
    }

    ent = root.find('*[bind="btn_complSwitch"]');
    ent.click(function(){ self.switch_assemble_mode(); });
    ent.mouseover(function(){ 
        var ent = this;
        self.swbtn_tooltip_tout = setTimeout(function(){
            self.swbtn_tooltip_in(ent);
        }, 750) 
    });
    ent.mouseout(function(){ self.swbtn_tooltip_out(this); });
    
    var a;
    a = ent.attr('class');
    a = a.match(/\bbg_(?:norm|hard)\b/);
    if (a && a.length && a[0] == 'bg_hard')
        this.assemble_mode_idx = 1;

    this.dialog = {};

    ent = root.find('*[bind="btn_edit"]').get(0);
    this.dialog['edit'] = new this._dialog_edit(this, undefined, {'trigger': ent});
}
extend(imgblock, _ui);
mixin(imgblock.prototype, {
    '_dialog_edit': imgblock_dialog_edit,

    '_get_mode': function(idx) {
        if (idx === undefined)
            idx = this.assemble_mode_idx;
        else
            idx %= this.assemble_modes.length;
        return this.assemble_modes[idx];
    },

    'switch_assemble_mode': function() {
        var mode_idx = this.assemble_mode_idx + 1;
        mode_idx %= this.assemble_modes.length;
        var mode = this._get_mode(mode_idx);

        this.assemble_url_ent.attr('href', mode.url);

        var btn = this.btns_wrap;
        var a;
        switch (mode_idx) {
        case 0:
            btn.css({backgroundPosition: '435px 50%'});
            btn.removeClass('bg_hard');
            btn.animate({backgroundPosition:"255px 50%"}, 1000);
            break;
        case 1:
            btn.css({backgroundPosition: '255px 50%'});
            btn.removeClass('bg_norm');
            btn.animate({backgroundPosition:"75px 50%"}, 1000);
            break;
        }

        this.assemble_mode_idx = mode_idx;
    },

    'swbtn_tooltip_in': function(btn) {
        var ent;

        ent = document.createElement('div');
        ent.setAttribute('class', 'tooltip opacity_85');
        ent.appendChild(document.createElement('h3'));
        ent = ent.lastChild;
        ent.appendChild(document.createTextNode('Переключатель уровня сложности'));
        ent = ent.parentNode;

        ent.appendChild(document.createElement('hr'));
        ent.appendChild(document.createElement('div'));
        ent = ent.lastChild;
        ent.appendChild(document.createElement('p'));
        ent = ent.lastChild;
        
        var mode = this._get_mode();
        $(ent).html(mode.tooltip_html);

        ent = ent.parentNode.parentNode;

        $('.page').append(ent);
        btn.tooltip = ent;

        var offs_x, offs_y, offs;
        offs = $(btn).offset();
        offs_x = offs.left + $(btn).width() + 5;
        offs_y = offs.top - $(ent).height() - 10;

        $(ent).css({
            top: offs_y + 'px',
            left: offs_x + 'px'});
    },
    'swbtn_tooltip_out': function(btn) {
        if (btn.tooltip) {
            $(btn.tooltip).remove();
            delete btn.tooltip;
        } else {
            clearTimeout(this.swbtn_tooltip_tout);
        }
    }});

function imgblock_listing(root) {
    imgblock_listing.superclass.constructor.apply(this, arguments);
}
extend(imgblock_listing, imgblock);
mixin(imgblock_listing.prototype, {
    '_dialog_edit': imgblock_dialog_edit_listing});
/* --- js/imgblock.js -- end --------------------------------------- */

