/**
 * ActiveImages 1.1
 */

var isIE = (navigator.userAgent.indexOf("MSIE") != -1) ? true : false;
var isIE6 = (navigator.userAgent.indexOf("MSIE 6.0") != -1) ? true : false;

function ActiveImages()
{
        this.containerid = 'null';
	this.img_target = null;
	this.img_description = null;
	this.img_button = null;
	this.img_description_height = 0;
	this.current_height = 0;
	this.timeout_id = null;

	//this.debug_counter = 0;
}

ActiveImages.prototype = {
	init : function(height) {
		if(isIE6) {
			height += 25;
		}
        
        // Extrafunktion für Adserverbanner
        // div -> first a -> img
        if (this.containerid != 'null') {
            var conObj = document.getElementById(this.containerid);
            for (i=0; conObj.childNodes[i]; i++) {
                if (conObj.childNodes[i].tagName == "A") {
                    conObj.childNodes[i].href = "#";
                    conObj.childNodes[i].target = "_self";
                    activeimg.img_target = conObj.childNodes[i].firstChild;
                    break;
                }
            }
        } else {
    		activeimg.img_target = $('titelbild');
        }
            
		activeimg.img_description = $('titelbild_beschreibung');
		activeimg.img_button = $('titelbild_button');
		activeimg.img_description_height = height;
		activeimg.setupLink();
	},

	setupLink : function() {
		activeimg.img_target.style.cursor = "pointer";
		if(isIE) {
			activeimg.img_target.onclick = activeimg.open;
			activeimg.img_button.onclick = activeimg.open;
		} else {
			addEvent(activeimg.img_target, 'click', activeimg.open);
			addEvent(activeimg.img_button, 'click', activeimg.open);
		}
	},

	open : function() {
		if(isIE) {
			activeimg.img_description.style.visibility = "";
			activeimg.img_target.onclick = activeimg.close;
			activeimg.img_button.onclick = activeimg.close;
		} else {
			removeEvent(activeimg.img_target, 'click', activeimg.open);
			addEvent(activeimg.img_target, 'click', activeimg.close);
			removeEvent(activeimg.img_button, 'click', activeimg.open);
			addEvent(activeimg.img_button, 'click', activeimg.close);
		}
		activeimg.slideAnimation('open');
		activeimg.img_button.className = "close";
        return false;
	},

	close : function() {
		activeimg.slideAnimation('close');
		if(isIE) {
			activeimg.img_target.onclick = activeimg.open;
			activeimg.img_button.onclick = activeimg.open;
		} else {
			removeEvent(activeimg.img_target, 'click', activeimg.close);
			addEvent(activeimg.img_target, 'click', activeimg.open);
			removeEvent(activeimg.img_button, 'click', activeimg.close);
			addEvent(activeimg.img_button, 'click', activeimg.open);
		}
		activeimg.img_button.className = "";
	},

	slideAnimation : function(direction) {
		//debug(activeimg.current_height + " - " + direction);
		if(direction == 'close') {
			activeimg.current_height -= 10;
			if(activeimg.current_height < 0) {
				//activeimg.debug_counter++;
				//debug(activeimg.debug_counter);
				activeimg.img_description.style.height = "0px";
				activeimg.img_description.style.display = "none";
				if(isIE) {
					/* IE7 BugFix */
					activeimg.img_description.style.visibility = "hidden";
				}
				activeimg.current_height = 0;
				window.clearTimeout(activeimg.timeout_id);
				//debug("ready closed");
				return;
			}
		} else if(direction == 'open') {
			activeimg.img_description.style.height = "0px";
			activeimg.img_description.style.display = "block";
			if(activeimg.current_height > activeimg.img_description_height) {
				activeimg.img_description.style.height = activeimg.img_description_height + "px";
				activeimg.current_height = activeimg.img_description_height;
				window.clearTimeout(activeimg.timeout_id);
				//debug("ready opened");
				return;
			}
			activeimg.current_height += 10;
		}
		activeimg.img_description.style.height = activeimg.current_height + "px";
		activeimg.timeout_id = window.setTimeout("activeimg.slideAnimation('" + direction + "')", 10);
	}
};

/**
 * Short alternate to document.getElementById()
 * 
 * @param {String} name
 * @return {Object} The HtmlElement in Document
 */
function $(name) {
	return(document.getElementById(name));
}

/**
 * BrowserCross-Function for a easy way to add EventHandler
 * 
 * @param {Object} object
 * @param {String} event
 * @param {Object} func
 * @return {bool} ...
 */
function addEvent(object, event, func) {
	if(object.addEventListener) {
		//debug("addEventListener(" + event + ")");
		object.addEventListener(event, func, false);
		return(true);
	} else if(object.attachEvent) {
		//debug("attachEvent(" + event + ")");
		object.attachEvent('on' + event, func);
		return(true);
	} else {
		return(false);
	}
}

/**
 * BrowserCross-Function for a easy way to remove EventHandler
 * 
 * @param {Object} object
 * @param {String} event
 * @param {Object} func
 * @return {bool} ...
 */
function removeEvent(object, event, func) {
	if(object.removeEventListener) {
		//debug("removeEventListener(" + event + ")");
		object.removeEventListener(event, func, false);
		return(true);
	} else if(object.detachEvent) {
		//debug("detachEvent(" + event + ")");
		object.detachEvent('on' + event);
		return(true);
	}
	return(false);
}

function debug(msg) {
	var txt = $('debug');
	txt.value += msg + "\n";
}
