function BannersSet(div_class, banners_set_data){
    this.data              = banners_set_data;
    this.div_class         = div_class;
    
    this.current_banner    = -1;
    this.banners_count     = 0;
    this.banners           = {};

    this.addBanner = function(banner_obj){
        this.banners[this.banners_count] = banner_obj;
        this.banners_count++;
        
        jQuery("<img>").attr("src", banner_obj.image_url);
        
    }

    this.getNextBanner = function(){        
        if (1 == this.banners_count) return 0;
        
        if (this.banners_count == this.current_banner + 1)
            return 0;
        else
            return this.current_banner + 1;
    }
    
    this.maxProductImgWidth = false;
    this.getMaxProductImgWidth = function(){
        if (false === this.maxProductImgWidth){
            var max_width = 0;
            for (var i = 0; i < this.banners_count; i++){
                if (undefined !== this.banners[i].product_data && this.banners[i].product_data.product_img_width > max_width){
                    max_width = this.banners[i].product_data.product_img_width;
                }
            }
            this.maxProductImgWidth = max_width;
        }
        return this.maxProductImgWidth;
    }
        

    this.rotateEffect = function(){
        this.current_banner = this.getNextBanner();

        var banner_div = jQuery('.' + this.div_class);
        var ob = this;
        switch (this.data.effect){
            case 'slide':
                banner_div.slideUp('slow', function(){
                    ob.rotate();
                    banner_div.slideDown();
                });
                break;
            case 'fade':
                banner_div.fadeOut('slow', function(){
                    ob.rotate();
                    banner_div.fadeIn();
                });
                break;
            case 'none':            
                ob.rotate();
                break;
        }

    }
    
    this.rotate = function(){
        var banner_div = jQuery('.' + this.div_class);
        
        var banner = this.banners[this.current_banner];

        var active_sections = banner.active_sections.split(',');
        if (jQuery.inArray('picture', active_sections) != -1){
            banner_div.find('.banner_image').attr('src', banner.image_url);
            banner_div.find('.bannel_link').attr('href', banner.image_link);
        } else {
            banner_div.find('.banner_image').attr('src', '');
        }
        
        if (jQuery.inArray('product', active_sections) != -1 && undefined != banner.product_data){
            banner_div.find('.banner_product_img').attr('src', banner.product_data.image_url);
            banner_div.find('.banner_product_img').css('display', '');
            banner_div.find('.banner_product_name').text(banner.product_data.name);
            banner_div.find('.banner_product_name').css('width', this.getMaxProductImgWidth() + 'px');
            banner_div.find('.banner_custom_text').text(banner.custom_text);
            banner_div.find('.product_link').attr('href', banner.product_link);
            
        } else {
            banner_div.find('.banner_product_img').attr('src', '');
            banner_div.find('.banner_product_img').css('display', 'none');
            banner_div.find('.banner_product_name').text('');
            banner_div.find('.banner_product_name').css('width', '0px');
            banner_div.find('.banner_custom_text').text('');
            banner_div.find('.product_link').attr('href', '');
        }
        
        if (jQuery.inArray('additional', active_sections) != -1){
            banner_div.find('.top_text').html(banner.top_text);
        }
    }
} 

BannersSets = function(){}; 
BannersSets.items = new Array();
BannersSets.addBannerSet = function(banners_set){
    BannersSets.items.push(banners_set);
}

BannersSets.startRotation = function(){
    for (var i = 0; i < BannersSets.items.length; i++){
            
        var handler = function(obj){
            return function(){
                obj.rotateEffect();
            }
        }(BannersSets.items[i]);
        handler();
        
        setInterval(
            handler,
            BannersSets.items[i].data.rotation_interval * 1000
        );
    
    }
}
function fluidBanners(id) {
	var height = 0;
	jQuery('#banner-' + id + ' img').each(function(index, child) {
		if(jQuery(child).outerHeight() > height) {
			height = jQuery(child).outerHeight();
		}
	});	
	jQuery('#banner-'+id+' .banner-item').height(height);
	jQuery('#banner-'+id).animate({height:height}, {queue:false, duration:500, easing:"easeInOutQuart"});
}

