$(function () {
    // Replace font
    Cufon.replace(' .phone_number');
    
    // Sub navigation tabs
    $('#subnav').tabs(".panes > div").history();
 
    // Featured photos slideshow
    var headerSlideshow = $('.header-slideshow');
    var slideshowItems = headerSlideshow.find('li');
    
    if (slideshowItems.length) {
        var tabsAPI = headerSlideshow.tabs(slideshowItems, {
            effect:       'fade', 
            fadeOutSpeed: 'slow',
            rotate:       true,
            api :         true
        });
        
        headerSlideshow.slideshow({
            autoplay:  true,
            interval:  5000,
            clickable: false
        });
      
        tabsAPI.onClick(function (e) {
            // Is it a real or a synthetic event?
            if (e.originalEvent) {
                tabsAPI.stop();
            }
        });
        
        slideshowItems.find('a').click(function (e) {
            location.href = this.href;
            e.stopPropagation();
            e.preventDefault();
        });
        
    }



    // Photo gallery overlay
    if ($('.largegallery').length) {
      $('.largegallery').find('a').overlay({
        expose: '#000',
        target: $('.overlay')
      }).gallery({
        opacity:  1.0,
        speed:    'fast',
        template: '<strong>${title}</strong> <span>Bild ${index} von ${total}</span>'
      });
    }

    // Generic overlays

    if ($('.overlay-trigger').length) {
      $('.overlay-trigger').overlay({ expose: '#000' });
    }

    
    // Scrollable gallery

    $('.scrollable').scrollable({ size: 9 });
    
    $('.items img').click(function() {
      var url  = $(this).attr("rel"),
          wrap = $(".wrap"),
          img  = new Image();
      
      img.onload = function() {
          wrap.find("img").attr("src", url);
      };
      
      img.src = url; 
    }).filter(":first").click();


    // Site selectors
	var standorte = $('#standorte');
	if (standorte.length) {
	 standorte.data('defaultValue', standorte.val());
	 $('#standorte').change(function () {
	  var el = $(this), val = el.val();
	  if (val != el.data('defaultValue')) {
	   location.href = val;
	  }
	 });
	}
    
    // Flash
    $('.flash')
      .click(function () { $(this).slideUp(); });
});

DMI = {
  load_map: function (div_id, lat, lng, zoom) {
    var bird = new VELatLong(lat, lng),
        map  = new VEMap('map');
    
    map.LoadMap(bird, zoom);
    map.SetBirdseyeOrientation(VEOrientation.West);
    map.SetBirdseyeScene(bird);
    map.Resize(550, 370);
  }
}

$.fn.extend({
  validatable: function (required_fields) {
    $(this).submit(function () {
      $.each(required_fields, function (i, id) {
        var field = $('#' + id),
            li    = field.closest('li');
        
        li.toggleClass('validation-failed', (field.val().length == 0));
      });
      
      if ($('li.validation-failed').length != 0) {
        alert('Bitte füllen Sie alle rot markierten Felder aus.');
        return false;
      }
    });
  }
});