
var iframecount;

function txwidget(siteGUID, rows, cols, style_deprecated, w, h, category) {
  if (typeof rows != 'number') rows = 2;
  if (typeof cols != 'number') cols = 3;

  cell_width = 160;
  cell_height = 190;
  table_width = 7 + cell_width * cols;
  table_height = 7 + cell_height * rows;
  if (typeof w != 'number') w = table_width;
  if (typeof h != 'number') h = table_height;
  if (typeof style_deprecated == 'undefined') style_deprecated = '';
  if (typeof category == 'undefined') category = 'default';

  var pages = 10;
  var cookietag = "widget_page-" + siteGUID + "-" + rows + "-" + cols + "-" + category;
  var page = GetCookie(cookietag);
  if (page == null) {
    // randomly choose a starting page
    var currentTime = new Date();
    page = currentTime.getTime() % pages;
  } else {
    page++;
    if (page >= pages) page = 0;
  }
  SetCookie(cookietag, page);

  if (typeof iframecount == 'undefined') {
    iframecount = 0;
  }
  iframecount++;
  fid = 'txframe' + iframecount;
  var site = "http://scribol.com/";
  var host_page = window.document.location.href;
  var url = encodeURI(site + "txwidget/" + siteGUID + "/" + rows + "/" + cols + "/" + page + "/") + encodeURIComponent(category);
  url = url + '?hpage=' + encodeURIComponent(host_page);
  // NB LINEFEEDS NOT ALLOWED IN JS STRING CONSTANTS
  var output = '<div><iframe id="' + fid + '" scrolling="no" frameborder="0" class="txframe" height="' + h + 'px"	width="' + w + 'px" name="txframe" src="' + url + '"></iframe></div>';
  document.write(output);
}
function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1) {
      endstr = document.cookie.length;
   }
   return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen) {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg) {
         return getCookieVal (j);
      }
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) {
         break;
      }
   }
   return null;
}
function SetCookie (name,value,expires,path,domain,secure) {
   document.cookie = name + "=" + escape (value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "; path=/") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
}

function txwidget_track(siteid, src, ids) {
  var ids = encodeURIComponent(ids);
  var src = encodeURIComponent(src);
  var url = '/traffix/widget_tracker/' + siteid + '?src=' + src + '&ids=' + ids;
  jQuery.get(url);
}


