var oFileAddBtn;
var aFiles = new Object();
var iFileCount = 1;
var oFileAddBtnFirstParent;

function showHideDropDownMenu(DropDownCatalog, DropDownMenu) {
	if (document.getElementById(DropDownCatalog)) {
		catalog = document.getElementById(DropDownCatalog);
		menu = document.getElementById(DropDownMenu);
		li = catalog.getElementsByTagName('li');
		td = menu.getElementsByTagName('td');
		for(var i = 0; i < li.length; i++) {
			li[i].onmouseover = function () {
				this.className='ItemSelected'
				this.parentNode.className = 'RowSelected';
				
			}
			li[i].onmouseout = function () {
				this.className=''
				this.parentNode.className = '';
			}
		}
	}
}

function highliteCatalogRow(columnNum) {
	if (document.getElementById(columnNum)) {
		column = document.getElementById(columnNum);
		column.onclick = function () {
			if (catalog.style.display != 'block') {
				catalog.style.display = 'block';
			} else {
				catalog.style.display = 'none';
			}
		}
	}
}

function showHideFeedbackForm(FeedbackForm, ShowHideFeedbackForm) {
	if (document.getElementById(FeedbackForm)) {
		feedbackForm = document.getElementById(FeedbackForm);
		showHideFeedbackForm = document.getElementById(ShowHideFeedbackForm);
		showHideFeedbackForm.onclick = function () {
			if (feedbackForm.style.display != 'block') {
				feedbackForm.style.display = 'block';
			} else {
				feedbackForm.style.display = 'none';
			}
		}
	}
}

function File() {
	var self = this;
	this.ID = iFileCount;
	
	this.oNewFileInput = document.createElement('input');
	this.oNewFileInput.type = 'file';
	this.oNewFileInput.size = 50;
	this.oNewFileInput.name = 'File[' + iFileCount + ']';
	
	this.oNewFileDescTextarea = document.createElement('textarea');
	this.oNewFileDescTextarea.cols = 60;
	this.oNewFileDescTextarea.rows = 6;
	this.oNewFileDescTextarea.name = 'FileDesc[' + iFileCount + ']';

	this.oFileMinusBtn = document.createElement('input');
	this.oFileMinusBtn.type = 'button';
	this.oFileMinusBtn.name = 'FileMinus';
	this.oFileMinusBtn.value = ' - ';
	this.oFileMinusBtn.onclick = function(){ self.RemoveSelf(); return false; }
											
	this.oFirstRow = document.createElement('tr');
	var oTempElement = document.createElement('td');
	oTempElement.colspan = 2;
	oTempElement.appendChild(document.createTextNode('Файл'));
	oTempElement.appendChild(document.createElement('br'));
	oTempElement.appendChild(this.oNewFileInput);
	this.oFirstRow.appendChild(oTempElement);

	this.oSecondRow = document.createElement('tr');
	oTempElement = document.createElement('td');
	oTempElement.appendChild(document.createTextNode('Описание файла'));
	oTempElement.appendChild(document.createElement('br'));
	oTempElement.appendChild(this.oNewFileDescTextarea);
	this.oSecondRow.appendChild(oTempElement);
	oTempElement = document.createElement('td');
	oTempElement.style.textAlign = 'right';
	oTempElement.style.verticalAlign = 'bottom';
	this.oSecondRow.appendChild(oTempElement);
	
	oFileAddBtn.parentNode.parentNode.parentNode.insertBefore(this.oSecondRow, oFileAddBtn.parentNode.parentNode.nextSibling);
	oFileAddBtn.parentNode.parentNode.parentNode.insertBefore(this.oFirstRow, oFileAddBtn.parentNode.parentNode.nextSibling);

	oTempElement.appendChild(oFileAddBtn);
	oTempElement.appendChild(this.oFileMinusBtn);
	
	this.bLast = true;
}
									
// Добавляем фотографию
function AddFile() {
	if(aFiles[iFileCount]) {
		aFiles[iFileCount].bLast = false;
	}

	iFileCount++;
	aFiles[iFileCount] = new File();
}
									
// Удаляем фотографию
File.prototype.RemoveSelf = function() {
	if(this.bLast) {
		var bHaveSome = 0;
		for(var bJ in aFiles) {
			if(aFiles[bJ] != false) bHaveSome++; 
		}
		if(bHaveSome > 1) {
			for(var j = this.ID - 1; j > 0;  j--) {
				if(aFiles[j]) {
					aFiles[j].bLast = true;
					aFiles[j].oFileMinusBtn.parentNode.insertBefore(oFileAddBtn, aFiles[j].oFileMinusBtn);
					break;
				}
			}
		} else {
			oFileAddBtnFirstParent.appendChild(oFileAddBtn);
		}
	}
	this.oFirstRow.parentNode.removeChild(this.oFirstRow);
	this.oSecondRow.parentNode.removeChild(this.oSecondRow);
	aFiles[this.ID] = false;
}

function highliteCatalogItem(item, bool) {
	if (document.getElementById(item)) {
		oCatalogItem = document.getElementById(item);
		if (bool) {
			oCatalogItem.className = 'Selected';
		} else {
			oCatalogItem.className = '';
		}
	}
}

window.onload = function () {
	showHideDropDownMenu('DropDownCatalog', 'DropDownMenu');
	highliteCatalogRow('FirstColumn');
	highliteCatalogRow('SecondColumn');
	highliteCatalogRow('ThirdColumn');
	showHideFeedbackForm('FeedbackForm', 'ShowHideFeedbackForm');
	if (document.getElementById('FileAdd')) {
		oFileAddBtn = document.getElementById('FileAdd');
		oFileAddBtn.onclick = AddFile;
		oFileAddBtnFirstParent = oFileAddBtn.parentNode;
	}
/*
	highliteCatalogItem('CatalogItem_0');
	highliteCatalogItem('CatalogItem_1');
	highliteCatalogItem('CatalogItem_2');
	highliteCatalogItem('CatalogItem_3');
	highliteCatalogItem('CatalogItem_4');
	highliteCatalogItem('CatalogItem_5');
*/
}