/** 
 *    Author: Arne Eckhoff <eckhoff@kdo.de>
 *      Date: 14.11.2007 16:50:00
 *   Project: KIM - Gis Bürgerclient
 * Copyright: 2007 Kommunale Datenverarbeitung Oldenburg (KDO)
 *      Link: http://www.kdo.de/
*/

// global variables
// following some brain variables :-)
// were the search results shown, when switching to another tab
var wasShownSearchResults = false;
// were the search results of Categories shown, when switching to another tab
var wasShownRubrikSearch = false;
// was the list with category entries shown, when switching to another tab
var wasShownRubrikList = false;
// were the search results of branches shown, when switching to another tab
var wasShownBrancheSearch = false;
// was the list with companies shown, when switching to another tab
var wasShownBrancheList = false;
// were the search results of associations shown, when switching to another tab
var wasShownVereinSearch = false;
// was the list with associations shown, when switching to another tab
var wasShownVereinList = false;
// were the search results of accomodations shown, when switching to another tab
var wasShownUnterkunftSearch = false;
// was the list with accomodation entries shown, when switching to another tab
var wasShownUnterkunftList = false
// currently selected Tab
var selectedTab = 'categorys';


// global objects
// create a standard icon for google markers
var baseIcon = new GIcon();
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.image = "img/map_icons/blue-dot.png";
baseIcon.iconSize = new GSize(24, 24);
baseIcon.shadowSize = new GSize(24, 24);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(15, 30);


// functions

// Show and hide HTML elements
$.fn.extend({
	showSlow: function() { return this.show("normal", function() { $(this).css({height: "", visibility: "visible"}); }); },
	hideSlow: function() { return this.hide("normal", function() { $(this).css({height: "", visibility: "hidden"}); }); }
});


// Create a tabbed Marker
function createTabbedMarker(point,html1,html2,label1,label2,icon) {
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowTabsHtml([new GInfoWindowTab(label1,html1), new GInfoWindowTab(label2,html2)]);
	});
	return marker;
}


// Hide elements from various Tabs to clean up
function hideOtherTools() {
	try {
		document.getElementById('query_rubrik_results').style.display = 'none';
		document.getElementById('rubrikAdressen').style.display = 'none';
		$("#myRubriken").hideSlow();
		document.getElementById("rubriken_auswaehlen_up").style.visibility = "hidden";
	} catch(ex) { }
	
	try {
		document.getElementById('query_accomodation_results').style.display = 'none';
		document.getElementById('unterkunftAdressen').style.display = 'none';
		$("#myUnterkunft").hideSlow();
		document.getElementById("uk_auswaehlen_up").style.visibility = "hidden";
	} catch(ex) { }
	
	try {
		document.getElementById('query_branchen_results').style.display = 'none';
		document.getElementById('branchenAdressen').style.display = 'none';
		$("#branchenListe").hideSlow();
		document.getElementById('branchen_auswaehlen_up').style.visibility="hidden";
	} catch(ex) { }
	
	try {
		document.getElementById('query_vereine_results').style.display = 'none';
		document.getElementById('vereineAdressen').style.display = 'none';
		$("#vereineListe").hideSlow();
		document.getElementById('sparten_auswaehlen_up').style.visibility="hidden";
	} catch(ex) { }
	
	try {
		document.getElementById('listeUnterkunftKIM').style.display = 'none';
	} catch(ex) { }
	
	try {
		document.getElementById('listeVeranstaltungen').style.display = 'none';
		$("#kategorieListe").hideSlow();
	} catch(ex) { }
	
	if(document.getElementById('routing').style.display!='none') {
		showHideRouting();
	}
	if(document.getElementById('area2').style.display!='none') {
		showHidemeasureArea();
	}
	if(document.getElementById('meetingDiv2').style.display!='none') {
		selectMeetingPoint();
	}
}


// Hide al the toolbars of this application
function hideToolbars(name) {
	// Hide Menu Toolbars
	//document.getElementById('toolbar_mapfunctions').style.display = 'none';
	try { document.getElementById('query_search_results').style.display = 'none'; } catch(ex) { }
	try { document.getElementById('toolbar_categorys').style.display = 'none'; } catch(ex) { }
	try { document.getElementById('toolbar_branches').style.display = 'none'; } catch(ex) { }
	try { document.getElementById('toolbar_vereine').style.display = 'none'; } catch(ex) { }
	try { document.getElementById('toolbar_accomodationKIM').style.display = 'none'; } catch(ex) { }
	try { document.getElementById('toolbar_accomodation').style.display = 'none'; } catch(ex) { }
	try { document.getElementById('toolbar_events').style.display = 'none'; } catch(ex) { }
	
	// Do not hide windows, if only mapfunctions will by shown
	// becaus you sould be able to use it with your results and markers ;-)
	// otherwise this functions would be needless
	if(name!="mapfunctions") {
		hideOtherTools();
	}
	
}


// Select a Tab and application category
function selectTab(name) {
	
	// first it's easier to hide everything
	hideToolbars(name);
	
	// display the right images for the tabs
	MM_swapImage(selectedTab,'','img/nav_main/' + selectedTab + '.gif',1);
	// new tab select
	MM_swapImage(name,'','img/nav_main/' + name + '_sel.gif',1);
	
	// get rid of this annoying selection...
	document.getElementById(name).blur();
	// now display the right toolbar for this application category
	document.getElementById('toolbar_' + name).style.display = 'block';
	
	// if the new selected tab is the category tab...
	if(name=='categorys') {
		map.clearOverlays();
		
		if(MMgrLayer.length>0) {
			// lade layer
			for(key in MMgrLayer) {
				map.addOverlay(MMgrLayer[key]);
			}
		}
		
		// ...check if the category list was shown the last time?
		if(wasShownRubrikList==true) {
			// yes, so show the list
			$("#rubrikAdressen").showSlow();
			// check if there are markers in the Manager
			if(MMgrRubriken.length>0) {
				// display markers...
				for (var i in MMgrRubriken) {
					var MMgrRubrikenUnter = MMgrRubriken[i];
					for (var j in MMgrRubrikenUnter) {
						map.addOverlay(MMgrRubrikenUnter[j]);
					}
				}
			}
		} 
		// ...check instead if the category search resuts were shown the last time?
		else if(wasShownRubrikSearch==true) {
			// yes, so show the list
			$("#query_rubrik_results").showSlow();
			// check if there are markers in the Manager
			if(markerRubrikSuche.length>0) {
				// display markers...
				for (var i=0;i<markerRubrikSuche.length;i++) {
					map.addOverlay(markerRubrikSuche[i]);
				}
			}
		}
	}
	
	// if the new selected tab is the accomodation tab ...
	else if(name=='accomodation') {
		map.clearOverlays();
		
		if(MMgrUKLayer.length>0) {
			// lade layer
			for(key in MMgrUKLayer) {
				map.addOverlay(MMgrUKLayer[key]);
			}
		}
		
		// ...check if the category list was shown the last time?
		if(wasShownUnterkunftList==true) {
			// yes, so show the list
			$("#unterkunftAdressen").showSlow();
			// check if there are markers in the Manager
			if(MMgrUnterkunft.length>0) {
				// display markers...
				for (var i in MMgrUnterkunft) {
					var MMgrUnterkunftUnter = MMgrUnterkunft[i];
					for (var j in MMgrUnterkunftUnter) {
						map.addOverlay(MMgrUnterkunftUnter[j]);
					}
				}
			}
		} 
		// ...check instead if the category search resuts were shown the last time?
		else if(wasShownUnterkunftSearch==true) {
			// yes, so show the list
			$("#query_accomodation_results").showSlow();
			// check if there are markers in the Manager
			if(markerUnterkunftSuche.length>0) {
				// display markers...
				for (var i=0;i<markerUnterkunftSuche.length;i++) {
					map.addOverlay(markerUnterkunftSuche[i]);
				}
			}
		}
	}
	
	// if the new selected tab is the category tab...
	else if(name=='branches') {
		map.clearOverlays();
		if(wasShownBrancheList==true) {
			$("#branchenAdressen").showSlow();
			if(selectedTab!="mapfunctions") {
				if(MMgrBranchen.length>0) {
					for (var i in MMgrBranchen) {
						var MMgrBranchenUnter = MMgrBranchen[i];
						for (var j in MMgrBranchenUnter) {
							map.addOverlay(MMgrBranchenUnter[j]);
						}
					}
				}
			}
		} 
		else if(wasShownBrancheSearch==true) {
			if(selectedTab!="mapfunctions") {
				if(MMgrBranchenSuche.length>0) {
					for (var i=0;i<MMgrBranchenSuche.length;i++) {
						map.addOverlay(MMgrBranchenSuche[i]);
					}
				}
			}
			$("#query_branchen_results").showSlow();
		}
	}
	
	else if(name=='vereine') {
		map.clearOverlays();
		if(wasShownVereinList==true) {
			$("#vereineAdressen").showSlow();
			if(selectedTab!="mapfunctions") {
				if(MMgrVereine.length>0) {
					for (var i in MMgrVereine) {
						var MMgrVereineUnter = MMgrVereine[i];
						for (var j in MMgrVereineUnter) {
							map.addOverlay(MMgrVereineUnter[j]);
						}
					}
				}
			}
		} 
		else if(wasShownVereinSearch==true) {
			if(selectedTab!="mapfunctions") {
				if(markerVereineSuche.length>0) {
					for (var i=0;i<markerVereineSuche.length;i++) {
						map.addOverlay(markerVereineSuche[i]);
					}
				}
			}
			$("#query_vereine_results").showSlow();
		}
	}
	
	// set the new selected tab to the global variable
	selectedTab = name;
}

function sleep(ms){
	var zeit=(new Date()).getTime();
	var stoppZeit=zeit+ms;
	while((new Date()).getTime()<stoppZeit){};
}
