// JavaScript Document

var HIGH_SCORES_POD_ID = "highScoresPod";

function initPageHandlers () {
}

function getElement ( id ) {
	return document.getElementById (id);
}

function selectPopQuizTab () {
	var highScoresPod = getElement (HIGH_SCORES_POD_ID);
	highScoresPod.className = "popQuizTab";
	// alert (" POP QUIZ " );
}

function selectPop4Tab () {
	var highScoresPod = getElement (HIGH_SCORES_POD_ID);
	highScoresPod.className = "pop4Tab";	
	//alert (" POP 4 " );
}

function selectPopSpinTab () {
	var highScoresPod = getElement (HIGH_SCORES_POD_ID);
	highScoresPod.className = "popSpinTab";		
	//alert (" POP SPIN " );
}

   function selectPopQuiz () {
      //alert ( "MY PROFILE" );
	  $("highScoresPodTabs").className = "topSlice popQuizSelected";
	  $("popQuizScoresDiv").style.display = "block";
	  $("pop4ScoresDiv").style.display = "none";
	   $("popSpinScoresDiv").style.display = "none";
   }
   
   function selectPop4 () {
      // alert ( "MY Account" );
	   $("highScoresPodTabs").className = "topSlice pop4Selected";
	   $("popQuizScoresDiv").style.display = "none";
	   $("pop4ScoresDiv").style.display = "block";
	    $("popSpinScoresDiv").style.display = "none";
   }

   function selectPopSpin () {
      // alert ( "MY Account" );
	   $("highScoresPodTabs").className = "topSlice popSpinSelected";
	   $("popQuizScoresDiv").style.display = "none";
	   $("pop4ScoresDiv").style.display = "none";
	    $("popSpinScoresDiv").style.display = "block";
   }




   /**
   * This is invoked after the user has logged in via an Ajax call.  
   * It will show the "Member Page" link and will remember the popjax user id of the logged in user.
   */
   function afterLoginCallback ( _pjuid ) {
      m_pjuid = _pjuid;
	  $("memberPageLink").style.display="block";
	  m_pjuserName = $("userId").value;
	  
	 // alert ( "m_pjuid: " + m_pjuid );
	  
	//  alert ( "AFTER LOGIN m_pjuserName: " + m_pjuserName );
	  
	  /**
	  * This causes IE6 and below to re GET the page.  This ensures the page is in the right state.
	  */
	  //refreshPage ();
   }
	
   /**
   * This is invoked after the user has registered and been logged in via an Ajax call.  It will show the "Member Page" link and will remember 
   * the popjax user id of the logged in user.
   */	
   function afterRegistrationCallback ( _pjuid ) {
	   m_pjuid = _pjuid;
	   $("memberPageLink").style.display="block";
	   m_pjuserName = $("registrationScreenName").value;
	   
	  /**
	  * This causes IE6 and below to re GET the page.  This ensures the page is in the right state.
	  */
	  //refreshPage ();	   
   }

   /**
   * This is invoked after the Ajax call to log out has returned.
   */ 
   function afterLogOutCallback () {
	   $("memberPageLink").style.display="none";
	   m_pjuid = null;
	   m_pjuserName = null;
   }
	
	/**
	* This is called to grab the data for the page via asynchronous Ajax calls.
	*/
	function getPageData () {
	    
		placeCenterContentInGameboard ();
		
		// showSpinner ( "gameBoardSpinner" );
		 
		 getSubmitQuestionWinnerList ( {divId:"winnersCirclePodTopContents"} );
		 
		 getDrawingWinnersList ( {divId:"winnersCirclePodLeftContents"} );
		 
		 getQualifiersList ( {divId:"winnersCirclePodRightContents"} );
		 
		// getHighScoresList ( {pop4DivId:"pop4HighScoresContent", popQuizDivId:"popQuizHighScoresContent", popSpinDivId:"popSpinHighScoresContent"} );
		 
		  getHighScoresList ( {pop4DivId:"pop4ScoresDiv", popQuizDivId:"popQuizScoresDiv", popSpinDivId:"popSpinScoresDiv"} );
		 
		 getHitQuestionList ( {divId:"hitQuestionsDiv"} );
		 
		 getLandingPageHitGamesList ( handleLandingPageHitGamesListLoaded );
		 
	}
	
    /**
	* This function places one of the thumbnail videos into the "video wall" on the home page.
	*/	
    function placeThumbnailVideo ( thumbDivId, row, col, videoPath, caption, gameId, longCaption ) {
	
	
	   var swfContentPath = "thumbnailVideo.swf?videoPath=" + videoPath + "&caption=" + caption; //PlayGamesChooser.swf"; // contentPreLoader.swf
	   
	   var so = new SWFObject( swfContentPath, "flashThumbRow" + row + "Col" + col, "100%", "100%", "9", "#ffffff" ); //, backColor );
	   so.addParam("quality", "high");
       so.addParam("wmode", "transparent" ); 
       so.addParam("allowScriptAccess", "always");
       so.addParam("salign", "tl" ); //tl");
	   so.useExpressInstall('expressinstall.swf');
	   so.write( thumbDivId );
	   	
       var target = $( thumbDivId );
	   var origX = target.offsetLeft;
	   var origY = target.offsetTop;
	   var origWidth = target.offsetWidth;
	   var origHeight = target.offsetHeight;
	   
       var fx = new Fx.Styles(target, {duration: 300, wait: false});
	   
		target.addEvent('mouseenter', function(){
		    if ( $(m_landingPageCenterFlashId) ) {
			   $(m_landingPageCenterFlashId).setHoverMessage (longCaption);
			}
			fx.start({
				'width': origWidth + 10,
				'height': origHeight + 10,
				'top':origY-5,
				'left':origX-5
			});
		});
	 
		target.addEvent('mouseleave', function(){
		    if ( $(m_landingPageCenterFlashId) ) {
			   $(m_landingPageCenterFlashId).clearHoverMessage ();
			}		
			fx.start({
				'width': origWidth,
				'height': origHeight,
				'top':origY,
				'left':origX
			});
		});	   

        /**
		* When the user clicks a thumbnail video on the video wall, lanch the "HIT" game that video represents.
		*/		
		target.onmousedown = function () {
			
			var pjuid = getCookie ( "pjuid" );
			
		//	alert ( " pjuid: " + pjuid + " m_pjuid: " + m_pjuid );
			
			if ( m_pjuid == null && pjuid != null ) {
				m_pjuid = pjuid;
			}
			// alert ( "PJUID: " + pjuid );
			
           var gamePageUrl = "/game/standAloneGame.jsp?game_type=HIT&game_id=" + gameId + "&pjuid=" + m_pjuid + 
		      "&pjusername=" + m_pjuserName + "&gohome=true";
		   
	//alert ( " gamePageUrl: " + gamePageUrl );
	
	       window.location.href = gamePageUrl;		
		}
		
	}  
	

	
	
	var m_totalThumbnailVideos = 12;
	var m_thumbnailVideosStarted = 0;
	
	function thumbnailVideoStarted () {
	   m_thumbnailVideosStarted++;
	   if ( m_totalThumbnailVideos == m_thumbnailVideosStarted ) {
	      // setTimeout ( removeGameboardSpinner, 10 ); 
	   }
	}
	
	var m_landingPageCenterFlashId = "landingPageCenterFlash";
	
	function placeCenterContentInGameboard () {
		
	   var hasTheVersion = isCorrectFlashVersionInstalled ();
	   if ( !hasTheVersion ) {
		  // alert ( "BAR" );
		   $("homePageGameContainer").innerHTML = "<p style='padding:10px; text-align:left; color:#000000;'>In order to enjoy this site, you will need to install the Adobe Flash Player. Please <a href='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash'>Click Here</a> to download and install the Adobe Flash Player.</p>";
		  // alert ( "FOO" );
		   return;
	   }
	   
	// alert ( "hasTheVersion: " +  hasTheVersion );
	
	   $("gameboardToolTipArea").style.display = "block";
	   
	   var swfContentPath = "landingPageGameCenter.swf";
	   var so = new SWFObject( swfContentPath, m_landingPageCenterFlashId, "100%", "100%", "9", "#ffffff" ); //, backColor );
	   so.addParam("quality", "high");
       so.addParam("wmode", "transparent" ); 
       so.addParam("allowScriptAccess", "always");
       so.addParam("salign", "tl" ); //tl");
	   so.useExpressInstall('expressinstall.swf');
	   so.write( "gameboardToolTipArea" );			
	}
	
	/*
	function removeGameboardSpinner () {
	  // alert ( "REMOVE GAMEBOARD SPINNER" );
	   hideSpinner ( "gameBoardSpinner" );
	   $("gameboardToolTipArea").style.display = "block";
	   
	   var swfContentPath = "landingPageGameCenter.swf";
	   var so = new SWFObject( swfContentPath, m_landingPageCenterFlashId, "100%", "100%", "9", "#ffffff" ); //, backColor );
	   so.addParam("quality", "high");
       so.addParam("wmode", "transparent" ); 
       so.addParam("allowScriptAccess", "always");
       so.addParam("salign", "tl" ); //tl");
	   so.useExpressInstall('expressinstall.swf');
	   so.write( "gameboardToolTipArea" );	   
	}
	*/
	
	//var m_videosFilePath = "http://67.192.74.245/game/resources.active/smallVideos/";
	var m_videosFilePath = "resources/video/";
	
    /**
	* This is called once the landing page's video wall "HIT GAMES" data is retrieved.
	*/	
	function handleLandingPageHitGamesListLoaded ( data, xml ) {
	   // alert ( "HIT GAMES STUFF READY data: " + data );
		 
		var landingPageHitGames = data.landingPageHitGames.landingPageHitGames;
		
	    m_totalThumbnailVideos = landingPageHitGames.length;
	    m_thumbnailVideosStarted = 0;		
		
		loadOneThumbVideo ( landingPageHitGames, 11 );
		loadOneThumbVideo ( landingPageHitGames, 3 );
		loadOneThumbVideo ( landingPageHitGames, 8 );
		loadOneThumbVideo ( landingPageHitGames, 4 );
		loadOneThumbVideo ( landingPageHitGames, 10 );
		loadOneThumbVideo ( landingPageHitGames, 5 );
		loadOneThumbVideo ( landingPageHitGames, 6 );
		loadOneThumbVideo ( landingPageHitGames, 1 );
		loadOneThumbVideo ( landingPageHitGames, 9 );
		loadOneThumbVideo ( landingPageHitGames, 7 );
		loadOneThumbVideo ( landingPageHitGames, 0 );
		loadOneThumbVideo ( landingPageHitGames, 2 );
		
		// alert ( "HIT GAMES: " + landingPageHitGames );
		
		//for ( var idx = 0; idx < landingPageHitGames.length; idx++ ) {
			
			// loadOneThumbVideo ( landingPageHitGames, idx );
			/*
		   var landingPageHitGame = landingPageHitGames[idx];
		   
		   var row = landingPageHitGame.row;
		   var col = landingPageHitGame.col;
		   var videoPath = landingPageHitGame.videoPath;
		   var title = landingPageHitGame.title;
		   var hitGameId = landingPageHitGame.hitGameId;
		   var longCaption = landingPageHitGame.longCaption;

           var divId = "thumbRow" + row + "Col" + col;
		   		   
		   placeThumbnailVideo ( divId, row, col, videoPath, title, hitGameId, longCaption ); 
		   */
		 
		//}
	}
	
	function loadOneThumbVideo ( dataArray, indexOfThumb ) {
		
		   var landingPageHitGame = dataArray[indexOfThumb];
		   
		   var row = landingPageHitGame.row;
		   var col = landingPageHitGame.col;
		   var videoPath = landingPageHitGame.videoPath;
		   var title = landingPageHitGame.title;
		   var hitGameId = landingPageHitGame.hitGameId;
		   var longCaption = landingPageHitGame.longCaption;

           var divId = "thumbRow" + row + "Col" + col;
		   		   
		   placeThumbnailVideo ( divId, row, col, videoPath, title, hitGameId, longCaption ); 		
	}
	
	
	
