if (!$defined(oDownloads)){
	var oDownloads = {};
}
if (!$defined(oAnzahl)){
	var oAnzahl = {};
}
if (!$defined(oBoxJahr)){
	var oBoxJahr = {};
}
window.addEvent('domready', function(){
		initDownloadCenter();
});

window.addEvent('domchanged', function(){
		initDownloadCenter();
});

function initDownloadCenter(){
	var oBoxen = $$('.downloadcenter_boxinhalt');
	var blFirst = false;
	oBoxen.each(function(oBox, iBox){
		blFirst = false;
		sBoxId = oBox.id;
		if($('documents_' + sBoxId).hasClass('boxOpen')){
			blFirst = true;
		}
		oDownloads[sBoxId] = {
			'blFirst':blFirst,
			'slider':new Fx.Slide('documents_' + sBoxId)
		};
		oAnzahl[sBoxId] = {
			'inanzahl': 0
		};
		initDownloadCenterAjax(sBoxId);
	});
}

function initDownloadCenterAjax(stBlock){
	var oKopfzeile = $('headrow_' + stBlock).getElements('a.blocklink')[0];
	var oLoader = $('dropdown_' + stBlock);
	if($defined($('frmDropdown_' + stBlock))){
		$('frmDropdown_' + stBlock).style.display = 'none';
	}
	document.getElementById('documents_' + stBlock).innerHTML = "";
	if($defined(oKopfzeile)) {
		if($defined(oLoader)){oLoader.style.display = 'none';}
		//Clickevent mit Ajaxaufruf beim ersten Click erzeugen
		oKopfzeile.addEvent('click', function(e){
			e = new Event(e).stop();
			if (!document.getElementById('documents_' + stBlock).innerHTML){
				ajaxLoadDownloadCenter(stBlock);
			}
			else{
				oDownloads[stBlock].slider.toggle().chain(function(){
					showHideDownloadCenter(stBlock);
				});
			}
		});
		if (oDownloads[stBlock].blFirst) {
			ajaxLoadDownloadCenter(stBlock);
			oDownloads[stBlock].blFirst = false;
		};
	}
}

function ajaxLoadDownloadCenter(stBlock){
	var oLoader = $('dropdown_' + stBlock);
	var iSetJahr = '';
	//Jahr der Box auswählen
	if($defined(oBoxJahr[stBlock])){
		iSetJahr = oBoxJahr[stBlock].injahr;
	}
	if($defined($('frmDropdown_' + stBlock))){
		var oSelectAuswahl = $('frmDropdown_' + stBlock).inauswahl;
		var oSelectJahr = $('frmDropdown_' + stBlock).injahr;
	}
	if($defined(oSelectAuswahl)){
		for(i = 0; i<oSelectAuswahl.length; ++i){
			if(oSelectAuswahl.options[i].value == oDownloads['konstanten_' + stBlock].iAuswahl){
				oSelectAuswahl.options[i].selected = true
			}
		}
	}
	if($defined(oSelectJahr)){
		if(iSetJahr == ''){oSelectJahr.options[0].selected = true}
		else{
			for(i = 0; i<oSelectJahr.length; ++i){
				if(oSelectJahr.options[i].value == iSetJahr){
					oSelectJahr.options[i].selected = true
				}
			}
		}
	}
	new Ajax('showpage.asp?pageid=' + oDownloads['konstanten'].CONST_RUBRIK_DOWNLOADCENTER_DOKUMENTEAUSGABE + '&mode=text&stblock=' + stBlock + '&inauswahl=' + oDownloads['konstanten_' + stBlock].iAuswahl + '&injahr=' + iSetJahr + '&pketf=' + oDownloads['konstanten'].sPketf + '&inlinkrubrik=' + oDownloads['konstanten'].iLinkRubrik +"&stsprachzuordnung=" + oDownloads['konstanten_' + stBlock].sprachzuordnung, {
		evalScripts: true,
		method: 'get',
		update:$('documents_' + stBlock),
		onRequest: function() { 
			oDownloads[stBlock].slider.hide();
			if($defined(oLoader)){oLoader.style.display = 'none';}
			$('load_' + stBlock).innerHTML = oDownloads['konstanten'].stHTML;
			$('load_' + stBlock).style.display = 'block';
			},
		onSuccess: function() {
				$('load_' + stBlock).style.display = 'none';
				if($defined(oLoader)){oLoader.style.display = 'block';}
				oDownloads[stBlock].slider.slideIn().chain(function(){
					showHideDownloadCenter(stBlock);
				});
			},
		onFailure: function(){
			oDownloads[stBlock].slider.hide();
			$('load_' + stBlock).innerHTML = oDownloads['konstanten'].stFehler;
			document.getElementById('documents_' + stBlock).innerHTML = '';
		}
	}).request();
}

function wechsleDownloadCenterAuswahl(stBlock) {
	showHideDownloadCenter(stBlock);
	ajaxLoadDownloadCenter(stBlock);
}

function showHideDownloadCenter(stBlock) {
	if (document.getElementById('head_' + stBlock).src.match(/minus/)) {
		document.getElementById('head_' + stBlock).src = apUrlZusatz + 'images/plus.gif';
		if($defined($('frmDropdown_' + stBlock))){
			$('frmDropdown_' + stBlock).style.display = 'none';
		}
	} else {
		document.getElementById('head_' + stBlock).src = apUrlZusatz + 'images/minus.gif';
		if($defined($('frmDropdown_' + stBlock))){
			$('frmDropdown_' + stBlock).style.display = 'inline';
		}
	}
	if ($defined(oAnzahl[stBlock])){
		document.getElementById('anzahl_' + stBlock).innerHTML = '(' + oAnzahl[stBlock].inanzahl + ')';
	}
}