$(document)
		.ready(function() {
			$("#navigation > ul > li").mouseover(function() {
				$(this).children("ul").css('display', 'block');
			}).mouseout(function() {
				$(this).children("ul").css('display', 'none');
			});

			// ---------------------------------------------------------
				// Slideshowactions begin
				// erstes Bild einblenden
				changeImageClick(1);

				// click auf Listenelemente
				$('.slideshowListButton').click(function() {
					var rel = $(this).attr('id').split('_');
					changeImageClick(rel[1]);
				});

				// slidehshow list background verschieben
				$('.slideshowListButton').each(function(index) {
					var rel = $(this).attr('id').split('_');
					var multiplier = (rel[1]) - 1;
					var offset = -18 * multiplier;
					$(this).css('background-position', offset + 'px 0px');
				});
				
				$('.openSlideshowTeaser').click(function(){
					var id = $(this).attr('id').split('_');
					$('#teaser_'+ id[1]).css('display', 'block');
				});
				
				$('.slideshowTeaserClose').click(function(){
					var id = $(this).attr('id').split('_');
					$('#teaser_'+ id[1]).css('display', 'none');
				});
				
				function changeImageClick(id) {
					// altes Bild aus- und neues Bild einblenden
					$('.slideshow').each(function(index) {
						$(this).css('display', 'none');
						if ($(this).attr('id') == 'fwSl_'+ id) {
							$(this).css('display', 'block');
						}
					});
					$('.slideshowListButton').each(function(index) {
						$(this).css('color', '#85A3A5');
						if ($(this).attr('id') == 'fwSlBtn_'+ id) {
							$(this).css('color', '#FFFFFF');
						}
					});
				}
				
				// ---------------------------------------------------------
				// Slideshowactions ende

				// ---------------------------------------------------------
				// Produkt
				// begin
				// Klappboxen begin
				$('.close').click(function() {
					var rel = $(this).attr('id').split('_');
					toggle(rel[1], '-');
					$(this).css('display', 'none');
					$('#open_' + rel[1]).css('display', 'block');
				});
				$('.open').click(function() {
					var rel = $(this).attr('id').split('_');
					toggle(rel[1], '+');
					$(this).css('display', 'none');
					$('#close_' + rel[1]).css('display', 'block');
				});

				function toggle(id, action) {
					if (action == '+') {
						/* $('#box_'+ id).slideDown('fast'); */
						$('#box_' + id).css('display', 'block');
					} else if (action == '-') {
						/* $('#box_'+ id).slideUp('fast'); */
						$('#box_' + id).css('display', 'none');
					}
				}
				// Klappboxen ende
				// ---------------------
				// Images begin
				// erstes Bild einblenden
				//changeThumbClick(1);
//				var anz = $('.produktPictures').size() - 1;
//				for(var i = 0; i <= anz; i++){
//					$('.produktPictures')[0]
//				}
				$('.produktPictures').each(function(index){
					var element = $(this).children().children().first();
					element.css('display', 'block');
					element.click(function() {
						var tmp = element.attr('id').split("_");
						zoomPicture(tmp[tmp.length-1], 500);
					});
				});
				$('.prdoduktList').each(function(index){
					$(this).children('.produktListButton').first().css('color', '#FFFFFF');
				});

				// click auf Listenelemente
				$('.produktListButton').click(function() {
					var parent = $(this).parent().parent().attr('id');
					var rel = $(this).attr('id').split('_');
					changeThumbClick(rel[1], parent);
				});

				// Button background verschieben
				$('.produktListButton').each(function(index) {
					var tmp = $(this).attr('rel').split("_");
					var multiplier = (tmp[tmp.length-1]) - 1;
					var offset = -18 * multiplier;
					$(this).css('background-position', offset + 'px 0px');
				});

				function changeThumbClick(id, parent) {
					// altes Bild aus- und neues Bild einblenden
					$('#'+ parent +' .img img').each(function(index) {
						$(this).css('display', 'none');
					});
					$('#'+ parent +' .produktListButton').each(function(index) {
						$(this).css('color', '#85A3A5');
						var plb = $(this).attr('id').split('_');
						if (plb[1] == id) {
							$(this).css('color', '#FFFFFF');
						}
					});
					$('#zoom').attr('rel', id);
					$('#thumb_' + id).css('display', 'block');
					$('#thumb_' + id).click(function() {
						zoomPicture(id, 500);
					});
					$('#zoom').click(function() {
						zoomPicture(id, 500);
					});
				}

				function zoomPicture(id, picturewidth) {
					// Positionierung der Elemente
					var posLeft = (picturewidth + 44) / 2;
					var positionLeft = ($("body").innerWidth() / 2) - posLeft;
					$("#leinwand").css('left', positionLeft);
					positionLeft = positionLeft + picturewidth + 27;
					$('#closePicture').css('left', positionLeft);

					// vorhang, leinwand, closebutton und bild einblenden
					$('#curtain').css('display', 'block');
					$('#leinwand').css('display', 'block');
					$('#closePicture').fadeIn('slow');
					$('#pic_' + id).fadeIn('slow');

					// Höhe des Vorhangs
					var height = $("html").innerHeight();
					var leinwandHeight = $('#leinwand').innerHeight() + 147;
					if ($("body").innerHeight() > height)
						height = $("body").innerHeight();
					if (leinwandHeight > height)
						height = leinwandHeight;
					if ($(window).height() > height)
						height = $(window).height();
					$("#curtain").css('height', height);

					// und mit klick wieder schliessen
					$('.closePicture').click(function() {
						$('#pic_' + id).css('display', 'none');
						$('#closePicture').css('display', 'none');
						$('#leinwand').css('display', 'none');
						$('#curtain').css('display', 'none');
					});
				}
				// Images ende
				// ---------------------------------------------------------
				// Produkt
				// ende
				// ---------------------------------------------------------
				// Bezugsquellen begin
				$('.selectLand').change(function() {
					var id = $(this).val();
					$('.selectCity').each(function(index) {
						$(this).css('display', 'none');
					});
					showAddress(id);
					if (id != 0) {
						$('#label_cities').css('display', 'block');
						$('#cities_' + id).css('display', 'block');
					}
					//layer aus
					$('.adressenWrapper').each(function(index) {
						$(this).css('display', 'none');
					});
				});
				
				$('.selectCity').change(function(){
					var temp = $(this).val();
					showAddress(temp);
					//layer
					var rel =  $('#'+ $(this).attr('id') +' option:selected').attr('rel');
					$('.adressenWrapper').each(function(index) {
						$(this).css('display', 'none');
					});
					$('#adressen_'+ rel).css('display', 'block');
					//erste seite einblenden
					$('.adressen_seite_1').each(function(index) {
						$(this).css('display', 'block');
					});
				});
				
				$('.adressenPagebrowser > li').click(function(){
					//alle seiten ausblenden
					$('.adressenWrapper > span').each(function(index) {
						$(this).css('display', 'none');
					});
					//geklickte seite einblenden
					$('.adressen_seite_'+ $(this).attr('rel')).css('display', 'block');
				});
				
				if($('.adressenPagebrowser').children().size() <= 2 ){
					$(this).css('display', 'none');
				}

				// ---------------------------------------------------------
				// Bezugsquellen ende

				$('.newsletterText').click(function() {
					$(this).attr('value', '');
				});

				// ---------------------------------------------------------
				// Presse
				$('#thumb').click(function() {
					zoomPicture($(this).attr('rel'), 800);
				});

				// ---------------------------------------------------------
				// FAQ
				var old
				var rel
				$('.copylink').click(function(){
					old = rel;
					var temp = rel = $(this).attr('id').split('_');
					rel = temp[1]
					if(rel != undefined){
						$('#faq_'+ old).css('display', 'none');
						$('#faq_'+ rel).css('display', 'block');
					}
				});
				$('.faqLeft form input').click(function(){
					$(this).attr('value', '');
				});
				
				
				// -----------------------------------------------------------
				// Shop
				$('#copyData').click(function(){
					//alert('val='+ $('#m2b_name').val() +' \nhtml='+ $('#m2b_name').html() +' \ntext='+ $('#m2b_name').text());
					$('#rechnung_m2b_name').val($('#m2b_name').val());
					$('#rechnung_m2b_vorname').val($('#m2b_vorname').val());
					$('#rechnung_m2b_strasse').val($('#m2b_strasse').val());
					$('#rechnung_m2b_strassennummer').val($('#m2b_strassennummer').val());
					$('#rechnung_m2b_plz').val($('#m2b_plz').val());
					$('#label_ort2').val($('#label_ort').val());	
					$('#label_land2').val($('#label_land').val());
					$('#m2b_lieferfirma').val($('#m2b_rechnungfirma').val());
					$('#m2b_rzusatz').val($('#m2b_zusatz').val());
					$('#m2b_rzusatz_2').val($('#m2b_zusatz_2').val());
					$('#liefer_zusatz').val($('#rechnung_zusatz').val());
					$('#liefer_zusatz_2').val($('#rechnung_zusatz_2').val());
					$('#rechnung_m2b_ort').val($('#m2b_ort').val());
					$('#rechnung_m2b_land').val($('#m2b_land').val());
				});
				
			});

var map;
var geocoder;

function initializeGMap() {
	//if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		map.addControl(new GSmallZoomControl3D());
		
		geocoder = new GClientGeocoder();

		icon = new GIcon();
		icon.image = "/fileadmin/img/googlemarker.png";
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
		
		//map.setCenter(new GLatLng(51.031722, 10.301513), 5);
		showAddress('Deutschland');

		GDownloadUrl("./index.xml?eID=getMarker", function(data) {
			var xml = GXml.parse(data);
	
			var markers = xml.documentElement.getElementsByTagName("marker");
			for ( var i = 0; i < markers.length; i++) {
				var latlng = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
										 parseFloat(markers[i].getAttribute("lon")));
				var html = '<strong>' + markers[i].getAttribute("titel") + '</strong>';
				html += '<br />' + markers[i].getAttribute("strasse");
				html += '<br />' + markers[i].getAttribute("ort");
				html += '<br /><br />Tel.: ' + markers[i].getAttribute("tel");
				var marker = createMarker(latlng, html, icon);
				map.addOverlay(marker);
			}
		});
	//}
}

function createMarker(point,html,icon) {
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker,"click",function(){
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function showAddress(address) {
	/*fixes begin*/
	if(address == 'Bergheim'){
		address = 'Hauptstr. 41, Bergheim'
	}
	if(address == '54' || address == '0'){
		address = 'Deutschland';
		$('#pfeil').css('left','324px');
	}
	if(address == '20'){
		address = 'Belgien';
		$('#pfeil').css('left','306px');
	}
	if(address == '74'){
		address = 'England';
		$('#pfeil').css('left','294px');
		$('#pfeil').css('top','30px');
	}
	/*fixes ende*/
	var zoom = 11;
	if(address == 'Deutschland' || address == 'Belgien'|| address == 'England'){
		zoom = 5;
		$('.selectCity').val('0');
	}
	geocoder.getLatLng(address, function(point) {
		if (!point) {
	    	alert(address + " not found");
	    } else {
	        map.setCenter(point, zoom);
	    }
	});
}
