var liveFormCheck;

function tipLiveButton() {
	return "Bitte einen Suchbereich ausw&auml;hlen";
}
function displayBMResults(results) {
	$('bodymap_overlay').setStyle('display', 'block');
	displayResults(results);
	setupSlider();
	setupBodymap();
}

function displayResults(results) {
	/*if (results == '') {
		$('livesuche-results').fade('out');
	}
	else { 
		$('livesuche-results').fade('in');*/
		$('bodymap_overlay').setStyle('display', 'block');
		$('livesuche-results').setStyle('display', 'block');
		$('livesuche-results').set('html', results);
		$$('.closebox').addEvent('click', function() {
			$('livesuche-results').setStyle('display', 'none');
			$('bodymap_overlay').setStyle('display', 'none');
		});
	//}
}

function liveSearch(inpElem) {
		var searchSelect = '';
		$$('#livesuche .radio').each(function(item) {
			if(item.get('checked')) {
				searchSelect = item.get('value');
			}
		});
		//var kuerzel = $$('#livesuche input[type=hidden]')[0].get('value');
		
		jsSearchAlias = conf_livesuche[searchSelect+'_js'][conf_lang];
                //if(inpElem.get('value').length > 0 && jsSearchAlias) {
                if(jsSearchAlias) {
					var myRequest = new Request({url: jsSearchAlias+'/search/'+inpElem.get('value')+'.html', method: 'get', onSuccess: displayResults}).get();
                } else {
                        displayResults('');
			$('livesuche-results').setStyle('display', 'none');
                }
}

function liveSucheFeld(inpElem) {
	inpElem.addEvent('focus', function() {
		if(!inpElem.getAttribute('readonly'))
			inpElem.set('value', '');
		else
			liveFormCheck.onSubmit();
			
	});
	inpElem.addEvent('keyup', function() {
		if(!inpElem.getAttribute('readonly'))
			liveSearch(inpElem);
	}); 
	/*$$('#livesuche .radio_container span').each(function(item, idx) {
		item.addEvent('click', function() {
			liveSearch(inpElem);
		});
	});*/	
}

function bodymapLink(inpElem) {
	var bmlink = 'bodymap-embedded.html';
	if(conf_lang == 'en') {
		bmlink = 'bodymap-embedded-en.html';
	} else if(conf_lang == 'ru') {
                bmlink = 'bodymap-embedded-ru.html';
        }

        var myRequest = new Request({url: bmlink, method: 'get', onSuccess: displayBMResults}).get();
        return false;
}

window.addEvent('domready', function() {
	//var tipsButtons = [];
	$$('#livesuche .mandatory input').each(function(item,idx) {
		item.addClass("validate['required']");
	});
	liveFormCheck = new FormCheck($$('#livesuche form')[0], {display: {indicateErrors: 2}});
	$('bodymap_overlay').set('opacity', '0.5');
	$('bodymap_overlay').addEvent('click', function() {
		$('livesuche-results').setStyle('display', 'none');
		$('bodymap_overlay').setStyle('display', 'none');
	});
	$$('.no-autocomplete').each(function(item, idx) {
		item.setAttribute('autocomplete', 'off');
	});
        $('bodymap-banner').addEvent('click', bodymapLink);
	$$('.livesuche-feld').each(function(item,idx) {
		item.setAttribute('readonly', 'readonly');
	});
	$$('.livesuche-feld').each(liveSucheFeld);
	$$('#livesuche span span').each(function(elem,idx) {
		elem.setStyle('display', 'block');
		//tipsButtons[idx] = new Tips(elem, {title: tipLiveButton, fixed: true});
	});
	$$('#livesuche span span').addEvent('click', function() {
		$$('.livesuche-feld').each(function(item,idx) {
         	       item.removeProperty('readonly');
        	});
		$$('#livesuche span span').each(function(elem) {
			elem.removeClass('active');
		});
		this.addClass('active');
		$$('#livesuche span.'+this.getParent().get('class')+' input').each(function(elem) {
			elem.set('checked', 'true');
		});
		liveFormCheck.onSubmit();
		if($('livesuche-results').getStyle('display') == 'none') {
			$$('.livesuche-feld').set('value', '');
			liveSearch($$('.livesuche-feld)')[0]);
		} else {
			liveSearch($$('.livesuche-feld)')[0]);
		}
	});
});

function setupBodymap() {
	$$('#bodymap_cont area').each(function(elem) {
		elem.addEvent('mouseover', function() {
			var curImg = $('bodymap_img').get('src');
			var delInd = curImg.lastIndexOf('_');
			$('bodymap_img').set('src',curImg.substring(0, delInd)+'_'+this.get('class')+'.png');
		});
		elem.addEvent('mouseout', function() {
			var curImg = $('bodymap_img').get('src');
                        var delInd = curImg.lastIndexOf('_');                        $('bodymap_img').set('src',curImg.substring(0, delInd)+'_bg.png');
		});
		elem.addEvent('click', function() {
			$$('#bodymap_right .'+this.get('class')).each(function(elem) {
				if(elem.get('id') && elem.get('id').length > 0) {
					numInd = elem.get('id').lastIndexOf('_'); 
					indSlide = elem.get('id').substring(numInd+1);
					thisSlider[indSlide].toggle();
				}
				toggleElem = elem.getParent('.bm_accordion').getElement('.v_toggle');
				if(toggleElem.hasClass('active'))
					toggleElem.removeClass('active');
				else
					toggleElem.addClass('active');
			});	
		});
	});
}

