if(!Utils) throw('NewsProjector betoltesehez hianyzik: Utils');

var NewsProjector = {
	container : 'news_container',
	buttonContainer : null,
	leadContainer : null,
	leadContent : null,
	leadHeader : null,
	leadText : null,
	imageContainer: null,

	width : 0,
	height : 0,
	buttonSize : 14,
	buttonMargin : 6,
	buttonBg : 'url(images/bg_np_butoncontainer.png)',
	leadHeight : 120,
	leadPadding : 20,
	leadBg : '#0F7820',//'url(images/bg_mainlead.png)',

	news : [],
	current : 0,
	previous : -1,
	autorot : true,
	delay : 5000,

	timeout : null,
	timeoutShow : null,
	timeoutHide : null,
	timeoutLeadShow : null,
	timeoutLeadHide : null,
	timeoutImage : null,
	buttonOpac : 0,
	buttonOpacMax : 80,
	buttonOpacDif : 10,
	leadOpac : 0,
	leadOpacMax : (Utils.isIE)?70:80,
	leadOpacDif : 10,
	imageOpac : 0,
	imageOpacMax : 100,
	imageOpacDif : 10,

	init : function(){
		this.container=$(this.container);
		this.width=this.container.offsetWidth-2;	// -2 a bal- és jobboldali keret miatt van
		this.height=this.container.offsetHeight-1;	// -1 az alsó keret miatt van

		Utils.addListener(this.container,'mouseover',this.mouseOver);
		Utils.addListener(this.container,'mouseout',this.mouseOut);

		if(ImgPreloader){
			var images=[];
			for(var i=0;i<this.news.length;i++) if(this.news[i].image) images[images.length]=this.news[i].image;
			ImgPreloader.preLoad(images);
		}

		this.create();
	},

	create : function(){
		if(this.container){
			Utils.setStyle(this.container,{'position':'relative'});

			this.imageContainer = Ł('div',{'id':'np_imageContainer'},{
				'width'		: this.width+'px',
				'height'	: this.height+'px'
			});

			this.buttonContainer = Ł('div',{'id':'np_buttonContainer'},{
				'width'		: this.width+'px',
				'height'	: (this.buttonSize+2*this.buttonMargin+2)+'px',
				'position'	: 'absolute',
				'top'		: '0px',
				'left'		: '0px',
				'background': this.buttonBg,
				'clear'		: 'right'
			});
			Utils.changeOpac(this.buttonContainer,this.buttonOpac);

			this.leadContainer = Ł('div',{'id':'np_leadContainer'},{
				'width'		: (this.width-2*this.leadPadding)+'px',
				'height'	: (this.leadHeight-2*this.leadPadding)+'px',
				'position'	: 'absolute',
				'top'		: (this.height-this.leadHeight)+'px',
				'left'		: '0px',
				'background': this.leadBg,
				'padding'	: this.leadPadding+'px'
			});
			Utils.changeOpac(this.leadContainer,this.leadOpac);

			this.leadContent = Ł('div',{'id':'np_leadContent'},{
				'width'		: (this.width-2*this.leadPadding)+'px',
				'height'	: (this.leadHeight-2*this.leadPadding)+'px',
				'position'	: 'relative',
				'top'		: '0px',
				'left'		: '0px',
			});

			this.leadHeader = Ł('h1',{'id':'np_leadHeader'},{
				'width'		: (this.width-2*this.leadPadding-20)+'px',
				'margin'	: '5px 10px 10px 10px',
				'fontSize'	: '18px'
			});
			this.leadText = Ł('p',{'id':'np_leadText'},{
				'width'		: (this.width-2*this.leadPadding-10)+'px',
				'margin'	: '5px',
				'fontSize'	: '14px'
			});

			Utils.appendChild(this.container,
				this.imageContainer,
				this.buttonContainer,
				Utils.appendChild(this.leadContainer,
					Utils.appendChild(this.leadContent,
						this.leadHeader,
						this.leadText
					)
				)
			);

			this.displayButtons();
			this.loadImages();
			this.loadNews(this.current);
		}
	},

	displayButtons : function(){
		for(var i=this.news.length-1;i>=0;i--){
			Utils.appendChild(this.buttonContainer,this.createButton(i))
		}
	},

	loadNews : function(i){
		Utils.removeChildren(this.leadHeader);
		Utils.removeChildren(this.leadText);

		Utils.appendChild(this.leadHeader,
			document.createTextNode(this.news[i].title)
		);
		Utils.appendChild(this.leadText,
			document.createTextNode(this.news[i].lead)
		);

		Utils.setStyle($('btn_newsprojector_'+i),{'background':'green'});
		this.showLead();
		this.changeImage();
	},

	loadImages : function(){
		var image;
		for(var i=0;i<this.news.length;i++){
			image=Ł('img',{
				'id'		: 'np_image_'+i,
				'src'		: this.news[i].image,
				'newsID'	: this.news[i].newsID,
			},{
				'position'	: 'absolute',
				'top'		: '0px',
				'left'		: '0px',
				'cursor'	: 'pointer',
				'display'	: 'none',
				'width'		: this.width+'px',
				'height'	: this.height+'px'
			});

			Utils.addListener(image,'click',function(e){
				var ed=Utils.getEventDetails(e);
				window.location.href='index.php?pg=hirek&newsID='+ed.target.getAttribute('newsID');
			});
			//Utils.addListener(image,'load',this.imageLoaded);
			//Utils.addListener(image,'mouseover',this.imageMouseOver);
			//Utils.addListener(image,'mouseout',this.imageMouseOut);

			Utils.appendChild(this.imageContainer,image);
		}
	},

	changeNews : function(e){
		var ed=Utils.getEventDetails(e);
		var idx=ed.target.getAttribute('idx');

		if(idx&&(idx!=NewsProjector.current)){
			Utils.setStyle($('btn_newsprojector_'+NewsProjector.current),{'background':'#fff'});
			NewsProjector.previous=NewsProjector.current;
			NewsProjector.current=idx;
			NewsProjector.hideLead();
		}
	},

	rotateNews : function(){
		Utils.setStyle($('btn_newsprojector_'+this.current),{'background':'#fff'});
		this.previous=this.current;
		this.current++;
		if(this.current>=this.news.length) this.current=0;
		this.hideLead();
	},

	add : function(news){
		if(news instanceof Array) for(var i=0;i<news.length;i++) this.news[this.news.length]=news[i];
		else this.news[this.news.length]=news;
	},

	createButton : function(idx){
		var button=Ł('div',{'id':'btn_newsprojector_'+idx,'idx':idx},{
			'width'		: this.buttonSize+'px',
			'height'	: this.buttonSize+'px',
			'margin'	: this.buttonMargin+'px',
			'float'		: 'right',
			'background': '#fff',
			'border'	: 'solid 1px rgb(44,158,9)',
			'color'		: 'rgb(44,158,9)',
			'textAlign'	: 'center',
			'fontWeight': 'bold',
			'fontSize'	: '11px',
			'cursor'	: 'pointer'
		});
		Utils.appendChild(button,document.createTextNode(idx+1));
		Utils.addListener(button,'click',this.changeNews);
		Utils.addListener(button,'mouseover',function(e){
			var ed=Utils.getEventDetails(e);
			Utils.setStyle(ed.target,{'borderColor':'red'});
		});
		Utils.addListener(button,'mouseout',function(e){
			var ed=Utils.getEventDetails(e);
			Utils.setStyle(ed.target,{'borderColor':'rgb(44,158,9)'});
		});
		return button;
	},

	showLead : function(){
		if(this.leadOpac+this.leadOpacDif<this.leadOpacMax) this.leadOpac+=this.leadOpacDif;
		else this.leadOpac=this.leadOpacMax;

		Utils.changeOpac(this.leadContainer,this.leadOpac);

		if(this.leadOpac<this.leadOpacMax) this.timeoutLeadShow=setTimeout('NewsProjector.showLead();',20);
		else if(this.autorot) this.timeout=setTimeout('NewsProjector.rotateNews();',this.delay);
	},

	hideLead : function(){
		if(this.timeoutLeadShow) clearTimeout(this.timeoutLeadShow);
		if(this.leadOpac-this.leadOpacDif>0) this.leadOpac-=this.leadOpacDif;
		else this.leadOpac=0;

		Utils.changeOpac(this.leadContainer,this.leadOpac);

		if(this.leadOpac>0) this.timeoutLeadHide=setTimeout('NewsProjector.hideLead();',20);
		else this.loadNews(this.current);
	},

	showButtons : function(){
		if(this.buttonOpac+this.buttonOpacDif<this.buttonOpacMax) this.buttonOpac+=this.buttonOpacDif;
		else this.buttonOpac=this.buttonOpacMax;

		Utils.changeOpac(this.buttonContainer,this.buttonOpac);

		if(this.buttonOpac<this.buttonOpacMax) this.timeoutShow=setTimeout('NewsProjector.showButtons();',20);
	},

	hideButtons : function(){
		if(this.timeoutShow) clearTimeout(this.timeoutShow);
		if(this.buttonOpac-this.buttonOpacDif>0) this.buttonOpac-=this.buttonOpacDif;
		else this.buttonOpac=0;

		Utils.changeOpac(this.buttonContainer,this.buttonOpac);

		if(this.buttonOpac>0) this.timeoutHide=setTimeout('NewsProjector.hideButtons();',20);
	},

	changeImage : function(){
		if(this.imageOpac+this.imageOpacDif<this.imageOpacMax) this.imageOpac+=this.imageOpacDif;
		else this.imageOpac=this.imageOpacMax;

		if(this.previous>-1){
			var imgPrevious=$('np_image_'+this.previous);
			if(imgPrevious) Utils.changeOpac(imgPrevious,this.imageOpacMax-this.imageOpac);
		}

		var imgCurrent=$('np_image_'+this.current);
		if(imgCurrent){
			if(imgCurrent.style.display!='block') Utils.setStyle(imgCurrent,{'display':'block'});
			Utils.changeOpac(imgCurrent,this.imageOpac);
		}

		if(this.imageOpac<this.imageOpacMax) this.timeoutImage=setTimeout('NewsProjector.changeImage();',50);
		else {
			this.imageOpac=0;
			if(imgPrevious) Utils.setStyle(imgPrevious,{'display':'none'});
		}
	},

	isMouseOver : function(e){
		var ed=Utils.getEventDetails(e);
		var pos=Utils.getPosition(this.container);

		return ((ed.mouseX>pos.left)&&(ed.mouseX<(pos.left+this.width))&&(ed.mouseY>pos.top)&&(ed.mouseY<(pos.top+this.height)));
	},

	mouseOver : function(e){
		if(NewsProjector.timeout) clearTimeout(NewsProjector.timeout);
		NewsProjector.showButtons();
	},

	mouseOut : function(e){
		if(!NewsProjector.isMouseOver(e)){
			NewsProjector.hideButtons();
			if(NewsProjector.autorot) NewsProjector.timeout=setTimeout('NewsProjector.rotateNews();',NewsProjector.delay);
		}
	}
}

Utils.addInit('NewsProjector.init');
