// 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 = parseFloat($('baner_rotation_a').innerHTML) * 1000;
			this.news_pages = this.container.getElements('.one');
			if(this.news_pages.length == 1){
				return;
				}else{
				this.pagerCreate();
				var $this = this;
				this.flip();
				this.intervalID = this.autorun.periodical(this.time, this);
				}
			//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));
		},
		pagerCreate: function(){
			
			this.news_pager = $('newsPager');
			
/* ilosc newsow*/
			this.news_pages =this.news_pages;
			this.news_pages.each( function(index, val){
					var pager_li = new Element('li',{
							'class':'dot',
							'id':'page_'+val,
							'num':val
					 });
					var pager_link = new Element('a',{
							'class':'dot',
							'href':val
					 });
					var pager_img = new Element('img',{
							'class':'dot',
							'src':'_images/dot.png',
							'class':'alpha'
					 });
					var pager_itm = pager_link.grab(pager_img);
					pager_itm = pager_li.grab(pager_itm);
					this.news_pager.grab(pager_itm);
// id dla newsa	
					index.set('id','news'+val);
					index.set('num',val)
			}.bind(this));
			
		},

		flip: function(){
			this.pager_links =$('newsPager').getElements('li');
			this.pager_links[0].getElement('img').set('src','_images/dot_act.png');
			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); 
		},
		
		show: function(el){
			this.pager_links =$('newsPager').getElements('img');
			this.pager_links.each( function(index, val){
								index.removeClass('activ');
								index.set('src','_images/dot.png');
								});
			this.pager_links[el].addClass('activ');
			this.pager_links[el].set('src','_images/dot_act.png');
			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]);
			myFx.start('opacity',[0],[1]);
			this.news_pages[el].addClass('show');
		}
});


// wywolanie
$(window).addEvent('domready', function(){
	if($('homeNews')){var newsbox = new newsBox($('homeNews'))};
	if($('topnews') && $$('.one').length>1){var newsbox = new newsBox($('topnews'))};
});