var did_you_know = new Class({
	initialize: function(element, options) {
		this.setOptions({
			elementSelector: "div.d_y_k_item",
			useThemes: false,
			themeSelector: "span.d_theme",
			contentSelector: "div.d_content",
			themeElement: "top_theme",
			currentElement:"d_y_k_current",
			contentElement:"d_y_k_inside",
			populateFrom: 'did_you_know',
			prevElement: 'd_y_k_prev',
			nextElement : 'd_y_k_next',
			topElement : 'd_y_k_top',
			bottomElement : 'd_y_k_bottom',
			scrollContent: false,
			showCurrentNum: false
		}, options);
		
		this.aData = Array();
		this.populateFrom = $(this.options.populateFrom);
		this.currentChosen=0;
		this.presenterInit = 1;
		
			if(this.options.useThemes)
			this.themeElement = $(this.options.themeElement);
			if(this.options.showCurrentNum)	
			this.currentElement = $(this.options.currentElement);
		this.contentElement = $(this.options.contentElement);
		this.prevElement = $(this.options.prevElement);
		this.nextElement = $(this.options.nextElement);
		
		this.topElement = $(this.options.topElement);
		this.bottomElement = $(this.options.bottomElement);		

		this.currentIter = 0;
		
		this.populateData();
		
		this.doInit();
		
		
		this.maxIter = this.aData.length;
		
		this.changeActivity();
		
		if(this.options.scrollContent)
		this.changeScrollersActivity();
	},
	
	populateData: function() {
		currentArrayPlace = this.aData.length;
		options = this.options;
		var data = $A(this.aData);
		data.extend(this.populatePresenter(this.populateFrom, currentArrayPlace));
		this.aData = data;
	},
	populatePresenter: function(element, startNumber) {
		var data = [];
		options = this.options;
		currentArrayPlace = startNumber;
		element.getElements(options.elementSelector).each(function(el) {
		if(options.useThemes)	 {													   
			elementDict = {
				theme: el.getElement(options.themeSelector).innerHTML,
				content: el.getElement(options.contentSelector).innerHTML
				};
		}
		else		{
			elementDict = {
				content: el.getElement(options.contentSelector).innerHTML
				};
		}
			data.extend([elementDict]);
			currentArrayPlace++;
			if (this.options.destroyAfterPopulate)
				el.remove();
		});
		return data;
	},
	doInit: function() {
		
		this.nextElement.addEvent('click',this.nextItem.bind(this));
		this.prevElement.addEvent('click',this.prevItem.bind(this));
		
		if (this.presenterInit == 1)
		{
			if(this.options.useThemes)
			this.themeElement.addClass('theme_' + this.aData[this.currentIter].theme);	
	
	this.contentElement.innerHTML = this.aData[this.currentIter].content;
	
	if(this.options.showCurrentNum)
		this.currentElement.innerHTML = 1;
	//this.changeCurrent(0);
		this.onload();
		}
	},
	nextItem: function() {
		if( this.currentIter+1 < this.maxIter) {
		this.nextIter = this.currentIter+1;
		if (this.nextIter >= this.maxIter)
			this.nextIter = 0;
		this.galleryInit = 0;
		this.goTo(this.nextIter);
		}
	},
	prevItem: function() {
		
		if(this.currentIter-1 >= 0)
		{
		this.nextIter = this.currentIter-1;
		if (this.nextIter <= -1)
			this.nextIter = this.maxIter - 1;
		this.galleryInit = 0;
		this.goTo(this.nextIter);
		}
	},
	
	goTo: function(num) {

		this.onload();

			if(this.options.useThemes) {
			this.themeElement.setOpacity(0);
			var efekt = new Fx.Style(this.themeElement, 'opacity',{wait:true});
			this.themeElement.removeClass('theme_' + this.aData[this.currentIter].theme);
			}
		this.currentIter = num;
		
		if(this.options.useThemes) {
		this.themeElement.addClass('theme_' + this.aData[this.currentIter].theme);
		efekt.start(1);
		}
		
		this.contentElement.innerHTML = this.aData[this.currentIter].content;
			if(this.options.showCurrentNum)
		this.currentElement.innerHTML = this.currentIter+1;
		
		this.changeActivity();
		
		if(this.options.scrollContent)
		this.changeScrollersActivity();
		
		
	},
	
	changeActivity: function() {
 if(this.currentIter>0) {this.prevElement.removeClass('off');} else {this.prevElement.addClass('off');}
 if(this.currentIter<this.maxIter-1) {this.nextElement.removeClass('off');} else {this.nextElement.addClass('off'); }
 },
 changeScrollersActivity: function() {
	 
var size =  this.contentElement.getSize();
var sizeParent =  this.contentElement.getParent().getSize();
	 
	 
 if(sizeParent.scrollSize.y>=sizeParent.size.y) {
	 this.topElement.removeClass('top_off'); this.topElement.addClass('top');
	 this.bottomElement.addClass('bottom'); this.bottomElement.removeClass('bottom_off');
	 } else {
		 this.topElement.removeClass('top'); this.topElement.addClass('top_off');
	this.bottomElement.removeClass('bottom'); this.bottomElement.addClass('bottom_off');	 
		 
	}
 /*if(sizeParent.scrollSize.y<size.size.y) {this.bottomElement.removeClass('bottom_off'); this.bottomElement.addClass('bottom');} else {this.bottomElement.addClass('bottom'); this.bottomElement.addClass('bottom_off'); }*/
 },
 
 onload:function() {
	if(this.options.scrollContent) {
	this.przewijanie = new Fx.Scroll("d_y_k_content",{ wait: false, duration: 1500});
	$('d_y_k_content').setOpacity(0);
	}
	this.aktualna_pozycja = 0;
	//this.przewijanie.scrollTo(0,this.aktualna_pozycja);
	
if(this.options.scrollContent) {
		var ooo = $("d_y_k_content").getStyle('line-height').toInt();
	this.topElement.addEvent("click",function(){
		if(this.aktualna_pozycja > 0){
			this.aktualna_pozycja -= ooo;  
			this.przewijanie.scrollTo(0,this.aktualna_pozycja); 
		}
	}.bind(this));

	this.bottomElement.addEvent("click",function(){
	var wysokosc_diva = ($("d_y_k_content").getSize()).scrollSize.y;												 
		if((this.aktualna_pozycja+ooo) < wysokosc_diva){
			this.aktualna_pozycja += ooo;  
			this.przewijanie.scrollTo(0,this.aktualna_pozycja); 	
		}
	}.bind(this));
}
	new Fx.Style($('d_y_k_content'), 'opacity',{wait:true}).start(1);
 
 }
 
});


did_you_know.implement(new Events, new Options);
