var lastSection = 0;

function addArticleSection(){
	var button = document.getElementById('add-section-button');
	var template = document.getElementById('master-section');

	var range = document.createRange()
	range.selectNodeContents(template);

	var fragment = range.cloneContents();
	recurseAndModifyArticleSection(fragment);

	button.parentNode.insertBefore(createTitleField(), button);
	button.parentNode.insertBefore(fragment, button);

	lastSection++;
}

function recurseAndModifyArticleSection(fragment){
	for(var i = 0; i < fragment.childNodes.length; i++){
		var node = fragment.childNodes[i];
		if(node.nodeName == 'DIV') {
			if(node.getAttribute('class').substr(0, 13) == 'article-image'){
				node.parentNode.removeChild(node);
				i--;
				continue;
			}
			if((node.innerHTML) && (node.getAttribute('class') == 'description') && (node.innerHTML.match(/SEO purposes/))) {
				node.innerHTML = '';
			}
		}
		if(node.nodeName == 'FIELDSET'){
			node.parentNode.removeChild(node);
			i--;
			continue;
		}
		if(node.nodeName == 'LABEL'){
			if(node.getAttribute('class') == 'option'){
				node.parentNode.removeChild(node);
				i--;
				continue;
			}
			fieldFor = node.getAttribute('for');
			lastNum = fieldFor.substr(-1, 1);
			if(isNaN(parseInt(lastNum))){
				newFor = fieldFor.substr(0, fieldFor.length - 1) + '_' + lastSection;
			}
			else{
				newFor = fieldFor.substr(0, fieldFor.length - 1) + lastSection;
			}
			
			node.setAttribute('for', newFor);
			node.innerHTML = node.innerHTML.replace('Primary Image', 'Section Image');
			
			if(node.innerHTML.match(/New image filename/)) {
				node.innerHTML = 'Image filename:';
			}
		}
		if(node.nodeName == 'INPUT' || node.nodeName == 'TEXTAREA'){
			fieldName = node.getAttribute('name');			
			node.value = '';
			
			if(fieldName.substr(0, 5) == 'edit['){
				var lastNum = fieldName.substr(-2, 1);
				if(isNaN(parseInt(lastNum))){
					newName = fieldName.substr(0, fieldName.length - 1) + '_' + lastSection + ']';
					newID = fieldName.substr(0, fieldName.length - 1).replace('[', '-') + '_' + lastSection;
				}
				else{
					newName = fieldName.substr(0, fieldName.length - 2) + lastSection + ']';
					newID = fieldName.substr(0, fieldName.length - 2).replace('[', '-') + lastSection;
				}
				node.setAttribute('name', newName);
				node.value = '';
				
				if(fieldName.match(/edit\[image_\d+\]/)) {
					node.setAttribute('type', 'file');
					node.setAttribute('size', '40');
					node.setAttribute('maxlength', '256');
				}
				
				node.setAttribute('id', newID);
			}
		}
		if(node.hasChildNodes()){
			recurseAndModifyArticleSection(node);
		}
	}
}

function createTitleField(){
	var form_item = document.createElement('div');
	form_item.setAttribute('class', 'form-item');
	form_item.innerHTML = '<label for="edit-subtitle-' + lastSection + '">Section Title:</label><input type="text" maxlength="128" name="edit[subtitle_' + 
							lastSection + ']" id="edit-subtitle-' + lastSection + '"  size="60" class="form-text"/>';
	return form_item;
}

function showDetailedImage(path, width, height){
	window.open(path, 'image_detail', 'screenX=30,left=30,screenY=30,top=30,scrollbars=no,width=' + width + ',height=' + height + ',toolbar=no,location=no,directories=no,status=no', true)
}
