/*# Metadata ################################### Header v.2
# $URL: http://gt-nas-1/svn/gamebase.frontend/trunk/xsl_gamebase/_stargames/data/casino_net.js $
# $Rev: 4657 $
# $Date: 2011-09-12 12:40:26 +0200 (Mo, 12 Sep 2011) $
# $Author: pmang $
### TOC ###################################################
#
### Description ###########################################
#
#########################################################*/
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(){
			var imageSizeX = activeGamesElements[activeGameId][0].getElement('img').getStyle('width').toInt();
			var imageSizeY = activeGamesElements[activeGameId][0].getElement('img').getStyle('height').toInt();
			var imagePosX = activeGamesElements[activeGameId][0].getStyle('right').toInt();
			var imagePosY = activeGamesElements[activeGameId][0].getStyle('top').toInt();
			(function(){
			
				activeGamesElements[activeGameId][0].fade("out");
				activeGamesElements[activeGameId][1].fade("out");
				
				activeGamesElements[activeGameId][0].morph({width:imageSizeX-10, height: imageSizeY-10, right:imagePosX-20, top:imagePosY+5});
				activeGamesElements[activeGameId][0].getElement("img").morph({width:imageSizeX-10, height: imageSizeY-10});
				
				var nextGameId = (activeGameId < activeGamesElements.length-1)?activeGameId+1:0;
				var lastGameId = (activeGameId == 0)?activeGamesElements.length-1:activeGameId-1;

				activeGamesElements[lastGameId][0].setStyles({width:imageSizeX, height: imageSizeY, right:imagePosX, top:imagePosY});
				activeGamesElements[lastGameId][0].getElement("img").setStyles({width:imageSizeX, height: imageSizeY});
				
				activeGamesElements[nextGameId][0].fade("in");
				activeGamesElements[nextGameId][1].fade("in");
				
				activeGamesElements[nextGameId][0].morph({width:imageSizeX+10, height: imageSizeY+10, right:imagePosX-10, top:imagePosY-5 });
				activeGamesElements[nextGameId][0].getElement("img").morph({width:imageSizeX+10, height: imageSizeY+10});
				
				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 gameImage = $H(value).get('image');
		//var gameLogo = siteFolder+"/imgs/casino/"+$H(value).get('logo');
		var gameLogo = $H(value).get('logo');
		
		
		var gameVariation = "";
		if($H(value).get('variation')){
			gameVariation = "/variation~"+$H(value).get('variation');
		}
		
		var gameId = $H(value).get('gameid');
		//var roomId = (isTv == 1)?"/id_room~"+ $H(value).get('roomid'):"";
		
		var gamesStartLink = "/games_start/"+userLang+"/"+warningEmail+"gameid~"+gameId+"/type~1/"+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":{
				"cursor":"pointer"
			},
			"events":{
				"click":function(event){
					event.preventDefault();
					if(!theConfirmMsg){
						//windewsize
						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 1xoeffnen und wenn geoeffnet 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){
						//windowsize
						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 1xoeffnen und wenn geoeffnet 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");
	
	var imageSizeX = activeGamesElements[activeGameId][0].getElement('img').getStyle('width').toInt();
	var imageSizeY = activeGamesElements[activeGameId][0].getElement('img').getStyle('height').toInt();
	var imagePosX = activeGamesElements[activeGameId][0].getStyle('right').toInt();
	var imagePosY = activeGamesElements[activeGameId][0].getStyle('top').toInt();
	
	var loader = false;
	
	new Asset.images(activeGamesImages,{
		onProgress:function() {
			if(!loader) {
				loader = new Element('div', {
					id: 'loadInfo'
				});
				loader.inject($('heading'));
			}
		},
		onComplete:function(){
		//wenn die bilder geladen wurden
			loader.fade('out');
		
	var imageSizeX = activeGamesElements[activeGameId][0].getElement('img').getStyle('width').toInt();
	var imageSizeY = activeGamesElements[activeGameId][0].getElement('img').getStyle('height').toInt();
	var imagePosX = activeGamesElements[activeGameId][0].getStyle('right').toInt();
	var imagePosY = activeGamesElements[activeGameId][0].getStyle('top').toInt();
		
			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:imageSizeX, height: imageSizeY, right:imagePosX, top:imagePosY});
				activeGamesElements[activeGameId][0].getElement("img").morph({width:imageSizeX, height: imageSizeY});
				
			});
		}
	});
	
	$("heading").grab(gamesContainer);//stage anzeigen
	$("heading").grab(logosContainer);//stage anzeigen
});








