var textCheckbox = 'select/deselect all';
var textSubmit = 'download selected';

function renderDLBlocks() {
	// get download blocks and create forms
	var DLBlocks = YAHOO.util.Dom.getElementsByClassName('storyContent', 'div', 'boxDLBlocks');
	var DLBlock;

	for (var i=0; i<DLBlocks.length; i++) {
		var headline;
		var linkNew;
		var linkNewIcon;
		var anchor = null;
		var cookie;
		var formNew;
		var boxControlsNew;
		var boxControlsLeftNew;
		var boxControlsRightNew;
		var checkboxNew;
		var checkboxTextNew;
		var buttonNew;

		// add a link on <h1>, but keep CMS-generated anchor out of link
		headline = YAHOO.util.Dom.getElementsBy(isH1, 'h1', DLBlocks[i])[0];
		headline.className = 'h1DLs';
		headline.setAttribute('id', 'h1DLs-' + i);
		linkNew = document.createElement('a');
		linkNew.className = 'link';

		linkNewIcon = document.createElement('div');
		linkNewIcon.className = 'linkIcon';

		linkNew.appendChild(linkNewIcon);

		if (headline.hasChildNodes() && (headline.firstChild.tagName.toLowerCase() == 'a')) {
			anchor = headline.firstChild.cloneNode(true);
			headline.removeChild(headline.firstChild);
		}
		while (headline.hasChildNodes()) {
			var nodeClone = headline.firstChild.cloneNode(true);
			headline.removeChild(headline.firstChild);
			linkNew.appendChild(nodeClone);
		}
		if (anchor != null) {
			headline.appendChild(anchor);
		}
		headline.appendChild(linkNew);

		// create a form to contain downloads
		
		formNew = document.createElement('form');
		formNew.className = 'formDLs';
		formNew.setAttribute('id', 'formDLs-' + i);
		formNew.setAttribute('name', '');
		formNew.setAttribute('action', 'batchdownload');
		formNew.setAttribute('method', 'post');
		formNew.setAttribute('onsubmit', 'return CheckInput("formDLs-'+i+'");');

		DLBlock = YAHOO.util.Dom.getElementsByClassName('boxDLs', 'div', DLBlocks[i]);

		for (var j=0; j<DLBlock.length; j++) {
			var nodeClone = DLBlock[j].cloneNode(true);
			DLBlocks[i].removeChild(DLBlock[j]);
			formNew.appendChild(nodeClone);
		}
		// add controls (toggle checkbox, submit button) to form
		boxControlsNew = document.createElement('div');
		boxControlsNew.className = 'boxControls';

		boxControlsLeftNew = document.createElement('div');
		boxControlsLeftNew.className = 'boxControlsLeft';

		boxControlsRightNew = document.createElement('div');
		boxControlsRightNew.className = 'boxControlsRight';

		checkboxNew = document.createElement('input');
		checkboxNew.className = 'checkbox';
		checkboxNew.setAttribute('type', 'checkbox');
		
		checkboxNew.setAttribute('value', '-1');
		checkboxNew.setAttribute('id', 'checkbox-' + i);
		checkboxNew.setAttribute('onchange', 'selectAll(' + i + ');');

		checkboxTextNew = document.createTextNode(textCheckbox);

		buttonNew = document.createElement('input');
		buttonNew.className = 'submit';
		buttonNew.setAttribute('type', 'submit');
		
		buttonNew.setAttribute('value', textSubmit);
		buttonNew.setAttribute('id', 'submit-' + i);

		boxControlsLeftNew.appendChild(checkboxNew);
		boxControlsLeftNew.appendChild(checkboxTextNew);

		boxControlsRightNew.appendChild(buttonNew);

		boxControlsNew.appendChild(boxControlsLeftNew);
		boxControlsNew.appendChild(boxControlsRightNew);

		formNew.appendChild(boxControlsNew);

		DLBlocks[i].appendChild(formNew);
	}
	// initially show the first download block
	if (DLBlocks.length) {
		changeHeadlineById('h1DLs-0', true);
		changeFormById('formDLs-0', true);
	}
}

function isH1(elem) {
	return (elem.tagName == 'h1' || elem.tagName == 'H1');
}

function changeHeadlineById(id, show) {
	var formId = 'form' + id.substring(2, id.length);
	var headlines = YAHOO.util.Dom.getElementsByClassName('h1DLs', 'h1', 'boxDLBlocks');

	for (var i=0; i<headlines.length; i++) {
		headlines[i].className = 'h1DLs hidden';
		headlines[i].lastChild.setAttribute('href', 'javascript: changeFormById(\'formDLs-' + i + '\', true); changeHeadlineById(\'h1DLs-' + i + '\', true);');
		headlines[i].lastChild.setAttribute('title', 'Downloads einblenden');
	}
	if (show) {
		document.getElementById(id).className = 'h1DLs current';
		document.getElementById(id).lastChild.setAttribute('href', 'javascript: changeFormById(\'' + formId + '\', false); changeHeadlineById(\'' + id + '\', false);');
		document.getElementById(id).lastChild.setAttribute('title', 'Downloads ausblenden');
	}
}

function changeFormById(id, show) {
	var forms  = YAHOO.util.Dom.getElementsByClassName('formDLs', 'form', 'boxDLBlocks');

	for (var i=0; i<forms.length; i++)
	{
		forms[i].setAttribute('name', '');
		YAHOO.util.Dom.setStyle(forms[i], 'display', 'none');
	}
	if (show) {
		document.getElementById(id).setAttribute('name', 'oFrontEnd_GroupDownload');
		YAHOO.util.Dom.setStyle(id, 'display', 'block');
	}
}

function selectAll(no) {
	var form       = document.getElementById('formDLs-' + no);
	var checkboxes = YAHOO.util.Dom.getElementsByClassName('checkbox', 'input', form);

	if (document.getElementById('checkbox-' + no).checked) {
		for (var i=0; i<checkboxes.length; i++) {
			checkboxes[i].checked = true;
		}
	}
	else {
		for (var i=0; i<checkboxes.length; i++) {
			checkboxes[i].checked = false;
		}
	}
}

function CheckInput(formDLs) {
	var checkboxes = YAHOO.util.Dom.getElementsByClassName('checkbox', 'input', formDLs);
	for(var i=0; i<checkboxes.length; i++){
		if(checkboxes[i].checked) {
			try {
				setTimeout("setIframe()",2000);
			} catch(e){}
			return true;
		}
	}
	return false;
}

YAHOO.util.Event.onDOMReady(renderDLBlocks);

