/**
 * Copyright by netlands edv consulting GmbH 2010-
 * $Id: jquery.imgrotator.js 1171 2010-08-12 07:33:09Z abo $
 */
;(function($) {

function include(ar, value) {
    var found = false;
    $.each(ar, function(index,arval) {
        if (arval == value) {
            found = true;
            return false;
        }
    });
    return found;
}


function ImgRotator(bannerContainer, conf) {
    var self = this;
    self.conf = conf;
    var zindex = 10;
	var currentIndex = 0;
    var banners = $.map($(bannerContainer).find('.'+conf.bannerClass) , function(banner, index) {
                        if(conf.debug) console.log(banner, index);
                        if(index>0) {
                            $(banner).hide();
                        }
                        $(banner).data('index', index);
                        return $(banner);});
    if(conf.debug) console.log(banners);
    if(banners.length <= 1) return self;
    var randomBannersLeft = [];
    var previousBanner = banners[0];
    var previousBanners = [];
    $.extend(self, {
        getNextBanner : function() {
	        var size = banners.length;
		    var newIndex = (currentIndex + 1) % size;
		    currentIndex = newIndex;
            var banner = banners[currentIndex];
		    if(self.conf.debug) console.log($(previousBanner).data('index') + ' => ' + $(banner).data('index'));
            return banners[newIndex];
	    },
	    getNextBannerRandom  : function() {
            if(randomBannersLeft.length==0) randomBannersLeft = $.extend(true, [], banners);
	        var newIndex = Math.round(Math.random() * (randomBannersLeft.length-1));
            var banner = randomBannersLeft[newIndex];
            while(include(previousBanners, banner)) {
                newIndex = Math.round(Math.random() * (randomBannersLeft.length-1));
                banner = randomBannersLeft[newIndex];
            }
            randomBannersLeft.splice(newIndex,1);
		    if(self.conf.debug) console.log($(previousBanner).data('index') + ' => ' + $(banner).data('index'));
		    currentIndex = $(banner).data('index');
            if(previousBanners.length==conf.bannerPreviousLength) {
                previousBanners.shift();
            }
            previousBanners.push(banner);
            return banner;
	    },
        rotate : function(noeffect) {
            var bottom = null;
		    if(self.conf.BANNER_CHANGE_RANDOM)
		        bottom = self.getNextBannerRandom();
		    else
		        bottom = self.getNextBanner();
            bottom.hide();
            bottom.css('z-index', zindex++);
            var hideBanner = previousBanner;
            if(noeffect) {
                hideBanner.hide();
                bottom.show();
            }
            else {
                if(hideBanner) hideBanner.fadeOut(conf.BANNER_FADE_TIME);
                bottom.fadeIn(self.conf.BANNER_FADE_TIME, function() {
                              if(hideBanner) {
                                  hideBanner.hide();
                              }
                          });
            }
            previousBanner = bottom;
        }
    });
    if(conf.BANNER_CHANGE_RANDOM) { // gleich das erste auswechseln
        self.rotate(true);
    }
    self.interval = setInterval(self.rotate, conf.BANNER_CHANGE_TIMEOUT);
    return self;
};
var $conf = {
    bannerClass : 'banneritem',
    BANNER_CHANGE_TIMEOUT : 7000,
    BANNER_CHANGE_RANDOM : false,
    bannerPreviousLength : 3,
    BANNER_FADE_TIME : 1000,
    debug:false,
    attWatch : ['untertitel']
};

$.fn.imgRotator = function(conf, key,value) {
    var api = null;
    if($(this).data('imgRotator')) {
        api = $(this).data('imgRotator');
        if(conf=='option') {
            if(typeof value != undefined) {
                api.conf[key] = value;
                if(api.conf.debug) console.log(key + ' => ' + value, api.conf);
                return value;
            }
            else {
                return api.conf[key];
            }
        }
    }
    else {
        conf = $.extend({}, $conf, conf);
        var bannerContainer = $(this);
        bannerContainer.find('img.banner').each(function(index,el) {
            var img = $(el).detach();
            var title = img.attr('title');
            conf.bannerTemplate = conf.bannerTemplate || '<div class="' + conf.bannerClass + '"></div>';
            var banner = $(conf.bannerTemplate);
            banner.append(img);
            banner.append('<p class="title">' + title + '</p>');
            $.each(conf.attWatch, function(i, attrName) {
		               var val = img.attr(attrName);
			           if(val) {
				           banner.append('<span class="'+attrName+'">' + val + '</span>');
			           }
            });
            bannerContainer.append(banner);
        });
        api = new ImgRotator(bannerContainer,conf);
        $(this).data('imgRotator', api);
    }
    return $(this);

};

})(jQuery);
