var the_timeout;

function change_banner(bid,stop){
	
	
	if(stop === undefined) {
		stop = false;
	}
	
	// get data with ajax
	var data1;
	if(bid == 4){
		data1 = noutate;
	}else{
		$.getJSON('http://www.grupcertificare.ro/banner.php?id='+bid, function(data){
			data1 = data;
		});
	}
	
	// change each element
	$('.tophome').fadeOut('slow', function(){
		$('.tophome').attr('style','background-image: url("http://www.grupcertificare.ro/images/tops/'+data1['img']+'");');
		$('.tophome').fadeIn('slow');
	});
	
	$('#th1 span').fadeOut('slow', function(){
		$('#th1 span').html(data1['titlu']);
		$('#th1 span').fadeIn('slow');
	});
	
	$('#th1 p').fadeOut('slow', function(){
		$('#th1 p').html(data1['text']);
		$('#th1 p').fadeIn('slow');
	});

	$('#th1 a').fadeOut('slow', function(){
		if(bid != 4){
			$('#th1 a').attr('href',site_url+data1['link']);
		}else{
			$('#th1 a').attr('href',data1['link']);
		}
		$('#th1 a').fadeIn('slow');
	});
	
	// show which is the current tab
	$('.toptab').removeClass('tab_sel');
	$('#tab_'+bid).addClass('tab_sel');
	
	if(stop) {
		clearTimeout(the_timeout);
	}
}

function auto_banner(bid) {
	bid = bid + 1;
	if(bid > 4) {
		bid = 1;
	}
	change_banner(bid,false);
	the_timeout = setTimeout('auto_banner('+bid+')',5000);
}

$(document).ready(function(){
	auto_banner(0);
	
	$('.toptab').click(function(){
		
		var bid = $(this).attr('id').split('_');
		bid = bid[1];
		
		change_banner(bid,true);
	});
});
