var Application = function()
{
  var me = this;
  
  $(document).ready(function()
  {
    me.init();
    me.initUI();
  });
}

Application.prototype.init = function()
{
  this.setupVisitorTracking();
}

Application.prototype.initUI = function()
{
  var me = this;
  
  this.affiliate = eval('(' + $("#affiliate_infos").text() + ')');
  this.webSite = eval('(' + $("#web-site-infos").text() + ')');
  this.user = eval('(' + $("#user-infos").text() + ')');
  this.context = eval('(' + $("#context-infos").text() + ')');
  
  document.body.onunload = function(){me.free()};
  
  this.initStats();
}

Application.prototype.free = function()
{
  if (typeof(eval(window)["GUnload"]) == "function") 
    GUnload();
}

Application.prototype.initStats = function()
{
  var event = {name:"app_ready", type:"run_time", object:"application", data:{pageType:this.context.module + "_" + this.context.action}};
  
  if ($("#store-infos").length > 0)
  {
    var storeInfos = eval('(' + $("#store-infos").text() + ')');
    event.data.store = storeInfos;
  }
  
  Tool.sendStats(this, event);
}

Application.prototype.setupVisitorTracking = function()
{
  window["_gaq"] = window["_gaq"] || [];
  window["_gaq"].push(['_setAccount', 'UA-18418889-1']);
  window["_gaq"].push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
}

var application = new Application();
