var $points;

function carousel_callback(carousel) {
  carousel.buttonNext.bind('click', function() {
    carousel.startAuto(0);
  });

  carousel.buttonPrev.bind('click', function() {
    carousel.startAuto(0);
  });

  // Pause autoscrolling if the user moves with the cursor over the clip.
  carousel.clip.hover(
    function() {
      carousel.stopAuto();
    }, function() {
      carousel.startAuto();
    }
  );
  
  $points = $("<div class='jcarousel-points'></div>");
  carousel.buttonPrev.parent().append($points);

  var width = 10 + carousel.options.size * 15
  $points.css("width", width + "px");
  $points.css("left", (845 / 2 - width / 2) + "px");
  for (var i = 0; i < carousel.options.size; i++) {
  	$points.append("<div class='jcarousel-point'></div>");
  } 
}

function carousel_itemload(carousel, state) {
	var idx = (carousel.first - 1) % carousel.options.size;
	$points.children(".jcarousel-point").removeClass("on");
	$($points.children(".jcarousel-point")[idx]).addClass("on");
}

function layout2_callback(carousel) {
	$('.tabs a').bind('click', function() {
    carousel.scroll(jQuery.jcarousel.intval($(this).attr("rel")));
    return false;
  });
  $('.tabs-right a').bind('click', function() {
    carousel.scroll(jQuery.jcarousel.intval($(this).attr("rel")));
    return false;
  });
}

$(document).ready(function() {
  $(".nojs").removeClass("nojs");
  
  var fadeSpeed = 200;
  
  activatePlaceholders();
  
  $("#navi > li.dropdown").mouseenter(function() {
    $(this).animate({backgroundColor: "#514d4e"}, fadeSpeed);
  }).mouseleave(function() {
    $(this).animate({backgroundColor: "white"}, fadeSpeed);
  });
  
  $("#navi > li.single").mouseenter(function() {
    $(this).animate({backgroundColor: "#514d4e"}, fadeSpeed);
  }).mouseleave(function() {
    $(this).animate({backgroundColor: "white"}, fadeSpeed);
  });

  $("#navi > li").mouseenter(function() {
    $(this).find("ul").fadeIn(fadeSpeed);
  }).mouseleave(function() {
    $(this).find("ul").fadeOut(fadeSpeed);
  });
  
  $(".box").mouseenter(function() {
    $(this).animate({backgroundColor: "#e5e5e5"}, fadeSpeed);
  }).mouseleave(function() {
    $(this).animate({backgroundColor: "white"}, fadeSpeed);
  });

  $(".box-1-3").mouseenter(function() {
    $(this).animate({backgroundColor: "#e5e5e5"}, fadeSpeed);
  }).mouseleave(function() {
    $(this).animate({backgroundColor: "white"}, fadeSpeed);
  });

	$('#carousel').jcarousel({
		wrap: 'circular',
		initCallback: carousel_callback,
		itemLoadCallback: carousel_itemload,
		scroll: 1,
		auto: 5
	});

  $("#layout2").jcarousel({
    scroll: 1,
    initCallback: layout2_callback,
    buttonNextHTML: null,
    buttonPrevHTML: null
  });
  
  $("a[href$=.jpg],a[href$=.png],a[href$=.gif]").fancybox({
		'padding': 0,
	    'autoScale': true,
	    'speedIn': 600, 
	    'speedOut': 200, 
	    'overlayShow': true,
     	'overlayOpacity': 0.8,
     	'overlayColor': '#5a5e5f'
  });
  
  $(".iframe").fancybox({
      'width': 700,
      'height': 600,
      'autoScale': false,
      'speedIn': 600, 
      'speedOut': 200, 
      'type': 'iframe',
      'overlayShow': true,
      'overlayOpacity': 0.8,
      'overlayColor': '#5a5e5f'
  });
  
  
  $(".land").mouseenter(function() {
    var id = $(this).attr("alt");
    $("#map-img").attr("src", "/images/map/" + id + ".png");
    $("#plz" + id).css("display", "block");
    //$("#plz" + id).fadeIn(fadeSpeed);
    $("#info" + id).addClass("active");
  }).mouseleave(function() {
    var id = $(this).attr("alt");
    $("#map-img").attr("src", "/images/map/map.png");
    //$("#plz" + id).fadeOut(fadeSpeed);
    $("#plz" + id).css("display", "none");
    $("#map-info li").removeClass("active");
  });
  
  $("#map-info li").mouseenter(function() {
    var id = $(this).attr("id").substr(4,1);
    $("#map-img").attr("src", "/images/map/" + id + ".png");
    $("#plz" + id).css("display", "block");
    //$("#plz" + id).fadeIn(fadeSpeed);
    $("#info" + id).addClass("active");
  }).mouseleave(function() {
    var id = $(this).attr("id").substr(4,1);
    $("#map-img").attr("src", "/images/map/map.png");
    //$("#plz" + id).fadeOut(fadeSpeed);
    $("#plz" + id).css("display", "none");
    $("#map-info li").removeClass("active");
  });
});

function activatePlaceholders() {
  var detect = navigator.userAgent.toLowerCase(); 
  if (detect.indexOf("safari") > 0) return;
  var inputs = document.getElementsByTagName("input");
  
  for (var i=0;i<inputs.length;i++) {
    if (inputs[i].getAttribute("type") == "text") {
      activatePlaceholderOn(inputs[i]);
    }
  }
  
  var textareas = document.getElementsByTagName("textarea");
  
  for (var i=0;i<textareas.length;i++) {
    activatePlaceholderOn(textareas[i]);
  }
}

function activatePlaceholderOn(elem) {
  if (elem.getAttribute("placeholder") && elem.getAttribute("placeholder").length > 0) {
    elem.value = elem.getAttribute("placeholder");
    elem.onclick = function() {
      if (this.value == this.getAttribute("placeholder")) {
        this.value = "";
      }
      return;
    }
    elem.onblur = function() {
      if (this.value.length < 1) {
        this.value = this.getAttribute("placeholder");
      }
    }
  }
}

