// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

jQuery(function($) {
  function getInnerDimensions() {
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      //IE 4 compatible
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
    }
    
    return { width : myWidth, height : myHeight };
  }
  
  var windowSize = getInnerDimensions();
  var sidebarWidth = 247 - 10;
  
  function testForTopNavigation() {
    if(windowSize.width > 1024) {
      $('#top-navigation').hide();
      $('#sidebar-column').show();
      $('section.content').css('width', 'auto');
      $('section.content').css('overflow', 'none');
      sidebarWidth = 247 - 10;
    } else {
      $('#top-navigation').show();
      $('#sidebar-column').hide();
      $('#top-navigation').css('width', (768 - 20 * 2) + 'px');
      $('section.content').css('width', (768 - 20 * 2) + 'px');
      $('section.content').css('overflow', 'hidden');
      sidebarWidth = 0;
    }
  }
  
  testForTopNavigation();
  var calcWidth = windowSize.width - sidebarWidth
  if(calcWidth > 970) calcWidth = 970;
  $('#content-columns').css('width', calcWidth + 'px');
  $('#sidebar-column').css('left', calcWidth + 'px');
  if(windowSize.width > 1024) {
    $('#language-select').css('left', (calcWidth - $('#language-select').width()-25) + 'px');
  } else {
    $('#language-select').css('left', '10px');    
  }
  $(window).resize(function() {
    windowSize = getInnerDimensions();
    testForTopNavigation();
    calcWidth = windowSize.width - sidebarWidth;
    if(calcWidth > 970) calcWidth = 970;
    $('#content-columns').css('width', calcWidth + 'px');
    $('#sidebar-column').css('left', calcWidth + 'px');
    if(windowSize.width > 1024) {
      $('#language-select').css('left', (calcWidth - $('#language-select').width()-25) + 'px');
    } else {
      $('#language-select').css('left', '10px');    
    }
  });
   
  // CommentBox
  // sadly the client does not want it
  // $('#unleash-comment-box').click(function() {
  //    $('#comment-box').slideDown();
  // });

  // aligner
  $('.aligner').masonry({
      columnWidth: $('.aligner').attr('data-column-width'),
      itemSelector: '.box:not(.invis)',
      animate: true
  });
  
  $('a.nav-filter').click(function(){
      var colorClass = $(this).attr('href');
  
      if(colorClass=='.all') {
          // show all hidden boxes
          $('.aligner').children('.invis')
              .toggleClass('invis').animate({opacity: 1},{ duration: 500 });
      } else {    
          // hide visible boxes 
          $('.aligner').children().not(colorClass).not('.invis')
              .toggleClass('invis').animate({opacity: 0},{ duration: 500 });
          // show hidden boxes
          $('.aligner').children(colorClass+'.invis')
              .toggleClass('invis').animate({opacity: 1},{ duration: 500 });
      }
      $('.aligner').masonry();
  
      return false;
  });
  
  // slideshow
  var slideshow = $('ul.slideshow');
  var preloadingSlides = $('li.preload', slideshow);
  var preloadingTotal = preloadingSlides.size();
  var loaded = 0;
  
  slideshow.click(function() {
    location.href = $(this).attr('data-click-action');
  });
  
  $(preloadingSlides).each(function(i,e) {    
    var element = $(e);
    var image = new Image;
    
    function loadCallback() {
      var li = $('<li class="slide">');
      li.append(image)
      slideshow.append(li);
      loaded++;

      if(loaded == preloadingTotal) {
        var slides = $('li.slide', slideshow);

        var totalSlides = slides.length;

        var transitionInterval = parseInt($('ul.slideshow').attr('data-transition-interval'));
        var transitionTime = parseInt($('ul.slideshow').attr('data-transition-time'));
        var currentSlide = 0;

        $(slides).eq(currentSlide).show();

        var intervalHandler = window.setInterval(function() {
          $(slides).eq(currentSlide).fadeOut(transitionTime, function() {
            currentSlide++;
            currentSlide %= totalSlides;
            $(slides).eq(currentSlide).fadeIn(transitionTime);
          });
        }, transitionInterval);
      }
    }
    
    // if (image.addEventListener) {  
    //   image.addEventListener('load', loadCallback, false);
    // } else if (image.attachEvent) {
    //   image.attachEvent('load', loadCallback);
    // }
    image.onload = loadCallback;
    
    image.src = element.attr('data-preload-image');    
    
    element.remove();
  });
  
  // hide flashes on click
  $('.flash').click(function() {
    $(this).slideUp();
  });
  
  $('form.validate').submit(function(e) {
    var form = $(this);
    var valid = true;
    
    $('.validate-presence', this).each(function(i, e) {
      var el = $(e);
      if(el.hasClass('error')) el.removeClass('error')
      if(el.val() == "") {
        valid = false;
        el.addClass("error");
      }
    });
    
    if(!valid) alert(form.attr('data-error-validate-presence'));
    return valid;
  });
  
  $('a#find-nearby-location').click(function() {
    if (navigator.geolocation) {
      function geoSuccess(position) {
        location.href = "/events/?lat=" + position.coords.latitude + "&lon=" + position.coords.longitude;
      }
      function geoError(msg) {
        alert(msg);
      }
      navigator.geolocation.getCurrentPosition(geoSuccess, geoError);
    } else {
      alert("Not supported on your Browser!");
    }
    
    return false;
  })
});