window.onload = function()
{
	loadSearchSettings();
	document.getElementById("title").innerHTML = opener.top.strSearchInputTitle;
	document.getElementById("casesenstext").innerHTML = opener.top.strSearchInputCaseSens;
	document.getElementById("fragmenttext").innerHTML = opener.top.strSearchInputFragmentSearch;
	document.getElementById("textError").innerHTML = opener.top.strErrorWrongSearchInput;
}

function loadSearchSettings()
{
	heads 							= document.getElementsByTagName("head");
	scriptElem					= document.createElement("script");
	scriptElem.charset	= "UTF-8";
	scriptElem.type 		= "text/javascript";
	scriptElem.src 			= "../js/search_settings.js";
	heads[0].appendChild(scriptElem);		
}

function searchSettingsLoaded()
{
	addSelection();
	addClasses(searchParams.getAllModelTypes());
	
	var currElement = document.getElementById("searchStart")
	currElement.onclick			= function(){opener.prepareSearch()}
	currElement.onmouseover	= function(){opener.changeImage(true, this, "searchStart")}
	currElement.onmouseout	= function(){opener.changeImage(false, this, "searchStart")}
	
	var currElement = document.getElementById("searchtext")
	currElement.onkeydown 	= catch_enter;
}

function catch_enter(Ereignis)
{
	if ((window.event && window.event.keyCode == 13) || (Ereignis && Ereignis.which == 13))
	{
   	opener.prepareSearch();
	}
}

function modelTypeClasses()
{
	var modelTypeList = new Array();
	var allModelTypes = new Array();
	var modelTypeCounter = 0;

	this.addModelType = function(modeltype)
	{
		modelTypeList[modeltype] = new Array();
		modelTypeCounter = modelTypeCounter + 1;
		allModelTypes.push(modeltype);
	}
	
	this.addClass = function(modeltype, classname)
	{
		modelTypeList[modeltype].push(classname);
	}
	
	this.getNumberOfModelTypes = function()
	{
		return modelTypeCounter;
	}
	
	this.getAllModelTypes = function()
	{
		return allModelTypes;
	}
	
	this.getAllClasses = function(modeltype)
	{
		return modelTypeList[modeltype];
	}
}

Array.prototype.unique = function()
{
	var o={};
	for (var i = 0; i < this.length; i++)
	o[this[i]] = true;
	var tmp = new Array();
	for (var i in o) tmp[tmp.length] = i;
	return tmp;
}

function removeAllChilds(node)
{
	var count =node.childNodes.length;
	for (var i = count - 1; i >= 0; i--)
	{
		node.removeChild(node.childNodes[i])
	}
}

function isInList(searchArray, strElem)
{
	for (var i = 0; i < searchArray.length; i++)
	{
		if (searchArray[i] == strElem)
		{
			return true
		}
	}
	return false
}

function addSelection()
{
	var objLeftModel = document.getElementById("modeltype-left");
	var objRightModel = document.getElementById("modeltype-right");

	numberOfModelTypes = searchParams.getNumberOfModelTypes();
	numberOfModelTypesSep = Math.ceil(numberOfModelTypes/2) + 1;
	listModelTypes = searchParams.getAllModelTypes();
	for (var i = 0; i < numberOfModelTypes; i++)
	{
		var objModel = (i < numberOfModelTypesSep) ? objLeftModel : objRightModel;
		var listElem = document.createElement("li");
		var inputElem = document.createElement("input");
		var textElem = document.createTextNode(listModelTypes[i]);
		inputElem.setAttribute("class", "checkbox");
		inputElem.setAttribute("type", "checkbox");
		inputElem.setAttribute("name", listModelTypes[i]);
		inputElem.onclick = function(){actualizeClasses()};
		objModel.appendChild(listElem);
		objModel.lastChild.appendChild(inputElem);
		objModel.lastChild.appendChild(textElem);
	}
	var listElem = document.createElement("li");
	var inputElem = document.createElement("input");
	var textElem = document.createTextNode(opener.top.strSearchInputWholeSelection);
	inputElem.setAttribute("class", "checkbox");
	inputElem.setAttribute("type", "checkbox");
	inputElem.setAttribute("name", "all");
	inputElem.setAttribute("id", "allModels");
	inputElem.onclick = function(){actualizeClasses()};
	objRightModel.appendChild(listElem);
	objRightModel.lastChild.appendChild(inputElem);
	objRightModel.lastChild.firstChild.checked = true;
	objRightModel.lastChild.appendChild(textElem);
}

function addClasses(listModelTypes)
{
	var objLeftClass = document.getElementById("class-left");
	var objRightClass = document.getElementById("class-right");
  var numberOfModelTypes = listModelTypes.length;
  var collectedClasses = new Array();
  var arrayClasses = new Array();
  var listElems = new Array();

  if (document.getElementById("allClasses"))
  {
		listElems = document.getElementById("classSelect").getElementsByTagName("input");

		if (!document.getElementById("allClasses").checked)
		{
			for (var i = 0; i < listElems.length; i++)
			{
				var currElem = listElems[i];
				if (currElem.name != "allClasses" && currElem.checked)
				{
					arrayClasses.push(currElem.name);
				}
			}
		}
	}
	removeAllChilds(objLeftClass);
	removeAllChilds(objRightClass);
	for (var i = 0; i < numberOfModelTypes; i++)
	{
		collectedClasses = collectedClasses.concat(searchParams.getAllClasses(listModelTypes[i]));
	}
	collectedClasses.sort();
	collectedClasses = collectedClasses.unique();
	numberOfClassesSep = Math.floor(collectedClasses.length/2);	
	for (var i = 0; i < collectedClasses.length; i++)
	{
		var objClass = (i <= numberOfClassesSep) ? objLeftClass : objRightClass;
		var listElem = document.createElement("li");
		var inputElem = document.createElement("input");
		var textElem = document.createTextNode(collectedClasses[i]);
		inputElem.setAttribute("class", "checkbox");
		inputElem.setAttribute("type", "checkbox");
		inputElem.setAttribute("name", collectedClasses[i]);
		objClass.appendChild(listElem);
		objClass.lastChild.appendChild(inputElem);
		objClass.lastChild.appendChild(textElem);
		if (isInList(arrayClasses, collectedClasses[i]))
		{
			objClass.lastChild.firstChild.checked = true;
		}
	}
	var listElem = document.createElement("li");
	var inputElem = document.createElement("input");
	var textElem = document.createTextNode(opener.top.strSearchInputWholeSelection);
	inputElem.setAttribute("class", "checkbox");
	inputElem.setAttribute("type", "checkbox");
	inputElem.setAttribute("name", "allClasses");
	inputElem.setAttribute("id", "allClasses");
	objRightClass.appendChild(listElem);
	objRightClass.lastChild.appendChild(inputElem);
	objRightClass.lastChild.appendChild(textElem);
	if (arrayClasses.length == 0)
	{
		objRightClass.lastChild.firstChild.checked = true;
	}
}

function actualizeClasses()
{
	if (document.getElementById("allModels").checked)
	{
		addClasses(searchParams.getAllModelTypes());	
	}
	else
	{
		var listElems = document.getElementById("modeltypeSelect").getElementsByTagName("input");
		var erg = new Array();
		
		for (var i = 0; i < listElems.length; i++)
		{
			var currElem = listElems[i];
			if (currElem.name != "all" && currElem.checked)
			{
				erg.push(currElem.name)		
			}
		}
		addClasses(erg);
	}
}