/**
 * Mix-up games: a cell
 * Serge Dolgov (C) 2002
 * studio.quazzle.com
 */

function QMixUpCell_load(src) {
    if (src) {
        this.face = new Image(this.width, this.height);
        this.face.src = src;
        this.loaded = this.invalid = true;
    }
}
 
function QMixUpCell_show(show) {
    if (this.loaded) {
        if (show && this.face.complete && this.invalid) {
            this._img_.src = this.face.src;
            this.invalid = false;
        }
        this.layer.show(this.visible = show);
    }
}

function QMixUpCell_stop() {
    if (this._TID) {
        clearTimeout(this._TID);
        this._TID = null;
    }
}

function QMixUpCell_moveTo(x, y) {
    this.stop();
    this.layer.moveTo(this.posx = x, this.posy = y);
}

function QMixUpCell_slideTo(x, y) {
    if (this.visible) {
        this.stop();
        this._TX = x;
        this._TY = y;
        this.trackSliding(x, y);
    } else {
        this.moveTo(x, y);
    }
}

function QMixUpCell_shake() {
    if (this.visible) {
        this.stop();
        this.trackShaking(0, this.posx, this.posy);
    }
}

function QMixUpCell_trackSliding(x, y) {
    if ((this._TX == x) && (this._TY == y)) {
        this._TID = null;
        var dx = Math.round(x - this.posx);
        var dy = Math.round(y - this.posy);
        if (dx || dy) {
            if (dx) dx = dx > 0 ? Math.ceil(dx/4) : Math.floor(dx/4);
            if (dy) dy = dy > 0 ? Math.ceil(dy/4) : Math.floor(dy/4);
            this.layer.moveTo(this.posx += dx, this.posy += dy);
            this._TID = setTimeout(this.name + ".trackSliding(" + x + "," + y + ")", 30);
        } else {
            this.layer.moveTo(this.posx = x, this.posy = y);
        }
    }
}

function QMixUpCell_trackShaking(phase, x, y) {
    this._TID = null;
    if (phase < 20) {
        var m = 3 * Math.sin(.16 * phase);
        this.layer.moveTo(Math.round(x + m * Math.sin(phase)),
            Math.round(y + m * Math.cos(phase)));
        this._TID = setTimeout(this.name + ".trackShaking(" + (++phase) + "," +
            x + "," + y + ")", 20);
    } else {
        this.layer.moveTo(this.posx, this.posy);
    }
}

function QMixUpCell_focus() {
    this.layer.focus();
}

function QMixUpCell_doClick() {
    if (!this._TID) {
        if (this.onClick) this.onClick(this.tag);
    }
    return false;
}

function QMixUpCell_content() {
    with (this.parent) {
        document.write('<table border="0" cellspacing="0" cellpadding="0" width="' + (width + 8) + '" height="' + (height + 8) +
            '"><tr><td align="left" valign="top"><a href="#" title="" onClick="return false" onMouseDown="return ' + name +
            '.doClick()" onMouseOver="window.top.status=\'\';return true"><img name="' + name + '" src="' + imagesrc +
            '" border="0" width="' + width + '" height="' + height + '" alt=""></a></td></tr></table>');
    }
}

function QMixUpCell(parent, name, width, height, posx, posy, imagesrc, createstub) {
    this.init(parent, name);
    this.width = width;
    this.height = height;
    this.visible = false;
    this.loaded = !createstub;
    this.invalid = false;
    this.posx = posx;
    this.posy = posy;
    this.imagesrc = imagesrc || null;
    this.load = QMixUpCell_load;
    this.show = QMixUpCell_show;
    this.stop = QMixUpCell_stop;
    this.moveTo = QMixUpCell_moveTo;
    this.slideTo = QMixUpCell_slideTo;
    this.shake = QMixUpCell_shake;
    this.focus = QMixUpCell_focus;
    this.doClick = QMixUpCell_doClick;
    this.trackSliding = QMixUpCell_trackSliding;
    this.trackShaking = QMixUpCell_trackShaking;
    this.layer = new QLayer(this, "layer", posx, posy, width + 8, null, this.visible, QMixUpCell_content);
    this.layer.effects = QLayer.FAST;
    this.layer.setFilter(QLayer.SHADOW);
    if (document.layers) {
        this._img_ = this.layer.layer.document.images[this.name]; //NS4
    } else {
        this._img_ = document.images[this.name];
    }
    this.face = this._img_;
}
QMixUpCell.prototype = new QControl();