var EXPRESS_INSTALL = "/expressInstall.swf";
$.overlay = function(html, classes, timeout){
  var b = $("body");
  b.append("<div class=\"overlay\"></div><div class=\"overlay_content   " + classes + "\"><a class=\"close\" href=\"javascript: void(0);\"></a>" + html + "</div>");
  var overlay = $(".overlay");
  var content = $(".overlay_content");
  var close = content.children("a:first");
  overlay.css({"width": "100%", "height" : b.height() + "px", "opacity" : "0.0"});
  
  var scrollTopOffset;
  if (window.pageYOffset){
    scrollTopOffset = (window.pageYOffset + 200) + 'px';
  } else {
    scrollTopOffset = document.documentElement.scrollTop + 200 + 'px';
  }
  
  content.css({"left" : ((b.width() - content.width()) / 2) + "px", "top" : scrollTopOffset});
  
  
  overlay.animate({"opacity": "0.8"}, 200, function(){
    content.fadeIn(100);
  });
  
  close.click(function(){ 
    overlay.remove();
    content.remove();
  });
  if (timeout > 0){
    setTimeout(function(){ 
      content.animate({"opacity": "0.0"}, 200, function(){
        overlay.fadeOut(100);
      });
      content.remove();
    }, timeout);
  }
}

$.fn.overlay = function(timeout){
  html = $(this).html();
  $.overlay(html, $(this).attr("class"), timeout);
}

$.loadGame = function(game, swfWidth, swfHeight, user_id){
  var b = game.filename.split("/");
  pageTracker._trackPageview("/game/" + b[b.length - 1]);
  b.pop();
  var basePath = b.join("/") + "/";
  var html = "<div id='game'><a class=\"close\" href=\"javascript: void(0);\"></a><h2>You need to install Flash player</h2><p>You can install it from <a href=\"http://www.adobe.com/support/flashplayer/downloads.html\">Adobe</a></div>" + game['footer'];
  if (swfWidth == null){
    swfWidth = 600;
  }
  if (swfHeight == null){
    swfHeight = 400;
  }
  var game_id = b.pop();
  var b = $("body");
  b.append("<div class=\"overlay\"></div><div class=\"overlay_content\"><a class=\"close\" href=\"javascript: void(0);\"></a>" + html + "</div>");
  var overlay = $(".overlay");
  var content = $(".overlay_content");
  var close = content.children("a:first");
  overlay.css({"width": "100%", "height" : b.height() + "px", "opacity" : "0.0"});
  var footer_height = $(".game_footer").height();
  content.css({"left" : ((b.width() - swfWidth) / 2) + "px", "width" : swfWidth + "px", "min-height" : (parseInt(swfHeight) + footer_height) + "px"});
  var flashVars = {register_url : "http://" + document.location.host + "/users/new", gameID : game_id, serviceURL : document.location.protocol + "//" + document.location.host + "/"}
  if (user_id != "null"){
    flashVars.user = user_id;
  }
  swfobject.embedSWF(game.filename, "game", swfWidth, swfHeight, "10.0.0", EXPRESS_INSTALL, flashVars, {base : basePath, allowscriptaccess : "always"}, null, function(e){
    $('html,body').animate({scrollTop: 180}, 100, function(){
      overlay.animate({"opacity": "0.8"}, 200, function(){
        content.fadeIn(100);
      });
    });
  });
  close.click(function(){
    overlay.remove();
    content.remove();
  });
}


$.loadVideo = function(game, swfWidth, swfHeight){
  var b = game.filename.split("/");
  pageTracker._trackPageview("/video/" + b[b.length - 1]);
  b.pop();
  var basePath = b.join("/") + "/";
  var html = "<div id='video'><a class=\"close\" href=\"javascript: void(0);\"></a><h2>You need to install Flash player</h2><p>You can install it from <a href=\"http://www.adobe.com/support/flashplayer/downloads.html\">Adobe</a></div>" + game.footer;
  if (swfWidth == null){
    swfWidth = 600;
  }
  if (swfHeight == null){
    swfHeight = 400;
  }
  var b = $("body");
  b.append("<div class=\"overlay\"></div><div class=\"overlay_content\"><a class=\"close\" href=\"javascript: void(0);\"></a>" + html + "</div>");
  var overlay = $(".overlay");
  var content = $(".overlay_content");
  var close = content.children("a:first");
    
  overlay.css({"width": "100%", "height" : b.height() + "px", "opacity" : "0.0"});
  content.css({"left" : ((b.width() - swfWidth) / 2) + "px", "width" : swfWidth + "px", "min-height" : swfHeight + "px"});
  
  close.click(function(){ 
    overlay.remove();
    content.remove();
  });
  swfobject.embedSWF("/VideoPlayer.swf?url=" + game.filename, "video", swfWidth, swfHeight, "10.0.0", "/expressInstall.swf", null, {base : basePath}, null, function(data){
    $('html,body').animate({scrollTop: 180}, 100, function(){
      overlay.animate({"opacity": "0.8"}, 200, function(){
        content.fadeIn(100);
      });
    });
  });
}