var ani={
  images:[],
	preload:function(){
		var files=['bg.jpg','spotlight.jpg','spotlight-hover.jpg','text-cs.gif','text-en.gif'];
		$('#spotlight .controls a').each(function(i){
		  var f,
			dir=base_static_dir+'images/spotlight/'+(i+1)+'/';
			ani.images[i]=[];
			for(f in files){
			ani.images[i][files[f]]=$(new Image())
			.attr('src',dir+files[f]);
			}
		});

	},
	init:function(){
		$('#spotlight .controls a').each(function(i){
			$(this).click(function(){
				if(!($(this).parent().is('.selected')||$(this).parent().is('#preselected'))){
					var trigger=$('#spotlight .controls a')[i];			
					ani.stopAnims();
					$('#preselected').removeAttr('id');
					$(trigger).parent().attr('id','preselected');
					ani.change(i,function(){
						var trigger=$('#spotlight .controls a')[i];
					});
					return false;
				}
			});
		});
		$('#spotlight .controls').hover(
			function(){
				ani.stopAnims();
				$('#spotlight .controls a').css('background-position','-248px 0');
			},
			function(){
				var selected,
					selector=$('#preselected')[0]?'#preselected':'.selected';
				$('#spotlight .controls a').each(function(i){
					if($(this).parent().is(selector)){
						$(this).parent().removeAttr('id');
						selected=i;
					}
				});
				var trigger=$('#spotlight .controls a')[selected];
				$('#spotlight .controls a').each(function(i){
					ani.timer(i,false);
				});
				$('#spotlight .controls .selected').removeClass('selected');				
				$(trigger).parent().addClass('selected');
				ani.timer(selected);
			}
		);
		ani.timer(0);
	},
	fadeOut:function(callback){
		$('#leftImages').fadeOut('slow');
		$('#body-bg').fadeOut('slow',callback);
	},
	fadeIn:function(callback){
		$('#leftImages').fadeIn('slow');
		$('#body-bg').fadeIn('slow',callback);
	},
	stopAnims:function(){
		$('#spotlight .controls a').each(function(i){
			ani.timer(i,false);
		});
		$('#body-bg,#leftImages').stop().css('opacity','');		
	},
	changeTheme:function(i){
		var dir=base_static_dir+'images/spotlight/'+(i+1)+'/',
			trigger=$('#spotlight .controls a')[i],
			alt=$(trigger).children(":first").html();

		$('body').attr('class','spotlight'+(i+1));
		
    $('#spotlight img.photo')
			.attr('src',ani.images[i]['spotlight.jpg'].attr('src'))
			.attr('alt',alt);
		$('#spotlight a.text img')			
			.attr('src',ani.images[i]['text-'+lang+'.gif'].attr('src'))
			.attr('alt',alt);		

		$('#spotlight a.referenceLink,#spotlight a.text,#spotlight p.show-link a').attr('href',$(trigger).attr('href'));					
		$(trigger).css('background-position','-248px 0');
	},
	timer:function(i,action){
		var trigger=$('#spotlight .controls a')[i],
			width=$(trigger).width()+35,
			action=typeof(action)=='undefined'?true:action;
		if(action){
			$(trigger)
				.css('background-position','-248px 0')
				.animate({backgroundPosition:(width-248)+'px 0'},6000,function(){
					var next=((i+1)>=$('#spotlight .controls a').length)?0:i+1;
					ani.change(next,function(){
						var trigger=$('#spotlight .controls a')[next];
						ani.timer(next);
					});
				});
		}else{
			$(trigger).stop(true);
		}
	},
	change:function(i,callback){
		ani.fadeOut(function(){
			var trigger=$('#spotlight .controls a')[i];
			$('#spotlight .controls .selected').removeClass('selected');
			$(trigger).parent().addClass('selected');
			ani.changeTheme(i);
			ani.fadeIn(callback);
		})
	}
};
$(document).ready(function(){
	ani.preload();
});
window.onload=function(){
	ani.init();
//	$('body').append('<div id="debug" style="position:absolute;left:0;top:0;width:200px;height:500px;background:yellow;color:red;"></div>');
};
