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

function cContentManager() {
	this.xhr = new cXHR();
	
	this.zIndexes = [];
	this.bubbles = {};
	this.slider = null;
	this.anim = null;
	
	var THIS = this;
	this.or_handler = window.onresize;
	window.onresize = function() { THIS.onresize_(THIS.or_handler); };
}

/**
 * @return Object
 */
cContentManager.prototype.destructor = function() {
    this.zIndexes = null;
    this.xhr = this.xhr.destructor();
    if (this.slider != null) this.slider = this.slider.destructor();
    window.onresize = this.or_handler;
    if (this.anim != null) this.anim = this.anim.destructor();
    
    for (var b in this.bubbles) {
        for (var i = this.bubbles[b].length - 1; i >= 0; i--) {
            this.bubbles[b][i] = this.bubbles[b][i].destructor();
        }
        delete this.bubbles[b];
    }
    this.bubbles = null;
    
    return null;
}

/**
 * @param content String
 * @param is_ref Boolean
 * @param xhr_content String
 */
cContentManager.prototype.load = function(content, is_ref, xhr_content) {
    if (this.bubbles[content] != null) {
        for (var i = this.bubbles[content].length - 1; i >= 0; i--) {
            this.setZIndex_(this.bubbles[content][i].DOM_div);
        }
	    return;
	}
	
	var THIS = this, div, msp;
	if (content == 'kontakt') {
	    if ($('frm_kontakt') != null) return;
	    
	    if (typeof xhr_content == 'undefined') {
    	    var cb = function(content_html) { THIS.load(content, is_ref, content_html); };  
    	    this.xhr.get('../bubbles/' + content + '/content1.html', cb);
	    } else {
	        var ref = $('ref_animation_div');
	        if (ref != null) { document.body.removeChild(ref); }
	        
	        this.close_all_bubbles_();
	        
	        msp = getAbsoluteDimensions($('main_scroll'));
	        
	        div = document.createElement('div');
	        div.style.position = 'absolute';
	        div.style.left = msp.offsetLeft + 50 + 'px';
	        div.style.top = msp.offsetTop + 20 + 'px';
	        document.body.appendChild(div);
	        div.innerHTML = xhr_content;
	    }
	    return;
	} else if (is_ref == true) {
	    var ref = $('ref_animation_div');
        if (ref != null && ref.getAttribute('content') == content) return;
	    
	    if (typeof xhr_content == 'undefined') {
    	    var cb = function(content_html) { THIS.load(content, is_ref, content_html); };
    	    this.xhr.get('../refs/' + content + '.html', cb);
	    } else {
	        var frm = $('frm_kontakt');
	        if (frm != null) { document.body.removeChild(frm.parentNode); }
	        
	        ref = $('ref_animation_div');
            if (ref != null) { document.body.removeChild(ref); }
	        
	        this.close_all_bubbles_();
	        
	        msp = getAbsoluteDimensions($('main_scroll'));
	        
	        div = document.createElement('div');
	        div.id = 'ref_animation_div';
	        div.setAttribute('content', content);
            div.style.position = 'absolute';
            div.style.left = msp.offsetLeft + 50 + 'px';
            div.style.top = msp.offsetTop + 11 + 'px';
            div.style.width = '600px';
            document.body.appendChild(div);
            div.innerHTML = xhr_content;
            if (this.slider != null) {
                this.slider = this.slider.destructor();
            }
            this.slider = new cImageSlider("img_sli1", 1, 10, 20);
	    }
	} else {
	    var frm = $('frm_kontakt');
	    if (frm != null) { document.body.removeChild(frm.parentNode); }
	    
	    var ref = $('ref_animation_div');
	    if (ref != null) { document.body.removeChild(ref); }

    	this.close_wir_bubbles_();
    	
        var dims = {
            bubble: {
                top: 100,
                left: 100,
                width: 0,
                height: 0,
                steps: 50,
                speed: 5
            },
            close: {
                top: 30,
                left: 0,
                width: 0,
                height: 0
            },
            text: {
    	        top: 0,
    	        left: 0,
    	        width: 0,
    	        height: 0
    	    }
    	}
    
    	switch(content) {
    	    case 'kommunikation':
    	        dims.bubble.left = 670;
    	        dims.bubble.top = 155;
    	        dims.bubble.width = 330;
    	        dims.text.top = 80;
    	        dims.text.left = 20;
    	        dims.text.width = 290;
    	        break;
    	    case 'beratung':
    	        dims.bubble.left = 500;
    	        dims.bubble.top = 260;
    	        dims.bubble.width = 340;
                dims.text.top = 70;
                dims.text.left = 35;
                dims.text.width = 270;
    	        break;
    	    case 'freunde':
    	        dims.bubble.left = 160;
                dims.bubble.top = 130;
                dims.bubble.width = 400;
                dims.text.top = 85;
                dims.text.left = 30;
                dims.text.width = 330;
                break;
    	    case 'webdesign':
    	        dims.bubble.left = 230;
    	        dims.bubble.top = 195;
    	        dims.bubble.width = 400;
    	        dims.text.top = 85;
    	        dims.text.left = 30;
    	        dims.text.width = 330;
    	        break;
    	    case 'programmierung':
    	        dims.bubble.left = 500;
    	        dims.bubble.top = 150;
    	        dims.bubble.width = 330;
    	        dims.text.top = 80;
    	        dims.text.left = 20;
    	        dims.text.width = 290;
    	        break;
    	    case 'fotografie':
    	        dims.bubble.left = 730;
                dims.bubble.top = 340;
                dims.bubble.width = 260;
                dims.text.top = 90;
                dims.text.left = 10;
                dims.text.width = 240;
    	        break;
    	    case 'stephie':
    	    case 'jacqueline':
    	    case 'falko':
    	    case 'sven':
    	    case 'annett':
    	    case 'andreas':
    	        this.close_all_bubbles_();
    	        dims.bubble.left = 250;
                dims.bubble.top = 180;
                dims.bubble.width = 310;
                dims.text.top = 70;
                dims.text.left = 10;
                dims.text.width = 290;
                break;
    	    case 'start':
    	        dims.bubble.left = 270;
                dims.bubble.top = 190;
                dims.bubble.width = 200;
                dims.text.top = 80;
                dims.text.left = 10;
                dims.text.width = 180;
    	        break;
            default:
                return;
    	}
    	dims.bubble.height = dims.close.width = dims.bubble.width;
    	
    	var cb = function(content_html) { THIS.show_bubble_(content, dims, content_html); };
    	this.xhr.get('../bubbles/' + content + '/content1.html', cb);
    	
    	if (content == 'start' ||
	        content == 'stephie' ||
    	    content == 'jacqueline' ||
    	    content == 'falko' ||
    	    content == 'sven' ||
    	    content == 'annett' ||
    	    content == 'andreas') {
    	    var bubb = ObjectUtils.copy(dims.bubble);
    	    var clos = ObjectUtils.copy(dims.close);
    	    var text = ObjectUtils.copy(dims.text);
	        if (content == 'start') {
	            bubb.left = 540;
	            bubb.top = 185;
	            bubb.width = 350;
	            bubb.height = 350;
	            text.top = 75;
	            text.left = 40;
	            text.width = 270;
	            clos.width = 350;
            } else {
                bubb.left = 590;
                text.top = 60;
            }
    	    var d_copy = {
                bubble: bubb,
                close: clos,
                text: text,
                second: true
    	    };
    	    
    	    var cb = function(content_html) { THIS.show_bubble_(content, d_copy, content_html); };
            this.xhr.get('../bubbles/' + content + '/content2.html', cb);
    	}
	}
}

/**
 * @param content String
 * @param dims Object
 * @param text String
 */
cContentManager.prototype.show_bubble_ = function(content, dims, text) {
    var THIS = this;
 
    var iz = new cImageZoom('/images/bubble.' + ((IE == 1) ? 'gif' : 'png'), 
                            50, 
                            50, 
                            dims.bubble.top, 
                            dims.bubble.left, 
                            function(v) { THIS.bubble_closed_(v); }, 
                            'globalframe');
    
    var cb = function() {
        iz.set_text_position(dims.text.top, dims.text.left, dims.text.width, dims.text.height);
        iz.set_close_position(dims.close.top, dims.close.left, dims.close.width, dims.close.height);
        var curr_handler = iz.dnd.elem.onmousedown; 
        iz.dnd.elem.onmousedown = function(e) { THIS.setZIndex_(iz.DOM_div); curr_handler(e); }
        THIS.wir_animation_(content);
    }
    
    iz.set_text(text);
    iz.animate_WH(dims.bubble.width, dims.bubble.height, dims.bubble.steps, dims.bubble.speed, cb);
    
    var max = ArrayUtils.get_max_nbr(this.zIndexes);
    iz.DOM_div.style.zIndex = ++ max;
    this.zIndexes.push(max);
    
    if (this.bubbles[content] == null) this.bubbles[content] = [];
    this.bubbles[content].push(iz);
}

/**
 * @param content String
 */
cContentManager.prototype.wir_animation_ = function(content) {
    if (content != 'stephie' && content != 'jacqueline' && content != 'falko' && content != 'sven' && content != 'annett' && content != 'andreas') return;
    
    var base_path = '/images/animations/' + content + '/';
    var images = [], count;
    
    switch (content) {
        case 'sven':
        case 'stephie':
        case 'annett': count = 11; break;
        default: count = 0; break;
    }
    
    for (var i = 1; i <= count; i++) {
        images.push(base_path + content + '_' + i + '.gif');
    }
    
    if (images.length > 0) {
        if (this.anim != null) this.anim.destructor();
        this.anim = new cAnim(content, images);
        this.anim.go();
    }
}

/**
 * @param elem Element
 */
cContentManager.prototype.setZIndex_ = function(elem) {
    var max = ArrayUtils.get_max_nbr(this.zIndexes);
    
    var zIndex = elem.style.zIndex;

    for (var b in this.bubbles) {
        for (var i = this.bubbles[b].length - 1; i >= 0; i--) {
            if (this.bubbles[b][i].DOM_div.style.zIndex > zIndex) {
                this.bubbles[b][i].DOM_div.style.zIndex = this.bubbles[b][i].DOM_div.style.zIndex - 1;
            }
        }
    }
    
    elem.style.zIndex = max;
}

/**
 * @param imageZoom Object
 */
cContentManager.prototype.bubble_closed_ = function(imageZoom) {
    var zIndex = imageZoom.DOM_div.style.zIndex;
    
    for (var b in this.bubbles) {
        var new_a = [];
        for (var i = this.bubbles[b].length - 1; i >= 0; i--) {
            if (this.bubbles[b][i] != imageZoom) {
                new_a.push(this.bubbles[b][i]);
            } else {
                this.bubbles[b][i] = null; 
            }
        }
        if (new_a.length > 0) {
            this.bubbles[b] = new_a;
        } else {
            delete this.bubbles[b];
        }
    }
    
    this.recalc_zIndex_(zIndex);
}

/**
 * @param zIndex Number
 */
cContentManager.prototype.recalc_zIndex_ = function(zIndex) {
    this.zIndexes = [];
    for (var b in this.bubbles) {
        for (var i = this.bubbles[b].length - 1; i >= 0; i--) {
            var e = this.bubbles[b][i].DOM_div;
            if (e.style.zIndex > zIndex) {
                e.style.zIndex = e.style.zIndex - 1;
            }
            this.zIndexes.push(parseInt(e.style.zIndex));
        }
    }
}

cContentManager.prototype.close_all_bubbles_ = function() {
    if (this.anim != null) this.anim = this.anim.destructor();
    
    for (var b in this.bubbles) {
        for (var i = this.bubbles[b].length - 1; i >= 0; i--) {
            this.bubbles[b][i] = this.bubbles[b][i].destructor();
        }
    }
    this.bubbles = {};
    this.zIndexes = [];
}

cContentManager.prototype.close_wir_bubbles_ = function() {
    if (this.anim != null) this.anim = this.anim.destructor();
    
    var zIndex = 0;
    for (var b in this.bubbles) {
        if (b == 'start' ||
            b == 'stephie' ||
            b == 'jacqueline' ||
            b == 'falko' ||
            b == 'sven' ||
            b == 'annett' ||
            b == 'andreas') {
            for (var i = this.bubbles[b].length - 1; i >= 0; i--) {
                zIndex = Math.max(zIndex, this.bubbles[b][i].DOM_div.style.zIndex);
                this.bubbles[b][i] = this.bubbles[b][i].destructor();
            }
            delete this.bubbles[b];
        }
    }
    
    if (zIndex != 0) {
        this.recalc_zIndex_(zIndex);
    }
}

/**
 * @param fn Function
 */
cContentManager.prototype.onresize_ = function(fn) {
    var msp = getAbsoluteDimensions($('main_scroll'));
    var gfp = getAbsoluteDimensions($('globalframe'));
    
    var frm = $('frm_kontakt');
    if (frm != null) {
        frm.parentNode.style.left = msp.offsetLeft + 50 + 'px';
        frm.parentNode.style.top = msp.offsetTop + 20 + 'px';
    }
    
    var ref = $('ref_animation_div');
    if (ref != null) {
        ref.style.left = msp.offsetLeft + 50 + 'px';
        ref.style.top = msp.offsetTop + 20 + 'px';
    }

    for (var b in this.bubbles) {
        for (var i = this.bubbles[b].length - 1; i >= 0; i--) {
            this.bubbles[b][i].set_position();
        }
    }
    
    if (typeof fn == 'function') fn();
}
