/**
 * @author Falko Zander, www.falko-zander.de
 */

/**
 * @param src String
 * @param width Number
 * @param height Number
 * @param top Number
 * @param left Number
 * @param cb_on_close Function
 * @param relative_to_DOM Element
 * @return Object 
 */
function cImageZoom(src, width, height, top, left, cb_on_close, relative_to_DOM) {
    this.width = width;
    this.height = height;
    this.relative_to_DOM = relative_to_DOM || null;
    this.top = top;
    this.left = left;
    
    this.DOM_div = document.createElement('div');
    this.DOM_div.style.display = 'none';
    this.DOM_div.className = 'image_zoom';
    this.DOM_img = document.createElement('img');
    this.DOM_img.src = src;
    this.DOM_img.width = width;
    this.DOM_img.height = height;
    this.DOM_img.alt = '';
    this.DOM_div.appendChild(this.DOM_img);
    this.DOM_txt = document.createElement('div');
    this.DOM_div.appendChild(this.DOM_txt);
    this.DOM_cls = document.createElement('div');
    var img = document.createElement('img');
    img.src = '/images/basic/close.png';
    img.style.cursor = 'pointer';
    this.DOM_cls.appendChild(img);
    this.DOM_div.appendChild(this.DOM_cls);
    document.body.appendChild(this.DOM_div);
    
    var THIS = this;
    img.onmouseover = function() { this.src = '/images/basic/close_hover.png'; }
    img.onmouseout = function() { this.src = '/images/basic/close.png'; }
    img.onclick = function() { THIS.close(cb_on_close); }
    this.DOM_cls.style.display = 'none';
        
    this.set_position();
    
    this.text = null;
    this.interval = null;
    this.dnd = null;
    
    this.steps = -1;
}

/**
 * @return Object
 */
cImageZoom.prototype.destructor = function() {
    if (this.interval != null) window.clearInterval(this.interval);
    document.body.removeChild(this.DOM_div);
    
    this.width = null;
    this.height = null;
    this.DOM_div = null;
    this.DOM_img = null;
    this.DOM_txt = null;
    this.DOM_cls = null;
    this.text = null;
    this.interval = null;
    if (this.dnd != null) this.dnd = this.dnd.destructor();
    this.steps = null;
    
    return null;
}

cImageZoom.prototype.set_position = function() {
    var top = this.top, left = this.left;
    if (typeof this.relative_to_DOM != null) {
        var p = getAbsoluteDimensions($(this.relative_to_DOM));
        top = this.top + parseInt(p.offsetTop);
        left = this.left + parseInt(p.offsetLeft);
    }
    
    this.DOM_div.style.top = top + 'px';
    this.DOM_div.style.left = left + 'px';
}

/**
 * @param text String
 */
cImageZoom.prototype.set_text = function(text) {
    this.text = text;
}

/**
 * @param top Number
 * @param left Number
 * @param width Number
 * @param height Number
 */
cImageZoom.prototype.set_text_position = function(top, left, width, height) {
    this.DOM_txt.style.top = top + 'px';
    this.DOM_txt.style.left = left + 'px';
    if (typeof width != 'undefined' && width != 0) this.DOM_txt.style.width = width + 'px';
    if (typeof height != 'undefined' && height != 0) this.DOM_txt.style.height = height + 'px';
}

/**
 * @param top Number
 * @param left Number
 * @param width Number
 * @param height Number
 */
cImageZoom.prototype.set_close_position = function(top, left, width, height) {
    this.DOM_cls.style.top = top + 'px';
    this.DOM_cls.style.left = left + 'px';
    if (typeof width != 'undefined' && width != 0) this.DOM_cls.style.width = width + 'px';
    if (typeof height != 'undefined' && height != 0) this.DOM_cls.style.height = height + 'px';
}

/**
 * @param percent Number
 * @param steps Number
 * @param speed Number
 * @param callback Function
 * @param f_post_animation Function
 */
cImageZoom.prototype.animate_P = function(percent, steps, speed, callback, f_post_animation) {
    percent = percent / 100;
    var new_width = this.width * percent;
    var new_height = this.height * percent;
    this.animate_WH(new_width, new_height, steps, speed, callback, f_post_animation);
}

/**
 * @param new_width Number
 * @param new_height Number
 * @param steps Number
 * @param speed Number
 * @param callback Function
 * @param f_post_animation Function
 */
cImageZoom.prototype.animate_WH = function(new_width, new_height, steps, speed, callback, f_post_animation) {
    var step_width = Math.abs(new_width - this.width) / steps;
    var step_height = Math.abs(new_height - this.height) / steps;
    var percent = new_width / this.width;
    
    this.steps = steps;
    
    this.DOM_div.style.display = 'block';
    if (this.interval == null) {
        var THIS = this;
        var f;
        if (percent > 1) {
            f = function() {
                if (THIS.DOM_img.width + step_width <= new_width &&
                    THIS.DOM_img.height + step_height <= new_height) {
                    THIS.DOM_img.width += step_width;
                    THIS.DOM_img.height += step_height;
                } else {
                    if (typeof f_post_animation == 'function') f_post_animation(new_width, new_height, callback);
                    else THIS.post_animation_(new_width, new_height, callback);
                }
            };
        } else {
            f = function() {
                if (THIS.DOM_img.width - step_width >= new_width &&
                    THIS.DOM_img.height - step_height >= new_height) {
                    THIS.DOM_img.width -= step_width;
                    THIS.DOM_img.height -= step_height;
                } else {
                    if (typeof f_post_animation == 'function') f_post_animation(new_width, new_height, callback);
                    else THIS.post_animation_(new_width, new_height, callback);
                }
            };
        }
        this.interval = window.setInterval(f, speed);
    }
}

/**
 * @param callback Function
 */
cImageZoom.prototype.close = function(callback) {
    var THIS = this;
    
    this.width = this.DOM_img.width;
    this.height = this.DOM_img.height;
    
    removeAllChilds(this.DOM_txt);
    this.DOM_cls.style.display = 'none';
    this.animate_WH(1, 1, this.steps, 5, null, function() { THIS.destructor(); });
    
    if (typeof callback == 'function') callback(this);
}

/**
 * @param new_width Number
 * @param new_height Number
 * @param callback Function
 */
cImageZoom.prototype.post_animation_ = function(new_width, new_height, callback) {
    window.clearInterval(this.interval);
    this.interval = null;
    
    this.DOM_img.width = new_width;
    this.DOM_img.height = new_height;
    
    this.DOM_txt.innerHTML = this.text;
    this.DOM_cls.style.display = 'block';
    
    this.dnd = new cDndWidget(this.DOM_div, this.DOM_txt);
    
    if (typeof callback == 'function') callback();
}
