  var shuffy;

  function initExhibitionSlideshow() {
    $.getJSON("/servlet/json",
        { method : "getExhibitions", type : "category", category : "", sort : 'Rating', max : 100
        }, gotTvExhibitionResult);
  }

  function addTvBannerSlideshow(uid) {
    $.getJSON("/servlet/json",
        { method : "getUserBanners", uid : uid, max : 100
        }, gotTvBannerResult);
  }

  var resultArray ;
  function gotTvExhibitionResult(data) {
    var html = '';

    if (data.result) {
      shuffy = fisherYatesShuffle(data.result);

      for (var i = 0; i < shuffy.length; i++) {
        var exhibition = shuffy[i] < 10 ? '0' + shuffy[i] : shuffy[i];
        if (exhibition && exhibition.mediumurl) {
            var url = '/exhibition/' + exhibition.safeTitle + '/' + exhibition.id;
            html += '<a class="tvimage" style="display:none" id="tvimage'+i+'" href="' + url +'"';
            html += ' title="' + exhibition.mediumurl + '"/>';
        }
      }
      $('#tv').append(html);
      $('#tv').dynamicSlideshow();
    } else {
      initDefaulttv();
    }
  }

  function gotTvBannerResult(data) {
    var html = '';

    if (data.result) {
      shuffy = fisherYatesShuffle(data.result);

      for (var i = 0; i < shuffy.length; i++) {
        var banner = shuffy[i] < 10 ? '0' + shuffy[i] : shuffy[i];
        if (banner && banner.imageUrl) {
            var url = banner.link;
            html += '<a class="tvimage" style="display:none" id="tvimage'+i+'" href="' + url +'"';
            html += ' title="' + banner.imageUrl + '"/>';
        }
      }
      $('#tv').append(html);
      $('#tv').dynamicSlideshow();
    } else {
      initDefaulttv();
    }
  }

  var cyclestarted = false;

  function fisherYatesShuffle ( myArray ) {
    var i = myArray.length;
    if ( i == 0 ) return false;
    while ( --i ) {
       var j = Math.floor( Math.random() * ( i + 1 ) );
       var tempi = myArray[i];
       var tempj = myArray[j];
       myArray[i] = tempj;
       myArray[j] = tempi;
     }
     return myArray;
  }

  function initDefaulttv() {
    var shuffy = new Array();
    for (var x=1;x<95;x++) {
      shuffy [x]= x;
    }
    shuffy = fisherYatesShuffle(shuffy);
    var html = '';
    for (var i=1; i<= 60; i++) {
      var tel = shuffy[i] < 10 ? '0' + shuffy[i] : shuffy[i];
      if (tel) {
        html += '<a class="tvimage defaulttv" style="display:none" name="" id="tvimage'+i+'" href="http://www.google.be" title="/img/slideshow/slideshow-' + tel + '.jpg"/>';
      }
    }
    $('#tv').append(html);
  }

  function resizeImage(img) {
    var maxwidth = 300;
    var maxheight = 250;
    var aspectRatio = 6/5

    try {
      $(img).css({"width": 'auto', "height": 'auto'});

      w = $(img).width();
      h = $(img).height();
      imgAspect = w/h;

      //alert("w :" + w + " h :" + h + " aspectratio:" + imgAspect  + " defaultAspect " + aspectRatio);
      if (aspectRatio <= imgAspect) {
        var delta = (maxheight - ((h*maxwidth/w))) / 2;
        $(img).css("margin-top", delta +"px");
        $(img).css("width", maxwidth +"px");
      } else {
        var delta = (maxwidth - ((w*maxheight/h))) / 2;
        $(img).css("margin-left", delta +"px");
        $(img).css("height", maxheight +"px");
      }
    } catch(ex) {
      //alert(ex);
    }
  }

function addTvBanner(bannerImg, link) {
    var html = '';
    if (link.length > 0) {
      html += '<a class="link" title="' + link + '" target="_new" >';
    }
    html += bannerImg;
    if (link.length > 0) {
      html += '</a>';
    }
    $('#tv').html(html);
    $('#tv img').removeAttr('width').removeAttr('height').addClass('tvimage');
    $('#tv img').load(function(){
      resizeImage(this);
    });
}


// dynamic slidesho
  jQuery.fn.dynamicSlideshow = function(attr) {
      attr = attr || {};
      attr.duration = attr.duration || 7000;

      function initSlider(container, img) {
        var curr = 2;
        setInterval( function(){
          if (curr == img.length) {
            curr = 0;
          }
          var i = new Image();
          $(i).load(function(){
            $(container).append(this);
            resizeImage(this);
            $(container).find('img:first').css({'z-index': 1});
            $(this).css({opacity: 0.0, 'z-index': 2}).animate({opacity: 1.0}, 1000, function() {
                $(container).find('img:first').remove();
              })
          }).attr('src', img[curr++]).attr('title', img[curr++]).css({'z-index':8});
          $(i).click(function () {
          if ($(this).attr('title')) {
            document.location = $(this).attr('title');
          }
        }).addClass('tvimage');;
          
        }, attr.duration );
      };

      $(this).each(function(){
        var img = [];
        $(this).find("a").each(
          function(){
            img.push($(this).attr("title"));
            img.push($(this).attr("href"));
          //$(img).attr('title', $(this).attr("title"));
          });
        var j = new Image();
        var container = this;
        $(this).empty();

        $(j).attr('src', img[0]).attr('title', img[1]).css({'z-index':0}).load(function(){
          $(container).append(this);
          resizeImage(this);
          initSlider(container, img);
        }).click(function () {
          if ($(this).attr('title')) {
            document.location = $(this).attr('title');
          }
        }).addClass('tvimage');
      });
    }

