var currentVisual 	= 0;
var fadeOutVisual	= 0;
var thisFlash		= null;
var visuals		 	= 5;
var interval 		= 7; // in Seconds
var timer			= null;
var fading			= false;  
 
   $(document).ready(
   
	function(){
		
		$("#extraDiv1").fadeOut(1);
		$("#extraDiv2").fadeOut(1);
		$("#extraDiv3").fadeOut(1);
		$("#extraDiv4").fadeOut(1);
		$("#extraDiv5").fadeOut(1);

		
		$("#extraDiv1").css('zIndex', 101);
		$("#extraDiv2").css('zIndex', 102);
		$("#extraDiv3").css('zIndex', 103);
		$("#extraDiv4").css('zIndex', 104);
		$("#extraDiv5").css('zIndex', 105);
		
		$("#topblock1").fadeOut(1);
		$("#topblock2").fadeOut(1);
		$("#topblock3").fadeOut(1);
		$("#topblock4").fadeOut(1);
		$("#topblock5").fadeOut(1);

		$("#topblock1").css('zIndex', 201);
		$("#topblock2").css('zIndex', 202);
		$("#topblock3").css('zIndex', 203);
		$("#topblock4").css('zIndex', 204);
		$("#topblock5").css('zIndex', 205);
			
		timer = $.timer(interval * 1000, function (timer) {
				newVisual = currentVisual + 1;
		
				if(newVisual > visuals){
					newVisual = 1;
				}
			  	activate(newVisual,"SCRIPT");
		});
		activate(1,"SCRIPT");
 	}
	
 );
 function fadeDone(){
  		fading = false;
		$("#topblock"+fadeOutVisual).css('visibility', 'hidden');
 }
 function startFlash(){
 		if(thisFlash!=null){
			var delay = setTimeout("getFlashMovieObject('"+thisFlash+"').start()",800);
		}
 }
 function stopFlash(){
 		if(thisFlash!=null){
			var delay = setTimeout("getFlashMovieObject('"+thisFlash+"').stop()",500);
			thisFlash = null;
		}
 }
  
  
function activate(id,event){
  
	if(fading == false){
  
  		if(id!=currentVisual){
  
	  		if(document.getElementById("visualitem"+id) == null){
	  			return;
	 		}
	 			
	  		document.getElementById("visualitem"+id).className = "active";
	  
	  		if(currentVisual != 0){
				document.getElementById("visualitem"+currentVisual).className = "";
				//$("#topblock"+currentVisual).css('display', "none");
				
			
				
				// change Visual
				var curVisZ = parseInt($("#extraDiv"+currentVisual).css('zIndex'));
				var newVisZ = parseInt($("#extraDiv"+id).css('zIndex'));
				
				if(curVisZ < newVisZ){
					
					$("#extraDiv"+currentVisual).css('zIndex', newVisZ);
					$("#extraDiv"+id).css('zIndex', curVisZ);
				}
				
				// set Flash
				stopFlash();
				thisFlash = hasFlash(id); 
				
				$("#extraDiv"+id).fadeIn(1, startFlash);
				$("#extraDiv"+currentVisual).fadeOut(500);
				
				// change topblock
				var curTxtZ = parseInt($("#topblock"+currentVisual).css('zIndex'));
				var newTxtZ = parseInt($("#topblock"+id).css('zIndex'));
				
				if(curTxtZ < newTxtZ){
					$("#extraDiv"+currentVisual).css('zIndex', newTxtZ);
					$("#extraDiv"+id).css('zIndex', curTxtZ);
				}
				
				fading = true;
				$("#topblock"+id).fadeIn(500, fadeDone);
				$("#topblock"+currentVisual).fadeOut(500);
			}
			else{
				// on Init
				$("#extraDiv"+id).fadeIn(1);
				$("#topblock"+id).fadeIn(1);
			}
			
			// look for flash object
			if(thisFlash!= null){
				timer.stop();
			}
			else{
				timer.reset(interval * 1000);
			}
			
			// change currentVisual
			currentVisual = id;
		
		}
	}
}

function hasFlash(id){
	var obj = document.getElementById("extraDiv"+id);
	if(obj == null){
		return null;
	}
	if(obj.innerHTML != ""){
	
		if(obj.firstChild.nodeName == "OBJECT" || obj.firstChild.nodeName == "EMBED"){
			return obj.firstChild.id;
		}
		else{
			return null;
		}
	}
	else{
		return null;
	}
}  
  
  
function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}
