var baseURL = "/";
var minHeight = 667;
var isFlashFullyLoaded = false;
var flashPage = '';

//for colorbox video from flash or html
var wasMusicPlaying = false;

function getViewportSize() {
  var size = [0, 0];
  if (typeof window.innerWidth != "undefined") {
  size = [window.innerWidth, window.innerHeight];
  }
  else if (typeof document.documentElement != "undefined" && typeof document.documentElement.clientWidth != "undefined" && document.documentElement.clientWidth != 0) {
  size = [document.documentElement.clientWidth, document.documentElement.clientHeight];
  }
  else {
  size = [document.getElementsByTagName("body")[0].clientWidth, document.getElementsByTagName("body")[0].clientHeight];
  }
  return size;
}

function rePositionFooter(){
  



  /*
  var size = getViewportSize();

  if(size[1] >=minHeight){
    var newTop = size[1] + jQuery(window).scrollTop() - 38;
      jQuery("#footer-wrapper").css("top", newTop);
  }else{
    var newTop = jQuery(document).height() - 38;
    jQuery("#footer-wrapper").css("top", newTop );
  }
  */

alert( $(document).height() );
  $("#footer-wrapper").css("position","absolute").css("top", $(document).height() );



}


function createFullBrowserFlash() {
  

  swfobject.createCSS("html", "height:100%;");
  swfobject.createCSS("body", "height:100%;");
 

  swfobject.createCSS("#flashContentContainer", "margin:0; width:100%; height:100%");

  $(window).resize(function(){
   reSizeWindow(minHeight);
  });


}

function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}
  
function loadFlashPage(page){
  
  if(isFlashFullyLoaded){

      jQuery("#bodyOverlay").hide();
      jQuery("#sub_menu_content").hide('slow');    
       
    getFlashMovie("flashContent").subpageChange(page);
  }
}

function switchLanguage(lang,loadFlash){
  
  if(typeof(loadFlash) == undefined){
    loadFlash = true;
  }
  var code;
  
  switch(lang){
    case "en":
      code = 1;
      break;
    case "cn":
      code = 3;
      break;
    default:
      code = 1;
      break;
  }
  
  $("#currentLanguage").val(lang);
 

  jQuery.ajax({
    url: "/data-gateway/getcurrentlanguagevariables.dot?com.dotmarketing.htmlpage.language="+code,
    dataType: 'json',
    success: function(json){
      
      jQuery("#nav-hotel").html(json.menu_hotels);
      jQuery("#nav-careers").html(json.menu_careers);
      jQuery("#nav-contact").html(json.menu_contact);
      jQuery("#nav-legal").html(json.menu_legal);
      jQuery("#nav-switchHtml").html(json.menu_switchHtml);
      jQuery("#nav-mLife").html(json.menu_mLife);
      jQuery("#label-copyright").html(json.label_copyright);
      
      jQuery("#label_share").html(json.label_share);



    jQuery("#label_sortBy").html(json.label_sortBy);
    jQuery("#subMenuSortByBrand").html(json.label_brand);
    jQuery("#subMenuSortByRegion").html(json.label_region);
    jQuery("#label_asia").html(json.label_asia);
    jQuery("#label_northamerica").html(json.label_northamerica);

    jQuery(".label_mgmlasvegas").html(json.label_mgmlasvegas);
    jQuery(".label_mgmsanya").html(json.label_mgmsanya);

    jQuery(".label_mgmdetroit").html(json.label_mgmdetroit);
    jQuery(".label_bellagio").html(json.label_bellagio);
    jQuery(".label_skyloftslasvegas").html(json.label_skyloftslasvegas);

  jQuery(".label_lasvegas_usa").html(json.label_lasvegas_usa);

  jQuery(".label_sanya_china").html(json.label_sanya_china);
  jQuery(".label_detroit_usa").html(json.label_detroit_usa);





    

      jQuery("#switcher-en").removeClass("active");
      jQuery("#switcher-cn").removeClass("active");
   
      var _lang = $("#currentLanguage").val();
    
      jQuery("#nav-careers").click(function(){
        loadFlashPage('careers');
      });
      jQuery("#nav-contact").click(function(){
        loadFlashPage('contact');
      });
      jQuery("#nav-legal").click(function(){
        loadFlashPage('legal');
      });
      
      switch(lang){
        case 'en':
            $("#nav-switchHtml").attr("href","/html/default");
            break;
        case "cn": 
             $("#nav-switchHtml").attr("href","/cn/html");
            break;         
      }
      
   
$(".navHide").show();
      
      jQuery("#switcher-"+lang).addClass("active");
      if(loadFlash){
        getFlashMovie("flashContent").changeLang(lang,code);
      }

    }
  });
}


function setMinScrollHeight(height){


  minHeight = height;
  reSizeWindow(height);


}

function reSizeWindow(height){
  if( $(window).height() < height ){

    $("#flashContentContainer").css('height',height);
    $("#footer-wrapper").css("position","absolute").css("top",height-30);

  }else{

    $("#flashContentContainer").css('height','100%');
    $("#footer-wrapper").css("position","absolute").css("top", $(window).height() - 38 );

    minHeight = 667;
  }

}

var isMusicBeingPlayed = true;

function changeFlashVolume(to){
  getFlashMovie('flashContent').changeVolume(to);
}

function toggleFlashMusic(){
  var control = $("#audio-control");
  
  if(isFlashFullyLoaded){
  
    if(isMusicBeingPlayed){
      
      control.removeClass('audiohover-on').addClass('audiohover-off');
                        
       getFlashMovie('flashContent').changeVolume(0);
      isMusicBeingPlayed = false;
    }else{
      control.removeClass('audiohover-off').addClass('audiohover-on');
      
      
       getFlashMovie('flashContent').changeVolume(1);
       isMusicBeingPlayed = true;
    }
  
  }
}


function setFlashFullyLoaded(){
  
  
  isFlashFullyLoaded = true;
 // $("#language").css("display","inline");
  $("#audio").show();
  
  /*
  if(flashPage.match(/#/i)){
      var languageSep = flashPage.split("#");
      var splited = languageSep.split("/");
      flashPage = splited[2];
  //  loadFlashPage(flashPage);
  }
  */



  $("#audio-control").addClass("audiohover-on").removeClass("audiohover-off");
 
}

function showSubMenuContent(show,hide){
  $("#subMenuContent"+hide).fadeOut(200,function(){
   $("#subMenuContent"+show).fadeIn(400);
  });
  $("#subMenuSortBy"+show).addClass('active');
  $("#subMenuSortBy"+hide).removeClass('active');
}  


function openCareersIframe(section,lang){

  if(lang == 'cn'){
    lang = 'zh-CN';
  }

  var jobSearch = "https://mgmhospitality.taleo.net/careersection/2/jobsearch.ftl?lang="+lang;
  var createProfile = "https://mgmhospitality.taleo.net/careersection/iam/accessmanagement/login.jsf?lang=";
  createProfile += lang;
  createProfile += "&redirectionURI=https%3A%2F%2Fmmhospitality.taleo.net%2Fcareersection%2Fprofile.jss%3Flang%3D";
  createProfile += lang;
  createProfile += "%26type%3D1%26csNo%3D2%26portal%3D101430233%26postdata%3D%24oWlVNFPaGe5fJ6mXmSOIxQ%3D%3D&TARGET=https%3A%2F%2Fmmhospitality.taleo.net%2Fcareersection%2Fprofile.jss%3Flang%3D";
  createProfile += lang+"%26type%3D1%26csNo%3D2%26portal%3D101430233%26postdata%3D%24oWlVNFPaGe5fJ6mXmSOIxQ%3D%3D";

  
    var clickapply = "https://mgmhospitality.taleo.net/careersection/2/jobsearch.ftl?lang="+lang;
  
  var pages = {
    jobsearch: jobSearch,
    createprofile: createProfile,
    clickapply: clickapply
  }

  jQuery.fn.colorbox({
    href: pages[section],
    iframe: true,
    innerWidth: 800,
    innerHeight: 600,
    open: true
  });


}

function detectMobileAndRedirect(){
  if( navigator.userAgent.match(/Android/i) ||
       navigator.userAgent.match(/webOS/i) ||
       navigator.userAgent.match(/iPhone/i) ||
       navigator.userAgent.match(/iPod/i) ||
       navigator.userAgent.match(/iPad/i) ||
       navigator.userAgent.match(/BlackBerry/i)
       
       ){
    
      if( location.href.match(/cn/i)){
        location.href = '/cn/html';
      }else{
        location.href = '/html/default';
      }
  }

}




function showVideo(deeplink){

  //save current music state
  if(isMusicBeingPlayed){
    wasMusicPlaying = true;
  }else{
    wasMusicPlaying = false;
  }
  
  //mute the volume
  changeFlashVolume(0);

    jQuery.colorbox({
    href:'/pages/en/html/media-video.dot?file='+deeplink,
    iframe: true,
    innerWidth:649,
    innerHeight:300,
    scrolling: false,
    onClosed: function(){

      //if music was playing before opening this colorbox, play the music again
      if(wasMusicPlaying){
        changeFlashVolume(1);
      }else{
        //if not, mute
        changeFlashVolume(0);
      }
    }
    });
    

}



function showFullscreenPhoto(deeplink){
  

  var splited = deeplink.split("/");
  
  splited[splited.length - 1] = "fullsize/"+splited[splited.length - 1];

  var fullsizeUrl = "http://"+splited[2]+"/"+splited[3]+"/"+splited[4]+"/"+splited[5]+"/"+splited[6]+"/"+splited[7]+"/"+splited[8];
  

  

  jQuery.colorbox({
  href: fullsizeUrl,
  open: true
  });

}

function loadColorbox(){



}

function initialize(){
  
  /*
    flashPage = window.location.href;
    if(window.location.href.match(/cn/i)){
      switchLanguage('cn',false);
      
    }else{
        $(".navHide").show(); 
    }
  */
  
  $("#container").show();
  
  
  $(".clickToMute").click(function(){
    toggleFlashMusic();
  });

    detectMobileAndRedirect();


  

    
    
      jQuery("#nav-careers").click(function(){
        loadFlashPage('careers');
      });
      jQuery("#nav-contact").click(function(){
        loadFlashPage('contact');
      });
      jQuery("#nav-legal").click(function(){
        loadFlashPage('legal');
      });

   
      
    

    jQuery(window).scroll(function(){
  //rePositionFooter();
    });    
    
    var bodyOverlay = $("<div/>");
    bodyOverlay.attr("id","bodyOverlay")
      .addClass('overlay')
      .css('background','#000000')
      .css('opacity',0.8);
    
    jQuery("body").prepend(bodyOverlay);
    
    
    jQuery("#nav-hotel").click(function(){
      jQuery("#bodyOverlay").show();
      jQuery("#sub_menu_content").show('slow');
    });
    
    $("#subMenuClose").click(function(){
      jQuery("#bodyOverlay").hide();
      jQuery("#sub_menu_content").hide('slow');
    });
    
    $("#subMenuSortByBrand").click(function(){
      showSubMenuContent('Brand','Region');
    });
    
    $("#subMenuSortByRegion").click(function(){
      showSubMenuContent('Region','Brand');
    });

     $(".menu-li-hover").hover(function(){
    $(this).css('color','#fff');
   $(this).siblings('img').attr('src','/resources-html/en/images/chrome/menu-arrow-on.png')
  },function(){
    $(this).css('color','#475056');
   $(this).siblings('img').attr('src','/resources-html/en/images/chrome/menu-arrow-off.png');   
  });
  
  
}

