var RightImg=document.createElement("img");
RightImg.src=_gdoc_root+"imageLib/webImages/spacer.gif";
var RightHImg=document.createElement("img");
RightHImg.src=_gdoc_root+"imageLib/webImages/spacer.gif";
var _gMainPg='';
var _gMid=0;
function RepLR(mid,flag){
	if(flag==1){
		document.getElementById('M'+mid).className='topNavi left';
		document.getElementById('R'+mid).src=RightHImg.src;
	}else{
		document.getElementById('M'+mid).className='topNavi left';
		document.getElementById('R'+mid).src=RightImg.src;
	}
}
var t1;var t2;

/**
* this function calls geocodeLocation function of 
* FalkAutoSuggestControl and then set the returned values in a callback function into a session
* this values are later required on search page for a search with bbox parameter
* once all these is done redirect the page to search page using call back function for
* ajax class: page_redirect()
*/
function create_search_url(typ){
	var searchVal=document.getElementById('frm_search').value;
	var locVal=document.getElementById('frm_loc').value;
	if(locVal==''||locVal==' '){locVal='Wo?'}
	if(searchVal=='Was?'&&locVal=='Wo?'){
		var errorObj=document.getElementById('errorDivTop');
		errorObj.innerHTML="<div style=\"text-align:center\"><span style=\"font-family:'Arial'; font-size:13px; color:#000000;\">Gebe bitte einen Suchbegriff und einen Ort an.</span></div>";
		fadeDivSingle('errorDivTop');
		document.getElementById('searchbutton').disabled=true;
		document.getElementById('searchbutton').style.cursor='help';
		setTimeout("fadeDivSingle('errorDivTop')",3000);
		setTimeout("document.getElementById('searchbutton').disabled = false; document.getElementById('searchbutton').style.cursor = 'pointer';",3000);
		return false;
	}else if(locVal=='Wo?'){
		jQuery.ajax( {
        url: _gdoc_root+"ajax/unsetLocWord.php",
        type: "GET", //or may be "GET" as you wish
        complete: function( result ) {
            page_redirect();
        }
    } );
		
	}else if(locVal.indexOf(",") != -1){ 
		asc.geocodeLocation();
	}else{
    	jQuery.ajax( {
        	url: _gdoc_root+"ajax/geocode.php",
        	type: "GET", //or may be "GET" as you wish
        	data: { loc: locVal },
        	complete: function( result ) {
            	handleResult(result.responseText);
        	}
    	} );
	}
}

function handleResult(result){
	var geocodingResult = jQuery.parseJSON(result);
	//TODO: geocodingResult = null!!!
	var address = geocodingResult.address;
	var alternatives = geocodingResult.alternatives;
	var searchVal=document.getElementById('frm_search').value;
	if(trim(searchVal) == '' || searchVal == 'Was?'){
		searchVal = 'alle';
	}
	
	var resultType = address.resultType;
	var locVal = "";
	
		/**if(typeof alternatives !== 'undefined' && alternatives != null && alternatives.length > 0){
			//locVal = address.city;
		}else{
			locVal = address.zip + " ";
		}**/
		
		if(address.uniqueness == false){
			locVal = address.zip + " ";
		}
		
		if(address.resultType == "SINGLE_MATCHING_DISTRICT_RESULT" || address.resultType == "FIRST_MATCHING_DISTRICT_RESULT" || address.resultType == "CONTAINING_DISTRICT_RESULT"){
			locVal +=  address.district;
		}else{
			locVal +=  address.city;
		}
		var street = "";
		if(typeof address.street != 'undefined' && address.street != ""){
			street = address.street;
		}
		var countryCode = address.country.isoCode2Digits.toLowerCase();
		var replacer = new RegExp(" ","g");
	 	var url = fix_seourl(_gdoc_root+ countryCode +"/"+(jQuery.trim(locVal)).replace(replacer, "-") +"/"+searchVal.replace(replacer, "-")+"/");
		
		var frmObj = document.forms['search_form'];
		var userLocVal=document.getElementById('frm_loc').value;
		frmObj.elements['search_locVal'].value = userLocVal;
		frmObj.elements['search_street'].value = street;
		if(typeof alternatives == 'undefined' || alternatives == null || alternatives.length == 0){
			frmObj.elements['search_address'].value = JSON.stringify(address);
		}
		frmObj.action = url;
		frmObj.submit();
	
	
}

function page_redirect(dataArr,fnVars){
	var searchVal=document.getElementById('frm_search').value;
	var locVal=document.getElementById('frm_loc').value;
	if(searchVal==''||searchVal==' '){
		searchVal='Was?';
	}

	if(locVal==''||locVal==' '){locVal='Wo?'}
	if(searchVal=='Was?'&&locVal=='Wo?'){var errorObj=document.getElementById('errorDivTop');errorObj.innerHTML="<div style=\"text-align:center\"><span style=\"font-family:'Arial'; font-size:13px; color:#000000;\">Gebe bitte einen Suchbegriff und einen Ort an.</span></div>";fadeDivSingle('errorDivTop');document.getElementById('searchbutton').disabled=true;document.getElementById('searchbutton').style.cursor='help';setTimeout("fadeDivSingle('errorDivTop')",3000);setTimeout("document.getElementById('searchbutton').disabled = false; document.getElementById('searchbutton').style.cursor = 'pointer';",3000);return false;}
	if(searchVal==''||searchVal=='Was?'){searchVal="/alle";}else{searchVal="/"+searchVal.replace(/ /gi,'-');}
	if(locVal==''||locVal=='Wo?'){locVal="";}else{
		var boundaddr = locVal.split(",");
		var fboundaddr = boundaddr[0];
		var boundaddr2 = fboundaddr.split(" ");
		if(boundaddr2[0]){
			locVal = fboundaddr;
		}else{
			locVal = locVal;
		}
		if(fnVars['city'])
			locVal= fnVars['city'];		
		locVal="/"+locVal.replace(/ /gi,'-');}

	//if(typ=='1'){var url=fix_seourl(_gdoc_root+'de/seo-seite'+'/alle'+searchVal);}else if(typ=='2'){var url=fix_seourl(_gdoc_root+'de/seo-seite'+locVal+'/alle');}else{var url=fix_seourl(_gdoc_root+'de'+locVal+searchVal);}	
	
	
	var url = fix_seourl(_gdoc_root+'de'+locVal+searchVal);
	var searchWord = document.getElementById('frm_search').value;
	jQuery.ajax( {
        	url: _gdoc_root+"ajax/setSearchWord.php",
        	type: "POST", //or may be "GET" as you wish
        	data: { search: searchWord },
        	complete: function() {
            	location.href=url;
        	}
    	} );
	
}

function animate_bg(obj){
	var curCls = obj.className;
	if(curCls == 'linkbtn')
		obj.className = 'linkbthover';
	else
		obj.className = 'linkbtn';		
}

function isInt(n) {
   return typeof n == 'number' && n % 1 == 0;
}
function create_seo_url(typ){
	var searchVal=document.getElementById('frm_search').value;
	var locVal=document.getElementById('frm_loc').value;
	if(searchVal==''||searchVal==' '){
		searchVal='Was?';
	}
	if(locVal==''||locVal==' '){
		locVal=='Wo?'
	}
	if(searchVal==''||searchVal=='Was?'){
		searchVal="/alle";
	}else{
		searchVal="/"+searchVal.replace(/ /gi,'-');
	}
	if(locVal==''||locVal=='Wo?'){
		locVal="/alle";
	}else{
		locVal="/"+locVal.replace(/ /gi,'-');
	}

	if(typ=='1'){
		if(searchVal=="/alle"){
			searchVal="/Restaurants";
		}
		var url=fix_seourl(_gdoc_root+'de/uebersicht'+'/alle'+searchVal);
	}else if(typ=='2'){
		if(locVal=="/alle"){
			locVal="/Berlin";
		}
		var url=fix_seourl(_gdoc_root+'de/uebersicht'+locVal+'/alle');
	}else{
		var url=fix_seourl(_gdoc_root+'de'+locVal+searchVal);
	}
	location.href=url;
}

function createBoundRectSearch(result) {
    var address = result.geocodeResult[0];
    var searchVal = document.getElementById('frm_search').value;
    if (searchVal == '' || searchVal == 'Was?') {
        searchVal = "";
    } else {
        searchVal = "/" + searchVal;
    }
    if (address.zip) {
        var url = fix_seourl(_gdoc_root + 'de/' + address.zip + '-' + address.city + searchVal);
    } else {
        var url = fix_seourl(_gdoc_root + 'de/' + address.city + searchVal);
    }
}
function showError(result) {
    var o = YDom.get("error");
    if (o && result && result.error) {
        o.innerHTML = result.error.userText;
    }
}

function fix_seourl(url) {
    url = url.replace(/&/g, 'und');
    return url.replace(/&/g, 'und');
}

function changeTab(obj) {
    var objRel = obj.getAttribute('rel');
    divObj = document.getElementById('tabContainer');
    aCol = divObj.getElementsByTagName("A");
    for (i = 0; i < aCol.length; i++) {
        var relArt = aCol[i].getAttribute('rel');
        var tabDiv = document.getElementById('mapDiv' + i);
        if (relArt.indexOf(objRel) < 0) {
            if (relArt == 'tab0') {
                aCol[i].className = 'mapnviFirst';
            } else {
                aCol[i].className = 'mapnvi';
            }
            tabDiv.className = 'hidden';
        } else {
            if (relArt == 'tab0') {
                aCol[i].className = 'mapnviSelectFirst';
            } else {
                aCol[i].className = 'mapnviSelect';
            }
            tabDiv.className = 'visible';
        }
    }
}
function changePoiTab(obj) {
    $(".active").addClass("hand");
    $(".active").removeClass("active");
   
    $(obj).addClass("active");
    $(obj).removeClass("hand");
    
    locObj = document.getElementById('sel_loc');
    var tabIdx = obj.getAttribute('tabIdx');
    locObj.value = tabIdx;
    
    enableRelDiv();
    resetTicker();
}

function changeCat(obj) {
    if (obj.title == 'mehr Kategorien') {
        create_seo_url('2');
    } else {
        $(".matrixCat.lnvisel").removeClass("lnvisel");
        catObj = document.getElementById('sel_cat');
        divObj = document.getElementById('leftDiv');
        aCol = divObj.getElementsByTagName("A");
        for (i = 0; i < aCol.length; i++) {
            var relArt = aCol[i].getAttribute('rel');
            if (relArt == 'lnvisel') {
                aCol[i].setAttribute('rel', '');
            }
        }
        obj.className = 'lnvisel';
        obj.setAttribute('rel', 'lnvisel');
        $(obj).parents('.matrixCat').addClass("lnvisel");
        catObj.value = obj.getAttribute('catid');
        enableRelDiv();
        resetTicker();
    }
}

function enableRelDiv() {
    catObj = document.getElementById('sel_cat');
    locObj = document.getElementById('sel_loc');
    
    $('.matrixSingleDescription.visible').addClass('hidden');
    $('.matrixSingleDescription.visible').removeClass('visible');
    $('#description-' + catObj.value + '-' + locObj.value).addClass('visible');
    $('#description-' + catObj.value + '-' + locObj.value).removeClass('hidden');
    
    divObj = document.getElementById('poiContents');
    allDivs = divObj.getElementsByTagName("DIV");
    for (i = 0; i < allDivs.length; i++) {
        catRel = allDivs[i].getAttribute('catRel');
        if (catRel) {
            allDivs[i].className = 'hidden';
        }
    }
    var tabDiv = document.getElementById('tab-' + catObj.value + '-' + locObj.value);
    if (tabDiv) tabDiv.className = 'visible';
}

function appendDivsToTabs() {
    pdivObj = document.getElementById('poiDataTabs');
    allDivs = document.getElementsByTagName("DIV");
    for (i = 0; i < allDivs.length; i++) {
        cat_art = allDivs[i].getAttribute('catRel');
        if (cat_art) {
            var m_divObj = allDivs[i].cloneNode(true);
            allDivs[i].parentNode.removeChild(allDivs[i]);
            pdivObj.appendChild(m_divObj);
            clonenode = '';
        }
    }
    enableRelDiv();
}

function removeSelCat(opt) {
    divObj = document.getElementById('leftDiv');
    aCol = divObj.getElementsByTagName("A");
    for (i = 0; i < aCol.length; i++) {
        var relArt = aCol[i].getAttribute('rel');
        if (relArt == 'lnvisel') {
            if (opt){ 
            	aCol[i].className = 'lnvisel';
            	$(aCol[i]).parents('.matrixCat').addClass('lnvisel');
            }
            else {
            	aCol[i].className = 'lnvi';
            	$(".matrixCat.lnvisel").removeClass("lnvisel");	
            }
        }
    }
}

function mapInit() {
    var icon = new FalkIcon();
    icon.alignment = "sw";
    icon.offsetX = -16;
	icon.offsetY = 1;
    icon.url = _globalHttpPath + "imageLib/webImages/pointer.png";
    _gfmo = new FalkMapObject();
    _gfmo.icon = icon;
    mapObj = new map();
    mapObj.divId = 'mapDiv';
    //mapObj.mapWidth = '457';
    //mapObj.mapHeight = '235';
    mapObj.gotoFunction = 'tracePoint';
    mapObj.switchControl = false;
    mapObj.mapScroller = false;
    mapObj.mapLayerType = 1;
    mapObj.drawMap();
}
var idx = 0;
var _gfmo = false;
var tt = 0;

function resetTicker() {
    catIdx = document.getElementById('sel_cat').value;
    locIdx = document.getElementById('sel_loc').value;
    if (typeof ticker_arr != 'undefined') {
        if (typeof mapObj != 'undefined') {
            clearTimeout(tt);
            idx = 0;
            mapObj.wgs84 = new Array();
            mapObj.info = new Array();
            if (ticker_arr) {
                if (catIdx > 0) {
                    if (ticker_arr[catIdx][locIdx] != null) {
                        var arr_obj = ticker_arr[catIdx][locIdx];
                        if (arr_obj) {
                            for (i = 0; i < arr_obj['coordinates'].length; i++) {
                                mapObj.wgs84.push(new FalkPoint(arr_obj['coordinates'][i]['lon'], arr_obj['coordinates'][i]['lat']));
                                mapObj.info.push(new Array(arr_obj['title'][i], arr_obj['desc'][i], arr_obj['poi_id'][i], arr_obj['tag'][i]));
                            }
                            playTicker();
                        }
                    }
                }
            }
        }
    }
}

function isArray(obj) {
    return (typeof (obj.length) == "undefined") ? false : true;
}

function tracePoint(mapObj) {
    var map = mapObj.map;
    var overlay = map.getMapOverlay();
    overlay.subscribe(FalkOverlay.MSG_FALK_MAP_OBJECT_CLICKED, markerClicked);
    geocode = new FalkPoint(9.223379437790086, 48.72223860481109);
    map.centerMapToGeocode(geocode, 14);
    resetTicker();
}

function playTicker() {
    if (mapObj.wgs84[idx]) {
    	$('#poiContents .visible .listPoi.shown').removeClass('shown');
    	$($('#poiContents .visible .listPoi')[idx]).addClass('shown'); 
        spnObj = document.getElementById('poiTxtInner');
        spnObjOne = document.getElementById('poiDescInner');
        poiidObj = document.getElementById('divPoiid');
        var overlay = mapObj.map.getMapOverlay();
        _gfmo.setGeocode(mapObj.wgs84[idx]);
        if (spnObj) spnObj.innerHTML = mapObj.info[idx][0];
        if (spnObjOne) spnObjOne.innerHTML = mapObj.info[idx][1];
        if (poiidObj) poiidObj.innerHTML = mapObj.info[idx][2] + "||" + mapObj.info[idx][3];
        overlay.addFalkMapObject(_gfmo);
        mapObj.map.centerMapToGeocode(mapObj.wgs84[idx], 13);
        idx++;
        if (idx == mapObj.wgs84.length) {
            idx = 0;
        }
        tt = setTimeout("playTicker()", 6000);
    }
}

function markerClicked() {
    var poiTitleObj = document.getElementById('poiTxtInner');
    var poiCityObj = document.getElementById('poiDescInner');
    var poiidObj = document.getElementById('divPoiid');
    var split_vals = poiidObj.innerHTML.split('||');
    var builturl = 'de/';
    builturl += poiCityObj.innerHTML + '/';
    builturl += split_vals[1] + '/';
    builturl += poiTitleObj.innerHTML + '/';
    builturl += split_vals[0] + '/';
    var url = fix_seourl(_gdoc_root + decodeURI(builturl));
    location.href = url;
}

function check_search(obj) {
    if (obj.value == 'Was?' || obj.value == 'Wo?') {
        obj.value = "";
    }
}

function check_searchbox(obj, val) {
    if (obj.value == '' || obj.value == '') {
        obj.value = val;
    }
}

function fadeDiv(div_id) {
    return false;
    var docObj = document;
    var grpParent = docObj.getElementById('fadingDivs');
    var cNodes = grpParent.childNodes;
    for (i = 0; i < cNodes.length; i++) {
        if (typeof cNodes[i].getAttribute != 'undefined') {
            if (cNodes[i].getAttribute('fdeDiv') == 1) {
                if (div_id == cNodes[i].id) {
                    $('#' + cNodes[i].id).fadeIn('slow');
                } else {
                    cNodes[i].style.display = 'none';
                }
            }
        }
    }
}

function fadeDivSingle(div_id) {
    if (document.getElementById(div_id).style.display == 'block' || document.getElementById(div_id).style.display == "") {
        $('#' + div_id).fadeOut('slow');
    } else {
        $('#' + div_id).fadeIn('slow');
    }
}

function searchKeyPress(e) {
    if (window.event) {
        e = window.event;
    }
    if (e.keyCode == 13) {
        var searchVal = document.getElementById('frm_search').value;
        if (searchVal == '' || searchVal == ' ') {
            searchVal = 'Was?';
        }
        document.getElementById('searchbutton').focus();
        document.getElementById('searchbutton').click();
    }
}

function searchKeyPress2(e) {
    if (window.event) {
        e = window.event;
    }
    if (e.keyCode == 13) {
        var searchVal = document.getElementById('frm_search1').value;
        document.getElementById('searchbutton2').focus();
        document.getElementById('searchbutton2').click();
    }
}

function setupDemos() {
    Shadowbox.setup("a.mustang-gallery", {
        gallery: "mustang",
        continuous: true,
        counterType: "skip"
    });
    Shadowbox.setup("a.swf-gallery", {
        gallery: "swf",
        continuous: true
    });
}
checkKeyPress = function (event, id) {
    if (event.keyCode == 13) {
        $(id).click();
    }
};

/**
* Shows the given element in the lightbox. If the element is a dom node it gets positioned in the
* middle of the screen and displayed. If the element is a html string it gets inserted in the lightboxes
* div and displayed.
**/
function showLightboxWindow(element){
	var div = document.getElementById('light');
	var parent = div.parentNode;
	
//	var fadedLayer = document.getElementById('fade');
//	var innerPageSize = getInnerPageSize();
//	fadedLayer.style.width = innerPageSize[0] + 'px';
//	fadedLayer.style.height = innerPageSize[1] + 'px';
//	fadedLayer.style.display='block';
	
	//show faded layer
	$("#fade").css("width",$(window).width())
		.css("height",$(window).height())
		.show(); 
	
	if(isElement(element)){
		parent.insertBefore(element, div);
	}else{
		var contentDiv = document.createElement("div");
		contentDiv.id = "light_content";
		contentDiv.innerHTML = element;
		div.appendChild(contentDiv);
		element = div;
	}

	var s1 = "chrome";
	var s2 = "safari";	
	var agt=navigator.userAgent.toLowerCase();
	if(((i = agt.indexOf(s1)) >= 0)||((i = agt.indexOf(s2)) >= 0)){
		divDiff = document.body.scrollTop;	
	}else{
		divDiff = document.documentElement.scrollTop;	
	}
	
	if(element.classList){
		element.classList.add("white_content");
	}else{
		element.className = element.className + " white_content";
	}
	element.style.display = "block";
	
//	var top = Math.round((document.documentElement.clientHeight/2)-(element.clientHeight/2)+divDiff);
//	var left = Math.round((document.documentElement.clientWidth/2)-(element.clientWidth/2));

	var top = $(window).height() / 2 - $(element).height() / 2;
	var left = $(window).width() / 2 - $(element).width() / 2;
	
	element.style.top = top + 'px';
	element.style.left = left + "px";
	
	//the following two lines are a hack for ie7 to display the map on first click
	element.style.display = "none";
	element.style.display = "block";
	
	//position close div
	var close = document.getElementById("lightbox_content_close");
	close.style.display = "block";
	close.style.top = top - close.clientHeight/2 + "px";
	close.style.left = left + element.clientWidth - close.clientWidth/2 + "px";
}

/**
* Hides the content of the lightbox which includes the displayed content, 
* the fade layer and the close div
**/
function hideLightbox(){
	var elements = YDom.getElementsByClassName("white_content");
	for(var i = 0; i < elements.length; i++){
		elements[i].style.display = "none";
	}
	
	var light =document.getElementById('light'); 
	while(light.hasChildNodes()){
		light.removeChild(light.firstChild);
	}
	
	light.style.display='none';
	document.getElementById('fade').style.display='none';
	document.getElementById("lightbox_content_close").style.display='none';
}

/**
* Function to select the text in the given textarea and if it is the ie copy it to clipboard.
**/
function selectAndCopyToClipboard(textarea){
	textarea.focus(); 
	textarea.select(); 
	if(window.clipboardData){
		window.clipboardData.setData("Text", textarea.value);
	}
}

/**
* Helper function to check if the given object is a dom node
**/
function isElement(obj){
  return (typeof obj==="object") && (obj.nodeType===1) && (typeof obj.tagName==="string")
}
	
