var isSafari;

function xGetElementById(e) {
   if(typeof(e)!='string'){
    return e;
   }
   if(document.getElementById){
    e=document.getElementById(e);
   }else if(document.all){
    e=document.all[e];
   }else{
    e=null;
   }
   return e;
}



function initShadowbox(){

	Shadowbox.init({
    skipSetup: true,
    enableKeys: false,
    viewportPadding: 0,
    modal: true,
    //animate: false,
    //animateFade: false,
    onClose: onShadowboxClose,
    players: ["iframe"]
	});
}

function disableFlash(){
   try{
   	var mediaPlayer = xGetElementById('mediaPlayer');
    var countdownClock = xGetElementById('countdownClock');
    var placementTestPage = xGetElementById('placementTestPage');
      if(mediaPlayer != null && mediaPlayer != "undefined"){
  		  mediaPlayer.disableFlash();
  		  mediaPlayer.style.display="none";
  		}
  		if(countdownClock != null && countdownClock != "undefined"){
  	   	countdownClock.style.display="none";
      	}
      	if(placementTestPage != null && placementTestPage != "undefined"){
  	  		placementTestPage.style.display="none";
  	  	}
  	}catch(err){

  	}
}

function enableFlash(){

   try{
   	var mediaPlayer = xGetElementById('mediaPlayer');
    var countdownClock = xGetElementById('countdownClock');
    var placementTestPage = xGetElementById('placementTestPage');
      if(countdownClock != null && countdownClock != "undefined"){
  	   countdownClock.style.display="block";
      }
      if(mediaPlayer != null && mediaPlayer != "undefined"){
        mediaPlayer.style.display="block";
  		  mediaPlayer.enableFlash();
  	  }
  	  if(placementTestPage != null && placementTestPage != "undefined"){
  	  	placementTestPage.style.display="block";
  	  }
  }catch(err){
  
  }
  
}

function abcSearch(abcSearchText){
	openSiteExitWarning('http://app.abc.go.com/siteSearch/searchResults?search=' + abcSearchText + '&searchType=0&x=17&y=14');
}

function onShadowboxClose(){
	 enableFlash();
}

function launchBinderPromotionLink(){
	openSiteExitWarningABCStore('http://abctvstore.cafepress.com/LostUniversity');
}

function openSiteExitWarningABCStore(siteURL){
	shadowboxOpen(445, 318, 'exit_site_warning_abc.php?s=' + siteURL);
	setTimeout('redirectToExternalSite("' + siteURL + '")', 5000);
}

function redirectToExternalSite(siteURL){
	window.open(siteURL, "_blank");
	/*
	try{
		Shadowbox.close();
	}catch(error){
	
	}
	*/

}

function openSiteExitWarning(siteURL){
	shadowboxOpen(445, 318, 'exit_site_warning.php?s=' + siteURL);
	setTimeout('redirectToExternalSite("' + siteURL + '")', 5000);
}

function openRegisterLightbox(){
  disableFlash();
  shadowboxOpen(397, 632, 'register_form.php');
}

function startPlacementTest(){
  if(isSafari == true){
      redirectToPlacementTest();
  }else{
     shadowboxOpen(938, 633, 'placement_test.php');
  }
}

function openLoginLightbox(){
   disableFlash();
   shadowboxOpen(397, 349, 'login_form.php');
}

function showContinuePlacementTest(){
   shadowboxOpen(632, 230, 'continue_placement_test.php');
   
}

function openPhotoToolFromProfileUpdate(){
	setTimeout('closeLightbox()', 100);
	setTimeout('openPhotoUploadLightbox()', 800);
}

function openPhotoUploadLightbox(){
   shadowboxOpen(632, 500, 'photo_tool.php');
}

function openRegisterPhotoUploadLightbox(){
  if(isSafari == true){
    redirectToPlacementTest(true);
  }else{
    openPhotoUploadLightbox();
  }
}

function openProfileUpdateLightbox(){
   shadowboxOpen(397, 515, 'update_info_form.php');
}

function popup(url){
	window.open(url, 'lostuPop', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=1,resizable=1,width=400,height=600');
}

function showTermsPop(countryCode){
	if(countryCode == "JPN"){
		popup('terms_popup_jpn.php');
	}else{
		popup('terms_popup.php');
	}
}

function showEULAPop(){
	popup('eula_popup.php');
}

function showPrivacyPop(){
	popup('privacy_popup.php');
}

function shadowboxOpen(w, h, u){
  
	  Shadowbox.open({
    	content:    u,
   	 	player:     "iframe",
    	title:      "",
    	height:     h,
    	width:      w
    });
}

function continuePlacementTestOnPageLoad(){
  window.onload = function(){
    showContinuePlacementTest();
  }
}

function showPhotoToolOnPageLoad(){
  window.onload = function(){
    openPhotoUploadLightbox();
  }
}

function closeLightbox(){
	try{
		Shadowbox.close();
	}catch(err){
		//do nothing
	}
}

function redirectToBinder(){
  window.location.replace('binder.php');
}

function redirectToHomePage(){
  window.location.replace('index.php');
}

function redirectToBookstore(){
	window.location.replace('http://abctvstore.cafepress.com/LostUniversity');
}

function userLoggedIn(){
  window.location.replace('logged_in.php');
}

function redirectToPlacementTest(showPhotoPop){
    if(showPhotoPop == true){
       window.location.replace('placement_test_page.php?photo=1');
    }else{
       window.location.replace('placement_test_page.php');
    }	
}

function showPlacementTestResults(points){
	points = points + '';
	pointsEarned = points.substring(0, points.indexOf(','));
	pointsTotal = points.substring(points.indexOf(',')+1);
	window.location.replace('register_finish.php?pe=' + pointsEarned + '&pt=' + pointsTotal);
}

isSafari = false;
if(BrowserDetect.browser == 'Safari'){
  isSafari = true;
}

initShadowbox();



