// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//

$(document).ready( function() {
  var onLevel = 1;
  
  /* 
  $('ul.collection .preview_images').hover( 
    function() {
      rotatePreview(this);
    },
    function() {
      rotatePreview(this);
    }
  );
  */
  
  $('#portfolio_link').hide();
  setTimeout(function () {
    $('#portfolio_link').fadeIn(400);
  }, 1500);

  
  $('.page_portfolio #categories ul.collection li').click( function() {
    onLevel = onLevel + 1;
    
    $('#categories').animate({
      scale: [(1.7/onLevel), (1.7/onLevel)]
    }, 500);
    
    $('#categories_dim').fadeIn(600);
    
    if ($(this).attr('goto-type') == 'small_images') {
      //console.debug('show the images');
      $('#small_images').show().find('.image_set.' + $(this).attr('goto-val') ).fadeIn(600);
    }
    else if ($(this).attr('goto-type') == 'sub_categories') {
      //console.debug('show the sub category');
      $('#sub_categories').show().find('.category.' + $(this).attr('goto-val') ).fadeIn(600);
    }
    
  });
  
  $('.page_portfolio #sub_categories ul.collection li').click( function() {    
    //console.debug('show the images');
    onLevel = onLevel + 1;
    $('#categories').animate({
      scale: [(1.7/onLevel), (1.7/onLevel)]
    }, 500);
    
    $('#sub_categories').animate({
      scale: [(2.5/onLevel), (2.5/onLevel)]
    }, 500);
    
    $('#sub_categories_dim').fadeIn(600);
    
    $('#small_images').show().find('.image_set.' + $(this).attr('goto-val') ).fadeIn(600);
  });
  
  // To view a full sized image
  $('#small_images .image').click( function() {
    onLevel = onLevel + 1;
    $('#categories').animate({
      scale: [(1.7/onLevel), (1.7/onLevel)]
    }, 500);
    
    /* images can appear on level 2 and 3 */
    if (onLevel == 4) {
      $('#sub_categories').animate({
        scale: [(2.5/onLevel), (2.5/onLevel)]
      }, 500);  

      $('#small_images').animate({
        scale: [(3.8/onLevel), (3.8/onLevel)]
      }, 500);  
    } else {
      $('#small_images').animate({
        scale: [(2.5/onLevel), (2.5/onLevel)]
      }, 500);
    }
    
    // reset the single image view
    $('#full_image_viewer .box').removeAttr('style');
    $('#full_image_viewer img.image').removeAttr('style');
    
    $('#full_image_viewer .head').text($(this).attr('img-description'));
    $('#full_image_viewer img.image').attr('src', $(this).attr('img-large'));
    if ($(this).attr('img-scale-relative') == 'yes') {
      $('#full_image_viewer .box').width('90%');
      $('#full_image_viewer img.image').width('100%');
    }
    
    $('#full_image_viewer').fadeIn(400);
  });

  $('#full_image_viewer .offclick, #full_image_viewer .close').click( function() {    
    onLevel = onLevel - 1;
    
    $(this).parents('#full_image_viewer').fadeOut(300, function() {
      $('#small_images').transform({scale: [1,1]});
    });

    $('#sub_categories').animate({
      scale: [(2.5/onLevel), (2.5/onLevel)]
    }, 300);

    $('#categories').animate({
      scale: [(1.7/onLevel), (1.7/onLevel)]
    }, 300);
  });
  
  
  $('#sub_categories .offclick').click( function() {
    onLevel = onLevel - 1;
    //console.debug('hide subcategories LEVEL', onLevel);
    
    $(this).parent().fadeOut(300, function() {
      $('#sub_categories').transform({scale: [1,1]});
    });
    $('#categories_dim').fadeOut(300);

    $('#categories').animate({
      scale: [(1/onLevel), (1/onLevel)]
    }, 300);

  });
  
  $('#small_images .offclick').click( function() {
    onLevel = onLevel - 1;
    //console.debug('hide small images LEVEL', onLevel);
    
    if (onLevel == 1) {
      $(this).parent().fadeOut(300, function() {
        $('#small_images').transform({scale: [1,1]});
        $('#sub_categories').transform({scale: [1,1]});
      }); 
      $('#categories_dim').fadeOut(300);
      $('#categories').animate({
        scale: [(1/onLevel), (1/onLevel)]
      }, 300);     
    } else {
      $(this).parent().fadeOut(300, function() {
        $('#small_images').transform({scale: [1,1]});      
      });
      $('#sub_categories_dim').fadeOut(300);
      $('#sub_categories').animate({
        scale: [(2/onLevel), (2/onLevel)]
      }, 300);   
      $('#categories').animate({
        scale: [(1.7/onLevel), (1.7/onLevel)]
      }, 300);   
    }
    
    

  });
  
});
  
function rotatePreview(obj) {
  var showIndex = 1;
  var hideIndex = 0;
  var images = $(obj).find('.image');
  $(images).each( function(i) {
    if ($(images[i]).is(':visible')) {
      if ((i + 1) == images.length ) {
        showIndex = 0;
        hideIndex = i; 
      }
      else {
        showIndex = (i + 1) ;
        hideIndex = i ;                 
      }
    }
  });

  $(images[showIndex]).fadeIn(700);
  $(images[hideIndex]).fadeOut(700);
};

