function Portfolio() {

  var coords;
  var jcrop_api;
  var thumbs;
  var thumb_idx;

  var _this = this;

  $('#left_content li a').click( function() { _this.init( $(this).attr('type'), $(this).attr('name') ); } ).css('cursor', 'pointer');


  // ---------------------------------------------------------------------
  // thumbs
  // ---------------------------------------------------------------------

  function list_thumbs(type, name) {

    thumbs = new Array();

    function draw_thumb(data) {
      var img = document.createElement('img');
      img.src = data.thumb_path;
      img.id = 'thumb_' + type + '_' + name + '_' + data.filename;
      $(img).click( function() { get_sample(type, name, data.filename) } ).css('cursor','pointer');      
      $('#thumb_imgs').append(img);
    }

    function success(data) {

      $('#thumb_imgs').empty();

      if ( data.thumbs ) {

        if ( data.thumbs.length > 1 ) {

          $('#thumb_back').show();
          $('#thumb_fwd').show();

          var first = true;
          for (var i in data.thumbs) {
            draw_thumb(data.thumbs[i]);
            var thumb_data = { type: type, name: name, filename: data.thumbs[i].filename };
            thumbs.push( thumb_data );

            if (first) {
              first = false;
              get_sample(type, name, data.thumbs[i].filename);
            }
          }

        } else {
          $('#thumb_back').hide();
          $('#thumb_fwd').hide();
          get_sample(type, name, data.thumbs[0].filename);
        }

      }

      thumb_idx = 0;
      $('#thumb_back').unbind('click').click( decr_thumb ).css('cursor', 'pointer');
      $('#thumb_fwd').unbind('click').click( incr_thumb ).css('cursor', 'pointer');

    }

    function decr_thumb() {
      if (thumb_idx == 0)
        return;

      thumb_idx--;

      var data = thumbs[thumb_idx];
      get_sample(data.type, data.name, data.filename);
    }

    function incr_thumb() {
      if (thumb_idx == thumbs.length - 1)
        return;

      thumb_idx++;

      var data = thumbs[thumb_idx];
      get_sample(data.type, data.name, data.filename);
    }

    function error(req, stat, err) {
      alert(stat);
      alert(err);
    }

    var options = {
      url: 'ajax_portfolio.php',
      dataType: 'json',
      data: { action: 'list_thumbs',
              type: type,
              name: name },
      success: success,
      error: error
    }

    $.ajax(options);

  }

  function get_thumb(type, name, filename) {

    function success(data) {
      $('#thumbnail').attr('src', data.thumb);
      while ( $('#thumbnail').attr('src') != data.thumb )
        continue;
      $('#thumbnail').fadeIn('normal');
    }

    var options = {
      url: 'ajax_portfolio.php',
      dataType: 'json',
      data: { action: 'get_thumb',
              type: type,
              name: name,
              filename: filename },
      success: success
    }

    $.ajax(options);

  }

  // ---------------------------------------------------------------------
  // sample
  // ---------------------------------------------------------------------

  function get_sample(type, name, filename) {

    var overwrite = false;
    if (arguments.length > 3)
      overwrite = arguments[3];

    $('#sample_pic').fadeOut( 'normal',
                              function() {
                                $('.selected').removeClass('selected');

                                function success(data) {

                                  if (data.filename.match('http://')) {
                                    embed_vimeo_clip(data.filename);
                                    return;
                                  }                                  

                                  $('#sample_pic').attr( 'src', data.filename ).unbind('click').click( function() { show_original(type, name, filename) } ).css('cursor','pointer');
                                  $('#sample_pic').load( function() {
                                                           $('#sample_pic').fadeIn('normal');
                                                         } );
                                  var thumb_id = 'thumb_' + type + '_' + name + '_' + filename;
                                  $( document.getElementById(thumb_id) ).addClass('selected');
                                }

                                var options = {
                                  url: 'ajax_portfolio.php',
                                  dataType: 'json',
                                  data: { action: 'get_sample',
                                          type: type,
                                          name: name,
                                          filename: filename,
                                          overwrite: overwrite },
                                  success: success
                                }

                                $.ajax(options);

                              } );

  }

  function show_original(type, name, filename) {
    // alert(type + ':' + name + ':' + filename);
    //location.href = 'portfolio/' + type + '/' + name + '/' + filename;
    window.open('portfolio/' + type + '/' + name + '/' + filename, 'original');
  }

  function embed_vimeo_clip(video_url) {

    var url = 'http://vimeo.com/api/oembed.json?url=' + encodeURIComponent(video_url) + '&callback=embed_vimeo_clip';
    var js = document.createElement('script');
    js.setAttribute('type','text/javascript');
    js.setAttribute('src', url);
    $('head').append(js);

  }

  // ---------------------------------------------------------------------
  // description
  // ---------------------------------------------------------------------

  function _get_title(type, name) {

    function success(data) {
      if ( $('#form_title').length ) {
        if (data.title)
          $('#form_title').val(data.title);
      } else
        $('#title').empty().html(data.title);
    }

    var options = {
      url: 'ajax_portfolio.php',
      dataType: 'json',
      data: { action: 'get_title',
              type: type,
              name: name},
      success: success
    }

    $.ajax(options);

  }

  function _get_type(type, name) {

    function success(data) {
      if ( $('#form_type').length ) {
        if (data.type)
          $('#form_type').val(data.type);
      } else
        $('#type').empty().html(data.type);
    }

    var options = {
      url: 'ajax_portfolio.php',
      dataType: 'json',
      data: { action: 'get_type',
              type: type,
              name: name},
      success: success
    }

    $.ajax(options);

  }

  function _get_description(type, name) {

    function success(data) {
      if ( $('#form_description').length ) {
        if (data.description)
          $('#form_description').val(data.description);
      } else {
        var desc = '';
        if ( data.description !== false )
          desc = '<p>' + data.description.replace("\n\n",'</p><p>') + '</p>';
        $('#description').empty().html(desc);
      }

    }

    var options = {
      url: 'ajax_portfolio.php',
      dataType: 'json',
      data: { action: 'get_description',
              type: type,
              name: name},
      success: success
    }

    $.ajax(options);

  }

  function get_description(type, name) {
    _get_title(type, name);
    _get_type(type, name);
    _get_description(type, name);
  }

  this.init = function(type, name) {
    $('.selected_product').removeClass('selected_product');
    list_thumbs(type, name);
    get_description(type, name);
    $('#' + name.replace(' ','_')).addClass('selected_product');
  }

  // ---------------------------------------------------------------------
  // admin
  // ---------------------------------------------------------------------

  this.init_admin = function() {
    var options = { root: 'portfolio/',
                    script: 'ajax_file_tree.php' }; 
    $('#file_tree').fileTree( options, function(file) { file_tree_click(file) } );
    //    init_uploadify();
    clear_form();
  }

  function init_uploadify() {
    var options = { uploader: 'scripts/uploader.swf',
                    script: 'ajax_uploadify.php',
                    cancelImg: 'images/cancel.png',
                    auto: true,
                    folder: '/portfolio' };
                    
    $('#fileInput').fileUpload(options);
  }

  function clear_form() {
    $('#form_title').val('');
    $('#form_type').val('');
    $('#form_description').val('');
  }

  function init_jcrop(type, name, filename) {
    var jcrop_opts = { aspectRatio: 1/1,
                       setSelect: [0, 0, 25, 25],
                       onSelect: store_coords }
    jcrop_api = $.Jcrop('#sample_pic',jcrop_opts);
    $('#form_thumb').unbind('click');
    $('#form_create_thumb').fadeIn('normal').click( function() { crop(type, name, filename) } );
  }

  function store_coords(c) {
    coords = c;
  }

  function crop(type, name, filename) {

    jcrop_api.destroy();

    function success(data) {
      $('#form_create_thumb').unbind('click').fadeOut('normal');
      $('#thumbnail').attr('src', data.thumb);
    }

    var options = {
      url: 'ajax_portfolio_admin.php',
      dataType: 'json',
      data: { action: 'create_thumb',
              type: type,
              name: name,
              filename: filename,
              x1: coords.x,
              y1: coords.y,
              x2: coords.x2,
              y2: coords.y2,
              w: coords.w,
              h: coords.h },
      success: success
    }

    $.ajax(options);

  }

  function file_tree_click(file) {
    var parts = file.split('/');
    get_sample(parts[1], parts[2], parts[3]);
    get_thumb(parts[1], parts[2], parts[3]);
    get_description(parts[1], parts[2]);

    $('#form_save').unbind('click');
    $('#form_save').click( function() { save_file_info(parts[1], parts[2]) } ).css('cursor','pointer');

    $('#regen_sample').unbind('click');
    $('#regen_sample').click( function() { get_sample(parts[1], parts[2], parts[3], true) } );

    $('#form_thumb').click( function() { init_jcrop(parts[1], parts[2], parts[3]) } ).css('cursor','pointer');
  }

  function save_file_info( type, name ) {

    function success(data) {
    }

    var data = { action: 'save_description',
                 type: type,
                 name: name,
                 form_title: $('#form_title').val(),
                 form_type: $('#form_type').val(),
                 form_description: $('#form_description').val() };

    var options = {
      url: 'ajax_portfolio_admin.php',
      dataType: 'json',
      data: data,
      success: success
    }

    $.ajax(options);

  }

}
