// #ruby
// 
// array = [1,2,3,4,5]
// sum = array.inject(0) {|sum, element|
//     sum + element
// }
// 
// and in javascript
// 
// //note that Array.inject() is not part of normal javascript, I had to implement that myself.
// var array = [1,2,3,4,5]
// var sum = array.inject(0, function(sum, element) {
//     return sum + element
// })

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$(function(){

  // $('#flash').flash({
  //     src: '/images/flash/main.swf',
  //     width: 820,
  //     height: 361
  // });  
  
  
  $("#projects tbody tr td:not(.revoke)").click(function(){
    window.location = $(this).parent().attr("data-location");
  })
  
  
  
  $('.box').fancybox({overlayShow: false, transitionIn: 'elastic', transitionOut: 'fade', hideOnContentClick: true, showCloseButton: false, titleShow: false, padding: 0, changeSpeed: 800});
  
  
  
  $.map( $('#articles div.hover_show img.front'), function(img){ 
    $(img).mouseover( function(){ 
      $(img).parent().children("img.back").fadeIn("slow");
      $(img).parent().children("p.back").width(0);
      $(img).parent().children("p.back").animate({width:'316px'},500);
      $(img).parent().children(".text").fadeIn("slow");
    })
  });
  
  $.map( $('#articles div.hover_show img.back'), function(img){ 
    $(img).mouseout( function(){ 
      $(img).parent().children(".back").fadeOut("slow");
      $(img).parent().children(".text").slideUp("slow");
    });
  });

  var resizeTimer = null;
  
  $(window).bind('resize', function() {
    resizeCarousel();
  });

  //  randomizePhotos(); 
  // 
  //
  //   $('#carousel').everyTime(2000, '2s', function() {
  //     currentPhoto();
  //   });
  //   
  // 
  //   $('body').resize( function(){
  //      alert("asdasd");
  //   });
});

//////////////////////////////////////////////////////////////////////////////////////////////////////

jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);

};




function displayCarousel(controller, action) {
  // var controller = (controller == null) ? "default" : controller;
  // var action = (action == null) ? "default" : action;
  
  $(document).ready(function(){
    $('#carousel').flash(
      {  
        flashvars: { carousel: carousel_name(), env: merb_env() },
        src: '/flash/carousel.swf',
        width: "100%",
        
      },{ version: 10, expressInstall: true }
    );
    resizeCarousel();
    
    // switch(controller)
    // {
    //   case "pages":
    //     
    //     break;    
    //   case "works":
    // 
    //     break;
    //   case "publications":
    // 
    //     break;
    // 
    // }
  });
}

function resizeCarousel(){
  $("#carousel embed").height( $(window).width() / carousel_proportion() );

}


function randomizePhotos(){
  img_count = 4
  
  rand = Math.round( img_count* Math.random());
  for (var i = img_count ; i >= rand +2 ; i--){
    $( $("#carousel img")[i] ).hide();
    // $("#debug").append(i+" hidden, "); 
  };
  
}


function currentPhoto(){
  visibles      = $('#carousel img:visible');
  non_visibles  = $('#carousel img:not(:visible)');
  // $('#debug').append( " | vis:"+visibles.size() );
  // $('#debug').append( " non-vis:"+non_visibles.size() );
  
  if ( visibles.size() != 2 ){
    $('#carousel img:visible:last').fadeOut("slow");
  } else {
    $('#carousel img:visible:last').fadeOut("slow", function(){
      non_visibles.show();
    });
  }
}

function switchPhoto(){
  var current_photo = currentPhoto();
}





$(document).ready(function(){

  $('#link_asd').click(function(){
    // html = "dehehiehho"
    $.get("asd", function(html){
      $('#content_da_riempire').append(html);
    });
  });
  
});