var visibleHeight = 0;
var visibleWidth = 0;

$(document).ready(function(){
	Cufon.replace('h1.cufon');	

	initVisibleBrowserDimensions();
	initBackgroundHeight();

  $(window).resize(function(){
	  initVisibleBrowserDimensions();
	  initBackgroundHeight();
  });

  // Delete default value of search field on focus
  $("#search-form input").focus(function() {
    $("#sword").val('');
  });

  // Submit search form on click
  $("#search .left img").click(function() {
    $("#search-form").submit();
  });

  // Accordions
  // Hide all Accforions except first
  //$(".accordion").find('.content').first().nextAll('.content').hide();
  
  // Add class active for first headline
  //$(".accordion").find('h2').first().addClass("active");
  
  // Animate
  //$(".accordion h2").click(function(){
	  //activeAccordion  = $(this);
  //  if(!$(this).hasClass("active")) {		  
		  /*if($.browser.msie && $.browser.version.substr(0,1) < 7 ){
			$(this).css("z-index",2);
			$(this).next().css("z-index",2);
			$(this).parent().find('.content').hide(1000);
			$(this).next().show();
		  }
		  else {*/
  //		$(this).parent().find('h2.active + .content').slideUp();
  //		$(this).next().slideDown();
			//$(this).parent().find('h2.active + .content').slideUp('normal', function(){
		    // 	activeAccordion.next().slideDown();
		    // });        
		 // }
		  
  //	  $(this).parent().find('h2').removeClass("active");
  //	  $(this).addClass("active");
  //  }
  //});
  $(".accordion").accordion({autoHeight: false});
  
  // Show language selector
  $("#meta-navi li.last").mouseenter(function(){
	  $(this).find("a").fadeIn(500);
  });

  // Hide language selector
  $("#meta-navi li.last").mouseleave(function(){
    $(this).find("a[class!=current]").fadeOut(500);
  });

  // Show and move product popup
  $(".accordion .products img").mouseenter(function(){
    $.id = $(this).attr('id').split('-')[1];
    $("#productpopup-" + $.id).show();
  }).mousemove(function(e){
	  $("#productpopup-" + $.id).css({
		  left: e.pageX + 5 + 'px',
		  top: e.pageY - $("#header").height() - $("#productpopup-" + $.id).height() - 4 + 'px'
	  });
  });

  // Hide product popup on mouseleave
  $(".accordion .products img").mouseleave(function(){
	  $.id = $(this).attr('id').split('-')[1];
	  $("#productpopup-" + $.id).hide().css({ left: '0px', top: '0px'});
  });


  // Show poups on world map
	$("#world-map area").mouseenter(function(){
		$.href = $(this).attr("href");
		
		// Hide popups and white dot per default
		$("#dot-white").hide();
		$("div.bubble").hide();

		// Get coords of area
		$.coords = $(this).attr("coords").split(",");

		// Get popup div as object
		$.popup = $("div#bubble-" + ($(this).attr("id")));

		// Positioning the popup
		$.popup.css({
			"left" : $.coords[0] + 'px',
			"top"  : parseInt($.coords[1]) -
							 parseInt($.popup.height()) -
							 parseInt($("#dot-white").height()) -
							 parseInt($.popup.css("paddingTop")) -
							 parseInt($.popup.css("paddingBottom")) + 1 + 'px'
		});

		// Positioning the white dot
		$("#dot-white").css({
			"left" : parseInt($.coords[0]) - parseInt(($.coords[2])) - 1 + 'px',
			"top"  : $.coords[1] - $.coords[2] + 'px',
			"cursor": 'pointer'
		});

		// Show dot and popup
		$("#dot-white").show();
		$.popup.show();
	});

	// Hide popup and white dot
	$("#dot-white").mouseleave(function(){
		$.href = '';
		$("#dot-white").hide();
		$("div.bubble").hide();
	});
	
	// Fetch click on areas for worldmap
	$("#dot-white").click(function(){
		if($.href != '')
	      document.location.href = $.href;
	});


	// Submit contact form
	$("#submit_contactform").click(function(){
		document.contactform.submit();
	});

	// Submit search form
	$("#submit_searchform").click(function(){
		document.tx_indexedsearch.submit();
	});

	// Fix typoscripts ACTIFSUB, when sub pages are "hide in menu"
	$("#sub-navi ul li.active").each(function(){
		if($(this).children().length == 1)
		  $(this).attr("class", "active-nosub");
	});
	
}); // end $(document).ready


function initBackgroundHeight() {
	var height = ($("#container").height() < visibleHeight) ? visibleHeight : $("#container").height();
	$("#allcontent").css('height', height + 'px');

}

function showStandort(uid) {
  mapstraction.removeAllMarkers();
  thePoint = new LatLonPoint(standorte[uid]['lng'],standorte[uid]['lat']);
	mapstraction.setCenterAndZoom(thePoint, zoomLevel);
	theMarker = new Marker(thePoint);
	theMarker.setIcon('icon.gif');
	mapstraction.addMarker( new Marker( new LatLonPoint(standorte[uid]['lng'],standorte[uid]['lat'])));
	theMarker.setLabel(standorte[uid]['mouseover']);
	defaultText = '<style type="text/css">#mapstraction .gmnoprint,#mapstraction .gmnoprint a {font-size: 11px; color: #000000;}</style>' + standorte[uid]['bubble_text'];
	theMarker.setInfoBubble(defaultText);
	mapstraction.addMarker(theMarker);
	theMarker.openBubble();
}

function getWinSize(win) {
  if(!win) win = window;
  var s = new Object();

  if(typeof win.innerWidth != 'undefined') {
    s.width = win.innerWidth;
    s.height = win.innerHeight;
  }
  else {
    var obj = getBody(win);
    s.width = parseInt(obj.clientWidth);
    s.height = parseInt(obj.clientHeight);
  }
  return s;
}


// Der IE hat 2 verschiedene Objekte für den strict und quirks Mode.
function getBody(w) {
  return (w.document.compatMode && w.document.compatMode == "CSS1Compat") ? w.document.documentElement : w.document.body || null;
}


function initVisibleBrowserDimensions() {
	// Höhe des sichtbaren Browserbereichs
  visibleHeight = Number(getWinSize(window).height);

  // Breite des sichtbaren Browserbereichs
  visibleWidth  = Number(getWinSize(window).width);
}
