﻿
window.search_embedded = false;

// ajax history via rsh
if ( window.dhtmlHistory ) {
  window.dhtmlHistory.create();
}

var historyCount = 0;
var historyListener = function(newLocation, historyData)
{
	if ( newLocation.indexOf('search') == 0 )
	{
		// update search form
		initForm(historyData);
		// fetch results
		trigger_results(true);
	}
}

var postInitProcess = false;

function initForm(str)
{
  if (!str) { str = ""; }
	// get form
	var elem = document.forms.searchform.elements;
	// uncheck all checkboxes
	for(e=0; e<elem.length; e++)
	{
		if (elem[e].type == 'checkbox') elem[e].checked = false;
	}
	// get params
	var params = str.split("&");
	// loop params
	for(p=0; p<params.length; p++)
	{
		// split name/value pair
		var pair = params[p].split("=");
		// look for form field
		if ( elem[pair[0]] )
		{
			// set value
			if ( elem[pair[0]].type == 'checkbox' )
				elem[pair[0]].checked = true;
			else
				elem[pair[0]].value = pair[1];
		}
	}
}

function initSearch()
{
	// ajax history hook
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(historyListener);
	// preload loading gif
	var preload = new Image(); preload.src = "/WFV/exec/php/js/loading.gif";
	// init form
	resetForm();
	if ( typeof(postInit) == "undefined" ) { trigger_count(); }
	trigger_cat();
	// event handler
	document.body.onclick = wowClose;
	window.onscroll = wowClose;
}

function resetForm()
{
	// set dates
	// document.forms.searchform.reset();
	var elem = document.forms.searchform.elements;
	var today = new Date();
	if ( elem.start_date.value == "" )
	{
		elem.start_date.value = today.getDate() + "." + (today.getMonth()+1) + "." + today.getFullYear();
	}
	if ( elem.stop_date.value == "" )
	{
		var endDay = new Date();
		endDay.setTime( today.getTime() + (1000 * 60 * 60 * 24 * 40) );
		elem.stop_date.value = endDay.getDate() + "." + (endDay.getMonth()+1) + "." + endDay.getFullYear();
	}
	//
	trigger_count();
}

function trigger_cat()
{
  if (!window.search_embedded) {
    sendAjaxRequest("/WFV/exec/php/search/searchfilter.php?action=cat", hook_cat);
  }
}

function hook_cat(requestObject)
{
  if (!window.search_embedded) {
    var str = '<select name="catID" id="f_catID" onchange="trigger_haus(this.value)"><option value="0">Alle</option>';
    str += requestObject.xmlHttpResponseText + '</select>';
    document.getElementById("catID").innerHTML = str;
    // post init hook
    if (typeof(postInit) != "undefined") {
      postInit();
    }
  }
}

function trigger_haus(catID)
{
  if (!window.search_embedded) {
    sendAjaxRequest("/WFV/exec/php/search/searchfilter.php?action=haus&catID=" + catID, hook_haus);
  }
}

function hook_haus(requestObject)
{
  if (!window.search_embedded) {
    var str = '<select name="HOUSE" onchange="trigger_count()"><option value="">Alle</option>';
    str += requestObject.xmlHttpResponseText + '</select>';
    document.getElementById("haus").innerHTML = str;
    if (window.always_search) { searchOpen = true; }
    if (postInitProcess == false) {
      trigger_count();
    }
  }
}

function trigger_count()
{
	if ( postInitProcess == false )
	{
		// zero offset
		var elem = document.forms.searchform.elements;
		elem['offset'].value = 0;
		// count or search
		if ( ! searchOpen )
		{
			str = trigger_search();
			// clear results
			document.getElementById("searchresults").style.display = "none";
			// loading
			document.getElementById("candidates").innerHTML = '<div class="loading"><img src="/WFV/exec/php/js/loading.gif" width="32" height="32" alt=""/></div>';
			document.getElementById("candidates").style.display = "block";
			// ajax
			sendAjaxRequest("/WFV/exec/php/search/searchengine.php?type=count&" + str, hook_count);
		}
		else
		{
			trigger_results();
		}
	}
}

function trigger_results_if_open()
{
	if ( searchOpen || window.always_search ) trigger_results();
}

function trigger_results(skipHistory)
{
	str = trigger_search();
	// clear candidates
	document.getElementById("candidates").style.display = "none";
	// loading
	document.getElementById("searchresults").innerHTML = '<div class="loading"><img src="/WFV/exec/php/js/loading.gif" width="32" height="32" alt=""/></div>';
	document.getElementById("searchresults").style.display = "block";
	// add to history
	if ( ! skipHistory )
	{
		dhtmlHistory.add("search" + historyCount, str); historyCount++;
	}
	// ajax
	var withcount = window.no_count ? 0 : 1;
	sendAjaxRequest("/WFV/exec/php/search/searchengine.php?type=results&withcount=" + withcount + "&" + str, hook_results);
}

function setResultOffset(offset)
{
	var elem = document.forms.searchform.elements;
	elem['offset'].value = offset;
	trigger_results();
	return false;
}

function trigger_search()
{
	// get form
	var elem = document.forms.searchform.elements;
	// loop form elements
	var params = new Array();
	for ( var i=0; i<elem.length; i++ )
	{
		if ( elem[i].name != '' )
		{
			if ( elem[i].type == 'checkbox' )
			{
				if ( elem[i].checked )
					params.push(escape(elem[i].name) + "=1");
			}
			else
				params.push(escape(elem[i].name) + "=" + escape(elem[i].value));
		}
	}
	// join
	return params.join("&");
}

function hook_count(requestObject)
{
	document.getElementById("candidates").innerHTML = requestObject.xmlHttpResponseText;
}

var searchOpen = false;

function hook_results(requestObject)
{
	document.getElementById("searchresults").innerHTML = requestObject.xmlHttpResponseText;
	searchOpen = true;
}

var overID = '';

function wowOver(itemID, obj)
{
	obj.style.backgroundColor = "#eee";
	//wowClose();
	overID = itemID;
	// short delay
	setTimeout("wowOverLoad('" + itemID + "')", 100);
}

function wowOverLoad(itemID)
{
	if ( overID == itemID )
	{
		wowFront(1);
		// ajax
		sendAjaxRequest("/WFV/exec/php/search/searchwow.php?itemID=" + itemID, hook_item);
		sendAjaxRequest("/WFV/exec/php/search/searchcalendar.php?itemID=" + itemID, hook_calendar);
	}
}

function wowOut(obj)
{
	overID = '';
	obj.style.backgroundColor = "transparent";
}

function wowClose()
{
	overID = '';
	var node = document.getElementById("wowover");
	node.style.visibility = "hidden";
	node.innerHTML = "";
	var node = document.getElementById("wowcalendar");
	node.style.visibility = "hidden";
	node.innerHTML = "";
}

function hook_item(requestObject)
{
	var node = document.getElementById("wowover");
 	// content
	node.innerHTML = requestObject.xmlHttpResponseText;
  // position
 var position = getScrollingPosition();
  var y = position[1] + Math.round(getHeight()/2) - node.clientHeight + 25;
  var yr = position[1] + 20;
  node.style.top = Math.max(y, yr) + "px";
  node.style.visibility = "visible";
  // lightbox
  initLightbox();
}

function hook_calendar(requestObject)
{
	var node = document.getElementById("wowcalendar");
 	// content
	node.innerHTML = requestObject.xmlHttpResponseText;
  // position
  var position = getScrollingPosition();
  var y = position[1] + Math.round(getHeight()/2) - 25;
  var yr = position[1] + getHeight() - node.clientHeight - 20;
  node.style.top =  Math.min(y, yr) + "px";
  node.style.visibility = "visible";
}

function wowFront(type)
{
	var n1 = document.getElementById("wowover");
	var n2 = document.getElementById("wowcalendar");
	n1.style.zIndex = type == 1 ? 2 : 1;
	n2.style.zIndex = type == 1 ? 1 : 2;
}

function getScrollingPosition()
{
	var position = [0, 0];
	if (typeof window.pageYOffset != 'undefined')
	{
		position = [window.pageXOffset, window.pageYOffset];
	}
	else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0)
	{
		position = [document.documentElement.scrollLeft, document.documentElement.scrollTop];
	}
	else if (typeof document.body.scrollTop != 'undefined')
	{
		position = [document.body.scrollLeft, document.body.scrollTop];
	}
	return position;
}

function getHeight()
{
	var h = 0;
 if( typeof( window.innerWidth ) == 'number' )
  {
  	h = window.innerHeight;
  }
  else if( document.documentElement && document.documentElement.clientHeight )
  {
		h = document.documentElement.clientHeight;
	}
	else if( document.body && document.body.clientHeight  )
	{
  	h = document.body.clientHeight;
  }
  return h;
}

function toggleText(id, obj)
{
	var node = document.getElementById("text_" + id);
	node.style.display = node.style.display == 'block' ? 'none' : 'block';
	obj.innerHTML = obj.innerHTML == 'Mehr...' ? 'Kürzer...' : 'Mehr...';
	return false;
}
function toggleText2(id, obj)
{
	var node = document.getElementById("text_" + id);
	node.style.display = node.style.display == 'block' ? 'none' : 'block';
	obj.innerHTML = obj.innerHTML == 'Hausbeschreibung einblenden...' ? 'Ausblenden...' : 'Hausbeschreibung einblenden...';
	return false;
}

window.onload = initSearch;

