var cbl_arHeight = []; //asoziatives Array zum Speichern der Höhen

function cbl_show(instanceID, itemID, force, noSlide) {
	var div = ge('cbl_div_' + instanceID + '_' + itemID);
	var img = ge('cbl_img_' + instanceID + '_' + itemID);
	var allOpened = false;
	var slide = (typeof(noSlide) == 'undefined') ? true : !noSlide;
	var tmpDiv = '';
	var ar = eval('cbl_arID_' + instanceID);
	
	//Prüfen, ob alle Einträge geöffnet sind (nur wenn es das Symbol gibt)***
	if (cbl_OpenAllExits(instanceID)) {
		for (var i = 0; i < ar.length; i++) {
			tmpDiv = ge('cbl_div_' + instanceID + '_' + ar[i])
			if (itemID == ar[i]) {
				if (div.style.display == 'none') {
					allOpened = true;
				} else {
					allOpened = false;
					break;
				}
			} else {
				if (tmpDiv.style.display == 'none') {
					allOpened = false;
					break;
				} else {
					allOpened = true;
				}
			}
		}
	
		if (allOpened) 
			cbl_SetAllStatus(instanceID, 1);
		else
			cbl_SetAllStatus(instanceID, 0);
	}
	//*******


	if (div.style.display == 'block' && force != 1) {
		img.src = img.getAttribute('SlideOut');
		img.title = "Öffnen";
		if (cbl_arHeight[instanceID + '_' + itemID] == null) //Höhe zwischenspeichern
				cbl_arHeight[instanceID + '_' + itemID] = div.offsetHeight;
		
		if (div.offsetHeight >= 200) //Wenn der Inhalt zu groß, wird ohne clipping geschlossen
			div.style.display =  'none';
		 else {
			if (slide) cbl_Slide(instanceID, itemID, 1); //SlideIn
		}
	} else {
		if (div.style.display != 'block') {
			img.src = img.getAttribute('SlideIn');
			img.title = "Schließen";

			if (cbl_arHeight[instanceID + '_' + itemID] == null) {
				div.style.display =  'block';
				if (div.offsetHeight > 1) {
					if (cbl_arHeight[instanceID + '_' + itemID] == null) //Höhe zwischenspeichern
						cbl_arHeight[instanceID + '_' + itemID] = div.offsetHeight;
				}
				if (slide) div.style.height = "1px";
			} else {
				if (slide) div.style.height = "1px";
				div.style.display =  'block';
			}


			if (slide) cbl_Slide(instanceID, itemID, 0); //SlideOut
		}
	}
}

function cbl_hide(instanceID) {
	ge('cbl_containerTbl_' + instanceID).style.display = 'none';
}

function cbl_OpenAllExits(instanceID) {
	var img = ge('cbl_img_' + instanceID);
	if (img) return true;
	
	return false;
}

function cbl_SetAllStatus(instanceID, status) {
	var img = ge('cbl_img_' + instanceID);
	img.src = (status == 1) ? img.getAttribute('SlideIn') : img.getAttribute('SlideOut');
	img.title = (status == 1) ? "Alles schließen" : "Alles öffnen";	
	img.setAttribute('status', status ^ 1);
}

function cbl_showAll(instanceID) {
	var ar = eval('cbl_arID_' + instanceID);
	var img = ge('cbl_img_' + instanceID);
	var status = img.getAttribute('status');
	
	for (var i = 0; i < ar.length; i++) {
		if (ge('cbl_div_' + instanceID + '_' + ar[i]).getAttribute('status') != 'del') //ohne Löschstatus
			cbl_show(instanceID, ar[i], status);
	}
	
	cbl_SetAllStatus(instanceID, status);
}

//Setzen der Höhe von Außen in einem dynamischen Div (z.B. bei Nachladen per Ajax)
function cbl_OverwriteHeight(instanceID, itemID, height) {
	var div = ge('cbl_div_' + instanceID + '_' + itemID);
	div.style.height = height + 'px';	
	cbl_arHeight[instanceID + '_' + itemID] = height;
}

function cbl_SetContent(instanceID, itemID, contentStr, height) {
	var div = ge('cbl_div_' + instanceID + '_' + itemID);
	var content = ge('cbl_content_' + instanceID + '_' + itemID);
	
	if (typeof(height) != 'undefined') {
		div.style.height = height + 'px';	
		cbl_arHeight[instanceID + '_' + itemID] = height;
	} else {
		cbl_arHeight[instanceID + '_' + itemID] = null;
	}

	content.innerHTML = contentStr;
}

function cbl_Slide(instanceID, itemID, status) {
	var ACCEL_CONST = 20;
	var SLIDE_INTERVAL = 10;
	var div = ge('cbl_div_' + instanceID + '_' + itemID);
	var height = div.style.height.toString().substring(0, div.style.height.toString().indexOf('px')); //Höhe ohne "px"
	var cbl_tmpHeight = cbl_arHeight[instanceID + '_' + itemID]; //Gesamthöhe aus Array

	if (height == '')
		height = cbl_tmpHeight
    
   
	if (status == 0) { //SlideOut
		if (height >= cbl_tmpHeight - ACCEL_CONST) { 
		    if (cbl_tmpHeight != null)
			    div.style.height = cbl_tmpHeight + 'px'; //Höhe auf Maximum setzen
			div.style.height = '';	//Höhe leeren, falls in diesem Comboblock-Item wiederum ein Layer aufgemacht wird
			return;
		}
		div.style.height = (parseInt(height) + ACCEL_CONST) + 'px'; //Höhe setzen
	} else { //SlideIn
		if ((parseInt(height) - ACCEL_CONST) >= 0) {
			div.style.height = (parseInt(height) - ACCEL_CONST) + 'px'; //Höhe setzen
		}
		if (height <= ACCEL_CONST) {
			div.style.display =  'none';
			return;
		}
	}
	setTimeout('cbl_Slide("' + instanceID  + '","' + itemID + '",' + status + ')', SLIDE_INTERVAL);
}
	
function cbl_DeleteItem(instanceID, itemID) {
	var ar = eval('cbl_arID_' + instanceID);
	var deleted = true;
	
	if (ge('cbl_containerTbl_' + instanceID + '_' + itemID)) {
		ge('cbl_containerTbl_' + instanceID + '_' + itemID).style.display = 'none';
		ge('cbl_div_' + instanceID + '_' + itemID).style.display = 'none';
		ge('cbl_div_' + instanceID + '_' + itemID).setAttribute('status','del'); //Löschstatus setzen
		
		//Prüfen ob alle "gelöscht" wurden, dann Headline ausblenden "löschen".
		for (var i = 0; i < ar.length; i++) {
			if (ge('cbl_div_' + instanceID + '_' + ar[i]).getAttribute('status') != 'del') //ohne Löschstatus
				deleted = false;
		}
		
		if (deleted == true) {
			if (ge('cbl_hdl_' + instanceID)) 
				ge('cbl_hdl_' + instanceID).style.display = 'none';
		}
	}
}

function cbl_HideItem(instanceID, itemID) {
	var ar = eval('cbl_arID_' + instanceID);
	var hidden = true;
	
	if (ge('cbl_containerTbl_' + instanceID + '_' + itemID)) {
		ge('cbl_containerTbl_' + instanceID + '_' + itemID).style.display = 'none';
		ge('cbl_div_' + instanceID + '_' + itemID).style.display = 'none';
		
		//Prüfen ob alle ausgeblendet bzw. "gelöscht" wurden, dann Headline ausblenden.
		for (var i = 0; i < ar.length; i++) {
			if (ge('cbl_div_' + instanceID + '_' + ar[i]).getAttribute('status') != 'del' && ge('cbl_div_' + instanceID + '_' + ar[i]).style.display != 'none')
				hidden = false;
		}
		if (hidden == true) {
			if (ge('cbl_hdl_' + instanceID)) 
				ge('cbl_hdl_' + instanceID).style.display = 'none';
		}
	}
}

function cbl_HandleComboblock(instanceID, status) {
	ge('cbl_containerTbl_' + instanceID).style.display = (status == 1) ? '' : 'none';
}

function cbl_HandleComboblockItem(instanceID, itemID, status) {
	ge('cbl_containerTbl_' + instanceID + '_' + itemID).style.display = (status == 1) ? '' : 'none';
	if (status != 1) {
		if (ge('cbl_div_' + instanceID + '_' + itemID).style.display != 'none')
			ge('cbl_div_' + instanceID + '_' + itemID).style.display = 'none';
	} else {
		ge('cbl_div_' + instanceID + '_' + itemID).style.display = '';
		cbl_HandleComboblock(instanceID, status);
	}
	
}

function cbl_SetItemHeadline(instanceID, itemID, str) {
	ge('cbl_itemHl_' + instanceID + '_' + itemID).innerHTML = str;
}

function cbl_GetItemHeadline(instanceID, itemID) {
	return ge('cbl_itemHl_' + instanceID + '_' + itemID).innerHTML;
}

function cbl_SaveStatus(instanceID, itemID, lasid) {
    var status = (ge('cbl_div_' + instanceID + '_' + itemID).style.display == 'none') ? 1 : 0; //Gegenteil, da zu diesem Zeitpunkt noch nicht geöffnet
    new AjaxRequest('/SCR/INC/Info/comboblock_ajax.asp?LASID=' + lasid + '&ID=' + instanceID + '&ItemID=' + itemID + '&Status=' + status, null, false);
}
