var $$ = $.fn;

$$.extend({
    SplitID: function() {
        return this.attr('id').split('-').pop();
    },

    Slideshow: {
        Ready: function() {
            $('div.blockMediaList')
                    .hover(
                      function() {
                          $(this).addClass('hoverSelected');
                      },
                      function() {
                          $(this).removeClass('hoverSelected');
                      }
                    )
        .click(
          function() {
              $$.Slideshow.Interrupted = true;

              $('div.blockMainMultimediaContent').hide();
              var str = $(this)[0].className;
              if (str.lastIndexOf(" selected") > -1) {
                  $('div#blockMainMultimediaContent-' + $(this).SplitID()).show();
                  window.location = $(this).children().children("a").url();
              }
              else {
                  $(this).parent().children().removeClass("selected");
                  $(this).parent().children().removeClass("hoverSelected");
                  $(this).parent().children().addClass("noBg");
                  $(this).addClass('selected');
                  $(this).removeClass("noBg");
                  $('div#blockMainMultimediaContent-' + $(this).SplitID()).show()
              }
          }
        );
            this.Counter = 1;
            this.Interrupted = false;
            this.Transition();
        },

        Transition: function() {
            if (this.Interrupted) {
                return;
            }

            this.Last = this.Counter - 1;

            if (this.Last < 1) {
                this.Last = $('div.blockMediaList').length;
            }

            $('div#blockMainMultimediaContent-' + this.Last).fadeOut(
        'fast',
        function() {
            // remove last selection
            $('div#blockMediaList-' + $$.Slideshow.Last).removeClass('selected');
            $('div#blockMediaList-' + $$.Slideshow.Last).removeClass('hoverSelected');
            $('div#blockMediaList-' + $$.Slideshow.Last).addClass('noBg');

            // selection
            $('div#blockMediaList-' + $$.Slideshow.Counter).addClass('selected');
            $('div#blockMediaList-' + $$.Slideshow.Counter).removeClass('hoverSelected');
            $('div#blockMediaList-' + $$.Slideshow.Counter).removeClass('noBg');

            $('div#blockMainMultimediaContent-' + $$.Slideshow.Counter).fadeIn('fast');
            $$.Slideshow.Counter++;

            if ($$.Slideshow.Counter > $('div.blockMediaList').length) {
                $$.Slideshow.Counter = 1;
            }
            setTimeout('$$.Slideshow.Transition();', 5000);
        }
      );
        }
    }
});

$(document).ready(
  function() {
      $$.Slideshow.Ready();
  }
)
