var thumbs 			= []; // AJAX fills it with all the previews
var last_changed	= -1;
var images			= []; // Will hold Image instances

var divisions	= [
	'me',
	'pe',
	'tr',
	'fo'
];

var current = [
	0,
	0,
	0,
	0
];

var links = {
	'me': '/index.php?option=com_content&task=view&id=15&Itemid=',
	'pe': '/index.php?option=com_content&task=view&id=16&Itemid=',
	'tr': '/index.php?option=com_content&task=view&id=17&Itemid=',
	'fo': '/index.php?option=com_content&task=view&id=18&Itemid='
};
	

function init_thumbs(){
	new Ajax.Request('/header_images/index.pl',
	{
		method:			'get',
		onSuccess: function(transport){
			var response = transport.responseText || '';
			eval('thumbs = ' + response);
			fillInThumbs(thumbs);
			show_photo(0);
		},
		onFailure: function(transport){ alert("Something went wrong:\n" + transport.responseText) }
	});	
}

function fillInThumbs(){
	
	// reset it
	$('head_bar').innerHTML = '';
	
	divisions.each(function(item, count){
		$('head_bar').innerHTML = $('head_bar').innerHTML + 
			'<a id="' + item + '_link" href="' + links[item] + '">\
				<img id="' + item + '_img" src="/header_images/' + thumbs[item][0] + '" />\
			</a>\
			';
	});
	
	setTimeout("update_photo()",5000);
}

function update_photo(){
	while(1){
		var rnd = Math.round(Math.random()*3); // random number 0 - 3
		if (rnd != last_changed) {
			last_changed = rnd;
			break;
		}	
	}
	current[rnd] = current[rnd] + 1;
	
	if(current[rnd] >= thumbs[divisions[rnd]].size()){
			current[rnd] = 0;
	}
	
	// preload the image
	var photo = new Image();
	photo.src = '/header_images/' + thumbs[ divisions[rnd] ][ current[rnd] ];
	
	// place current one as background
	var img_src = $(divisions[rnd] + '_img').src;
	$(divisions[rnd] + '_link').style.backgroundImage = "url('" + img_src +"')";	

	// place new as image - but opacity 0 (display none?)
	$(divisions[rnd] + '_img').style.display = "none";
	$(divisions[rnd] + '_img').src = photo.src;
	
	// fade in effect
	Effect.Appear(divisions[rnd] + '_img');
	
	setTimeout("update_photo()",5000);
}

function show_normal(){
	zoom	= 1;
	if(current_effect == 'before'){
		the_photo('/gallery/' + item.before_normal);
	} else {
		the_photo('/gallery/' + item.after_normal);
	}
	
	reset_sh_border();
	
	$('sh_normal').style.borderLeft = '8px solid #696';
	$('sh_normal').style.paddingLeft = '4px';
}