var trace = function(){}; 
// makes Safari unhappy for some reason; 
//var trace = window.console ? console.log : function() {};
//var trace = window.console && window.console.log || function(){};

var Ajax =
{
  server: Site.Template + '/ajax.php',

  WGet: function(params, replace)
  {
    $.get(Ajax.server, params, function(data) {
      $('#' + replace).html(data);
    });
  }
};

var CutList = function(list, max)
{
  this.list = $(list);
  this.max = max;
  this.state = 0;
  this.overflow = [];
  this.toggler = null;
  this.init();
};

CutList.prototype = 
{
  init: function()
  {
    var p = this;
    this.cut();
    if (this.overflow.length > 0) {
      this.toggler = $(
	'<a href="javascript:void(0)"' +
	' class="cut-toggle">&lsaquo; more &rsaquo;</a>'
      );

      this.toggler.insertAfter(this.list).click(function() {
        p.Toggle();
        return false;
      });
    }
  },

  cut: function()
  {
    p = this;
    this.list.find('li').each(function(i, el) {
      if (i >= p.max) {
	p.overflow.push(el);
	$(el).remove();
      }
    });
  },
  
  Toggle: function()
  {
    var p = this;

    if (this.state == 0 && this.overflow.length > 0) {
      var iv = setInterval(function() {
	var el = p.overflow.shift();
	p.list.append($(el));
	if (p.overflow.length == 0)
	  clearInterval(iv);
      }, 30);
      
      this.toggler.html("&lsaquo; less &rsaquo;");
    } 
    else {
      this.list.find('li').each(function(i, el) {
	if (i >= p.max) 
	  p.overflow.push(el);
      });

      if (this.overflow.length > 0) {
	var i = this.max + this.overflow.length-1;
	var iv = setInterval(function() {
	  $(p.overflow[(i--)-p.max]).remove();
	  if (i < p.max) {
	    clearInterval(iv);
	  }
	}, 30);
      }

      this.toggler.html("&lsaquo; more &rsaquo;");
    }

    this.state = !this.state;
  }
};

var Tabber = function(container)
{
  var my = this;
  this.container = container;
  var items = this.items = [];
  this.currentA = null;
  this.currentDiv = null;
  this.hash = this.container.attr('id');

  $('<div class="clear"/>').insertAfter(this.container.find('ul.tabs'));

  this.container.find('ul.tabs li a').each(function(i, el) {
    el.index = i;
    var t = el.href.substr(el.href.lastIndexOf('#'));
    el = $(el);
    el.click(function() {
      my.activate(this.index);
      return false;
    });
    items.push([el, $(t)]);
  });
  
  var idx = this.getCookie();
  try {
    this.currentA = items[idx][0].addClass('selected');
    this.currentDiv = items[idx][1].addClass('selected');
  }
  catch (e) {}
};

Tabber.prototype =
{
  activate: function(index)
  {
    this.setCookie(index);
    this.currentA.removeClass('selected');
    this.currentDiv.removeClass('selected');
    this.currentA = this.items[index][0].addClass('selected');
    this.currentDiv = this.items[index][1].addClass('selected');
    this.currentA.blur();
  },
  
  getCookie: function()
  {
    var c = $.cookie('tablist');
    if (c) {
      var m = null, a = c.split(';');
      var re = /^(.[^:]*):(\d+)/;
      for (var i = 0; i < a.length; i++) {
	if ((m = a[i].match(re)) && m[1] == this.hash)
	  return m[2];
      }
    }

    return 0;
  },
  
  setCookie: function(index)
  {
    if (!this.hash || !this.hash.length)
      return;

    var o = [], c = $.cookie('tablist');

    if (c && c.length) {
      var m = null, a = c.split(';');
      var re = /^(.[^:]*):(\d+)/;
      for (var i = 0; i < a.length; i++) {
	if (a[i].match(re)) continue;
	o.push(p[i]);
      }
    }
 
    o.push(this.hash+":"+index);
    $.cookie('tablist', o.join(';'), { path: '/blog', expires: 1000*2 });
  }
};

jQuery.fn.extend({ tabber: function(settings) { new Tabber(this); } });

function inject_js(url, target, callback)
{
  var s = $('<script type="text/javascript" src="' + url + '"></script>');
  $(target||document.body).append(s);
}

function require(url)
{
  $('head').append($('<script type="text/javascript" src="'+url+'"></script>'));
}

var _gat = null;
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

$(function() {
  $('body').find("ul[class~=cut]").each(function(i, el) {
    new CutList(el, 5);
  });

  require(Site.Template + '/jquery.cookie.js');

  $('div.tabs').tabber();

  if (SOCIAL) {
    inject_js('http://track4.mybloglog.com/js/jsserv.php?mblID=2009101717042105');
    // Coogle Friend Connect
    var skin = {};
    skin['BORDER_COLOR'] = '#cccccc';
    skin['ENDCAP_BG_COLOR'] = '#f2f2f2';
    skin['ENDCAP_TEXT_COLOR'] = '#333333';
    skin['ENDCAP_LINK_COLOR'] = '#4879b7';
    skin['ALTERNATE_BG_COLOR'] = '#ffffff';
    skin['CONTENT_BG_COLOR'] = '#ffffff';
    skin['CONTENT_LINK_COLOR'] = '#4879b7';
    skin['CONTENT_TEXT_COLOR'] = '#333333';
    skin['CONTENT_SECONDARY_LINK_COLOR'] = '#4879b7';
    skin['CONTENT_SECONDARY_TEXT_COLOR'] = '#666666';
    skin['CONTENT_HEADLINE_COLOR'] = '#333333';
    skin['NUMBER_ROWS'] = '4';
    google.friendconnect.container.setParentUrl('/' /* location of rpc_relay.html and canvas.html */);
    google.friendconnect.container.renderMembersGadget({ 
      id: 'div-4402678322681180459',
      site: '15570492747412923687' 
    }, skin);
    
    // Google analytics
    inject_js(gaJsHost + "google-analytics.com/ga.js");
  
    var ival = 0, count = 0;;
    try {
      ival = setInterval(function() {
	if (count++ > 30) {
	  clearInterval(ival);
	  return;
	}
	if (_gat) {
	  trace("Found gat");
	  clearInterval(ival);
	  var pageTracker = _gat._getTracker("UA-11160439-1");
	  pageTracker._trackPageview();
	}
      }, 200);
    } catch(err) { clearInterval(ival); }
  }
  
  var jc;
  if ((jc = $('#jcaptcha')) && jc.length > 0) {
    $.get(Site.Template + '/ajax.php', { action: 'jcaptcha' }, function(data) {
      jc.val(data);
    });
  }
});