// lift-vimeo.js (revision 2)
// Author: Eli Dupuis, Greg Crossfield
// Date: Aug 23, 2011
// 
// Dependancies:
// jQuery 1.6.2
// Vimeo JSON feed
//
// Documentation:
// http://vimeo.com/api/docs/simple-api


$(function(){         

  // looks for a specific json feed based on some variables "channel/user/type/etc."

  var url = 'http://vimeo.com/api/v2/abnawmp/videos.json'
  if(window.console) window.console.log("requesting", url);
  $.ajax({
    url: url,
    get: 'GET',
    dataType: 'jsonp',
    success: function(data){
      if(window.console) window.console.log('success', data);
      var videos = data;
      var theHtml = '';

      for (var i=0; i < videos.length; i++) {
        var video = videos[i];

        //sends information to the console

        if(window.console) window.console.log( video.url, video.description, video.duration, video.thumbnail_small, video.title );


        //generates the HTML

        theHtml += '<div class="videothumb">';
        theHtml += '<a href="' + video.url + '">' + '<img src="' + video.thumbnail_small + '" alt="" />' + '</a>';
        theHtml += '<div class="titlevid">' + '<a href="' + video.url + '" class="titlebtn">' + video.title + '</a>' + '</div>';
        theHtml += '<div class="time">' + video.duration + '</div>';
        theHtml += '<div class="descr">' + video.description + '</div>';
        theHtml += '</div>';
        
        
        //converts duration from seconds to minutes:seconds

        jQuery.fn.time_from_seconds = function() {
            return this.each(function() {
                var t = parseInt($(this).text(), 10);
                $(this).data('original', t);
                var h = Math.floor(t / 3600);
                t %= 3600;
                var m = Math.floor(t / 60);
                var s = Math.floor(t % 60);
                if (s.toString().length === 1) {
                    s = '0'+s.toString();
                };
                $(this).text(('(Length: ') +
                             (m + ':' ) +
                             (s) + ')' );
            });
        };
        
      };
      
      //output to HTML

      $('#result').html(theHtml); 
      $('.time').time_from_seconds();
    },

	//error messages
	
    error: function(jqXHR, textStatus, errorThrown){
      if(window.console) window.console.log('AJAX error', jqXHR, textStatus, errorThrown);
    }

  });
  
});

