
// MMSN template
// Copyright exorbyte GmbH, 2008. All rights reserved.

var webText = "Treffer im Bereich";
var footerText = "Alle Ergebnisse f&uuml;r";
var redirectUrl = "index.php?";
var proxyUrl = "./exorbyte_suche/mmsn.php"
//var proxyUrl = "mmsn.php"

var searchFieldId = '';
var searchFieldPath = '';

function snSetup($, eid) {
	searchFieldId = "eid";
	searchFieldPath = "#" + eid;

	function myCallback(event, data, formatted, value) {
		// track the click
		triggerClick(value, data.SN_type, formatted);
		// handle the selection
		switch(data.SN_type) {
			case "web": 
				location.assign(redirectUrl + "q=" + decodeURIComponent(value) + "&MainCat=" + decodeURIComponent(formatted));
				break;
			default:
				location.assign(redirectUrl + "q=" + decodeURIComponent(formatted));
		}
	}

	$(searchFieldPath).result(myCallback);
 
	// To send the input value to the proxy script for use in eg the Keywords
	// field, simply add 'Keywords: getInputValue' to the list of extraParams
	// in the call to autocomplete() below.
	function getInputValue() {
		return jQuery(searchFieldPath).val();
	}

	// used to generated the header
	function formatHead() {

	}

	// used to generated the footer
	function formatFoot() {
		var inHtml = '<a href="index.php?q='+encodeURIComponent(jQuery(searchFieldPath).val())+'" class="allresults">' + 
			footerText +' "<span class="term">'+ jQuery(searchFieldPath).val() + '</span>"</a>';

		return inHtml;
	}


	// category/section header
	function sectionHeader(s,numHits, numSuggs) {
		var inHtml = '<h3>' + get_header(s) + '</h3>'; 
		// Kontakte 
		if (s == "kon" ) {
			if (numHits > 4 && numSuggs < 5) { 
				inHtml += "<a href='javascript:void(0);' " +
					"onmousedown='moreHits(\""+s+"\");'>Mehr</a>";
			} else {
				if (numHits > numSuggs) { 
					inHtml += '<a href="javascript:void(0);" onmousedown="fewerHits();">Weniger</a>';
				}
			}
		}
		// Systeme
		if (s == "sys" ) {
			if (numHits > 5 && numSuggs < 6) { 
				inHtml += "<a href='javascript:void(0);' " +
					"onmousedown='moreHits(\""+s+"\");'>Mehr</a>";
			} else {
				if (numHits > numSuggs) { 
					inHtml += '<a href="javascript:void(0);" onmousedown="fewerHits();">Weniger</a>';
				}
			}
			
		}
		return inHtml;
	}

	// generate row
	function formatRow(row) {
		var inHtml = row.SN_value;

		if (row.SN_type.split("_") !== null && row.SN_type.split("_")[0] == "cat") {
			inHtml = sectionHeader(row.SN_value, row.ID, row.Typ);
		}
		else {
			switch(row.SN_type) {
				case "suggs":
					inHtml = '<span class="col">' + inHtml + '</span>'; 
					break;
					
				case "web":
					inHtml = '<span class="col" title="' + row.ID + ' ' + webText + ' ' + 
						row.SN_value + '">' + inHtml + ' (' + row.ID + ')</span>'; 
					break;
					
				case "sys":
					var tooltip = row.Sys_nr + ' - ' + row.MetaTitel + ' - ' + row.Headline + ' - ' + 
						row.Subline + ' - ' + row.MetaKeywords + ' - ' + row.Attribute;
						
					inHtml = '<span class="col" title="' + tooltip + '">' +
								row.Sys_nr + ' ' + row.MetaTitel + ' - ' + row.MetaKeywords + '</span>';
					break;
					
				case "kon":
					// standort / adresse -> show Bezeichnung
					typ = row.Typ_Kontakt;

					var name2 =  trim(row.Subline);
					if(row.Subline == "") {
						name2 = trim(row.MetaTitel);
					}
					if(name2 != "") {
						name2 = ', ' + name2;
					}
					
					var strasse = row.Strasse.split("|");
					strasse = strasse[0];

					// set the tooltip
					var tooltip = typ + ': ' + row.Headline + 
						name2 + ', ' + strasse + ', ' + row.Plz + ' ' + row.Ort +
						', Telefon: ' + row.Telefon;
						
					// Displaystring
					inHtml = '<span class="col" title="' + tooltip + '">';
					inHtml += typ + ': ' + trim(row.Headline) + name2 + ', ' + trim(row.PLZ_Name);
					inHtml += '</span>'; 
					break;
				default: 
					inHtml = '<span class="col">' + inHtml + '</span>'; 
					break;
			}
		}

		return inHtml;
	}

	$(searchFieldPath).autocomplete(proxyUrl, {
		extraParams: { sn_type:"suggest-6-5", sn_type_mode:"EXACT" },
		formatItem: formatRow,
		max: 50,
		formatFooter: formatFoot,
		highlight: false,
		imgDir: "../exorbyte_suche/img/",
		showLogoDE: true
	}).trackZeroHits(zeroHits).trackSearchHits(currValue);
	
	
	$('.allresults').mousedown(function(event) {
		event.preventDefault();
		$(searchFieldPath).closest('form').submit();
	})	
}

// HELPER FUNCTIONS
function trim (zeichenkette) {
  // Erst führende, dann Abschließende Whitespaces entfernen
  // und das Ergebnis dieser Operationen zurückliefern
  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}

function get_header(s) {
	switch(s) {
		case "suggs":
			return "Suchvorschl&auml;ge";
		case "web":
			return 'Suche mit "<span class="term">'+jQuery(searchFieldPath).val()+'</span>" im Bereich:';
		case "sys":
			return "Systeme";
		case "kon":
			return "Kontakte";
		default: 
			return "Kategorie nicht bestimmt";
	}
}

//Helper Functions for the "more .." Link in the simple search form SN
function moreHits(s) {
	var expand = "suggest-6-11";
	if(s == "sys") {
		expand = "suggest-11-5";
	}
	jQuery("#suggest1").setOptions({
		extraParams: { sn_type: expand, sn_type_mode:"EXACT" }
	});
	jQuery(searchFieldPath).blur();
	timeout = setTimeout(triggerSuggest, 250);
}

function fewerHits() {
		jQuery(searchFieldPath).setOptions({
			extraParams: { sn_type: "suggest-6-5", sn_type_mode:"EXACT" }
		});
		jQuery(searchFieldPath).blur();
		timeout = setTimeout(triggerSuggest, 250);
}

// Perform search
function triggerSuggest() {
	jQuery(searchFieldPath).triggerSearch(jQuery(searchFieldPath).val());
}

// Functions to track the search 
function triggerClick(q,c,f) {
	if (typeof _gaq != 'undefined') {
		_gaq.push(['_trackEvent', 'Click on Suggest', c, 'Query: ' + q + ' - Clicked: ' + f ]);
	}
}

function zeroHits(e, pq) {
	if (typeof _gaq != 'undefined') {
		_gaq.push(['_trackEvent', 'Zero Hits', pq, '']);
	}
}

function currValue(e, q) {
	if (typeof _gaq != 'undefined') {
		_gaq.push(['_trackEvent', 'Suggest Displayed', q, '']);
		_gaq.push(['_trackEvent', 'Search', q, '']);
	}
}

