/* USAGE:
		eventSearchWindow = new ObjectSearchWindowClass();
		eventSearchWindow.id = 'eventSearchWindow';
		eventSearchWindow.name = 'eventSearchWindow';
		eventSearchWindow.type = 'events';
		eventSearchWindow.searchString = 'Name des Events...';
		eventSearchWindow.description = 'Gebe den Namen des Events ein, nach dem du suchst und klicke auf "Suchen"...';
		eventSearchWindow.eventHandler = this;
		eventSearchWindow.sendObjectName = true;
		eventSearchWindow.sendObjectThumbnail = true;
		eventSearchWindow.init();
*/

function ObjectSearchWindowClass() {
	var currentPage = 1;
	
	this.init = function() {
		this.buildContainer();
		this.box = document.getElementById(this.id + 'windowContainer');
		
		scrollPosition = getCurrentScrollPosition();
		
		this.box.style.top = scrollPosition["top"] + 50 + "px";

		leftPos = Math.ceil((scrollPosition["width"] / 2) - 325);
		
		if(leftPos < 0) {
			leftPos = 10;
		}
		
		this.box.style.left = leftPos + "px";		
		
		this.show();
	}
	
	this.show = function() {
		this.box.style.zIndex = windowManager.getCurrentZIndex() + 1;
		this.box.style.display = 'block'; 
	}
	
	this.search = function() {
		currentPage = 1;
		this.searchStringValue = document.getElementById(this.id + 'searchInputField').value;
		this.searchInt();
	}
	
	this.searchInt = function() {
		var output = new Object();
		output.searchString = encodeURIComponent(this.searchStringValue);
		output.searchType = this.type;
		output.limit = 10;
		output.page = currentPage;
			
		outputString = JSONstring.make(output);

		jsonPostData('jsApi/search/', escape(outputString), this.name + '.displayResults');		
	}

	this.displayResults = function(Object) {
		var showErrorMessage = true;
		document.getElementById(this.id + 'resultsList').innerHTML = '';
		
		if(Object) {
			if(Object["status"]) {
				if(Object["status"]=='ok') {

				
					if(Object["results"]) {
						if(Object["counter"]) {
							pageCounter = Math.ceil(Object["counter"] / 10);
							if(currentPage == 1) {
								prevPageLink = '<img src="/images/style/arrows/greyArrowLeft.gif" class="arrowLeftIcon"/>';				
							} else {
								prevPageLink = '<a href="JavaScript:' + escape(this.name) + '.prevPage();"><img src="/images/style/arrows/greyArrowLeftActive.gif" class="arrowLeftIcon"/></a>';							
							}
							
							if(currentPage == pageCounter) {
								nextPageLink = '<a href="#"><img src="/images/style/arrows/greyArrowRight.gif" class="arrowRightIcon"/></a>';
							} else {
								nextPageLink = '<a href="JavaScript:' + escape(this.name) + '.nextPage();"><img src="/images/style/arrows/greyArrowRightActive.gif" class="arrowRightIcon"/></a>';							
							}								

							document.getElementById(this.id + 'upperPageBar').innerHTML = prevPageLink + 'Seite ' + currentPage + ' von ' + pageCounter + nextPageLink;	
							document.getElementById(this.id + 'bottomPageBar').innerHTML = prevPageLink + 'Seite ' + currentPage + ' von ' + pageCounter + nextPageLink;	
						}
						
						showErrorMessage = false;
						
						if(Object["results"].length > 0) {
							evenString = 'even';
							for(resCnt = 0; resCnt <= Object["results"].length - 1; resCnt++) {
								var listContainer = document.createElement("DIV");
								listContainer.id = this.id + 'lC' + resCnt;
								listContainer.className = 'listContainer ' + evenString;
								
								eval("listContainer.onmouseover = function() { document.getElementById('" + this.id + 'lC' + resCnt + "').className = '" + listContainer.className + " hovered' };");
								eval("listContainer.onmouseout = function() { document.getElementById('" + this.id + 'lC' + resCnt + "').className = '" + listContainer.className + "' };");
								if(this.sendObjectName && !this.sendObjectThumbnail) {
									eval("listContainer.onclick = function() { " + this.name + ".eventHandler.objectSelected('" + Object["results"][resCnt]["ID"] + "', '" + Object["results"][resCnt]["Title"] + "'); " + this.name + ".close(); };");
								} else if(this.sendObjectThumbnail) {
									eval("listContainer.onclick = function() { " + this.name + ".eventHandler.objectSelected('" + Object["results"][resCnt]["ID"] + "', '" + Object["results"][resCnt]["Title"] + "', '" + Object["results"][resCnt]["ImagePath"] + "'); " + this.name + ".close(); };");									
								} else {
									eval("listContainer.onclick = function() { " + this.name + ".eventHandler.objectSelected('" + Object["results"][resCnt]["ID"] + "'); " + this.name + ".close(); };");									
								}														
								var listThumbnailContainer = document.createElement("DIV");
								listThumbnailContainer.className = 'listThumbnailContainer';
								
								if(Object["results"][resCnt]["ImagePath"]) {
									var listThumbnail = document.createElement("IMG");
									listThumbnail.className = 'listThumbnail';
									listThumbnail.src = Object["results"][resCnt]["ImagePath"];
									
									listThumbnailContainer.appendChild(listThumbnail);
								}
								
								listTitle = document.createElement("DIV");
								listTitle.className = 'listTitle';
								if(Object["results"][resCnt]["Title"].length > 39) {
									Object["results"][resCnt]["Title"] = Object["results"][resCnt]["Title"].slice(0, 38);
									Object["results"][resCnt]["Title"] = Object["results"][resCnt]["Title"] + '...';
								}
								listTitle.innerHTML = Object["results"][resCnt]["Title"];
								
								listSubTitle = document.createElement("DIV");
								listSubTitle.className = 'listSubTitle';
								listSubTitle.innerHTML = Object["results"][resCnt]["SubTitle"];
								
								listContainer.appendChild(listThumbnailContainer);
								listContainer.appendChild(listTitle);
								listContainer.appendChild(listSubTitle);
								
								document.getElementById(this.id + 'resultsList').appendChild(listContainer);
								
								if(evenString=='even') {
									evenString = 'uneven';
								} else {
									evenString = 'even';
								}
							}
						}
					}
				}
			}
		}
		
		if(showErrorMessage) {
			document.getElementById(this.id + 'upperPageBar').innerHTML = '';
			document.getElementById(this.id + 'bottomPageBar').innerHTML = '';
			
			var errorMessage = document.createElement("DIV");
			errorMessage.className = 'errorMessage';
			errorMessage.innerHTML = '<h1>Oooh ...</h1> Zu diesem Suchbegriff konnten wir nichts finden!<br /><br />';
			
			if(this.type == 'locations') {
				var createLocationButton = document.createElement("INPUT");
				createLocationButton.type = 'button';
				createLocationButton.value = 'Neue Location anlegen';
				eval("createLocationButton.onclick = function() { " + this.name + ".createLocation(); };");
				
				errorMessage.appendChild(createLocationButton);
			}
			document.getElementById(this.id + 'resultsList').appendChild(errorMessage);		
		}
	}

	this.close = function() {
		this.box.style.display = 'none';
		this.box.innerHTML = '';
		
		document.getElementsByTagName("body")[0].removeChild(this.box);
	}

	this.setHeader = function(headerString) {
		document.getElementById(this.id + 'ContainerHeader').innerHTML = headerString;
	}

	this.setSubHeader = function(headerString) {
		document.getElementById(this.id + 'ContainerSubHeader').innerHTML = headerString;
	}

	this.dragWindow = function(evt) {
		var mousePosition = getMousePosition(evt);
		
		windowManager.draggedWindow = this.box;	
		windowManager.windowPositionRelativeToMousePosition = getElementPositionRelativeToMousePosition(document.getElementById(this.id + 'windowContainer'), mousePosition);
		
		// Set Event-Handler for Dragging
		eval("document.onmousemove = " + this.name + ".mousemoveEventHandler;");
		eval("document.onmouseup = " + this.name + ".dropWindow;");
		  	
		// Disable Text-Selection
		disableTextSelection(document.getElementsByTagName("body")[0]);
	}
	
	this.dropWindow = function() {	
		document.onmousemove = null;
		document.onmouseup = null;

		enableTextSelection(windowManager.draggedWindow);
		
		windowManager.draggedWindow = false;
	}

	this.mousemoveEventHandler = function(evt) {
		var mousePosition = getMousePosition(evt);
		
		mousePosition["x"] = mousePosition["x"] - windowManager.windowPositionRelativeToMousePosition["x"];
		mousePosition["y"] = mousePosition["y"] - windowManager.windowPositionRelativeToMousePosition["y"];

		// Set DragBox-Position
		windowManager.draggedWindow.style.top = mousePosition["y"] + "px";
		windowManager.draggedWindow.style.left = mousePosition["x"] + "px";	
	}
	
	this.setToTop = function() {
		this.box.style.zIndex = windowManager.increaseZIndex();
	}

	this.nextPage = function() {
		currentPage++;
		this.searchInt();
	}
	
	this.prevPage = function() {
		if(currentPage >= 2) {
			currentPage--;
			this.searchInt();
		}
	}
	
	this.searchInputFieldOnFocus = function() {
		document.getElementById(this.id + 'searchInputField').value = '';
	}

	this.createLocation = function() {
		if(!this.locationCreator) {
			appManager.addApp('MYLOCATIONSAPP', false, this.name + '.createLocationInt');
			appManager.startApp('MYLOCATIONSAPP');			
		} else {
			this.locationCreator.init();
		}
	}

	this.createLocationInt = function() {
		this.locationCreator = new MyLocationsAppClass();
		this.locationCreator.id = this.id + 'locationCreator';
		this.locationCreator.name = this.name + '.locationCreator';
		this.locationCreator.eventHandler = this;
		
		this.locationCreator.init();		
	}
	
	this.locationCreated = function(locationId) {
		var output = new Object();
		output.locationId = locationId;
				
		outputString = JSONstring.make(output);

		jsonPostData('jsApi/locations/get/locationInfo', escape(outputString), this.name + '.locationCreatedInt');	
	}
	
	this.locationCreatedInt = function(Object) {
		if(Object["status"]) {
			if(Object["status"] == 'ok') {
				if(this.sendObjectName && !this.sendObjectThumbnail) {
					this.eventHandler.objectSelected(Object["info"]["PublicID"], Object["info"]["Name"]);
				} else if(this.sendObjectThumbnail) {
					this.eventHandler.objectSelected(Object["info"]["PublicID"], Object["info"]["Name"], Object["info"]["ImagePath"]);
				} else {
					this.eventHandler.objectSelected(Object["info"]["PublicID"]);									
				}
				
				this.close();
			}
		}		
	}
	
	this.buildContainer = function() {
		var windowContainer = document.createElement("DIV");
		windowContainer.id = this.id + 'windowContainer';
		windowContainer.className = 'objectSearchWindowContainer';
		eval("windowContainer.onclick = function() { " + this.name + ".setToTop(); };");
		
		windowDragBar = document.createElement("DIV");
		windowDragBar.id = this.id + 'windowDragBar';
		windowDragBar.className = 'objectSearchWindowDragBar';
		eval("windowDragBar.onmousedown = function(event) { " + this.name + ".dragWindow(event); };");
			
		var windowBackground = document.createElement("DIV");
		windowBackground.className = 'objectSearchWindowBackground';
		
		// CloseLink + Container
		var closeLinkContainer = document.createElement("DIV");
    	closeLinkContainer.id = this.id + "CloseLink";
    	closeLinkContainer.className = "objectSearchWindowContainerCloseLink";
	
 		var closeLink = document.createElement("A");
    	closeLink.href = "JavaScript:" + this.name + ".close();";
    	closeLink.innerHTML = "X";
    	closeLinkContainer.appendChild(closeLink); 	

    	windowContainer.appendChild(windowBackground);  	
		windowContainer.appendChild(windowDragBar);
    	windowContainer.appendChild(closeLinkContainer);  
    	// End CloseLink
	    	
    	// Header
		var containerHeader = document.createElement("DIV");
    	containerHeader.id = this.id + "ContainerHeader";
    	containerHeader.className = "searchWindowContainerHeader";
		containerHeader.innerHTML = 'Suchergebnisse ...';

		var searchBar = document.createElement("DIV");
		searchBar.className = 'searchBar';
		
		var searchForm = document.createElement("FORM");
		searchForm.action = 'javaScript:' + this.name + '.search();';
		
		var searchInputField = document.createElement("INPUT");
		searchInputField.type = 'text';
		searchInputField.id = this.id + 'searchInputField'
		searchInputField.className = 'searchInputField';
		searchInputField.value = this.searchString;
		eval("searchInputField.onfocus = function() { " + this.name + ".searchInputFieldOnFocus(); };");
		
		var searchButton = document.createElement("INPUT");
		searchButton.type = 'image';
		searchButton.className = 'searchButton';
		searchButton.src = '/images/style/searchButton.jpg'
		
		searchForm.appendChild(searchInputField);
		searchForm.appendChild(searchButton);
		searchBar.appendChild(searchForm);		

		var containerContent = document.createElement("DIV");
		containerContent.id = this.id + 'content';
		containerContent.className = 'containerContent';
		
		var searchResultsContainer = document.createElement("DIV");
		searchResultsContainer.id = this.id + 'searchResultsContainer';
		searchResultsContainer.className = 'searchResultsContainer';
		
		var upperPageBar = document.createElement("DIV");
		upperPageBar.id = this.id + 'upperPageBar';
		upperPageBar.className = 'pageBar';

		var resultsList = document.createElement("DIV");
		resultsList.id = this.id + 'resultsList';
		resultsList.className = 'resultsList';
		
		var instructionsMessage = document.createElement("DIV");
		instructionsMessage.className = 'errorMessage';
		instructionsMessage.innerHTML = '<h1>Suchen ...</h1>' + this.description;
		resultsList.appendChild(instructionsMessage);	

		var bottomPageBar = document.createElement("DIV");
		bottomPageBar.id = this.id + 'bottomPageBar';		
		bottomPageBar.className = 'pageBar bottom';

		searchResultsContainer.appendChild(upperPageBar);		
		searchResultsContainer.appendChild(resultsList);
		searchResultsContainer.appendChild(bottomPageBar);		
		
		containerContent.appendChild(searchBar);
		containerContent.appendChild(searchResultsContainer);
		
		// Add Header
    	windowContainer.appendChild(containerHeader);
		windowContainer.appendChild(containerContent);
		
		document.getElementsByTagName("body")[0].appendChild(windowContainer);		
	}
}

function searchobjectSearchWindowClassjs() {}
