$(function(){

new ajanlo('.top5');
stepTimer = setTimeout('automaticStep()',stepDelay);

})

var stepDelay = 5000;
var stepTimer;

function automaticStep() {
	$('.top5_panel .elore').trigger('click');
	stepTimer = setTimeout('automaticStep()',stepDelay);
}

function resetStepTimer() {
	clearTimeout(stepTimer);
	var subTimer = setTimeout(function() {
		stepTimer = setTimeout('automaticStep()', stepDelay);
	}, stepDelay);
}


function ajanlo( that ) {
	var t;
	var ido = 1000;

	this.main = $(that);
	this.current = 0;
	this.cikkek = eval( 'top5_data' );
	if(this.cikkek.length==0) { return false; }

	this.img = $(that).find('.top5_kep img');
	this.links = $(that).find('.top5_kep a, .cikkdata h2 a');
	this.title = $(that).find('.cikkdata h2 a');
	this.lead = $(that).find('.cikkdata p');
	this.nums = $(that).find('.lapok');

	this.loadCikk = function() {
		this.img.attr({
			src: this.cikkek[this.current].kepsrc,
			alt: this.cikkek[this.current].cim
		});
		this.title.text(this.cikkek[this.current].cim);
		this.lead.text(this.cikkek[this.current].lead);
		this.links.attr('href', this.cikkek[this.current].url);
		$('.lapok span').removeAttr('class');
		$('.lapok #lap_'+this.current).addClass('selected');

		resetStepTimer();
	}

	this.init = function(that) {
		var ajanlo = this;

		for (i=0; i<ajanlo.cikkek.length; i++ ) {
			$(that).find('.lapok').append('<span id="lap_'+i+'">'+(i+1)+'</span>');
		}

		$(that).find('.lapok span').click( function() {
			$('.lapok span').removeAttr('class');
			$(this).addClass('selected');
			ajanlo.current = $(this).attr('id').substr(4);

			ajanlo.loadCikk();
			return false;
		});

		$(that).find('.top5_panel .hatra').click( function() {
			if (ajanlo.current>0) ajanlo.current--;
			else ajanlo.current = ajanlo.cikkek.length-1;

			ajanlo.loadCikk();
			return false;
		});

		$(that).find('.top5_panel .elore').click( function() {
			if (ajanlo.current<(ajanlo.cikkek.length-1)) ajanlo.current++;
			else ajanlo.current=0;

			ajanlo.loadCikk();
			return false;
		});

		ajanlo.current = 0;
		ajanlo.loadCikk();
	}

	this.init(that);
}

