var startseite_timeout;

jQuery(document).ready(function($) {
  
  $("section > menu, section > div#menu").mouseenter(function () {
	  var parent = $(this);
	  var menu = parent.find("menu:first, div#menu:first");
      menu.stop(true, true).slideDown(400, 'easeInOutCirc', function() {
	  
	  var links 	= menu.offset().left;
	  var rechts 	= menu.offset().left + menu.outerWidth();
	  var oben 		= menu.offset().top;
	  var unten 	= menu.offset().top + menu.outerHeight() + 28;
	  
			$(document).bind('mousemove', function(event) {
  
			  if(event.pageX < links || event.pageX > rechts || event.pageY < oben || event.pageY > unten)
			  {
				menu.stop(true, true).slideUp(400, 'easeInOutCirc');
				$(document).unbind('mousemove');
			  }
			});
		});
	});
  
  if ( $("#termine").length > 0 ) {
    $('#termine').tablesorter(); 
  }
  
  if ( $(".startseite").length > 0 ) {
  var image = $(".startseite#showroom ul li.current").css("background-image");
  var patt=/\"|\'|\)|\(|url/g;
  var vorbei = false;
  
    $.getJSON("/ajax/json/startseite_bilder.html?img="+image.replace(patt,''),
    function(data) {
    $.each(data.items, function(i,item){
      if(!item.current) {
        if(vorbei)
          $(".startseite#showroom ul").append('<li style="background-image: url(\''+data.items[i].img+'\')"></li>')
        else
          $(".startseite#showroom ul").prepend('<li style="background-image: url(\''+data.items[i].img+'\')"></li>')
      }
      else
        vorbei = true;
    });
    
    startseite_timeout = window.setTimeout(changeBackground, 15000);
    
    });
  }
  
  $('a').tooltip({
  track: true,
  delay: 0,
  showURL: false,
  fade: 200
  });
  
  if ( $("#galerie .navi a").length > 0 ) {
    $(document).keydown(function(event) {
     
     back = $("#galerie .navi a.back");
     next = $("#galerie .navi a.next");
      
     if (event.keyCode == '37' && back.length > 0) {
       window.location.href = window.location.protocol + "//" + window.location.hostname + "/" + back.attr('href');
       back.toggleClass("hover");
     }
     else if(event.keyCode == '39' && next.length > 0) {
       window.location.href = window.location.protocol + "//" + window.location.hostname + "/" + next.attr('href');
       next.toggleClass("hover");
     }
   });
  }
  
});

function changeBackground(way) {
  
  var list    = $(".startseite#showroom ul");
  var current   = list.find("li.current");
  var patt    =/\"|\'|\)|\(|url/g;
  var next;
  var image;
  
  window.clearTimeout(startseite_timeout);
  
  if(way == 'back') {
 	  if(current.prev('li').length == 0)
		next = list.find('li:last-child');
	  else
		next = current.prev('li'); 
  } else {
	  if(current.next('li').length == 0)
		next = list.find('li:first-child');
	  else
		next = current.next('li');
  }
 
 
  current.fadeOut('slow', function() {
	current.removeClass("current");
  });

  next.fadeIn('slow', function() {
    next.addClass("current");
    
    image = next.css("background-image");
    
    $.post("/ajax/json/startseite_bilder.html", { 'image': image.replace(patt,'') } );
    startseite_timeout = window.setTimeout(changeBackground, 15000);
  }); 
}
