if (!window.console) {
  console = {};
}

if (!window.console.log) {
  console.log = function() {};
}


jQuery.fn.log = function (msg) {
      console.log("%s: %o", msg, this);
      return this;
};

var gm_util = function() {
  var pub = {};

  pub.openWin = function(url, name, width, height, scrollbar, resize, toolbar, menubar) {

    scrollbar = typeof(scrollbar) != 'undefined' ? scrollbar : 0;
    resize = typeof(resize) != 'undefined' ? resize : 0;
    toolbar = typeof(toolbar) != 'undefined' ? toolbar : 0;
    menubar = typeof(menubar) != 'undefined' ? menubar : 0;

    // from old global.js
    // if (scrollbar == 'yes') {
    //   if (is.ie == true || is.ns == true) { iWinWidth = iWinWidth + 16; }
    // }

    window.open(url, name, "toolbar=" + toolbar + ",scrollbars=" + scrollbar
                             + ",location=0,directories=0,status=1,resizable=" + resize
                             + ",width=" + width + ",height=" + height + ",menubar=" + menubar);

  }

  var logged_in = false;

  pub.open_login = function() {
    pub.openWin('https://' + document.domain + '/secure/bet-sg?action=go_acct_login',
                'account', 750, 600, 1);
  }

  pub.open_reg = function() {
    pub.openWin('https://www.bingouniversal.com/secure/bet-sg?action=GoRegister',
                'account', 750, 600, 1);
  }

  pub.open_myacct = function() {
    pub.openWin('https://' + document.domain + '/secure/bet-sg?action=go_acct_dep',
                'account', 750, 600, 1);
  }

  pub.logout = function() {
    // TODO
    console.log("Logging out");
    logged_in = false;
    document.location.href = 'https://' + document.domain + '/secure/bet-sg?action=go_logout&redirect_url=' + document.location.href;

  }

  pub.open_game = function(path) {
    if (path.charAt(path.length - 1) != '/') {
      path += '/';
    }

    pub.openWin('https://' + document.domain + '/secure' + path, 'game_window', 765, 560);
  }

  pub.open_bingo = function(path) {
    if (path.charAt(path.length - 1) != '/') {
      path += '/';
    }

    pub.openWin('https://' + document.domain + '/secure' + path,
                'bingo_window', screen.availWidth, screen.availHeight, 0, 1);
  }

  pub.initial_loggedin_check = function() {

    logged_in = ($.cookie('BSQ_Login_INSECURE') == null || $.cookie('BSQ_Login_INSECURE') == "") ? false : true;

    //console.log("Initial logged in state: " + logged_in);
    if (logged_in) {
      $('.nav_loggedout').css('display', 'none');
      $('.nav_loggedin').css('display', 'inline');
    } else {

      $('.nav_loggedin').log('undisplaying').css('display', 'none');
      $('.nav_loggedout').log('displaying').css('display', 'inline');
    }
  }

  pub.loggedin_check = function() {
    // console.log("li check [" + logged_in + "]");
    if (logged_in) {
      if ($.cookie('BSQ_Login_INSECURE') == null || $.cookie('BSQ_Login_INSECURE') == "") {
        logged_in = false;
        console.log('No longer logged in');
        $('.nav_loggedin').css('display', 'none');
        $('.nav_loggedout').css('display', 'inline');
      }
    } else {
       if ($.cookie('BSQ_Login_INSECURE') != null && $.cookie('BSQ_Login_INSECURE') != "") {
        logged_in = true;
        console.log('Logged in');
        $('.nav_loggedout').css('display', 'none');
        $('.nav_loggedin').css('display', 'inline');
      }
    }
    return true;
  }

  pub.is_logged_in = function() {
    return logged_in;
  }

  pub.getFlashHTML = function (file, width, height, wmode, params, version) {
    return "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\"" + width + "\" height=\"" + height + "\" id=\"flash_movie\">"
    + "<param name=\"movie\" value=\"" + file + "?version=" + version + "\">"
    + "<param name=\"quality\" value=\"high\">"
    + "<param name=\"menu\" value=\"false\">"
    + "<param name=\"wmode\" value=\"" + wmode + "\">"
    + "<param name=\"FlashVars\" value=\"" + params + "\">"
    + "<embed src=\"" + file + "?version=" + version + "\" quality=\"high\" type=\"application/x-shockwave-flash\" width=\"" + width + "\" height=\"" + height +"\" wmode=\"" + wmode + "\" menu=\"false\" FlashVars=\"" + params + "\" name=\"flash_movie\"></embed>"
    + "</object>";
  }
  return pub;

}();
