$(document).ready(function() {
  var splash = $('#splash');
  if(splash.length) {
    if($(splash).data('animation')) {
      $(splash).hide();
      var frames = parseFloat($(splash).data('frames'), 10),
          p = $(splash).parent();

      if(frames > 0) {
        $(p).addClass('anim');
        for (var i=1; i <= frames; i++) {
          $(p).append('<img src="/assets/splash/'+$(splash).data('animation')+'/'+i+'.png">');
        };
        $(this).remove();
 
        var stack = {
          delay: 1000,
          actions:[],
          run: function() {
            if (stack.actions.length) {
              stack.actions.shift()();
              setTimeout(stack.run, stack.delay);
            }
          }
        };


$(this)
        $('#content.splash img').each(function(){
          var el = this;
          $(el).css({
            'opacity': 0,
            'visibility': 'visible'
           });                           
           stack.actions.push(function(){
              $(el).animate({            
               'opacity': 1              
             }, 1000);                   
           });                           
         });                             

        stack.run();

      }
    }
    
  }

  $('.pager .page').html(1);
  $('.pages').each(function (){
    $(this).html($('#gallery .images img').length);
  });

  // next button
  $('.pager .next').click(function() {
    browseImages('next');
  });
  
  // prev page button
  $('.pager .prev').click(function() {
    browseImages('prev');
  });

  // prev page button
  $('#gallery .magnifier, #gallery .close').click(function() {
    var c = 'zoom';
    if($('#gallery').hasClass('zoom')) {
      c = 'small';
    }
    $('#gallery .images img').each(function() {
      $(this).get(0).src = $(this).data(c);
    });
   $('#gallery').toggleClass('zoom');
  });

  function browseImages(dir) {
    var imgs   = $('#gallery .images img'),
        next;
    var titles = $('#gallery div.photo'),
        next;
        
    // Images
    for (var i=0; i < imgs.length; i++) {
      if($(imgs[i]).hasClass('active') && dir == 'next') {
        if(imgs[i+1]) {
          next = i+1;
          break;
        } else {
          next = 0;
          break;
        }
      } else if($(imgs[i]).hasClass('active') && dir == 'prev') {
        if(imgs[i-1]) {
          next = i-1;
          break;
        } else {
          next = (imgs.length-1);
          break;
        }
      }
    }
    if(imgs[next]) {
      $(imgs).removeClass('active').addClass('hidden');
      $(imgs[next]).addClass('active').removeClass('hidden');
      $('.pager .page').html(next+1);
    }
    
    // Titles
    for (var i=0; i < titles.length; i++) {
      if($(titles[i]).hasClass('active') && dir == 'next') {
        if(titles[i+1]) {
          next = i+1;
          break;
        } else {
          nextTitle = 0;
          break;
        }
      } else if($(titles[i]).hasClass('active') && dir == 'prev') {
        if(titles[i-1]) {
          next = i-1;
          break;
        } else {
          next = (titles.length-1);
          break;
        }
      }
    }
    if(titles[next]) {
      $(titles).removeClass('active').addClass('hidden');
      $(titles[next]).addClass('active').removeClass('hidden');
    }


  };


  // // init
  // $('.pager .page').html(1);
  // $('.pages').each(function (){
  //   $(this).html($(this).parents('.gallery').find('.images img').length);
  // });
  // 
  // // prev button
  // $('.pager .prev').click(function() {
  //   browseImages('prev', $(this).parents('.gallery'));
  // });
  // 
  // // next button
  // $('.pager .next').click(function() {
  //   browseImages('next', $(this).parents('.gallery'));
  // });
  // 
  // // last page button
  // $('.pager .pages').click(function() {
  //   browseImages('last', $(this).parents('.gallery'));
  // });
  // 
  // // magnifier button
  // $('.controls .magnifier').click(function() {
  //   $(this).parents('.gallery').prev('.lightbox').css('top', $(this).parents('.gallery').position().top - 8);
  //   $(this).parents('.gallery').prev('.lightbox').removeClass('hidden');
  //   var arrImages = $(this).parents('.gallery').find('.images img');
  //   var objCurrent = arrImages.not('.hidden');
  //   var intIndex = arrImages.index(objCurrent);
  //   browseImages(intIndex, $(this).parents('.gallery').prev('.lightbox'));
  // });
  // 
  // // close button
  // $('.controls .close').click(function() {
  //   $(this).parents('.lightbox').addClass('hidden');
  //   var arrImages = $(this).parents('.gallery').find('.images img');
  //   var objCurrent = arrImages.not('.hidden');
  //   var intIndex = arrImages.index(objCurrent);
  //   browseImages(intIndex, $(this).parents('.gallery').next('.gallery'));
  // 
  // });
  // 
  // // play button
  // $('.controls .play').click(function() {
  //   // console.log('play');
  // });
  // 
  // // Browse through images and show the next, prev, last or given id
  // function browseImages(direction, objGallery) {
  // 
  //   var arrImages = objGallery.find('.images img');
  //   var arrTitles = objGallery.find('.controls .photo');
  //   var objPage   = objGallery.find('.pager .page');
  // 
  //   var objCurrent = arrImages.not('.hidden');
  //   var objCurrentTitle = arrTitles.not('.hidden');
  // 
  //   var intIndex = arrImages.index(objCurrent);
  // 
  //   objCurrent.addClass('hidden');
  //   objCurrentTitle.addClass('hidden');
  // 
  //   if(direction == 'next') {
  //     intIndex++;
  //     if(intIndex == arrImages.length) {
  //       intIndex = 0;
  //     }
  //   } else if (direction == 'prev') {
  //     intIndex--;
  //     if(intIndex == -1) {
  //       intIndex = arrImages.length - 1;
  //     }
  //   } else if (direction == 'last') {
  //     intIndex = arrImages.length - 1;
  //   } else if (!isNaN(direction)) {
  //     intIndex = direction;
  //   }
  // 
  //   arrImages.eq(intIndex).removeClass('hidden');
  //   arrTitles.eq(intIndex).removeClass('hidden');
  // 
  //   arrImages.eq(intIndex).get(0).src = arrImages.eq(intIndex).data('zoom');
  // 
  //   objPage.html(intIndex + 1);
  // 
  // }
  
  /* end: gallery controlls */

});

