var plg_guesthome_main_magazine_rotator = {
	options: {
		selectors: {
			container:			".jQ_magazineArticlesRotator",
			categoryButton:		".jQ_cat",
			categoryTeaser:		".jQ_teaser",
			categoryArticles:	".jQ_articles"
		},
		timeout: 5000,
		rotate: true
	},
	_interval: null,
	$: function( selector ){
		if( selector ){
			return this.$().find( selector );
		}
		return $(this.options.selectors.container);
	},
	init: function(){
		var me = this;
		
		function _rotate(){
			if( me.options.rotate ){
				me._lateRotate();
			}
		}
		
		_rotate();
		
		this.getCategories().click(function(){
			window.clearTimeout(me._interval);
			me.setActive( $(this) );
			_rotate();
			if( window['plg_page_pixels'] != undefined ){
				plg_page_pixels.redisplayPixels();
			}
		});
	},
	getCategories: function(){
		return this.$( this.options.selectors.categoryButton );
	},
	getArticles: function(){
		return this.$( this.options.selectors.categoryArticles );
	},
	getTeasers: function(){
		return this.$( this.options.selectors.categoryTeaser );
	},
	isActive: function( $cat ){
		return $cat.parent().parent().is(".act");
	},
	getActive: function(){
		var me = this;
		return this.getCategories().filter(function(){
			return me.isActive( $(this) );
		});
	},
	hasArticles: function( $cat ){
		return this.getArticles().filter("."+$cat.attr('rel')).size() > 0;
	},
	hasTeaser: function( $cat ){
		return this.getTeasers().filter("."+$cat.attr('rel')).size() > 0;
	},
	setActive: function( $cat ){
		var me = this;
		this.getCategories().each(function(){
			if( $cat.attr('rel') == $(this).attr('rel') ){
				$(this).parent().parent().addClass("act");
				me.$( "."+$(this).attr('rel') ).show();
			} else {
				$(this).parent().parent().removeClass("act");
				me.$( "."+$(this).attr('rel') ).hide();
			}
		});
	},
	next: function(){
		var a = 0;
		var me = this;
		var c = this.getCategories().size() - 1;
		var n=0;
		
		this.getCategories().each(function( i ){
			if( me.isActive( $(this) ) ){
				a = i;
			}
			
			n = a+1;
			
			if( a>=c ){
				a=0;
				n=0;
			}
		}).each(function(i){
			
			if( n == i ){
				me.setActive( $(this) );
				a = i;
			}
		});
	},
	_lateRotate: function(){
		var me = this;
		this._interval = window.setTimeout(function(){me.rotate();}, this.options.timeout );
	},
	rotate: function(){
		this.next();
		this._lateRotate();
	}
};

$(document).ready(function(){
	plg_guesthome_main_magazine_rotator.init();
});
