var NEWS = {};
var ileNews = parseInt(document.getElementById('ileNews').value);
var box = document.getElementById('box');
var newsW = 918;
var actNews=0;
//NAVIGATE
var lewaBtn = document.getElementById('lewaBtn');
var prawaBtn = document.getElementById('prawaBtn');
box.onmouseover = function(){
	lewaBtn.style.zIndex=2;
	prawaBtn.style.zIndex=2;
}
box.onmouseout = function(){
	prawaBtn.style.zIndex=-1;
	lewaBtn.style.zIndex=-1;
}


//********
for(var i=0; i<ileNews; i++){
	NEWS[i] = document.createElement('div');
	box.appendChild(NEWS[i]);
	NEWS[i].className='news';
	NEWS[i].style.left = (i * newsW) + 'px';
	NEWS[i].style.backgroundImage="url('" + 
		document.getElementById('News'+i+'i').value + "')";
}



//*************
//FUNKCJE ANIMACJA
function valueN(){
	
	var str ='';
	var table = new Array(ileNews);
	for (var i = 0; i < ileNews; i++) {
		var wCH = NEWS[i].style.left.indexOf('px');
		for (var j = 0; j < wCH; j++) {
			str += NEWS[i].style.left.charAt(j);
		}
		table[i]=parseInt(str);
		str='';
	}
	return table;
}
function getdPos(){
	return dPos;
}
function getactNews(){
	var VN = valueN();
	if(VN[0]==0){
		actNews=0;
		return actNews;
	}
	for (var i = 0; i < ileNews; i++) {
		if (VN[0]<=-(((i-1)*newsW)+1)&&VN[0]>=-(i*newsW)) {
			actNews = i;
			return actNews;
		}
	}
}


//SHOW CLOSE
//suwajka*****************************************************
//*************************************************************
//dane z input:
var infoboxTtable = new Array(ileNews);
for(var i=0; i<ileNews; i++){
	infoboxTtable[i] = document.getElementById('News'+i+'t').value;
}
//suwajka*****************************************************
var infobox = document.getElementById('infobox');
infobox.style.width = newsW + 'px';
var infoboxT = document.createTextNode('');
infobox.appendChild(infoboxT);
infobox.style.bottom=-20+'px';
infobox.innerHTML = infoboxTtable[actNews];

//TIMERY!!!!
var timerC = setInterval(prawaIntClick,6000);
var czytimer = true;
//***********


//FUNCKCJE ANIMACJI*******************************************
var speedb = 10;
if( navigator.appName == "Opera" )speedb = 5;
var refreshb = 1;
if( navigator.appName == "Opera" )refreshd = 1;
function getPx(){
	var wch = infobox.style.bottom.indexOf('px');
	var px = '';
	for (var i=0; i<wch; i++) {
		px += infobox.style.bottom.charAt(i);
	}
	return parseInt(px);
}


function show_a(){
	var px = getPx();
	if(px==0){
		if(!czytimer){
			timerC = setInterval(prawaIntClick,6000);
			czytimer = true;
		}
		hide=false;
		return;
	}
	infobox.style.bottom = ((px + speedb) + 'px');
	setTimeout('show_a();',refreshb);
}

function hide_a(){
	var px = getPx();
	if (px == -80) {
		
		hide=true;
		return;
	}
	infobox.style.bottom = ((px - speedb) + 'px');
	setTimeout('hide_a();',refreshb);
}

show_a();

//*************************************************************

//*************************************************************

//**********
var animacje=0;

//DANE ANIMACJA
var animationStep = 54;
if( navigator.appName == "Opera" )animationStep = 54;
 //TABLICAAAAAAAAAAAA
var refresh = 1;
if( navigator.appName == "Opera" )refresh = 10;
var dPos = 0;

//*****************
//anime_l() - jedynka w lewo
//anime_r() - jedynka w prawo
//NAWIGACJA
var animuje = false;
function anime(){
	animuje=true;
	animacje++
	var VN = valueN();
	if(VN[0]==-(dPos*(newsW))){
		animacje--
		if(animacje==0){
			getactNews();
			infobox.innerHTML = infoboxTtable[actNews];
			show_a();
			
		}
		clearTimeout(timer);
		animuje=false;
		getactNews();
		return;
	}
	if (VN[0] < -(dPos*newsW)) {
		for (var i = 0; i < ileNews; i++) {
			NEWS[i].style.left = (VN[i] + animationStep) + 'px'
		}
	}
	if (VN[0] > -(dPos*newsW)) {
		for (var i = 0; i < ileNews; i++) {
			NEWS[i].style.left = (VN[i] - animationStep) + 'px'
		}
	}
	animacje--
	timer = setTimeout(anime,refresh);
}
var hide=false;
function lewaBtnClick(){
	clearInterval(timerC);
	czytimer=false
	if(dPos==0){
		dPos=ileNews-1;
	}
	else{
		dPos--;
	}
	if(hide==false)hide_a();
	if(!animuje)anime();
}
lewaBtn.onclick = lewaBtnClick;
function prawaBtnClick(){
	clearInterval(timerC);
	czytimer=false;
	if(dPos==ileNews-1){
		dPos=0;
	}
	else{		
		dPos++;
	}
	if(hide==false)hide_a();
	if(!animuje)anime();
	
}
prawaBtn.onclick = prawaBtnClick;
function prawaIntClick(){
	if(dPos==ileNews-1){
		dPos=0;
	}
	else{		
		dPos++;
	}
	if(hide==false)hide_a();
	if(!animuje)anime();
}



