﻿// JavaScript Document
var newsBox = new Class({
		initialize: function(o){
			this.container = o;
		//	this.textbox = o.getElement('dl');
			this.textbox = this.container.getElements('dl');
			this.title_height = this.container.getElements('.title');
			this.current_page = 0;
			this.time = 10000;
			this.news_pages = this.container.getElements('.one');
			this.pager_links = $('newsPager').getElements('a');
			this.pagerCount();
			this.pause();
			
			if(this.news_pages.length == 1){
				return;
				}else{
				var $this = this;
				this.pager_links[0].getParent('li').addClass('activ');
				this.flip();
				this.intervalID = this.autorun.periodical(this.time, this);
				this.pNext();
				this.pPrev();
				}
			//this.dimensions();

			//this.textSwich();
		},
		dimensions: function(){
			textbox = this.height(this.textbox);
			title_height = this.height(this.title_height);
			Obj = textbox.associate(title_height);
			return Obj;
		},
// zwraca tablice z wysokoscia kontenera i tytulu		
		height: function(el){
				var array = new Array();
				el.each( function(index, val){
					height = index.getSize();
					array.push( height.y);
				});
				return array;
		},
		textSwich: function(){
					this.textbox_visible = this.container.getElement('.show').getElement('dl');
					this.toggler = this.textbox_visible.getElement('.topnews_swich');
					myFx = new Fx.Tween(this.textbox_visible);
					var height = this.textbox_visible.getSize();
					height = height.y - 30;
					this.toggler.addEvent('click', function(){
						if( this.textbox_visible.hasClass('topnews_hidden')){
								 myFx.start('bottom',0);
								 this.textbox_visible.removeClass('topnews_hidden');
								 this.toggler.setStyle('background-position','top');
							 }
						else {
								 myFx.start('bottom',-height);
								 this.textbox_visible.addClass('topnews_hidden');
								 this.toggler.setStyle('background-position',[0, -6]);
						}
					}.bind(this));
		},

		flip: function(){
			this.pager_links =$('newsPager').getElements('li');
			this.news_pages =this.news_pages;
			this.pager_links.each(function(index, val){
										index.addEvent('click', function(e){
										e.stop();
										page = index.get('num');
										
										
										this.current_page = parseInt(page);
										if(page == this.pager_links.length){
											this.current_page = 1;
											}
										
											
										this.show(page);
										$clear(this.intervalID);
										
										this.intervalID = this.autorun.periodical(this.time, this);
									}.bind(this));
										
								}.bind(this));
		},
		autorun: function(){		
			this.current_page++;
			var el = this.news_pages;
			if(this.current_page>=el.length)this.current_page = 0;
			
			this.show(this.current_page); 
		},
		pNext: function(){	
			var el = this.news_pages;
		
			$('box_next').addEvent('click', function(e){
				this.current_page++;
				e.stop();
				if(this.current_page>=el.length)this.current_page = 0;
				this.show(this.current_page); 
				
			 }.bind(this));
			
		},
		pPrev: function(){	
			var el = this.news_pages;
		
			$('box_prev').addEvent('click', function(e){
				this.current_page--;
				e.stop();
				if(this.current_page<0){
					this.current_page = el.length-1;
				}
				this.show(this.current_page); 
				
			 }.bind(this));
			
		},
		show: function(el){
			this.pager_links.each( function(index, val){
								index.removeClass('activ');
								});
			this.pager_links[el].addClass('activ');
			this.news_pages =this.news_pages;
			this.news_pages.each( function(index,val){
										   index.addClass('hide');
										   index.setStyle('visibility','hidden');
							   });

			this.news_pages[el].removeClass('hide');
			myFx = new Fx.Tween(this.news_pages[el], {duration:'short'});
			myFx.start('opacity',[0],[1]);
			this.news_pages[el].addClass('show');
		},
		pagerCount: function(){
			this.pager_links.each( function(index, val){
					index.getParent('li').set('num',val);
					
			});
		},
		pause: function(){
			this.container.addEvent('mouseover', function(){
										$clear(this.intervalID);
				//						this.intervalID = this.autorun.periodical(this.time, this);
	        }.bind(this));
			this.container.addEvent('mouseout', function(){
										this.intervalID = this.autorun.periodical(this.time, this);
	        }.bind(this));
			
		}

});


// wywolanie
$(window).addEvent('domready', function(){
	
	if($('topnews')){
			
		var ile = $('topnews').getElements('.one').length;
		
		if( ile >1 ){
			//console.log(ile);
			var newsbox = new newsBox($('topnews'));
		}
	}
		
});
