$(function() {
  // development helpers
  $(window).keypress(function(e) {
    // key g or G
    if(e.which == 103 || e.which == 71) $('#dev_16_grid_overlay').toggle();
  });
  // /development helpers
});

function scroller(prev) {
  //get the current selected item (with selected class), if none was found, get the first item
  var current_image = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first');
  var current_excerpt = $('#excerpt li.selected').length ? $('#excerpt li.selected') : $('#excerpt li.item:first');
  var current_locator = $('#picnav li.selected').length ? $('#picnav li.selected') : $('#picnav li:first');

  //if prev is set to 1 (previous item)
  if (prev) {
    //get previous sibling
    var next_image = (current_image.prev().length) ? current_image.prev() : $('#gallery li:last');
    var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('#excerpt li.item:last');
    var next_locator = (current_locator.prev().length) ? current_locator.prev() : $('#picnav li:last');
    //if prev is set to 0 (next item)
  } else {
    //get next sibling
    var next_image = (current_image.next().length) ? current_image.next() : $('#gallery li:first');
    var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('#excerpt li.item:first');
    var next_locator = (current_locator.next().length) ? current_locator.next() : $('#picnav li:first');
  }

  //clear the selected class
  $('#excerpt li.item, #gallery li, #picnav li').removeClass('selected');

  //reassign the selected class to current items
  next_image.addClass('selected');
  next_excerpt.addClass('selected');
  next_locator.addClass('selected');

  //scroll the items
  $('#mask-gallery').scrollTo(next_image, 800, {easing:'easeOutCirc'});
  $('#mask-excerpt').scrollTo(next_excerpt, 800, {easing:'easeOutCirc'});
}

function textboxscroller(prev) {
  //get the current selected item (with selected class), if none was found, get the first item
  var current_box = $('#textboxlist li.selected').length ? $('#textboxlist li.selected') : $('#textboxlist li:first');

  //if prev is set to 1 (previous item)
  if (prev) {
    //get previous sibling
    var next_box = (current_box.prev().length) ? current_box.prev() : $('#textboxlist li:last');
    //if prev is set to 0 (next item)
  } else {
    //get next sibling
    var next_box = (current_box.next().length) ? current_box.next() : $('#textboxlist li:first');
  }

  //clear the selected class
  $('#textboxlist li').removeClass('selected');

  //reassign the selected class to current items
  next_box.addClass('selected');

  //scroll the items
  $('#mask-textboxitem').scrollTo(next_box, 800, {easing:'easeOutCirc'});
}

function boxscroller(prev) {
  //get the current selected item (with selected class), if none was found, get the first item
  var current_box = $('#boxlist li.selected').length ? $('#boxlist li.selected') : $('#boxlist li:first');

  //if prev is set to 1 (previous item)
  if (prev) {
    //get previous sibling
    var next_box = (current_box.prev().length) ? current_box.prev() : $('#boxlist li:last');
    //if prev is set to 0 (next item)
  } else {
    //get next sibling
    var next_box = (current_box.next().length) ? current_box.next() : $('#boxlist li:first');
  }

  //clear the selected class
  $('#boxlist li').removeClass('selected');

  //reassign the selected class to current items
  next_box.addClass('selected');

  //scroll the items
  $('#mask-boxitem').scrollTo(next_box, 800, {easing:'easeOutCirc'});
}



// Bubble Tooltip
$(function () {
  $('.bubbleInfoTop').each(function () {
    var distance = 10;
    var time = 250;
    var hideDelay = 500;
    var hideDelayTimer = null;
    var beingShown = false;
    var shown = false;
    var trigger = $('.toptrigger', this);
    var info = $('.popup', this).css('opacity', 0);

    $([trigger.get(0), info.get(0)]).mouseover(function () {
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      if (beingShown || shown) {
        // don't trigger the animation again
        return;
      } else {
        // reset position of info box
        beingShown = true;

        info.css({
          top: -90,
          left: -33,
          display: 'block'
        }).animate({
          top: '+=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          beingShown = false;
          shown = true;
        });
      }

      return false;
    }).mouseout(function () {
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        info.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          shown = false;
          info.css('display', 'none');
        });

        }, hideDelay);

        return false;
      });
    });
    $('.bubbleInfoBottom').each(function () {
      var distance = 10;
      var time = 250;
      var hideDelay = 500;

      var hideDelayTimer = null;

      var beingShown = false;
      var shown = false;
      var trigger = $('.bottomtrigger', this);
      var info = $('.popup', this).css('opacity', 0);


      $([trigger.get(0), info.get(0)]).mouseover(function () {
        if (hideDelayTimer) clearTimeout(hideDelayTimer);
        if (beingShown || shown) {
          // don't trigger the animation again
          return;
        } else {
          // reset position of info box
          beingShown = true;

          info.css({
            top: -90,
            left: -33,
            display: 'block'
          }).animate({
            top: '-=' + distance + 'px',
            opacity: 1
          }, time, 'swing', function() {
            beingShown = false;
            shown = true;
          });
        }

        return false;
      }).mouseout(function () {
        if (hideDelayTimer) clearTimeout(hideDelayTimer);
        hideDelayTimer = setTimeout(function () {
          hideDelayTimer = null;
          info.animate({
            top: '+=' + distance + 'px',
            opacity: 0
          }, time, 'swing', function () {
            shown = false;
            info.css('display', 'none');
          });

          }, hideDelay);

          return false;
        });
      });
});



$(document).ready(function() {

  // code for google map
  if ($('#map').length > 0) {
    var map = new GMap(document.getElementById("map"));
    map.removeControl();
    map.enableScrollWheelZoom();
    
    var center = new GLatLng(49.42193692814534, 8.64199161529541);
    map.setCenter(center, 15);
    
    var myIcon = new GIcon();
    myIcon.image = '2SBM/images/marker_image.png';
    myIcon.shadow = '2SBM/images/marker_shadow.png';
    myIcon.iconSize = new GSize(35,35);
    myIcon.shadowSize = new GSize(53,35);
    myIcon.iconAnchor = new GPoint(18,35);
    myIcon.infoWindowAnchor = new GPoint(18,0);
    myIcon.printImage = '2SBM/images/marker_printImage.gif';
    myIcon.mozPrintImage = '2SBM/images/marker_mozPrintImage.gif';
    myIcon.printShadow = '2SBM/images/marker_printShadow.gif';
    myIcon.transparent = '2SBM/images/marker_transparent.png';
    myIcon.imageMap = [22,0,25,1,26,2,28,3,29,4,30,5,31,6,32,7,32,8,33,9,33,10,34,11,34,12,34,13,34,14,34,15,34,16,34,17,34,18,34,19,34,20,34,21,34,22,34,23,33,24,33,25,32,26,32,27,31,28,30,29,29,30,28,31,27,32,25,33,23,34,11,34,9,33,8,32,6,31,5,30,4,29,3,28,3,27,2,26,1,25,1,24,1,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,1,11,1,10,2,9,2,8,3,7,4,6,4,5,5,4,7,3,8,2,10,1,12,0];

    var markerOptions = {icon:myIcon};
    
    var point = new GLatLng(49.42199, 8.64207);
    var text = "<div class='infowindow'><p><br/><strong>2sbm</strong><br/>Maaßstraße 32/1<br>D-69123 Heidelberg</p></div>";
    var marker = new GMarker(point, markerOptions);
    
    GEvent.addListener(marker, "click", function () {
      marker.openInfoWindowHtml(text);
    });
    
    map.addOverlay(marker);
  }
  
  // code vor entry-page variant-a
  if ( $('#var-a').length > 0 ) {
    $('#mask-gallery, #gallery li').width($('#slider').width()-370);	
    $('#gallery').width($('#slider').width() * $('#gallery li').length);
    $('#mask-gallery, #gallery li').height($('#slider').height());
    
    // set the first container to be active
    $('#gallery li:first').addClass('selected');
    $('#boxlist li:first').addClass('active');
    
    // get the list of boxes in an array
    var boxlistelements = $('#boxlist li');
    
    // loop through the boxlist
    $.each(boxlistelements,function(index, item) {
      // assign hover event on each item
      $(item).hover(function(){
        $('#mask-gallery').stop();
        
        // only animate if image isn't already shown
        if (!$('#mask-gallery li:eq('+index+')').hasClass("selected")) {
          //remove all selected classes
          $('#mask-gallery li').removeClass("selected");
          $('#boxlist li').removeClass("active");
          // assign the selected class to the current image
          $('#mask-gallery li:eq('+index+')').addClass("selected");
          $(item).addClass("active");
          
          // get the correct index and length of the corresponding image
          var image = ($('#mask-gallery li:eq('+index+')').length) ? $('#mask-gallery li:eq('+index+')') : $('#gallery li:first');
          // scroll it finally
          $('#mask-gallery').scrollTo(image, 800, {easing:'easeOutQuint'});
        }
      });
    });
  }
  

  // code vor entry-page variant-b
  if ( $('#var-b').length > 0 ) {
    // calculation of containers
    $('#mask-boxitem, #boxlist li').width($('#boxslider').width()-10);
    $('#boxlist').width(1150);
    $('#mask-boxitem').height($('#boxslider').height());
    $('#boxlist li').height($('#boxslider').height()-20);
    
    // set the first container to be active
    $('#gallery li:first').addClass('selected');
    $('#boxlist li:first').addClass('selected');
    
    // settings for scroller
    var speed = 6000;
    var run = setInterval('boxscroller(0)', speed);
    var slideshowstate = 0;
    
    // set this to stop the slideshow
    //clearInterval(run);
  }
  
  // code vor entry-page variant-b
  if ( $('#var-d').length > 0 ) {
    // calculation of containers
    $('#mask-textboxitem, #textboxlist li').width($('#textboxslider').width()+20);
    $('#textboxlist li').width($('#textboxslider').width());
    $('#textboxlist').width(1150);
    $('#mask-textboxitem').height($('#textboxslider').height());
    $('#textboxlist li.textboxitem').height($('#textboxslider').height());
    
    // set the first container to be active
    $('#textboxlist li:first').addClass('selected');
    
    // settings for scroller
    var speed = 3500;
    var run = setInterval('textboxscroller(0)', speed);
    var slideshowstate = 0;
    
    // set this to stop the slideshow
    //clearInterval(run);
  }
  
  // code vor project-page
  if ( $('a.slideshow').length > 0 ) {
    // calculation of containers
    $('#mask-gallery, #gallery li').width($('#slider').width()-370);	
    $('#gallery').width($('#slider').width() * $('#gallery li').length);
    $('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li.item').height($('#slider').height());
    
    // set the first container to be active
    $('#gallery li:first, #excerpt li.item:first, #picnav li:first').addClass('selected');
    
    // settings for scroller
    var speed = 3500;
    var run = setInterval('scroller(0)', speed);
    var slideshowstate = 0;
    
    // pause it on default
    clearInterval(run);
    
    // start/stop slideshow
    $('a.slideshow').click(function () {
      // if slideshow is not running, start it
      if (slideshowstate == 0) {
        run = setInterval('scroller(0)', speed);
        slideshowstate = 1;
        // add selected class
        $(this).addClass("slideshows");
        $(this).html("slideshow off");
      } else { // if slideshow is running, stop it
        clearInterval(run);
        slideshowstate = 0;
        // remove selected class
        $(this).removeClass("slideshows");
        $(this).html("slideshow on&nbsp;");
      }
      return false;
    });
    
    // get the list of navelements in an array
    var navelements = $('#picnav li');
    // loop through the navelements
    $.each(navelements,function(index, item) {
      $(item).click(function(){
        // only animate if image/box is not the active one
        if (!$('#mask-gallery li:eq('+index+')').hasClass("selected")) {
          // get the correct index of the corresponding image/box
          var image = $('#mask-gallery li:eq('+index+')');
          var excerpt = $('#excerpt li.item:eq('+index+')');
          
          //remove all selected classes
          $('#mask-gallery li, #mask-excerpt li.item, #picnav li').removeClass("selected");
          // set the selected class to the current image/excerpt/nav
          $('#mask-gallery li:eq('+index+'), #mask-excerpt li.item:eq('+index+')').addClass("selected");
          $(item).addClass("selected");
          // scroll image/excerpt
          $('#mask-gallery').scrollTo(image, 800, {easing:'easeOutCirc'});
          $('#mask-excerpt').scrollTo(excerpt, 800, {easing:'easeOutCirc'});
          
        };
        return false;
      });

    });

    // get the list of images in an array
    var galleryelements = $('#gallery li');
    // loop through the image-elements
    $.each(galleryelements,function(index, item) {
      $(item).click(function(){
        // get the correct index and length of the next image/excerpt/nav
        var image = ($('#mask-gallery li:eq('+index+')').next().length) ? $('#mask-gallery li:eq('+index+')').next() : $('#gallery li:first');
        var excerpt = $('#excerpt li.item:eq('+index+')').next().length ? $('#excerpt li.item:eq('+index+')').next() : $('#excerpt li.item:first');
        var nav = ($('#picnav li:eq('+index+')').next().length) ? $('#picnav li:eq('+index+')').next() : $('#picnav li:first');
        
        //remove all selected classes
        $('#mask-gallery li, #mask-excerpt li.item, #picnav li').removeClass("selected");
        // set the selected class to the current image/excerpt/nav
        $('#mask-gallery li:eq('+index+'), #mask-excerpt li.item:eq('+index+')').addClass("selected");
        nav.addClass("selected");
        // scroll image/excerpt
        $('#mask-gallery').scrollTo(image, 800, {easing:'easeOutCirc'});
        $('#mask-excerpt').scrollTo(excerpt, 800, {easing:'easeOutCirc'});
      });
    });
  }
});