var MaxData = 8;
var idData = new Array(MaxData);
var ColLeftBoxes = 0;
var ColRightBoxes = 0;
var myArray = new Array();
var myArrayTitle = new Array();
var myArrayCdown = new Array();
var BoxesAll = new Array("ColLeftBoxes", "ColRightBoxes", "ColLeftBoxes2", "ColRightBoxes2");
var BoxesAllid = new Array("l", "r", "l2", "r2");
var ActId = 0;
function nav_init(){
	cmDraw ('myMenuID', myMenu, 'hbr',prop);
}

function WindowOnload(f) 
{
	var prev=window.onload;
	window.onload=function(){
		if(prev)prev(); f();
		Random_Box("rnd1", "first", 3);
	}
}
function requestCustomerInfo(div) {
	if(ActId >=0 && ActId < MaxData){
		var temp = $(div + ActId);
		idData[ActId] = temp.innerHTML;
		ActId++;
		if(ActId < MaxData)requestCustomerInfo(div);
	}
	else{
		alert("Betöltés kész!");
	}
}

function ajaxCustomerInfo() {
	if (ajaxRequest.readyState != 4) {
		 return;
	}
	//alert(ajaxRequest.responseText);
	idData[ActId] = ajaxRequest.responseText;
	ActId++;
	if(ActId < MaxData)requestCustomerInfo();
}

// Dobozokhoz a timer
function Counter(divID, start, intervall, from, num, isbig){
	//this.divID = divID;
	this.mydiv = $(divID);
	this.navdiv = $(divID + 'nav');
	this.mybutton = $(divID + 'p');
	this.bigctrl = isbig;
	// this.noControl = noControl;
	// this.count = start;
	this.count = from;
	this.intervall = intervall;
	this.from = from;
	this.num = num;
	var oThis = this;
	oThis.doTimer();
	Event.observe(divID + 'b', 'click', function(event) { oThis.step(-1); });
	Event.observe(divID + 'p', 'click', function(event) { oThis.go(); });
	Event.observe(divID + 'f', 'click', function(event) { oThis.step(1); });

}	
Counter.prototype = {
	aktiv:	0,
	next: 	function(){
			this.doTimer();
			this.count = Math.abs(this.count+1-this.from)%this.num+this.from;
			this.setbox();
	},
	doTimer:function(){
			this.aktiv = setTimeout(this.next.bind(this), this.intervall);
	},
	setbox:	function(){
			if(idData[this.count] != undefined){
				this.mydiv.innerHTML = idData[this.count];
				// if(this.bigctrl == "")this.navdiv.innerHTML  = (this.num) + '/' + (this.count-this.from+1);
			}				
	},
	step:	function(num){
			clearTimeout(this.aktiv);
			this.aktiv = 0;
			// this.mybutton.className = this.bigctrl + 'bpause_off';
			this.mybutton.className = 'bpause_off';
			var counter = (num > 0 ? num : (this.count==this.from ? this.num+num : num));
			this.count = Math.abs(this.count+counter-this.from)%this.num+this.from;
			this.setbox();
	},
	go:		function(){
			if(this.aktiv){
				clearTimeout(this.aktiv);
				this.aktiv = 0;
				// this.mybutton.className = this.bigctrl + 'bpause_off';
				this.mybutton.className = 'bpause_off';
			}
			else{
				this.doTimer();
				// this.mybutton.className = this.bigctrl + 'bpause';
				this.mybutton.className = 'bpause';
			}
	}

}
// div class-ok :  boxTitle, boxBody, navi, back, bpause, forw
function InitNewBox(num, startData, IsMidCol, banned_buttons){
var newbox = "";
if(IsMidCol == ""){
	newbox =  '<table border="0" width="194" cellpadding="0" cellspacing="0" class="box_around">' + 
		'<tr><td class="playert"></td></tr>' + 
		'<tr><td class="playerm">' + 
		'<div id="content' + num + '" class="playBody clearfix">' + startData + '</div>';

	newbox = newbox + '<table border="0" cellpadding="0" cellspacing="0" width="100%"';
	
	if(banned_buttons)
		newbox = newbox + ' style="display:none;"'; 
	
	newbox = newbox + '>' +
		    '<tr>' +
			  '<td align="right">' +
				'<div id="content' + num + 'nav">&nbsp;</div>' +
			  '</td>' +
			  '<td class="' + IsMidCol + 'back" id="content' + num + 'b" width="18" height="18">&nbsp;</td>' +
			  '<td class="' + IsMidCol + 'bpause" id="content' + num + 'p" width="18" height="18">&nbsp;</td>' +
			  '<td class="' + IsMidCol + 'forw" id="content' + num + 'f" width="18" height="18">&nbsp;</td>' +
			  '<td width="30%">&nbsp;</td>' +
			'</tr>' +
		  '</table>';
	// }
	// else{newbox = newbox + '<div id="content' + num + 'nav">&nbsp;</div>';}
	newbox = newbox + 
		  '</td></tr>' + 
		'<tr><td class="playerb"></td></tr>' + 
	  '</table>';

	// newbox =  '<table border="0" width="186" cellpadding="0" cellspacing="0"><tr><td>' + 
		// '<div id="content' + num + 'title" class="boxTitle"></div>' +
		// '<div class="boxBody">' +
		// '<div id="content' + num + '">' + startData + '</div>' +
		// '<br /></div>' +
		// '<div class="navi">' +
	      // '<table border="0" cellpadding="0" cellspacing="0" width="166">' +
		    // '<tr>' +
			  // '<td align="right">' +
				// '<div id="content' + num + 'nav">&nbsp;</div>' +
			  // '</td>' +
			  // '<td class="' + IsMidCol + 'back" id="content' + num + 'b" width="18" height="18">&nbsp;</td>' +
			  // '<td class="' + IsMidCol + 'bpause" id="content' + num + 'p" width="18" height="18">&nbsp;</td>' +
			  // '<td class="' + IsMidCol + 'forw" id="content' + num + 'f" width="18" height="18">&nbsp;</td>' +
			// '</tr>' +
		  // '</table>' +
		// '</div>' +
		// '</td></tr>' + 
	  // '</table><br />';
	// newbox = '<div id="content' + num + '" class="">' + startData + '</div>';
}
else{
	newbox =  '<table border="0" width="356" cellpadding="0" cellspacing="0" class="box_around">' + 
		'<tr><td class="bplayert"></td></tr>' + 
		'<tr><td class="bplayerm">' + 
		'<div id="content' + num + '" class="playBody clearfix">' + startData + '</div>' +
		  '<table border="0" cellpadding="0" cellspacing="0" width="100%">' +
		    '<tr>' +
			  '<td align="right" width="75%">' +
				// '<div id="content' + num + 'nav">&nbsp;</div>' +
			  '</td>' +
			  // '<td class="' + IsMidCol + 'back" id="content' + num + 'b" width="25" height="25">&nbsp;</td>' +
			  // '<td class="' + IsMidCol + 'bpause" id="content' + num + 'p" width="25" height="25">&nbsp;</td>' +
			  // '<td class="' + IsMidCol + 'forw" id="content' + num + 'f" width="25" height="25">&nbsp;</td>' +
			  '<td class="back" id="content' + num + 'b" width="18" height="18">&nbsp;</td>' +
			  '<td class="bpause" id="content' + num + 'p" width="18" height="18">&nbsp;</td>' +
			  '<td class="forw" id="content' + num + 'f" width="18" height="18">&nbsp;</td>' +
			  // '<td width="35%">&nbsp;</td>' +
			'</tr>' +
		  '</table>' +
		  '</td></tr>' + 
		'<tr><td class="bplayerb"></td></tr>' + 
	  '</table>';
	// newbox = '<div id="content' + num + '" class="">' + startData + '</div>';
}
return newbox;
}

function setBox() {
	var element = 0;
	var myCounter = 0;
	var whichCol = 0;
	var IsMidCol = 0;
	// var myleftCol = $('ColLeftBoxes');
	var myAllCol = new Object();
	for(m=0; m<BoxesAll.length; m++){
		myAllCol[BoxesAllid[m]] = $(BoxesAll[m]);		
	}
	//alert(myAllCol.l);
	var myAktivBox = new Array();
	var myAktivCounter = new Array();
	var myAktivElement = new Array();
	var myBigControl = new Array();
	// var myNoControl = new Array();
	//alert(myArray.length);
	for(var i=0;i< myArray.length; i++)
	{
		mydiv = myArray[i].substring(0,4);
		// alert(myArray[i].substring(3,5));
		if(myArray[i].substring(4,5) == "0")element = parseInt(myArray[i].substring(5));
		else element = parseInt(myArray[i].substring(4));
		whichCol = myArray[i].substring(6);
		// var teszt = "gale08";
		// if(whichCol == 'l2')alert(myArray[i].substring(4,1) + ' - ' + myArray[i] + ' - ' + element);
		if(whichCol.substring(0, 1) == "m")IsMidCol = "b";
		else IsMidCol = "";
		// IsMidCol = "";// Ezzel jelzem, ha kell a nagyobb gom, jelenleg csak a kicsi megy
		
		// Az 'alro' id-ju forgotban letiltom a gombokat
		if(mydiv == "alro"){//alert(mydiv);
			banned_buttons = 1;
		}
		else{
			banned_buttons = 0;
		}
		
		//alert(element);
		newdiv = false;
		for(j=0; j<element; j++){
			if(temp = $(mydiv + j)){
				newdiv = true;
				idData[ActId] = temp.innerHTML;
				// alert(idData[ActId]);
				ActId++;
				//new Counter('content' + i, 0, 5000, myCounter, element);
			}			
		}	
		//alert(newdiv);
		if(newdiv){
			//alert(whichCol);
			// if(whichCol == "l"){
				// myleftCol.innerHTML = myleftCol.innerHTML + InitNewBox(i);
				myAllCol[whichCol].innerHTML = myAllCol[whichCol].innerHTML + InitNewBox(i, idData[myCounter], IsMidCol, banned_buttons);
				if(whichCol == 'r'){
					$('napt0').innerHTML = '';
				}
				if(temp = $('content' + i)){
					myAktivBox.push(i);
					myAktivCounter.push(myCounter);
					myAktivElement.push(element);
					myBigControl.push(IsMidCol);
					// myNoControl.push(banned_buttons);
				}
				else{
					alert('nem létezik!');
				}
				//
			// }
			//new Counter('content' + i, 0, 5000, myCounter, element);
		}
		myCounter += element;
	}	
	// alert(myAktivBox.length);
	// alert('boxlenght: ' + myAktivBox.length + ' - counter: ' + myAktivCounter.length + ' - element: ' + myAktivElement.length);
	var idozitesDef = 20000;
	for(i=0; i<myAktivBox.length; i++){
		if(temp = $('content' + i + 'title')){
			// alert(myArrayTitle[i]);
			temp.innerHTML = myArrayTitle[i];
		}
		if(myArrayCdown[i]){
			// myIdozites = (myArrayCdown[i] > 1000 ? myArrayCdown[i] : idozitesDef);
			myIdozites = myArrayCdown[i];
			new Counter('content' + myAktivBox[i], 0, myIdozites, myAktivCounter[i], myAktivElement[i], myBigControl[i]);
		}
		else{
			myIdozites = idozitesDef;
		}
	}
}
Event.observe(
	window,
	'load',
	function() {
		// requestCustomerInfo('divtray');
		setBox();
		// new Counter('content1', 0, 10000, 0, 3);
		// new Counter('content2', 0, 15000, 3, 3);
		// new Counter('content3', 0, 5000, 4, 4);
	}
);

function Replace_Style(){
	num = Math.random()*10;
	num = (Math.round(num)%7)+1;
	// alert(num);
	// alert('<link href="style_' + num + '.css" rel="stylesheet" type="text/css">');
}

// mydiv - a réteg ahol megjelenik
// tray - a réteg id-jének karakteres része amiből a tratalom jön
// count a db szám amiből választok
function Random_Box(mydiv, tray, count){

	var num = 	(Math.round(Math.random()*10) % count);
	if(myDivCnt = $(mydiv)){
		if(temp = $(tray + num)){
			myDivCnt.innerHTML = temp.innerHTML;
		}
	}
}	
