// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
jQuery.ajaxSetup({
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});

function formsWithAjax() {
	$("form.ajax").ajaxForm({dataType: 'script'});
	$("form.ajax").addClass("ajaxed");
	$("form.ajax").removeClass("ajax");
}

jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

function linksWithAjax() {
	$("a.ajax").click(function () {
		window.lastclick = $(this);
		$.ajax({
        type: 'GET',
        url: $(this).attr("href"),
        dataType: "script"
    });
    return false;
	});
	$("a.ajax").addClass("ajaxed");
	$("a.ajax").removeClass("ajax");
};

function linksToJavascript() {
	$("a.jslink").addClass("jslinked");
	$("a.jslink").removeClass("jslink");
	for (i = 0; i < $("a.jslinked").length; i++) {
		widget = $("a.jslinked")[i];
		selecta = "#" + widget.attributes.loaddiv.nodeValue;
		$(selecta).html("Loading <img src=\"http://assets.spinfast.com.au/snc/images/loading-ball-animation.gif\" width=\"18\" height=\"18\">");
		$.ajax({type: 'GET', url: widget.href, dataType: "script"});
	}
};

function setupTabs() {
	tabvisible = ".tabheader #" + $(".tabvisible").attr("id");
	$(tabvisible).css("background-color", "#0B4F10");
	$(".tab").click(function () {
		tabvisible = ".tabheader #" + $(".tabvisible").attr("id");
		$(tabvisible).css("background-color", "");
		$(".tabvisible").removeClass("tabvisible");
		newtabid = $(this).attr("id");
		tabvisible = ".tabheader #" + newtabid;
		$(tabvisible).css("background-color", "#0B4F10");
		$(".tabmain").filter("#" + newtabid).addClass("tabvisible");
		return false;
	});
};

function uiWidgets() {
	
};

function loadGame(gameid, url) {
	$.ajax({type: 'GET', url: url + "?id=" + gameid, dataType: "script"})
}

$(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
  // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

$(document).ready(function() {
	linksWithAjax();
	formsWithAjax();
	linksToJavascript();
	xd = '/fb/xd_receiver.html';
	FB.init(window.facebook_api_key, xd);
	setupTabs();
	uiWidgets();
});
