function open_pay_by_check(page_url) {
  window.open('/pay-by-check');
  if (!typeof(page_url) == 'undefined') {
    document.location = page_url;
  }
}

function open_pay_by_epay(page_url) {
  window.open('/account/epayment');
  if (!typeof(page_url) == 'undefined') {
    document.location = page_url;
  }
}


jQuery(document).ready(function() {
  jQuery("input.hint").each(function() {
    jQuery(this).defaultValueActsAsHint();
  });
});


function ensure_facebook_permissions(redirect_path) {
  FB.login(function(response) {
    if (response.session) {
      if (response.perms) {
        window.location = redirect_path;
      } else {
        alert('You must allow access to your email address.');
      }
    }
  }, { perms:'email,user_birthday' });
}

function logout_of_facebook() {
  FB.logout(function(response) {
    window.location = '/logout';
  });
}

/**
* This is just an object to hold global variables needed in
* javascripts throughout a page's lifetime.
*
* Example:
*
* function a()
* {
*     Global.ClickedSomeLink = true;
* }
*
* function b()
* {
*     if (Global.ClickedSomeLink)
*     {
*         alert("Already clicked!");
*     }
* }
*
**/
var Global = new Object();

// Grab browser version
Global.BrowserIE = (navigator.appVersion.match(/\bMSIE\b/)) ? true : false;
Global.BrowserNS = (navigator.appName.match(/Netscape/)) ? true : false;
Global.BrowserFirefox = (navigator.userAgent.match(/Firefox/)) ? true : false;
Global.OSMac = (navigator.userAgent.match(/Macintosh/)) ? true : false;

// This will be called when the page loads
var page_init = function () {

  // We only want to correct the image if this is IE, otherwise we ignore it outright
  if(Global.BrowserIE) {
    for(var i=0; i<document.images.length; i++)
    {
      var img = document.images[i]
      var imgName = img.src.toUpperCase()
      if (imgName.match(/\.PNG/))
      {
        if ( img.src.indexOf("google.com") != -1 ) {
          continue;
        }
        var imgID = (img.id) ? "id='" + img.id + "' " : ""
        var imgClass = (img.className) ? "class='" + img.className + "' " : ""
        var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
        var imgStyle = "display:inline-block;" + img.style.cssText
        if (img.align == "left") imgStyle = "float:left;" + imgStyle
        if (img.align == "right") imgStyle = "float:right;" + imgStyle
        if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
        var strNewHTML = "<img " + imgID + imgClass + imgTitle
        + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
        + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
        + "(src=\'" + img.src + "\', sizingMethod='image');\" />"
        img.outerHTML = strNewHTML
      }
    }
  }
}

/* Some foo for sorting */
function sort_by(col) {
	var same_col = ($('order[col]').value == col)

	$('order[col]').value = col

	if( same_col) {
		if(!$('order[direction]').value || $('order[direction]').value == 'desc') {
			$('order[direction]').value = 'asc'
		}
		else {
			$('order[direction]').value = 'desc'
		}
	}
	else {
		$('order[direction]').value = 'asc'
	}
	$('sort_form').submit()
}
/* end of sorting foo */

function reload_ad_banners() {
  $$('.ad_banner iframe').each(
    function(value,index) {
      value.src = value.src;
    }
  );
}

function update_login() {
	// jQuery.get("/login_header", function(data) {
		// jQuery("#signin").html(data);
	// });
  new Ajax.Updater('signin', '/login_header', { method: 'get' });
}

function update_leaderboard_if_logged_in() {
  var login = get_user_login();

  if ( login.length > 0 ) {
    html = '';
    var one_month_one_buck_cookie = getCookie('onemonthonebuck');
		if (one_month_one_buck_cookie=="true") {
		  html += '<a href="http://yads.zedo.com/ads2/c?a=438893;g=0;c=838000032;p=8;f=482033;i=0;x=3840;n=838;s=11;k=http://www.spadeclub.com/promotions/one-month-one-dollar"><img src="/images/static_banners/one-month-one-buck.jpg" alt="One Month, One Buck" /></a>';
		}
		else {
		  html += '<a href="/refer-a-friend"><img src="/images/static_banners/refer-a-friend.jpg" alt="REFER A FRIEND" /></a>';
		}
		$('leaderboard').innerHTML = html;
  }
}

function update_divs_for_the_user(the_user) {
  var login = get_user_login();

  if ( login.length > 0 && login == the_user ) {
    Event.observe(window, 'load', function(){
      $$('.for_the_user').each(function(ele){ele.show();});
    });
  }
}

function this_is_the_user(the_user) {
  var login = get_user_login();

  if ( login.length > 0 && login == the_user ) {
    return true;
  }

  return false;
}

function get_user_login() {
  var enduser_cookie = '';
  var shadow_cookie = '';
  var login = '';

  enduser_cookie = getCookie('enduser');
  shadow_cookie = getCookie('shadow_enduser');

  if ( enduser_cookie != null ) {
    login = enduser_cookie.substring(enduser_cookie, enduser_cookie.indexOf('~'));
  }

  if ( login.length > 0 ) {
    return login;
  }

  if ( shadow_cookie != null ) {
    login = shadow_cookie.substring(shadow_cookie, shadow_cookie.indexOf('~'));
  }

  if ( login.length > 0 ) {
    return login;
  }

  return '';
}

function user_is_logged_in() {
  var login = get_user_login();

  if ( login.length > 0 ) {
    return true;
  }

  return false;
}

function profile_promo_exclusive() {
  var profile_promo_cookie = getCookie('profilepromoexclusive');
  return (profile_promo_cookie != null && profile_promo_cookie == 'true');
}

function open_flash_game() {
  var width = 957;
  var height = 717;
  var left = parseInt((screen.availWidth/2) - (width/2));
  var top = parseInt((screen.availHeight/2) - (height/2));

  window.open(
    '/flash-game',
    "game",
    "width=" + width + ", height=" + height + ", left=" + left 
    + ", top=" + top + ", screenX=" + left + ", screenY=" + top 
    + ", location=no, menubar=no, status=no, resizeable=no");
  return false;
}
