
var header_div;
var content_div;
var footer_div;
var loader_div;
var communityVideo_div;
var communityPlayer_div;
var communityClose_div;
var pageLoader_div;
var pages = {
  'home':
  {
    "url": "home.php",
    "title": "Home"
  },
  'about':{
    "url": "about.php",
    "title": "The Game",
    "callback": function(){
      Yoostar_animateAbout();
    }
  },
  'scenes':{
    "url": "scenes.php",
    "title": "Movie"
  },
  'community':{
    "url": "community.php",
    "title": "Community",
    "callback": function(){
      Yoostar_animateCommunity();
    }
  },
  'buy': {
    "url": "buy.php",
    "title": "Buy Now"
  },
  'news':{
    "url": "news.php",
    "title": "News"
  },
  'news/press':{
    "url": "news.php?type=press",
    "title": "News"
  },
  'xbox': {
    "url": 'xbox.php',
    "title": "Xbox",
    "callback": function(){
      Yoostar_animateXbox();
    }
  },
  'playstation':{
    "url": "playstation.php",
    "title": "PlayStation",
    "callback": function(){
      Yoostar_animatePlaystation();
    }
  },
  'privacy':{
    "url": "privacy.php",
    "title": "Privacy"
  },
  'terms':{
    "url": "terms.php",
    "title": "Terms and Conditions"
  },    
  'acting':{
    "url": "acting.php",
    "title": "Acting with the stars",
    "callback": function(){
      Yoostar_showActingBackground();
    }
  },
  'mtv':{
    "url": "mtv.php",
    "title": "Yoostar on MTV",
    "callback": function(){
      Yoostar_showMTVBackground();
    }
  },
  'mtv2':{
    "url": "mtv2.php",
    "title": "Yoostar on MTV",
    "callback": function(){
      Yoostar_showMTVBackground2();
    }
  }    
}
var hash = location.hash;
var home_url = 'home.php';
var home_title = '';
if (hash != '') {
  hash = hash.replace('#\/', '');
  if (hash.indexOf('news-article') != -1 && hash.indexOf('/') != -1) {
    var page = {};
    page.url = 'news_viewer.php?id=' + hash.substring(hash.indexOf('/') + 1);
  } else {
    var page = pages[hash];
  }
  if (page) {
    home_url = page.url;
    home_title = page.title;
    if (page.callback) {
      var callback = page.callback;
    }
    
    currentPage = page;
  }
}
  
jQuery().ready(function(){
  if(!FlashDetect.installed && BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 7)
  {
    Yoostar_showFlashError();
  }
  else
  {
    Yoostar_onReady("header", "footer", "loader", "page_loader", "page_content", "community_video", "community_player");
  }
	
});

function Yoostar_showFlashError()
{
  $("#site").hide();
}

/**
	Function executed when the page is ready to show contents.
 */
function Yoostar_onReady(header, footer, loader, pageLoader, content, community_video,community_player)
{
  $("#noflash").hide();
  
  
  header_div = $("#" + header);
  content_div = $("#" + content);
  footer_div = $("#" + footer);
  communityVideo_div = $("#" + community_video);
  communityPlayer_div = $("#" + community_player);
  communityClose_div = $("#community_close");
  loader_div = $("#" + loader);
  pageLoader_div = $("#" + pageLoader);

  header_div.hide();
  content_div.hide();
  footer_div.hide();
  communityVideo_div.hide();
  communityPlayer_div.hide();
  communityClose_div.hide();
  pageLoader_div.hide();
  loader_div.show();
	
  Yoostar_loadMain();
}



function Yoostar_loadPage(url, page, callback)
{  
  currentPage = page;
  content_div.load(url, function(){
    Yoostar_onPageLoad(callback);
    trackPageGA();
  });
	
  content_div.hide();
  footer_div.hide();
  pageLoader_div.show();
  $("#site").css("background","none");
}

function Yoostar_onPageLoad(callback)
{
  $.doTimeout( 1000, function(){
    Yoostar_initPage(callback);
  });
}


function Yoostar_initPage(callback)
{
  content_div.show();
  footer_div.show();
  pageLoader_div.hide();
  $("#site").css("background","url(img/background.jpg) no-repeat top center");
  if(typeof callback == 'function'){
    callback();
  }
  Yoostar_setTitle(currentPage);
}

function Yoostar_showCommunityVideo(video)
{
  communityClose_div.fadeIn("slow");
  communityPlayer_div.fadeIn("slow");
  communityVideo_div.css('filter', 'alpha(opacity=40)');
  communityVideo_div.fadeIn("slow");
  communityVideo_div.css("height",$(document).height());
  var player = $("#jquery_jplayer_community").data("jPlayer");
  if(player){
    $("#jquery_jplayer_community").jPlayer("setMedia", {
      m4v: video.m4v,
      ogv: video.ogv,
      poster: video.poster
    });
  } else { 
    $("#jquery_jplayer_community").jPlayer({
      ready: function(){
        $("#jquery_jplayer_community").jPlayer("setMedia", {
          m4v: video.m4v,
          ogv: video.ogv,
          poster: video.poster
        });
      },
      ended: function(event){
        trackVideoGA(location.hash.replace('#\/', ''), 'completed', 'jquery_jplayer_community');
      },
      swfPath: "js",
      supplied: "m4v, ogv",
      cssSelectorAncestor: '#jp_interface_index',
      cssSelector: {
        videoPlay: '#player_index_video_play',
        play: '#player_index_play',
        pause: '#player_index_pause'
      }
    });
  }
}
function Yoostar_showHomeVideo (video,autoPlay){
  if (autoPlay === true) {
    $("#jquery_jplayer_1").jPlayer("setMedia", {
      m4v: video.m4v,
      ogv: video.ogv,
      poster: video.poster
    });
    $('#home_close').fadeIn('slow');
    $('.showcase_boxes').fadeOut('slow');
    $('#jp_video_0').show();
    $('#jquery_jplayer_1 object').show();
    $("#jquery_jplayer_1").jPlayer("play");
	
  } else {
    $("#jquery_jplayer_1").jPlayer({
      ready: function(){
        //$('#home_close').fadeIn('slow');
        $('#jquery_jplayer_1').jPlayer("setMedia", {
          m4v: video.m4v,
          ogv: video.ogv,
          poster: video.poster
        });
      },
      ended: function(event){
        $('.showcase_boxes').fadeIn('slow');
        $('#jp_video_0').hide();
        $('#jquery_jplayer_1 object').hide();
        $('#player_home_video_play').fadeIn('slow');
        $('#jp_poster_0').fadeIn('slow');
        trackVideoGA('Home', 'completed', 'jquery_jplayer_1');
      },
      swfPath: "js",
      supplied: "m4v, ogv",
      cssSelectorAncestor: '#jp_interface_home',
      cssSelector: {
        videoPlay: '#player_home_video_play',
        play: '#player_home_play',
        pause: '#player_home_pause'
      }
    });
  }
}
function Yoostar_hideCommunityVideo()
{
  communityPlayer_div.fadeOut("slow");
  communityVideo_div.fadeOut("slow");
  communityClose_div.fadeOut("slow");
  //$("#jquery_jplayer_community").jPlayer("stop");
  $("#jquery_jplayer_community").jPlayer("clearMedia");
}

/***** Utility functions *****/

function Yoostar_loadMain()
{
  content_div.load(home_url, function(){
    Yoostar_onMainLoad();
    Yoostar_setTitle(home_title);
    trackPageGA();
    if(callback) {
      callback();
    }
  });
}

function Yoostar_onMainLoad()
{
  $.doTimeout( 500, function(){
    Yoostar_init();
  });
}

function Yoostar_init()
{
  loader_div.fadeOut('slow',Yoostar_onInit);
  header_div.show();
  content_div.show();
  footer_div.show();
}

function Yoostar_onInit()
{
  loader_div.hide();
}

function Yoostar_setTitle(title) {
  var page_title = 'Yoostar 2 - Movie Karaoke';
  
  if(title)
  {
    page_title = page_title + ": " + title;
  }
  document.title = page_title;
}

/* Track Pages with Google Analytics */
function trackPageGA() {
  _gaq.push(['_setAccount', 'UA-16785914-1']);
  if(location.hash.replace('#\/', '') != '') {
    _gaq.push(['_trackPageview', location.hash.replace('#\/', '')]);
  } else {
    _gaq.push(['_trackPageview', 'home']);
  }
}

/**
 * Push a click event to GA.
 */
function trackLinkGA(page, value) {
  _gaq.push(['_trackEvent', 'Links - ' + page, 'click', value]);
}

/**
 * Push a video play event to GA.
 * 
 */
function trackVideoGA(page, action, playerId) {
  var player = $("#" + playerId).data("jPlayer");
  if (player) {
    var value = getVideoName(player.status.src);
    var percentage = 0;
    if(action == 'completed') {
      percentage = 100;
    } else {
      percentage = Math.round(player.status.currentPercentAbsolute);
    }
    _gaq.push(['_trackEvent', 'Videos - ' + page, action, value, percentage]);
  }  
}

/**
 * Push a click to download to GA.
 */
function trackDownloadGA(page, action, value) {
  _gaq.push(['_trackEvent', 'Downloads - ' + page, action, value]);
}

function getVideoName(src) {
  return src.substring(src.lastIndexOf('/') + 1);
}

