window.addEvent('domready',function(){

	var theConfirmMsg = $("confirmEmailMsg")?$("confirmEmailMsg").get('text'):false;

	
	var activeGames = $H(theGamesList).filter(function(value,key){//auf aktive spiele filtern
		return $H(value).get("active");
		//return true;
	});

	var activeGamesImages = new Array();
	var activeGamesElements = new Array();

	var delaySec = 4;
	//start item
	var activeGameId = Math.floor(Math.random() * (activeGames.getLength()));
	
	var gamesContainer = new Element("ul",{
		"id":"gamesContainer",
		"styles":{
			"width":"700px",
			"opacity":0
		}
	});
	
	var logosContainer = new Element("ul",{
		"id":"logosContainer"
	});
	
	var fadeMoveImageAndLogo = new Fx.Tween(gamesContainer,{//animation
		duration:"long",
		transition:Fx.Transitions.Circ.easeInOut,
		link:"chain",
		onChainComplete:function(){
			(function(){
				activeGamesElements[activeGameId][0].fade("out");
				activeGamesElements[activeGameId][1].fade("out");
				
				activeGamesElements[activeGameId][0].morph({width:130, height: 123, right:90, top:6});
				activeGamesElements[activeGameId][0].getElement("img").morph({width:130, height: 123});
				
				var nextGameId = (activeGameId < activeGamesElements.length-1)?activeGameId+1:0;
				var lastGameId = (activeGameId == 0)?activeGamesElements.length-1:activeGameId-1;

				activeGamesElements[lastGameId][0].setStyles({width:150, height: 143, right:110, top:-6});
				activeGamesElements[lastGameId][0].getElement("img").setStyles({width:150, height: 143});
				
				activeGamesElements[nextGameId][0].fade("in");
				activeGamesElements[nextGameId][1].fade("in");
				
				activeGamesElements[nextGameId][0].morph({width:140, height: 133, right:100, top:0 });
				activeGamesElements[nextGameId][0].getElement("img").morph({width:140, height: 133});
				
				if(activeGameId == activeGamesElements.length-1) activeGameId = 0;
				else activeGameId++;
				
			}).periodical(delaySec * 1000);
		}
	});

	var i = 0;
	
	activeGames.each(function(value,key,hash){
		
		var gameImage = siteFolder+"/imgs/casino/"+$H(value).get('image');
		var gameLogo = siteFolder+"/imgs/casino/"+$H(value).get('logo');
		var gameVariation = "";
		if($H(value).get('variation')){
			gameVariation = "/variation~"+$H(value).get('variation');
		}
		
		var gameId = "/gameid~"+ $H(value).get('gameid');
		var roomId = (isTv == 1)?"/id_room~"+ $H(value).get('roomid'):"";
		
		var gamesStartLink = "/games_start/"+userLang+""+gameId+""+roomId+""+gameVariation+"/"+root_casino+"/"+GLOBAL_play+".html";
		
		activeGamesImages[i] = new Array();
		activeGamesImages[i][0] = (gameImage);//precaching
		
		var activeGameItem = new Element("a", {
			"id":key,
			"class":"showPositionIn_image room_casino",
			"href":"javascript:void(0)",
			"styles":{
				//"background-image":"url("+gameImage+")",
				"cursor":"pointer"
			},
			"events":{
				"click":function(event){
					event.preventDefault();
					//alert('d');
					if(!theConfirmMsg){
						//fenstergrösse
						var ratio = 1.25;
						var width = screen.width-100;
						var height = screen.height-100;
						if(width/height > ratio){
							width = (height * ratio).toInt();
						} 
						else {
							height = (width * ratio).toInt();
						}
						//spiel immer nur 1x öffnen und wenn geöffnet wieder anzeigen
						var theWindow = this.retrieve("window");//gespeichertes fenster
						if(theWindow && !theWindow.closed)//ist das fenster noch offen
							theWindow.focus();//fenster in den vordergrund holen
						else//fenster speichern
							this.store("window",window.open(gamesStartLink,"_blank","resizable=yes,left="+((screen.width-width)/2).toInt()+",top="+((screen.height-height)/2).toInt()+",height="+height+",width="+width));
							
					}
					else{
						alert(theConfirmMsg);
						top.location.replace('/email_confirm.asp')
					}
				}
			}
		});
		var activeGameItemImg = new Element("img",{
			"class":"gameImage",
			"src":gameImage					
		});
		activeGameItemImg.inject(activeGameItem);
		activeGameItem.inject(new Element("li").inject(gamesContainer));//mit li in die liste
		
		var activeGameLogo = new Element("a",{
			"id":key+"Logo",
			"class":"showLogoPosition room_casino",
			"href":"javascript:void(0)",
			"styles":{
				"background-image":"url("+gameLogo+")"
			},
			"events":{
				"click":function(event){
					event.preventDefault();
					//alert('d');
					if(!theConfirmMsg){
						//fenstergrösse
						var ratio = 1.25;
						var width = screen.width-100;
						var height = screen.height-100;
						if(width/height > ratio){
							width = (height * ratio).toInt();
						} 
						else {
							height = (width * ratio).toInt();
						}
						//spiel immer nur 1x öffnen und wenn geöffnet wieder anzeigen
						var theWindow = this.retrieve("window");//gespeichertes fenster
						if(theWindow && !theWindow.closed)//ist das fenster noch offen
							theWindow.focus();//fenster in den vordergrund holen
						else//fenster speichern
							this.store("window",window.open(gamesStartLink,"_blank","resizable=yes,left="+((screen.width-width)/2).toInt()+",top="+((screen.height-height)/2).toInt()+",height="+height+",width="+width));
					}
					else{
						alert(theConfirmMsg);
						top.location.replace('/email_confirm.asp')
					}
				}
			}
		});
		activeGameLogo.inject(new Element("li").inject(logosContainer));//mit li in die liste

		activeGamesElements[i] = new Array();
		activeGamesElements[i][0] = (activeGameItem);//precaching
		activeGamesElements[i][1] = (activeGameLogo);
		
		i++;
	});
	
	gamesContainer.getElements("a").fade("hide");
	logosContainer.getElements("a").fade("hide");

	new Asset.images(activeGamesImages,{
		onComplete:function(){//wenn die bilder geladen wurden
			fadeMoveImageAndLogo.start('opacity',0,1).chain(function(){
				activeGamesElements[activeGameId][0].fade("in"); // fadeIn screenshot
				activeGamesElements[activeGameId][1].fade("in"); // fadeIn logo

				activeGamesElements[activeGameId][0].morph({width:140, height: 133, right:100, top:2});
				activeGamesElements[activeGameId][0].getElement("img").morph({width:140, height: 133});
				
			});
		}
	});
	
	$("heading").grab(gamesContainer);//stage anzeigen
	$("heading").grab(logosContainer);//stage anzeigen
});







