
var lastOpenedGallery = '';

/** @id MochiKit.Visual.MyScale */
MochiKit.Visual.MyScale = function (element, percent, options) {
    this.__init__(element, percent, options);
};

MochiKit.Visual.MyScale.prototype = new MochiKit.Visual.Base();

MochiKit.Base.update(MochiKit.Visual.MyScale.prototype, {
    /***

    Change the size of an element.

    @param percent: final_size = percent*original_size

    @param options: several options changing scale behaviour

    ***/
    __init__: function (element, percent, /* optional */options) {
        this.element = MochiKit.DOM.getElement(element)
        options = MochiKit.Base.update({
            scaleX: true,
            scaleY: true,
            scaleContent: true,
            scaleFromCenter: false,
            scaleMode: 'box',  // 'box' or 'contents' or {} with provided values
            scaleFrom: 100.0,
            scaleTo: percent
        }, options || {});
        this.start(options);
    },

    /** @id MochiKit.Visual.MyScale.prototype.setup */
    setup: function () {
        this.restoreAfterFinish = this.options.restoreAfterFinish || false;
        this.elementPositioning = MochiKit.DOM.getStyle(this.element,
                                                        'position');

        var fe = MochiKit.Iter.forEach;
        var b = MochiKit.Base.bind;
        this.originalStyle = {};
        fe(['top', 'left', 'width', 'height', 'fontSize'],
            b(function (k) {
                this.originalStyle[k] = this.element.style[k];
            }, this));

        this.originalTop = this.element.offsetTop;
        this.originalLeft = this.element.offsetLeft;

        var fontSize = MochiKit.DOM.getStyle(this.element,
                                             'font-size') || '100%';
        fe(['em', 'px', '%'],
            b(function (fontSizeType) {
            if (fontSize.indexOf(fontSizeType) > 0) {
                this.fontSize = parseFloat(fontSize);
                this.fontSizeType = fontSizeType;
            }
        }, this));

        this.factor = (this.options.scaleTo - this.options.scaleFrom)/100;

        if (/^content/.test(this.options.scaleMode)) {
            this.dims = [this.element.scrollHeight, this.element.scrollWidth];
        } else if (this.options.scaleMode == 'box') {
            this.dims = [this.element.offsetHeight, this.element.offsetWidth];
        } else {
            this.dims = [this.options.scaleMode.originalHeight,
                         this.options.scaleMode.originalWidth];
        }
    },

    /** @id MochiKit.Visual.MyScale.prototype.update */
    update: function (position) {
        var currentScale = (this.options.scaleFrom/100.0) +
                           (this.factor * position);
        if (this.options.scaleContent && this.fontSize) {
            MochiKit.DOM.setStyle(this.element, {
                fontSize: this.fontSize * currentScale + this.fontSizeType
            });
        }
        this.setDimensions(this.dims[0] * currentScale,
                           this.dims[1] * currentScale);
    },

    /** @id MochiKit.Visual.MyScale.prototype.finish */
    finish: function () {
        if (this.restoreAfterFinish) {
            MochiKit.DOM.setStyle(this.element, this.originalStyle);
        }
    },

    /** @id MochiKit.Visual.MyScale.prototype.setDimensions */
    setDimensions: function (height, width) {
        var d = {};
        if (this.options.scaleX) {
            d.width = Math.round(width) + 'px';
        }
        if (this.options.scaleY) {
            d.height = Math.round(height) + 'px';
        }
        if (this.options.scaleFromCenter) {
            var topd = (height - this.dims[0])/2;
            var leftd = (width - this.dims[1])/2;
            if (this.elementPositioning == 'absolute') {
                if (this.options.scaleY) {
                    d.top = this.originalTop - topd + 'px';
                }
                if (this.options.scaleX) {
                    d.left = this.originalLeft - leftd + 'px';
                }
            } else {
                if (this.options.scaleY) {
                    d.top = -topd + 'px';
                }
                if (this.options.scaleX) {
                    d.left = -leftd + 'px';
                }
            }
        }
        //d['margin-bottom'] = Math.round(- 100 * (1 - ((this.dims[0] - this.element.clientHeight) / this.dims[0]))) + 'px';
        //d['margin-bottom'] = Math.round((-100.0 * (1 - (this.dims[0] - this.element.clientHeight))) / this.dims[0]) + 'px';
        //d['margin-bottom'] = Math.round(- 100 * (1.1 - ((this.dims[0] - height) / (1.0 * this.dims[0])))) + 'px';
        //d['margin-bottom'] = (Math.round(- 90 * (1.0 - ((this.dims[0] - height) / (1.0 * this.dims[0])))) - 10) + 'px'; // Firefox Ok!
        
        //d['margin-bottom'] = Math.round(- 100 * (1.0 - ((this.dims[0] - height) / (1.0 * this.dims[0])))) + 'px'; // IE Ok!
        //log('margin-bottom: ' + d['margin-bottom']);
        photoEmenent = $(this.element.id + 'photo')
        photoTopAndMarginBottom = Math.round(- 100 * (1.0 - ((this.dims[0] - height) / (1.0 * this.dims[0]))))
        
        MochiKit.DOM.setStyle(this.element, d);
        MochiKit.DOM.setStyle(photoEmenent, {'top': photoTopAndMarginBottom + 'px',
                                            'margin-bottom': photoTopAndMarginBottom + 'px'});
        //log(' * margin-bottom: ' + MochiKit.DOM.getStyle(this.element, 'margin-bottom'));
    }
});


MochiKit.Visual.mySlideDown = function (element, /* optional */ options) {
    /***

    Slide an element down.
    It needs to have the content of the element wrapped in a container
    element with fixed height.

    ***/
    var d = MochiKit.DOM;
    var b = MochiKit.Base;
    element = d.getElement(element);
    if (!element.firstChild) {
        throw "MochiKit.Visual.slideDown must be used on a element with a child";
    }
    d.removeEmptyTextNodes(element);
    var oldInnerBottom = d.getStyle(element.firstChild, 'bottom') || 0;
    var elementDimensions = MochiKit.Style.getElementDimensions(element);
    options = b.update({
        scaleContent: false,
        scaleX: false,
        scaleFrom: 0,
        scaleMode: {originalHeight: elementDimensions.h,
                    originalWidth: elementDimensions.w},
        restoreAfterFinish: true,
        afterSetupInternal: function (effect) {
            d.makePositioned(effect.element);
            d.makePositioned(effect.element.firstChild);
            if (b.isOpera()) {
                d.setStyle(effect.element, {top: ''});
            }
            d.makeClipping(effect.element);
            d.setStyle(effect.element, {height: '0px'});
            MochiKit.Style.showElement(effect.element);
        },
        afterUpdateInternal: function (effect) {
            d.setStyle(effect.element.firstChild,
               {bottom: (effect.dims[0] - effect.element.clientHeight) + 'px'})
        },
        afterFinishInternal: function (effect) {
            
            d.undoClipping(effect.element);
            // IE will crash if child is undoPositioned first
            
            if (b.isIE()) {
                d.undoPositioned(effect.element);
                d.undoPositioned(effect.element.firstChild);
            } else {
                d.undoPositioned(effect.element.firstChild);
                d.undoPositioned(effect.element);
            }
            
            d.setStyle(effect.element.id + 'dotpattern', {display: ''});
            d.setStyle(effect.element, {overflow: ''});
            lastOpenedGallery = '';
        },
        afterFinish: function (effect) {
            if (!b.isIE()) swapPhotoInfoSpanToNonGalleryViewingMode(effect.element.id);
        },
        duration: 0.5
    }, options || {});
    return new MochiKit.Visual.MyScale(element, 100, options);
};

MochiKit.Visual.mySlideUp = function (element, /* optional */ options) {
    /***

    Slide an element up.
    It needs to have the content of the element wrapped in a container
    element with fixed height.

    ***/
    
    var d = MochiKit.DOM;
    var b = MochiKit.Base;
    element = d.getElement(element);
    if (!element.firstChild) {
        throw "MochiKit.Visual.slideUp must be used on a element with a child";
    }
    d.removeEmptyTextNodes(element);
    var oldInnerBottom = d.getStyle(element.firstChild, 'bottom');
    options = b.update({
        scaleContent: false,
        scaleX: false,
        scaleMode: 'box',
        scaleFrom: 100,
        restoreAfterFinish: true,
        beforeStartInternal: function (effect) {
            d.makePositioned(effect.element);
            d.makePositioned(effect.element.firstChild);
            if (b.isOpera()) {
                d.setStyle(effect.element, {top: ''});
            }
            d.makeClipping(effect.element);
            MochiKit.Style.showElement(effect.element);
        },
        afterUpdateInternal: function (effect) {
            d.setStyle(effect.element.firstChild,
            {bottom: (effect.dims[0] - effect.element.clientHeight) + 'px'});
        },
        afterFinishInternal: function (effect) {
            MochiKit.Style.hideElement(effect.element);
            d.undoClipping(effect.element);
            d.undoPositioned(effect.element.firstChild);
            d.undoPositioned(effect.element);
            d.setStyle(effect.element.firstChild, {bottom: oldInnerBottom});
            
            $(effect.element.id + 'dotpattern').style.display = 'none';
            lastOpenedGallery = effect.element.id;
        },
        afterFinish: function (effect) {
            swapPhotoInfoSpanToGalleryViewingMode(effect.element.id);
        },
        duration: 0.5
    }, options || {});
    
    return new MochiKit.Visual.MyScale(element, 0, options);
};

MochiKit.Visual.PAIRS['myslide'] = ["mySlideDown", "mySlideUp"];


ToggleGallery = function(element, newLimit) {
    tmp = {queue:{scope:'togglegallery', position:'end', limit:newLimit}};
    MochiKit.Visual.toggle(element, 'myslide', tmp);
}

GoToNextOrToggleGallery = function(element, newLimit) {
    if ( ! MochiKit.DOM.isVisible(element)) {
        if (canMoveToNext(element)) {
            goToNextImg(element);
        } else {
            // Reset to first image
            JSImageGalleryData[element].index = 1;
            goToPrevImg(element);
            ToggleGallery(element)       
        }
    } else {
        ToggleGallery(element);
    }
}

SwitchToOrFromGallery = function(element) {
    if (lastOpenedGallery == '') {
        ToggleGallery(element, 1);
        //lastOpenedGallery = element;
    } else if (lastOpenedGallery == element) {
        ToggleGallery(element, 1);
        //lastOpenedGallery = '';
    } else {
        ToggleGallery(lastOpenedGallery, 2);
        ToggleGallery(element, 2);
        //lastOpenedGallery = element;
    }
}

GoToNextOrSwitchToOrFromGallery = function(element) {
    if (lastOpenedGallery == '') {
        GoToNextOrToggleGallery(element, 1);
        //lastOpenedGallery = element;
    } else if (lastOpenedGallery == element) {
        GoToNextOrToggleGallery(element, 1);
        //lastOpenedGallery = '';
    } else {
        ToggleGallery(lastOpenedGallery, 2);
        ToggleGallery(element, 2);
        //lastOpenedGallery = element;
    }
}

