/**
 * 
 * 
 */
var zinGdbConn_autoCompleters = new Array();
var zinGdbConn_dateSelectors = new Array();
var tmpObject, tmpModuleIdentifier;

function collapseFilter(src, moduleIdentifier) {
	document.getElementById("filterScroll_" + moduleIdentifier).style.height = "45px";
	src.style.display = 'none';
	document.getElementById("expandButton_" + moduleIdentifier).style.display = 'inline';
}

function expandFilter(src, moduleIdentifier) {
	document.getElementById("filterScroll_" + moduleIdentifier).style.height = "350px";
	src.style.display = 'none';
	document.getElementById("collapseButton_" + moduleIdentifier).style.display = 'inline';
}


// Setzt die vorherigen Werte in die Input-Felder ein
function previousSettings(object, targetIdentifier) {
	
	targetId = "tx_zingdbconn_pi1[" + targetIdentifier + "][filter]";
	sourceId = "tx_zingdbconn_pi1[" + targetIdentifier + "][backFilter]";
	var i;
	
	// backFilter is not present if filter has been submitted for the first time. In this case,
	// we'll just clear the filter.
	/*
	if (!document.getElementById(sourceId + "[0][attribute]")) {
		clearFilter(object, targetIdentifier);
	}
	*/

	for (i = 1; i < object.nextLine; i++) {
		
		document.getElementById("tx_zingdbconn_pi1[" + targetIdentifier + "][filter]["+i+"][attributeCell]").innerHTML =
			'<input type="hidden" name="tx_zingdbconn_pi1[' + targetIdentifier + '][filter]['+i+'][attribute]" value="'
			+	document.getElementById("tx_zingdbconn_pi1[" + targetIdentifier + "][backFilter]["+i+"][attribute]").value
			+	'" />';
		
		document.getElementById("tx_zingdbconn_pi1[" + targetIdentifier + "][filter]["+i+"][operatorCell]").innerHTML =
			'<input type="hidden" name="tx_zingdbconn_pi1[' + targetIdentifier + '][filter]['+i+'][operator]" value="'
			+	document.getElementById("tx_zingdbconn_pi1[" + targetIdentifier + "][backFilter]["+i+"][operator]").value
			+	'" />';
		
		document.getElementById("tx_zingdbconn_pi1[" + targetIdentifier + "][filter]["+i+"][valueCell]").innerHTML =
			'<input type="hidden" name="tx_zingdbconn_pi1[' + targetIdentifier + '][filter]['+i+'][value]" value="'
			+	document.getElementById("tx_zingdbconn_pi1[" + targetIdentifier + "][backFilter]["+i+"][value]").value
			+	'" />';
		
		submitSearch();
		
		/*
		document.getElementById("tx_zingdbconn_pi1[" + targetIdentifier + "][filter]["+i+"][attribute]").value = 
			document.getElementById("tx_zingdbconn_pi1[" + targetIdentifier + "][previousFilter]["+i+"][attribute]").value;
		
		document.getElementById("tx_zingdbconn_pi1[" + targetIdentifier + "][filter]["+i+"][operator]").value = 
				document.getElementById("tx_zingdbconn_pi1[" + targetIdentifier + "][previousFilter]["+i+"][operator]").value;
		
		document.getElementById("tx_zingdbconn_pi1[" + targetIdentifier + "][filter]["+i+"][value]").value = 
			document.getElementById("tx_zingdbconn_pi1[" + targetIdentifier + "][previousFilter]["+i+"][value]").value;
		*/
	}
	//document.getElementById("tx_zingdbconn_pi1[form]").submit();
}


function searchFilter(postfix) {
	zgdbResetStart(postfix);
	zgdbSubmitForm();
}


function addFilterLinkSymbols(count, moduleIdentifier) {
	
	document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier +"][filter_relation]").innerHTML = '';
	
	if (!count) count = 1;
	for (var i = 0; i < Math.floor(count/2); i++) {
		// if (var temp = document.getElementById("tx_zingdbconn_pi1[filter_relation]").innerHTML;
		document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier +"][filter_relation]").innerHTML +=
			'<img class="topImageRelation" src="typo3conf/ext/zin_gdb_conn/img/pi5_top.png"><br />'
			+	'<img src="typo3conf/ext/zin_gdb_conn/img/pi5_and.png"><br />'
			+	'<img id="img_bottom'+i+'" "style="margin-top: 1px; *margin-top: 0px;" src="typo3conf/ext/zin_gdb_conn/img/pi5_bottom.png"><br />';
		log('round: ' + i);
	} /*else {
		filter_relation = document.getElementById("img_bottom"+(i-1)+"");
		filter_relation.src = "typo3conf/ext/zin_gdb_conn/img/pi5_middle.png";
		var temp = document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier +"][filter_relation]").innerHTML;
		document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier +"][filter_relation]").innerHTML = temp
			+	'<img src="typo3conf/ext/zin_gdb_conn/img/pi5_and.png"><br />'
			+	'<img id="img_bottom'+i+'"style="margin-top: 1px; *margin-top: 0px;" src="typo3conf/ext/zin_gdb_conn/img/pi5_bottom.png"><br />';
	}*/
}

function addFilterLinkSymbol(count, object, moduleIdentifier) {
	document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier + "][filter_relation]").innerHTML = '';
	if (!count) count = 2;
	for (var i = 0; i < Math.floor(count/2); i++) {
		var temp = 	document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier + "][filter_relation]").innerHTML;
		document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier + "][filter_relation]").innerHTML = temp+''
			+	'<img class="topImageRelation_' + moduleIdentifier + '"  src="typo3conf/ext/zin_gdb_conn/img/pi5_top.png" alt=""><br />'
			+	'<img src="typo3conf/ext/zin_gdb_conn/img/pi5_and.png" alt=""><br />'
			+	'<img id="img_bottom_' + moduleIdentifier + '_'+object.nextLine+'" "style="margin-top: 1px; *margin-top: 0px;" src="typo3conf/ext/zin_gdb_conn/img/pi5_bottom.png" alt=""><br />';
	}
	
	/*else if (object.nextLine == 4){
		filter_relation = document.getElementById("img_bottom_" + moduleIdentifier + "_" + (object.nextLine-1)+"");
		filter_relation.src = "typo3conf/ext/zin_gdb_conn/img/pi5_middle.png";
		var temp = 	document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier + "][filter_relation]").innerHTML;
		document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier + "][filter_relation]").innerHTML = temp
		+	'<img src="typo3conf/ext/zin_gdb_conn/img/pi5_and.png"><br />'
		+	'<img id="img_bottom'+object.nextLine+'"style="margin-top: 1px; *margin-top: 0px;" src="typo3conf/ext/zin_gdb_conn/img/pi5_bottom.png"><br />';
	}*/
}


function newFilter(source, context, object, moduleIdentifier)
{
	tmpObject = object;
	tmpModuleIdentifier = moduleIdentifier;
	if (object.nextLine > 1) {
		addFilterLinkSymbol(object.nextLine, object, moduleIdentifier);
	}
	
	new Ajax.Request("index.php?eID=zgdbAjax&filterRow=1&objectTypeId="
		+	object.objectTypeId + "&nextLine=" + object.nextLine + "&sourceElement=line&langId="
		+	object.zinGdbConn_langId + "&context=" + context + "&module=" + moduleIdentifier
		+	"&ug="+object.group,  
		{
			method: 'get', 
			onComplete:showNewRow
		}
	);
	object.nextLine++;
	
	/*
	if (object.nextLine < zgdb_maxLines && source.removeAttribute) {
		source.removeAttribute('disabled');
	} else if(source.setAttribute) {
		source.setAttribute('disabled', 'disabled');
	}
	*/
} 

function showNewRow(originalRequest) {
	var result = originalRequest.responseText;
	var o = tmpObject;
	var m = tmpModuleIdentifier;
	
	if (document.getElementById("tx_zingdbconn_pi1[" + m + "][filter]["+ (o.nextLine)+"]")) {
		document.getElementById("tx_zingdbconn_pi1[" + m + "][filter]["+ (o.nextLine)+"]").innerHTML += originalRequest.responseText;
	} else {
		var row = document.createElement('div');
		row.setAttribute("id", "tx_zingdbconn_pi1[" + m + "][filter]["+ (o.nextLine-1)+"]");
		row.setAttribute("class", "filterRow");
		row.setAttribute("className", "filterRow");
		row.innerHTML = originalRequest.responseText;
		document.getElementById('tx_zingdbconn_pi1[' + m + '][filterRows]').appendChild(row);
	}
	
}
/////////////////////////////////////////////////////////////////////////

// Filtereingaben löschen
function clearFilter(object, moduleIdentifier) {

	//document.getElementById("tx_zingdbconn_pi1[form]").reset();
	
	zgdbResetStart(moduleIdentifier);
	
	if (document.getElementById('zgdbFilterStart_' + moduleIdentifier)) {
		document.getElementById('zgdbFilterStart' + moduleIdentifier).value = '0';
	}
	
	for (var i = (object.nextLine-1); i >= 1; i--) {
		
		if (zinGdbConn_autoCompleters[i]) {
			delete zinGdbConn_autoCompleters[i];
		}
		//var filterRow = document.getElementById("tx_zingdbconn_pi1[filterRows]").innerHTML;
		
		var filter = document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier + "][filterRows]");
		filterRow = filter.firstChild;
		
		while(filterRow != undefined){
			
			if (filterRow.nodeName.toLowerCase() != 'div'){
				filterRow = filterRow.nextSibling;
				continue;
			}
			
			document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier + "][filterRows]").removeChild(filterRow);
			filterRow = filterRow.nextSibling;
		}		
	}
	document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier + "][filter_relation]").innerHTML='';
	object.nextLine = 1;
	// newFilter(document.getElementById('newFilterImgButton[' + moduleIdentifier + ']'), object.context, object, moduleIdentifier);
	zgdbSubmitForm();
}


// Suchen 
function submitSearch() {
	zgdbSubmitForm();
}


// Call the new Filter
function callFilter(objectTypeId, nextLine, originator, sourceElement, langId, context, moduleIdentifier, object) {
	tmpObject = object
	tmpModuleIdentifier = moduleIdentifier;
	var attribute_id = originator.value; 
	
	new Ajax.Request(
			"index.php?eID=zgdbAjax&filterRow=0&objectTypeId="+objectTypeId+"&nextLine="+nextLine+"&attribute_id="
			+	attribute_id+"&sourceElement="+sourceElement+"&langId="+langId+"&context="+context+"&module="+moduleIdentifier+"&ug="+object.group,
			{
				method: 'get', 
				onComplete: attributeSelectorChanged
			}
	);
}



// Änderung eines Attributes
function attributeSelectorChanged(request) {
	var o = tmpObject;
	var m = tmpModuleIdentifier;
	var line = request.responseText;
	var results = line.split("||");
	var attribute = document.getElementById("tx_zingdbconn_pi1[" + m + "][filter]["+results[2]+"][attribute]").value;
	var lang_id = results[4];
	document.getElementById("tx_zingdbconn_pi1[" + m + "][filter]["+results[2]+"][operatorCell]").innerHTML = results[0];
	document.getElementById("tx_zingdbconn_pi1[" + m + "][filter]["+results[2]+"][valueCell]").innerHTML = results[1];
	
	switch(results[3]) {
	
		case 1:
		case '1':
			if (zinGdbConn_autoCompleters[results[2]]) {
				delete (zinGdbConn_autoCompleters[results[2]]);
			}
			
			/*
			var tmp = new Ajax.Autocompleter(
					"tx_zingdbconn_pi1[" + m + "][filter]["+results[2]+"][value]", 
					"tx_zingdbconn_pi1[" + m + "][filter]["+results[2]+"][value][autocompleter]",
					"typo3conf/ext/zin_gdb_conn/view/autocompleter.php?objectTypeId="+o.objectTypeId+"&row="+results[2]+"&attribute_id="+attribute+"&lang_id="+lang_id+"&ug="+o.group+"&context="+o.context,
					{
						'paramName'	: 'value'
					}
				);
			*/
			
			var tmp = new Ajax.Autocompleter(
				"tx_zingdbconn_pi1[" + m + "][filter]["+results[2]+"][value]", 
				"tx_zingdbconn_pi1[" + m + "][filter]["+results[2]+"][value][autocompleter]",
				zgdbAutoCompleteTarget + "&l=" + lang_id 
				+	"&c=" + o.context + "&ug=" + o.group + "&a=" + attribute 
				+	"&ot=" + o.objectTypeId,
				{
					'paramName'	:	'value',
					'row'		:	line	
				}
			)
			zinGdbConn_autoCompleters[results[2]] = tmp;
			
			break;
			
		case 2:
		case '2':
			A_TCALDEF.imgpath = 'typo3conf/ext/zin_gdb_conn/img/calendar/';
			var tmp = new tcal (
				{
					'formname': 'tx_zingdbconn_pi1[form]',
					'controlname': 'tx_zingdbconn_pi1[' + m + '][filter][' + results[2] + '][value]'
				}, 
				false, 
				'tx_zingdbconn_pi1[' + m + '][filter][' + results[2] + '][valueCell]'
			);
			zinGdbConn_dateSelectors[results[2]] = tmp;
			break;
			
		default:
			
			
	}
}



function updateOperator(request) {
	var activeLine = nextLine - 1; 
	document.getElementById("tx_zingdbconn_pi1[filter][" + activeLine + "][operator]").innerHTML = request.responseText; 
}



function zgdb_filterLink(attributeId, value, moduleIdentifier, target) {
	// try to get the filter
	var filter = document.getElementById("tx_zingdbconn_pi1[" + moduleIdentifier + "][filterRows]");
	
	// use hidden fields otherwise
	if (!filter) {
		filter = document.getElementById("tx_zingdbconn_pi1[filterAlternate]");
	}
	
	filter.innerHTML = '<input type="hidden" name="tx_zingdbconn_pi1[' + moduleIdentifier
	+	'][filter][1][attribute]" value="' + attributeId + '" />'
	+	'<input type="hidden" name="tx_zingdbconn_pi1[' + moduleIdentifier
	+	'][filter][1][operator]" value="equal" />'
	+	'<input type="hidden" name="tx_zingdbconn_pi1[' + moduleIdentifier
	+	'][filter][1][value]" value="' + value.replace("\"", "\\\"") + '" />';
	
	zgdbSubmitForm(null, target);
}
