$().ready(function(){
	// switchBox
	$(".switchBoxItem .headline").bind("click", function(){
		$(".switchBoxItemWrap:not(#" + this.id + "+ .switchBoxItemWrap)").slideUp("slow");
		$("#" + this.id + " + .switchBoxItemWrap").slideDown("slow");
	});
	
	
	$('div.fullPage .stageItemLink').click(function(){
			stageBgFix($(this));
			
	});
});
var last = '';

function stageBgFix(current){
	  var getId = $(current).parents('li').find('div').attr('id');
	  var id = getId.charAt(getId.length - 1);
	  
	  if(id != last){
		  if(last != 'firstElement' && last != ''){
			lastInt = parseInt(last);
			restoreBackground(lastInt -1);
		  }else{
			restoreBackground('firstElement');
		  }
		  
		  if(getId == 'firstElement'){
			last = getId;
		  }else{
			  if(id == 1){
				$("#firstElement").removeClass('bg');
				$("#firstElement").addClass('nobg');
				last = id;
			  }else if(id > 1){
				$("#stageitem"+(id -1)).parents('li').removeClass('bg');
				$("#stageitem"+(id -1)).parents('li').addClass('nobg');
				last = id;
			  }
		  }
	  }else{
		if(id == 'firstElement'){
			$("#firstElement").addClass('active');
			last = id;
		  }else if(id >= 1){
			$("#stageitem"+id).parents('li').addClass('active');
			if((id -1) == 0){
				$("#firstElement").removeClass('bg');
				$("#firstElement").addClass('nobg');
			}else{
				$("#stageitem"+(id -1)).parents('li').removeClass('bg');
				$("#stageitem"+(id -1)).parents('li').addClass('nobg');
			}
			last = id;
		  }
	  }
}

function restoreBackground(id){
	if(id == 'firstElement' || id == 0){
		//$('#firstElement').css('background-image', "url\('../img_shared/stageBoxRelaunchMenuItemShadow.png'\);");
	}else{
		$("#stageitem"+id).parents('li').addClass('bg');
		$("#stageitem"+id).parents('li').removeClass('nobg');
	}
}

/* #### jQuery Ligthbox ####*/
$().ready(function(){
	/*if($("a[rel*='lightbox']")){
		$("a[rel*='lightbox']").lightBox({
	  		txtImage:'Bild',
    		txtOf:'von'
  		});
  	}*/
});



/* #### switchBox #### */
function switchBox(inId){
	$("#switchBox" + inId + " .switchBoxItemWrap").hide();
	$("#switchBox" + inId + " .switchBoxItem .switchBoxItemWrap:first").show();
	$("#switchBox" + inId + " .switchBoxItem .headline").css("cursor", "pointer");
}



/* #### slide content #### */
function slideContent(inId, buttonText, closeText){
	$("#sildeContentHide" + inId).hide();
	$("#slideContentLink" + inId).html("<div class='links'><ul><li class='plus'><a href='#' onclick='slideContentShow(\"" + inId + "\",  \"" + buttonText + "\", \"" + closeText + "\");return false'><img src='/fileadmin/img_shared/plusIcon.gif' width='8' height='8' border='0' alt='' />" + buttonText + "</a></li></ul></div>");
}

function slideContentShow(inId, buttonText, closeText){
	$("#sildeContentHide" + inId).show();
	$("#slideContentLink" + inId).html("<div class='links'><ul><li class='plus'><a href='#' onclick='slideContent(\"" + inId + "\",  \"" + buttonText + "\", \"" + closeText + "\");return false'><img src='/fileadmin/img_shared/minusIcon.gif' width='8' height='8' border='0' alt='' />" + closeText + "</a></li></ul></div>");
}


/* #### tabs #### */
function renderTabMenu(id){
	$("#tabMenu" + id).show();
	$("#tabBox" + id + " .tabBoxItem").hide();
	menu = "<ul>";
	$("#tabBox" + id + " .tabBoxItem .tbiHeadline").each(function(index){
		linkid = $(this).parent(".tabBoxItem").attr("id");
		menu += "<li><a href=\"\" onclick=\"showTab('" + id + "', '" + linkid + "');return false;\" id=\"link" + linkid + "\">" + $(this).text() + "</a></li>";
	});
	menu += "</ul><div class=\"clearer\"><!-- clearer --></div>"
	first = $("#tabBox" + id + " .tabBoxItem .tbiHeadline").parent(".tabBoxItem").attr("id");
	$("#tabMenu" + id).html(menu);
	
	showTab(id, first);
}

function showTab(menuId, id){
	$("#tabMenu" + menuId + " a").attr("class", "");
	$("#link" + id).attr("class", "active");
	$("#tabBox" + menuId + " .tabBoxItem").hide();
	$("#" + id).show();
}

/* #### stageBox #### */
function stageClass(){
	var orgMenuHeight = 244;
	var newMenuHeight;
	
	var arrow = "fileadmin/img_shared/arrow.gif";
	//alert(window.location.hostname);
	if(window.location.hostname == "www.die-deutschen-buergerstiftungen.de"){
		arrow = "fileadmin/img_shared/arrowIBS.gif";		
	}
	
	if($('div.fullPage .stageBox').length > 0){
		var stageHtml = "<div class=\"imageLeftBox\">";
		stageHtml += "   <div class=\"boxWrap\">";
		stageHtml += "   	<div class=\"imgWrap\">";
		stageHtml += "       	<div class=\"image\" id=\"stageImage\" style=\"%display%\">";
		stageHtml += "           	<img src=\"%image%\" border=\"0\" alt=\"%alttext%\" />";
		stageHtml += "       	</div>";
		stageHtml += "       </div>";
		stageHtml += "       <div class=\"textWrap textRight\">";
		stageHtml += "           %text%";
		stageHtml += "       	 <div class=\"links\">";
		stageHtml += "           	<ul>";
		stageHtml += "               	<li>%link%</li>";
		stageHtml += "           	</ul>";
		stageHtml += "       	 </div>";
		stageHtml += "       </div>";
		stageHtml += "   </div>";
		stageHtml += "</div>";
		stageHtml += "<div class=\"clearer\"><!-- clearer --></div>";
		
	}else{
		var stageHtml = "<div class=\"imageTopBox\">";
		stageHtml += "   <div class=\"boxWrap\">";
		stageHtml += "       <div class=\"image\" id=\"stageImage\" style=\"%display%\">";
		stageHtml += "           <img src=\"%image%\" border=\"0\" alt=\"%alttext%\" />";
		stageHtml += "       </div>";
		stageHtml += "       <div class=\"clearer\"><!-- clearer --></div>";
		stageHtml += "       <div class=\"text\" style=\"padding-top:10px;\">";
		stageHtml += "           %text%";
		stageHtml += "       </div>";
		stageHtml += "       <div class=\"links\">";
		stageHtml += "           <ul>";
		stageHtml += "               <li>%link%</li>";
		stageHtml += "           </ul>";
		stageHtml += "       </div>";
		stageHtml += "   </div>";
		stageHtml += "</div>";
	}
	
	
	
	this.data = function(id){
		this.id = id;
		this.items = new Array();
	};
	
	this.dataItem = function(id, label, value, type){
		this.id = id;
		this.label = label;
		this.value = value;
		this.type = type;
		
		if(this.id == "image"){
			preLoadImage = new Image(); 
			preLoadImage.src = this.value; 
		}
	};
	
	this.showStage = function(itemId){
		for (var i = 0; this.stageItems.length > i; i++) {
			var obj = this.stageItems[i];
			var lastObj = this.stageItems[(this.stageItems.length-1)];
			if($('div.fullPage .stageBox').length > 0){
				var lastAttrInList = $('.fullPage .stageBox .stageBoxMenu ul li:last div').eq(0).attr('id');
				var lastItemIdInArr = findObjItem(lastObj, 'id').value;
			}else{
				var lastAttrInList = false;
				var lastItemIdInArr = false;	
			}
			if (findObjItem(obj, 'id').value == itemId) {
				setStageContent(obj, itemId, lastAttrInList, lastItemIdInArr);
			}
		}
		$('div.fullPage .stageBox #stageBoxMenu a img').each(function(){
			$(this).attr('src', arrow);
		});
		$('div.fullPage .stageBox #' + itemId + ' a img').attr('src', 'fileadmin/img_shared/arrowStageBlue.gif');
	};
	
	this.showOrgStage = function(orgHtml){
		var liElements = document.getElementById('stageBoxMenu').getElementsByTagName('li');
		for (var i = 0; i < liElements.length; i++) {
			liElements[i].className = '';
		}
		document.getElementById('firstElement').className = 'active';
		document.getElementById('stageBoxItem').innerHTML = orgHtml;
		
		$('div.fullPage .stageBox #stageBoxMenu a img').each(function(){
			$(this).attr('src', arrow);
		});
		
		$('div.fullPage .stageBox #firstElement a img').attr('src', 'fileadmin/img_shared/arrowStageBlue.gif');
		
		if($('div.fullPage .stageBox').length > 0){
			var itemHeight = $('#stageBoxItem').height();
			var menuHeight = $('#stageBoxMenu').height();			
			
			if((itemHeight > menuHeight) && (itemHeight > 242)){
				itemHeight += 8;
				$('#stageBoxMenu').css('height', itemHeight);
				$('.fullPage .stageBox .stageBoxMenu ul li').eq(-1).addClass('heighterMenu');
			}else{
				$('#stageBoxMenu').css('height', orgMenuHeight);
				$('.fullPage .stageBox .stageBoxMenu ul li').eq(-1).removeClass('heighterMenu');
			}
		}
	};
	
	this.stageItems = new Array();
	
	function findObjItem(obj, id){
		for (var i = 0; i < obj.items.length; i++) {
			s = obj.items[i].id;
			if (s == id) {
				var o = obj.items[i];
			}
			
		}
		if (o) {
			return o;
		}
		else {
			return false;
		}
	}
	
	function replaceInHtml(str, pat, repl){
		var regEx = new RegExp("%" + pat + "%", "g");
		return str.replace(regEx, repl);
	}
	
	function setStageContent(obj, itemId, lastAttrInList, lastItemIdInArr){
		var linkSrc = document.getElementById(findObjItem(obj, 'id').value).getElementsByTagName('a')[0].href;
		var linkTarget = document.getElementById(findObjItem(obj, 'id').value).getElementsByTagName('a')[0].target;
		var linkTitle = document.getElementById(findObjItem(obj, 'id').value).getElementsByTagName('a')[0].innerHTML;
		var link = '<a href="' + linkSrc + '" target="' + linkTarget + '">' + linkTitle + '</a>';
		
		var html = replaceInHtml(stageHtml, 'link', link);
		html = replaceInHtml(html, 'alttext', findObjItem(obj, 'alttext').value);
		html = replaceInHtml(html, 'text', findObjItem(obj, 'text').value);
		
		var image = findObjItem(obj, 'image');
		if (image) {
            html = replaceInHtml(html, 'image', findObjItem(obj, 'image').value);
            html = replaceInHtml(html, 'display', 'display:block;');
            image = '';
		}
		else {          
            html = replaceInHtml(html, 'display', 'display:none;');
            image = '';
		}
		
		document.getElementById('firstElement').className = '';
		var liElements = document.getElementById('stageBoxMenu').getElementsByTagName('li');
		for (var i = 0; i < liElements.length; i++) {
			liElements[i].className = '';
		}
		document.getElementById(findObjItem(obj, 'id').value).parentNode.className = 'active';
		
		document.getElementById('stageBoxItem').innerHTML = html;
		
		$('div.fullPage .stageBox #firstElement a img').attr('src', arrow);
		
		if($('div.fullPage .stageBox').length > 0){
			var itemHeight = $('#stageBoxItem').height();
			var menuHeight = $('#stageBoxMenu').height();
			
			if((itemHeight > menuHeight) && (itemHeight > 242)){
				itemHeight += 8;
				$('#stageBoxMenu').css('height', itemHeight);
				$('.fullPage .stageBox .stageBoxMenu ul li').eq(-1).addClass('heighterMenu');
				$('#' + lastItemIdInArr).parent().removeClass('lastList');
			}else if((itemId == lastAttrInList) && (itemId == lastItemIdInArr) && (itemHeight < menuHeight)){
				$('#stageBoxMenu').css('height', itemHeight + 'px');
				$('#' + lastItemIdInArr).parent().addClass('lastList');
			}else{
				$('#stageBoxMenu').css('height', orgMenuHeight);
				$('.fullPage .stageBox .stageBoxMenu ul li').eq(-1).removeClass('heighterMenu');
				$('#' + lastItemIdInArr).parent().removeClass('lastList');
			}
		}
	}
	
	$().ready(function(){
		$('div.fullPage .stageBox #firstElement a').prepend('<img height="9" border="0" width="9" src="fileadmin/img_shared/arrowStageBlue.gif" alt="" title="">');
		$('div.fullPage .stageBox #firstElement a').css('text-indent', '-14px');
		
		var itemHeight = $('#stageBoxItem').height();
		var menuHeight = $('#stageBoxMenu').height();
		newMenuHeight = itemHeight += 8;		
		
		if($('div.fullPage .stageBox').length > 0){	
			$('#stageBoxMenu').css('height', newMenuHeight);
		}
	});
}

/* #### addthis ####*/

/* #### googleMap ####*/
function googleMapsClass(){

	var infoHtml = '<p class="infoWindowHeadline">%headline%<\/p><p class="infoWindowText">%address%<\/p><p class="infoWindowText">%extraInfo%<\/p><div class="links"><ul><li><a href="%moreLink%" class="mail"><img src="../img_shared/mailIcon.gif" width="13" height="10" border="0" alt="" \/>Mail<\/a><\/li><\/ul><\/div>';
	var map;
	var marker;
	
	this.locations = new Array();
	
	this.Data = function(id){
		this.id = id;
		this.items = new Array();
	}
	this.DataItem = function(id, label, value, type){
		this.id = id;
		this.label = label;
		this.value = value;
		this.type = type;
	}
	function findObjItem(obj, id){
		for (var i = 0; i < obj.items.length; i++) {
			s = obj.items[i].id;
			if (s == id) {
				return obj.items[i];
			}
		}
	}
	
	function replaceInHtml(str, pat, repl){
		var r = new RegExp("%" + pat + "%", "g");
		return str.replace(r, repl);
	}
	
	function addIconWithMarker(lat, lng, html, icon){
		var point = new GLatLng(lat, lng);
		var marker = new GMarker(point, icon);
		GEvent.addListener(marker, "click", function(){
			marker.openExtInfoWindow(map, "infoWin", html, {
				breakOffset: 3,
				paddingY: 5
			});
		});
		return marker;
	}
	
	function createBsIcon(){
		var icon = new GIcon();
		var img = "../img_shared/pin.png";
		var shadow = "../img_shared/pin_schatten.png";
		if (img.length > 0 && shadow.length > 0) {
			icon.image = img;
			icon.shadow = shadow;
			var iw = 0;
			iw = "16";
			var ih = 0;
			ih = "21";
			icon.iconSize = new GSize(iw, ih);
			var sw = 0;
			sw = "26";
			var sh = 0;
			sh = "32";
			icon.shadowSize = new GSize(sw, sh);
			icon.iconAnchor = new GPoint(0, 14);
			icon.infoWindowAnchor = new GPoint(9, 9);
		}
		else {
			icon.image = "http://labs.google.com/ridefinder/images/mm_20_gray.png";
			icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			icon.iconSize = new GSize(12, 20);
			icon.shadowSize = new GSize(22, 20);
			icon.iconAnchor = new GPoint(6, 20);
			icon.infoWindowAnchor = new GPoint(5, 1);
		}
		return icon;
	}
	
	function setMarker(map, locations){
		var icon = createBsIcon();
		for (var i = 0; locations.length > i; i++) {
			var loc = locations[i];
			var coordinates = findObjItem(loc, 'coordinates').value;
			var p = coordinates.split(",");
			if (p.length == 2) {
				var lat = p[0];
				var lng = p[1];
				var html = replaceInHtml(infoHtml, "headline", findObjItem(loc, 'headline').value);
				html = replaceInHtml(html, "extraInfo", findObjItem(loc, 'extraInfo').value);
				html = replaceInHtml(html, "address", findObjItem(loc, 'address').value);
				html = replaceInHtml(html, "moreLink", findObjItem(loc, 'moreLink').value);
				var m = addIconWithMarker(lat, lng, html, icon);
				map.addOverlay(m);
			}
		}
		return marker;
	}
	
	function getBoundsForAllLocations(locations){
		var bounds = new GLatLngBounds();
		for (var i = 0; locations.length > i; i++) {
			loc = locations[i];
			var coordinates = findObjItem(loc, 'coordinates').value;
			var p = coordinates.split(",");
			if (p.length == 2) {//is a point
				var lat = p[0];
				var lng = p[1];
				p = new GLatLng(lat, lng);
				bounds.extend(p);
			}
		}
		return bounds;
	}
	
	function setZoomAndCenterForPointList(map, locations){
		bounds = getBoundsForAllLocations(locations);
		var oz = "";
		if (oz != "") {
			var z = "";
		}
		else {
			var z = map.getBoundsZoomLevel(bounds);
		}
		var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
		var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
		map.setCenter(new GLatLng(clat, clng), parseInt(z, 9));
	}
	
	this.Gload = function(){
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));
			var c = new GSmallMapControl();
			map.addControl(c);
			//map.addControl(new GMapTypeControl());
			setZoomAndCenterForPointList(map, this.locations);
			setMarker(map, this.locations);
		}
	}
	
	document.body.unload = GUnload();
}

