// <![CDATA[
  // Delay response
  Ajax.Request.prototype.originalInitialize = Ajax.Request.prototype.initialize;
  Ajax.Request.prototype.initialize = function(url, options) {
    options.onSuccess = options.onSuccess.wrap(function(proceed, request, json) {
      proceed.curry(request, json).delay(1);
    });
    this.originalInitialize(url, options);
  }

  Ajax.Response.prototype._getHeaderJSON = function() {
      var nbElements = 4;
	  var responseObj = this.transport.responseText.evalJSON();

      var from = this.request.parameters.from;
      var to   = Math.min(nbElements, this.request.parameters.to);
      var html = $R(from, to).inject("", function(html, n) { 
      if (responseObj.images[n]) {
      	html += '<li onclick="loadPreview('+n+');"><img alt="Window" onclick="loadPreview('+n+');" src="' + responseObj.images[n] + '" />'; 
          }
          return html;
      });
   
	  return {html: html, from: from, to: to, more: to != nbElements};
  }

  var carousel = null;
  function runTest() {
    updateCarouselSize();
    carousel = new UI.Ajax.Carousel("horizontal_carousel", {url: "/portfolio/thumbs/", elementSize: 160})
  .observe("request:started", function() {
        //$('spinner').show().morph("opacity:0.8", {duration:0.5});
    })
  .observe("request:ended", function() {
       // $('spinner').morph("opacity:0", {duration:0.5, afterFinish: function(obj) { obj.element.hide(); }});
        });
  }

  function resized() {
    updateCarouselSize();  
    if (carousel)
      carousel.updateSize();
  }

  function updateCarouselSize() {
	    var dim = document.viewport.getDimensions(); 
	    dim.width -= 20;                             
    //$("horizontal_carousel").style.width = dim.width + "px";
//$$("#horizontal_carousel .container").first().style.width =  (dim.width - 100) + "px";
  }

  function loadPreview(previewId) {
	var url = '/portfolio/preview/id/' + previewId;

new Ajax.Request(url, {
  method: 'get',
  onSuccess: function(transport) {
    
    if (transport.responseJSON) {
      var res = transport.responseJSON;
	  $('preview-block').setStyle({background:'url(' + res.image + ') repeat-y scroll center bottom'});
	  $('preview-textin').innerHTML = res.text;
	      
	    }
	  }
	});
  }
  
  Event.observe(window, "load", runTest);
  Event.observe(window, "resize", resized);
// ]]>