﻿
//var previous = 0;
//var inntimer = '';
var previous = new Array();
var inntimer = new Array();
(function($) {

  $.fn.innerfade = function(options, strNextEl,strPagineringEl,nrEl) {
    return this.each(function() {
      $.innerfade(this, options,strNextEl,strPagineringEl,nrEl);
    });
  };

  $.innerfade = function(container, options, strNextEl,strPagineringEl,nrEl) {
    var settings = {
      'animationtype': 'fade',
      'speed': 'normal',
      'type': 'sequence',
      'timeout': 2000,
      'containerheight': 'auto',
      'runningclass': 'innerfade',
      'children': null
    };
    previous[nrEl] = 0;
    inntimer[nrEl] = 0;
    
    if (options)
      $.extend(settings, options);
    if (settings.children === null)
      var elements = $(container).children();
    else
      var elements = $(container).children(settings.children);


    if (elements.length > 1) {

      $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
      for (var i = 0; i < elements.length; i++) {
        $(elements[i]).css('z-index', String(elements.length - i)).css('position', 'absolute').hide();
        $("#" + strPagineringEl).append("<a class='paginering_knop' href='javascript:void(null);' id='" + nrEl + i + "'>" + (i + 1) + "</a>");
        $('#' + nrEl + i).bind('click', function() {      
          $.innerfade.ganaar(elements, settings, this, strNextEl,strPagineringEl,nrEl);
          return false;
        });
      };
      //custom begin
      $("#" + strPagineringEl).append("<a class='paginering_knop' href='javascript:void(null);' id='" + strNextEl + "'>&rsaquo;</a>");
      $('#' + strNextEl).bind('click', function() {
        $.innerfade.ganaar(elements, settings, document.getElementById(nrEl + "1"),strNextEl,strPagineringEl,nrEl);
        return false;
      });
      //custom einde
      $('#' + nrEl + '0').addClass("first");
      $('#' + nrEl + '0').addClass("first_active");
      $('a[id^=' + nrEl + ']:last').addClass("last");

      $('#' + nrEl + '0').addClass("active");
      if (settings.type == "sequence") {
        inntimer[nrEl] = setTimeout(function() {
          $.innerfade.next(elements, settings, 1, 0, true, strNextEl,strPagineringEl,nrEl);
        }, settings.timeout);
        $(elements[0]).show();
      } else if (settings.type == "random") {
        var last = Math.floor(Math.random() * (elements.length));
        inntimer[nrEl] = setTimeout(function() {
          do {
            current = Math.floor(Math.random() * (elements.length));
          } while (last == current);
          $.innerfade.next(elements, settings, current, last,true, strNextEl,strPagineringEl,nrEl);
        }, settings.timeout);
        $(elements[last]).show();
      } else if (settings.type == 'random_start') {
        settings.type = 'sequence';
        var current = Math.floor(Math.random() * (elements.length));
        inntimer[nrEl] = setTimeout(function() {
          $.innerfade.next(elements, settings, (current + 1) % elements.length, current, true, strNextEl,strPagineringEl,nrEl);
        }, settings.timeout);
        $(elements[current]).show();
      } else {
        alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
      }
    }
  };

  $.innerfade.ganaar = function(elements, settings, el, strNextEl,strPagineringEl,nrEl) {
    i = parseInt(el.id.replace(nrEl, ""));
    current = i;

    last = previous[nrEl];
    if (current != last) {
      clearTimeout(inntimer[nrEl]);
      $.innerfade.next(elements, settings, current, last, true, strNextEl,strPagineringEl,nrEl);
    }
  }

  $.innerfade.next = function(elements, settings, current, last, faster, strNextEl,strPagineringEl,nrEl) {

    //  alert(current);
    for (var i = 0; i < elements.length; i++) {
      $('#' + nrEl + i).removeClass("active");
      $('#' + nrEl + i).removeClass("first_active");
      $('#' + nrEl + i).removeClass("last_active");
    }
    if (current == 0) {
      $('#' + nrEl + current).addClass("first_active");
    } else if (current == (($("#" + strPagineringEl + " a[id^=" + nrEl + "]").size()) - 1)) {
      $('#' + nrEl + current).addClass("last_active");
    } else {
      $('#' + nrEl + current).addClass("active");
    }

    if (settings.animationtype == 'slide') {
      $(elements[last]).slideUp(settings.speed);
      $(elements[current]).slideDown(settings.speed);
    } else if (settings.animationtype == 'fade') {
      $(elements[last]).fadeOut(settings.speed);
      $(elements[current]).fadeIn(settings.speed, function() {
        removeFilter($(this)[0]);
      });
    } else
      alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
    if (settings.type == "sequence") {
      if ((current + 1) < elements.length) {
        current = current + 1;
        last = current - 1;
      } else {
        current = 0;
        last = elements.length - 1;
      }
      //custom
      $('#' + strNextEl).unbind('click');
      $('#' + strNextEl).bind('click', function() {
        $.innerfade.ganaar(elements, settings, document.getElementById(nrEl + current),strNextEl,strPagineringEl,nrEl);
        return false;
      });
      //einde custom
    } else if (settings.type == "random") {
      last = current;
      while (current == last)
        current = Math.floor(Math.random() * elements.length);
    } else
      alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
    previous[nrEl] = last;
    inntimer[nrEl] = setTimeout((function() {
      $.innerfade.next(elements, settings, current, last, false, strNextEl,strPagineringEl,nrEl);
    }), settings.timeout);
  };

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
  try
  {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
	}catch(e){}
}

