var interstitialObj;
var currentLandingPage;
var addthis_config={services_compact: 'myspace,facebook,email,twitter,more'};
var trackGuides = [];


window.onbeforeunload = function(){
	clearNtrstl();
}

function clearNtrstl(){
  $('#ntrstlWrap').empty();
}


$(document).ready(function(){
  
	var isHome = ( $('#main').hasClass('home') ) ? true : false;
	interstitialObj = $('<div id="ntrstlWrap"></div>')
    .appendTo('body')
    .css({
      'position': 'absolute',
      'z-index': '99'
    })
    .ntrstl();

  $('#primaryNav li:has(ul)').mouseenter(function(){
    $(this).find('ul').stop(true, true).fadeIn(300);
    $(this).find('a').addClass('active');
  }); 
  $('#primaryNav li:has(ul)').mouseleave(function(){
    $(this).find('ul').stop(true, true).fadeOut(300);
    $(this).find('a').removeClass('active');
  }); 
  
  $('#soundToggle').click(function(){
	  speakerSwitch();
		$('.soundText').text($('.soundText').text()=='SOUND OFF' ? 'SOUND ON' : 'SOUND OFF');
		$('.soundOffIcon').toggleClass('soundOnIcon');
	});

	$('#primaryNav li:has(ul.subnav) > a').click(function(){
		var clickedClass = $(this).parent().attr("class");
	    
    if( isHome )
      NavigationClicked(clickedClass);
    else
      loadLandingPage(clickedClass);

		return false;
	});

  $('#facebookGuide').mouseleave(function(){
    if( $.inArray( 'facebook', trackGuides ) == -1 ){
      StopHomePageRender();
      loadLandingPage('thatguide-facebook');
      trackGuides.push( 'facebook' );
    }
  });

});

function loadLandingPage(linkID){
   var links ="";
   var swf = "";
       var height = "100%";
   var clickedClass =  $(this).parent().attr("class");
   var dim = true;
  
   //GRAB LINKS FROM SUB NAV
   $("#primaryNav li."+linkID).find("ul li a").each(function(){
      links += ($(this).attr("href"))+",";
      
  });
  links = links.substring(0,links.length-1);  //TAKE OFF TRAILING COMMA

  //MAP CLASS NAME TO SWF NAME
  switch(linkID)
  {
    
    case "comedy_club":  
      swf = "/_flash/comedyclubLandingPage.swf";
      height="650px";
      dim = false;
    break;
    
    case "bar":  
      swf ="/_flash/barLandingPage.swf";
      height="650px";
      dim = false;
    break;
    
    case "who_is_that_guy": 
      swf ="/_flash/whoIsThatGuyLandingPage.swf";
      height="700px";
      dim = false;
    break;
    
    case "facts": 
      swf ="/_flash/factsLandingPage.swf";
      height="650px";
      dim = false;
    break;
  
    case "multimedia":
      swf ="/_flash/multimediaLandingPage.swf";
      height = "550px";
      dim = false;
    break;
	
	case "buzz_kill":
	  window.location.href="/buzz-kill/buzz-kill.php";
	  return true;
	break;
    
	case "thatguide-homepage-bar-phone":
		  swf ="/_flash/That Guide/ThatGuideHomepageBarPhone.swf";
		  height = "100%";
		  links = "bar/bar.htm#/CellphoneGallery";
	   break;
	
    case "thatguide-homepage-comedy-club":
      swf ="/_flash/That Guide/ThatGuideHomepageComedyClub.swf";
      height = "100%";
      links = "comedy-club/live-comedy-club-videos.htm";
    break;
	
	case "thatguide-homepage-idle-smash":
      swf ="/_flash/That Guide/ThatGuideHomepageIdleSmash.swf";
      height = "100%"; 
    break;  
	
	case "thatguide-facebook":
      swf ="/_flash/That Guide/ThatGuideFacebook.swf";
      height = "100%";
      links = "http://www.facebook.com/thatguy?ref=ts";
    break;
    
    default: return true;
  }

  currentLandingPage = linkID;

  //Load SWF and set CurrentLandingPage
  interstitialObj.ntrstl('load', {swf: swf, height: height, flashVars: { data: links, animateOutJavascriptCallBack: "unloadLandingPage()"}, dim: dim});
};

function unloadLandingPage(){
  interstitialObj.ntrstl('unload');
  currentLandingPage = '';
}

