/*
 *
 */

function debug(data) {
  // Can we debug? - Do we have firebug
  var con = null;
  if (typeof console !== 'undefined' && typeof console.log !== 'undefined') {
    con = console;
  } else if (typeof window.console !== 'undefined'
	     && typeof window.console.log !== 'undefined') {
    con = window.console;
  }
			
  // Do the log
  if (con) {
    // Do we support arguments?
    if (typeof arguments !== 'undefined' && arguments.length > 1) {
      con.log(arguments);
      return arguments;
    } else {
      con.log(data);
      return data;
    }
  }
}

function parseUri (str) {
  /*
   * parseUri 1.2.1
   * (c) 2007 Steven Levithan <stevenlevithan.com>
   * MIT License
   */

  var o = parseUri.options,
    m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
    uri = {},
    i = 14;

  while (i--) uri[o.key[i]] = m[i] || "";

  uri[o.q.name] = {};
  uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
    if ($1) uri[o.q.name][$1] = $2;
  });

  return uri;
};

parseUri.options = {
  strictMode: false,
  key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
  q: {
    name: "queryKey",
    parser: /(?:^|&)([^&=]*)=?([^&]*)/g
  },
  parser: {
    strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
    loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
  }
};

function utf8(wide) {
  var c, s;
  var enc = "";
  var i = 0;
  while(i<wide.length) {
    c= wide.charCodeAt(i++);
    // handle UTF-16 surrogates
    if (c>=0xDC00 && c<0xE000) continue;
    if (c>=0xD800 && c<0xDC00) {
      if (i>=wide.length) continue;
      s= wide.charCodeAt(i++);
      if (s<0xDC00 || c>=0xDE00) continue;
      c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
    }
    // output value
    if (c<0x80) enc += String.fromCharCode(c);
    else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
    else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
  }
  return enc;
}

var hexchars = "0123456789ABCDEF";

function toHex(n) {
  return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}

var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

function encodeURIComponentNew(s) {
  var s = utf8(s);
  var c;
  var enc = "";
  for (var i= 0; i<s.length; i++) {
    if (okURIchars.indexOf(s.charAt(i))==-1)
      enc += "%"+toHex(s.charCodeAt(i));
    else
      enc += s.charAt(i);
  }
  return enc;
}

var si_err = "You're not signed in or your session expired. Do you want to reload this page?";
var del_msg = 'Are you sure want to delete this talk?\nThis action can not be undone.';

(function($) {
  $.activeTalk = function(data) {
    $.isFunction(data) ? data.call() : $.activeTalk.activate(data);

    return this;
  }

  $.fn.activeTalk = function() {
    $.activeTalk.activate(this);

    return this;
  }

  $.extend($.activeTalk,
  {
    active: null,

    _getTalk: function(talk) {
      if (talk.hasClass("talk"))
	return talk;

      var p = $j(talk.parents(".talk").get(0));

      if (p && p.hasClass("talk"))
	return p;

      return $j(".talk-list:first > li:first");
    },

    activate: function(talk) {
      if (!talk.hasClass('icon-report'))
	$.popupMenu.hide();

      talk = this._getTalk(talk);

      if (talk.hasClass("talk-page"))
	return;

      if (!talk)
	return;

      if (this.active && talk.attr("id") != this.active.attr("id"))
	this.deactivate();

      this.active = talk;

      var id = talk.attr("id").split('_')[1];

      talk.addClass("talk-active");
      $j("#vote_count_" + id).addClass("vote-count-active");

      $j("#icon_report_" + id).show();
      $j("#icon_share_" + id).show();
      $j("#icon_delete_" + id).show();
      $j("#icon_edit_" + id).show();
    },

    deactivate: function() {
      var id = this.active.attr("id").split('_')[1];

      this.active.removeClass("talk-active");
      $j("#vote_count_" + id).removeClass("vote-count-active");

      this.active = null;

      $j("#icon_report_" + id).hide();
      $j("#icon_share_" + id).hide();
      $j("#icon_delete_" + id).hide();
      $j("#icon_edit_" + id).hide();
    },

    toggle: function(talk) {
      talk = this._getTalk(talk);

      if (!talk)
	return;

      if (!this.active || this.active.attr("id") != talk.attr("id")) {
	this.activate(talk);
      } else {
	this.deactivate();
      }
    }
  });
})(jQuery);

(function($) {
  $.activeEditor = function(data) {
    $.isFunction(data) ? data.call() : $.activeEditor.activate(data);
    return this;
  }

  $.fn.activateEditor = function() {
    $.activeEditor.activate(this);
    return this;
  }

  $.extend($.activeEditor,
  {
    active: null,

    activate: function(editor) {
      if (this.active) {

	if (editor.attr("id") == this.active.attr("id"))
	  return;

	this.deactivate(true);
      }

      this.active = editor;

      var id = this.active.attr("id");

      type = id.substr(0, id.indexOf("_"));
      pid = id.substr(id.lastIndexOf("_") + 1);

      editor.show();

      if ("edit" == type)
	$j("#talk_" + pid + " > .talk-main").hide();
      else {
	var tl = editor.prev(".talk-list");
	tl.addClass("comment-list-rpl");

	if (tl.length && tl.is(":visible")) {
	  var o = editor.offset();
	  $j('html,body').animate({ scrollTop: o.top - 10 }, 1000);
	}
      }
    },

    deactivate: function(noscroll) {
      if (!this.active)
	return;

      var id = this.active.attr("id");

      type = id.substr(0, id.indexOf("_"));
      pid = id.substr(id.lastIndexOf("_") + 1);

      $j("#" + id).hide();
      
      if ("edit" == type)
	$j("#talk_" + pid + " > .talk-main").show();
      else {
	var t = $j("#talk_" + pid);
	var tl = t.children(".talk-list");

	tl.removeClass("comment-list-rpl");
	
	if (!noscroll) {
	  if (tl.length && tl.is(":visible")) {
	    var o = t.offset();
	    $j('html,body').animate({ scrollTop: o.top - 10 }, 1000);
	  }
	}
      }
      
      this.active = null;
    }
  });
})(jQuery);

(function($) {
  $.popupMenu = function(data) {
    $.isFunction(data) ? data.call() : $.popupMenu.toggle(null, data);
  };

  $.fn.popupMenu = function(arrow) {
    $.popupMenu.toggle(this, arrow);

    return this;
  };

  $.extend($.popupMenu,
  {
    arrow: null,
    menu: null,

    show: function(menu, arrow) {
      if (this.menu && this.menu.attr("id") != menu.attr("id")
	  && "none" != this.menu.css("display"))
	this.hide();

      this.arrow = arrow;
      this.menu = menu;
      this.menu.show();

      $(document).bind("click.popupMenu", this._hide);
    },

    _hide: function() {
      $.popupMenu.hide();
    },
    
    hide: function() {
      if (this.menu) {
	if (this.arrow)
	  this.arrow.removeClass("icon-arrow-visible");
	this.menu.hide();
	this.menu = null;
      }

      $(document).unbind("click.popupMenu");
    },

    toggle: function(menu, arrow) {
      if ('none' == menu.css('display'))
	this.show(menu, arrow);
      else
	this.hide();
    }
  });
})(jQuery);

function noAction(e) {
  e.stopPropagation();
  return false;
}

var toggleReply = function(e) {
  var id = this.id.substr(this.id.lastIndexOf("_") + 1);
  var pid = "#talk_" + id;
  var rid = "#reply_" + id;

  e.stopPropagation();

  $j.activeTalk.activate($j(pid));

  if ($j(this).hasClass("popup-login"))
    return false;

  if ($j(this).hasClass("tgl-rpl-a-dis")) {
    if ($j.popupMenu.menu)
      $j.popupMenu.hide();

    $j("#reply_err_popup_" + id).popupMenu();
    return false;
  }

  if ($j(rid).length) {
    if ($j(rid).is(":visible"))
      $j.activeEditor.deactivate($j(this).hasClass("tgl-rpl-a"));
    else
      $j.activeEditor.activate($j(rid));
  } else {
    var reply = $j("#reply").clone();

    reply.find(".div-tags").remove();
    
    reply.click(function(e) {
      e.stopPropagation();
      $j.activeTalk($j(pid));
      return false;
    });

    reply.attr("id", "reply_" + id);
    reply.prepend('<div class="close-rpl"><span id="close_rpl_' + id + '"><a>Close</a></span></div>');

    reply.find(".close-rpl span").click(toggleReply);

    $j(pid).append(reply);

    var title = new Array("text", "image", "video");

    var tabs = reply.find(".tabs li");
    tabs.each(function(i) {
      $j(this).attr("id", "tab_" + id + "_" + title[i]);
    });

    var bodies = reply.find(".tab-body");
    bodies.each(function(i) {
      if (i < 3)
	$j(this).attr("id", "tab_body_" + id + "_" + title[i + 1]);
      else
	$j(this).attr("id", "tab_body_" + id + "_" + title[0]);
    });

    var tab = new Tab();

    for (i = 0; i < 3; i++) {
      tab.add(id + "_" + title[i]);
    }
    
    tab.setCurrent(id + "_text");

    tab.setCallback(function(t) {
      $j.activeTalk($j(pid));

      var old_t = $j("#type_" + id).val();
      $j("#s_" + t + "_" + id).val($j("#s_" + old_t + "_" + id).val());

      $j("#s_type_" + id).val(t);

      if ($j("#div_preview_" + id).is(":visible"))
	observer.togglePreviewEdit();
    });
    
    reply.find(":input[name='s_content']").attr("id", "s_content_" + id);
    reply.find(":input[name='s_image']").attr("id", "s_image_" + id);
    reply.find(":input[name='s_video']").attr("id", "s_video_" + id);
    reply.find(":input[name='s_link']").attr("id", "s_link_" + id);
    reply.find(":hidden[name='s_type']").attr("id", "s_type_" + id).val(title[0]);
    reply.find(":hidden[name='s_parent_id']").val(id);
    reply.find(":button:eq(0)").attr("id", "btn_submit_" + id);
    reply.find(":button:eq(1)").attr("id", "btn_preview_" + id);
    reply.find(":button:eq(2)").attr("id", "btn_edit_" + id);
    reply.find(":button:eq(3)").attr("id", "btn_load_draft_" + id);
    reply.find(".chars-left span").attr("id", "chars_left_" + id);
    reply.find(".div-edit").attr("id", "div_edit_" + id);
    reply.find(".div-preview").attr("id", "div_preview_" + id);

    $j.activeEditor(reply);

    var observer = new ContentObserver(id);
  }

  return false;
}

var toggleEdit = function(e) {
  e.stopPropagation();

  $j.activeTalk.activate($j(this));

  var id = this.id.substr(this.id.lastIndexOf("_") + 1);
  var eid = "#edit_" + id;
  var pid = "#talk_" + id;

  if ($j(eid).length) {
    if ($j(eid).is(":visible"))
      $j.activeEditor.deactivate();
    else
      $j.activeEditor.activate($j(eid));
  } else {
    var reply = $j("#reply").clone();

    if (0 == $j(pid).parents(".talk-main-list").length)
      reply.find(".div-tags").remove();
    
    reply.click(function(e) {
      e.stopPropagation();
      $j.activeTalk($j(pid));
      return false;
    });

    reply.attr("id", "edit_" + id);
    reply.children("div:last").hide();

    reply.prepend('<div class="close-edt"><span id="close_edt_' + id + '"><a>Close</a></span></div><div class="edit-loading"><img src="/img/loading_big_w.gif"/><br/><br/>loading your smalltalk</div>');

    reply.find(".close-edt span").click(toggleEdit);

    $j(pid + " > .talk-main").before(reply);

    var title = new Array("text", "image", "video");

    reply.find(".tabs").click(noAction);
    reply.find(".submit").click(noAction);

    var tabs = reply.find(".tabs li");
    tabs.each(function(i) {
      $j(this).attr("id", "tab_e_" + id + "_" + title[i]);
    });

    var bodies = reply.find(".tab-body");
    bodies.each(function(i) {
      if (i < 3)
	$j(this).attr("id", "tab_body_e_" + id + "_" + title[i + 1]);
      else
	$j(this).attr("id", "tab_body_e_" + id + "_" + title[0]);
    });

    var tab = new Tab();

    for (i = 0; i < 3; i++)
      tab.add("e_" + id + "_" + title[i]);

    tab.setCallback(function(t) {
      var old_t = $j("#type_e_" + id).val();
      $j("#s_" + t + "_e_" + id).val($j("#s_" + old_t + "_e_" + id).val());
      
      $j("#s_type_e_" + id).val(t);

      if ($j("#div_preview_e_" + id).is(":visible"))
	observer.togglePreviewEdit();
    });

    reply.find(":input[name='s_content']").attr("id", "s_content_e_" + id);
    reply.find(":input[name='s_post']").attr("id", "s_btn_submit_e_" + id);
    reply.find(":input[name='s_image']").attr("id", "s_image_e_" + id);
    reply.find(":input[name='s_video']").attr("id", "s_video_e_" + id);
    reply.find(":input[name='s_link']").attr("id", "s_link_e_" + id);
    reply.find(":input[name='s_tags']").attr("id", "s_tags_e_" + id);
    reply.find(":hidden[name='s_type']").attr("id", "s_type_e_" + id).val(title[0]);
    reply.find(":hidden[name='s_talk_id']").val(id);
    reply.find(":button:eq(0)").attr("id", "btn_submit_e_" + id);
    reply.find(":button:eq(1)").attr("id", "btn_preview_e_" + id);
    reply.find(":button:eq(2)").attr("id", "btn_edit_e_" + id);
    reply.find(":button:eq(3)").attr("id", "btn_load_draft_e_" + id);
    reply.find(".chars-left span").attr("id", "chars_left_e_" + id);
    reply.find(".div-edit").attr("id", "div_edit_e_" + id);
    reply.find(".div-preview").attr("id", "div_preview_e_" + id);

    $j.activeEditor.activate($j(eid));

    var observer = new ContentObserver("e_" + id);

    $j.get("/talk/get/" + id, {}, function(r) {
      if ("" == r) {
	if (confirm(si_err))
	  window.location.reload();

	return;
      }

      var a = new Array();
      var lp = 0;

      for (var i = 0; i < 4; i++) {
	var p = r.indexOf('\n', lp);
	if (p < 0)
	  return;

	a[i] = r.substr(lp, p - lp);
	lp = p + 1;
      }

      a[4] = r.substr(lp);

      tab.setCurrent("e_" + id + "_" + a[0]);
      $j("#s_link_e_" + id).val(a[1]);
      $j("#s_tags_e_" + id).val(a[2]);
      $j("#s_" + a[0] + "_e_" + id).val(a[3]);
      $j("#s_content_e_" + id).val(a[4]);
      $j(eid + " .edit-loading").remove();
      $j(eid).children("div:last").show();
      $j(eid).show();

      $j("#s_content_e_" + id).focus();
    });
  }

  return false;
}

var slideContent = function(c, show) {
  var t = null;
  var ie6 = false;
  var ie7 = false;

  if ($j.browser.msie) {
    if (parseInt($j.browser.version) <= 6)
      ie6 = true;
    else
      ie7 = true;
  }

  if (ie6 || ie7) {
    var tl = c.find(".thumb-l");
    var tr = c.find(".thumb-r");
    var w = 0;

    if (tl.length)
      t = tl;
    else if (tr.length)
      t = tr;
  }

  if (show) {
    if (ie6) {
      c.show();
    } else {
      if (t)
	t.hide();

      c.slideDown(1000, function() {
	if (t)
	  t.show();
      });
    }
  } else {
    if (ie6)
      c.hide();
    else {
      if (t)
	t.hide();

      c.slideUp(1000);
    }
  }
}

var popupReport = function(e) {
  e.stopPropagation();

  if ($j.popupMenu.menu) {
    $j.popupMenu.hide();
    return;
  }

  var t = $j(e.target);
  var id = t.attr("id");
  if (id.length) {
    var a = id.split('_');
    if ($j("#report_" + a[2]).length)
      $j("#report_popup_" + a[2]).popupMenu();
  }
}

var toggleReport = function(e) {
  e.preventDefault();

  if ($j.popupMenu)
    $j.popupMenu.hide();

  var t = $j(e.target);
  var id = t.attr("href").split("/")[4];
  var c = $j("#content_" + id);
  var s = $j("#report_" + id);
  var i = $j("#icon_report_" + id);

  $j.get(t.attr("href"), {}, function(r) {
    if ("" == r) {
      if (confirm(si_err))
	window.location.reload();

      return;
    }

    var a = r.split('\n');

    var prev = i.hasClass("icon-report-on");

    if ("1" == a[0]) {
      if (!prev)
	i.addClass("icon-report-on");
    } else {
      if (prev)
	i.removeClass("icon-report-on");
    }

    if ("1" == a[0] || "1" == a[1]) {
      s.show();

      slideContent(c, false);
      
      var m = s.children(".your-report");
      if ("1" == a[0])
	m.show();
      else
	m.hide();

      m = s.children(".global-report");
      if ("1" == a[1])
	m.show();
      else
	m.hide();
    } else {
      s.hide();

      slideContent(c, true);
    }
  });

  return false;
}

var scrollPage = function(e) {
  e.preventDefault();
  e.stopPropagation();

  var p1 = $j(this).parent();
  if ($j(this).hasClass("page-start"))
    var p2 = p1.nextAll(".talk-page");
  else
    var p2 = p1.prevAll(".talk-page");

  o1 = p1.offset();
  o2 = p2.offset();
  
  var s = $j('html,body').scrollTop();

  $j('html,body').scrollTop(s - (o1.top - o2.top));

  return false;
}

var togglePage = function(e) {
  e.stopPropagation();
  e.preventDefault();

  var id = $j("#talk_id").val();
  var p = $j(this);

  if (!$j(this).hasClass("talk-page"))
    p = p.parent();

  debug(p);

  if (p.find("img").length) {
    p.find("img:last").show();

    var url = "/talk/comment/" + id;

    if ($j(".selector-title a:first").prev().length)
      url += '/voted';

    $j.post(url, {
      p_start: p.find(":input[name='p_start']").val(),
      p_end: p.find(":input[name='p_end']").val()
    }, function(r) {
      p.find("img:last").remove();
      p.find("a:eq(0)").show();
      p.find("a:eq(1)").show();
      p.find("a:eq(2)").hide();
      p.find("span").hide();
      var pc = p.clone();
      var t = p.find("a:eq(1)").text();
      var l = t.length;
      pc.find("a:eq(1)").html("&laquo; " + t.substr(0, l - 3));
      pc.find("a:eq(0)").removeClass("page-start").addClass("page-end")
      .bind("click", scrollPage);
      pc.addClass("page-bottom");
      pc.click(togglePage);
      p.after(pc);
      p.after(r);

      init_talks();
    });
  } else {
    var v = p.find("a:eq(0)").is(":visible");

    if (p.hasClass("page-bottom")) {
      var ps = p.prevAll(".talk-page:first");
      var os = ps.offset();
      var oe = p.offset();

      var s = $j('html,body').scrollTop();

      p.hide();
      
      p.prevAll().each(function(i) {
	if ($j(this).hasClass("talk-page"))
	  return false;

	$j(this).hide();
      });

      p = ps;

      $j('html,body').scrollTop(s - (oe.top - os.top));
    } else {
      var s = p.find(":input[name='p_start']").val();
      var e = p.find(":input[name='p_end']").val();
      var c = e - s;

      p.nextAll().each(function(i) {
	if ((c + 1) < i)
	  return false;

	if (v)
	  $j(this).hide();
	else
	  $j(this).show();
      });
    }

    if (v) {
      p.find("a:eq(0)").hide();
      p.find("a:eq(1)").hide();
      p.find("a:eq(2)").show();
      p.find("span").show();
    } else {
      p.find("a:eq(0)").show();
      p.find("a:eq(1)").show();
      p.find("a:eq(2)").hide();
      p.find("span").hide();
    }
  }

  return false;
}

var votedFunc = function(e) {
  $j.get($j(e.target).attr("href"), {}, function(r) {
    $j.lightbox.replace(r);
    $j(".link_voted").click(votedFunc);
  });

  return false;
}

function init_talks() {
  $j(".talk").unbind("click");
  $j(".talk").click(function(e) {
    if ('A' == e.target.tagName || 'IMG' == e.target.tagName
	|| ('SPAN' == e.target.tagName
	    && 'A' == $j(e.target).parent().get(0).tagName))
      return true;

    e.stopPropagation();

    if (!$j.popupMenu.menu
	|| !$j.activeTalk.active
	|| $j.activeTalk.active.attr("id") != $j(this).attr("id")) {
      $j.activeTalk.toggle($j(this));
      
      return false;
    }

    $j.popupMenu.hide();

    return true;
  });

  $j(".friend-tip").unbind("mouseover");
  $j(".friend-tip").mouseover(function(e) {
    $j(this).next().show();
  });

  $j(".friend-tip").unbind("mouseout");
  $j(".friend-tip").mouseout(function(e) {
    $j(this).next().hide();
  });

  $j(".tgl-cmt-a").unbind("click");
  $j(".tgl-cmt-a").click(function(e) {
    $j.activeTalk.activate($j(this));

    var elem = $j(this).parent().next();
    if ($j(this).hasClass("tgl-cmt-show-a")) {
      $j(this).removeClass("tgl-cmt-show-a");
      elem.show();
    } else {
      $j(this).addClass("tgl-cmt-show-a");
      elem.hide();
    }

    return false;
  });

  $j(".tgl-rpl-a").unbind("click");
  $j(".tgl-rpl-a").click(toggleReply);

  $j(".icon-edit").unbind("click");
  $j(".icon-edit").click(toggleEdit);

  $j(".icon-delete").unbind("click");
  $j(".icon-delete").click(function(e) {
    e.preventDefault();

    if (!confirm(del_msg)) {
      return false;
    }

    var id = $j(this).attr("id");
    id = id.substr(id.lastIndexOf("_") + 1);

    $j.get("/talk/delete/" + id, {}, function(r) {
      if ("" == r) {
	if (confirm(si_err))
	  window.location.reload();

	return;
      }

      if ("0" == r) {
	alert("This talk was not deleted.\nPlease try again later.");
	return;
      }

      var r = $j("#report_" + id);
      var c = $j("#content_" + id);

      var delFunc = function() {
	r.remove();
	c.html('<span class="deleted">[This talk was deleted]</span>').show();
      }

      $j("#icon_delete_" + id).remove();
      $j("#icon_edit_" + id).remove();

      if (r.is(":visible")) {
	r.slideUp(1000, delFunc);
      } else {
	c.slideUp(1000, delFunc);
      }
    });

    return false;
  });

  $j(".icon-vote-signed-in").unbind("click");
  $j(".icon-vote-signed-in").click(function(e) {
    e.preventDefault();

    var t = $j(this);

    $j.activeTalk.activate(t);

    var id = t.attr("href").split("/")[3];

    if ($j("#vote_err_popup_" + id).length) {
      if ($j.popupMenu.menu)
	$j.popupMenu.hide();

      $j("#vote_err_popup_" + id).popupMenu();
      return false;
    }

    $j.get($j(this).attr("href"), {}, function(r) {
      if ("" == r) {
	if (confirm(si_err))
	  window.location.reload();

	return;
      }

      var a = r.split('\n');
      var prev = t.hasClass("icon-vote-on");

      if ("1" == a[0]) {
	$j("#vote_err_popup_" + id).show();
	return false;
      }

      if ("1" == a[1]) {
	if (!prev)
	  t.addClass("icon-vote-on");
      } else {
	if (prev)
	  t.removeClass("icon-vote-on");
      }

      $j("#vote_count_" + id).text(a[2]);
    });

    return false;
  });

  $j(".icon-vote").unbind("mouseover");
  $j(".icon-vote").mouseover(function(e) {
    $j(this).next().addClass("icon-arrow-visible");
  });

  $j(".icon-vote").unbind("mouseout");
  $j(".icon-vote").mouseout(function(e) {
    if (!$j.popupMenu || !$j.popupMenu.menu)
      $j(this).next().removeClass("icon-arrow-visible");
  });

  $j(".vote-arrow").unbind("mouseover");
  $j(".vote-arrow").mouseover(function(e) {
    $j(this).addClass("icon-arrow-visible icon-arrow-active");
  });

  $j(".vote-arrow").unbind("mouseout");
  $j(".vote-arrow").mouseout(function(e) {
    if (!$j.popupMenu || !$j.popupMenu.menu)
      $j(this).removeClass("icon-arrow-visible");

    $j(this).removeClass("icon-arrow-active");
  });

  $j(".vote-arrow").unbind("click");
  $j(".vote-arrow").click(function(e) {
    e.stopPropagation();

    var a = $j(this).attr("id").split('_');
    $j("#vote_popup_" + a[2]).popupMenu($j(this));
  });

  $j(".who-voted").unbind("click");
  $j(".who-voted").click(function(e) {
    $j.activeTalk.activate($j(this));

    $j.lightbox({
      loading: true,
      modal: true
    });

    votedFunc(e);

    return false;
  });

  $j(".report-menu").unbind("click");
  $j(".report-menu").click(function(e) {
    toggleReport(e);

    return false;
  });

  $j(".tgl-report").unbind("click");
  $j(".tgl-report").click(function(e) {
    $j.activeTalk.activate($j(this));

    var id = $j(this).attr("id");
    var c = $j("#content_" + id.substr(id.lastIndexOf("_") + 1));

    if (c.is(':visible')) {
      slideContent(c, false);
      $j(this).text("show");
    } else {
      slideContent(c, true);
      $j(this).text("hide");
    }

    return false;
  });

  $j(".icon-report-signed-in").unbind("click");
  $j(".icon-report-signed-in").click(function(e) {
    e.preventDefault();

    $j.activeTalk.activate($j(this));

    if ($j(this).hasClass("icon-report-on"))
      toggleReport(e);
    else
      popupReport(e);

    return false;
  });

  $j(".icon-share-signed-in").unbind("click");
  $j(".icon-share-signed-in").click(function(e) {
    e.preventDefault();

    $j.activeTalk.activate($j(this));

    var id = $j($j(this).parents(".talk").get(0)).attr("id").substr(5);

    $j.lightbox({
      loading: true,
      modal: true
    });

    $j.get("/talk/share_info/" + id, {}, function(r) {
      if ("" == r) {
	if (confirm(si_err))
	  window.location.reload();

	return;
      }

      $j.lightbox.replace(r);
    });

    return false;
  });

  $j(".video-play").unbind("click");
  $j(".video-play").click(function(e) {
    e.preventDefault();

    $j.activeTalk.activate($j(this));

    $j.lightbox({
      gray: true,
      loading: true
    });

    var id = $j($j(this).parents(".talk").get(0)).attr("id").substr(5);

    $j.get("/talk/video/" + id, {}, function(r) {
      $j.lightbox.replace(r);
    });

    return false;
  });

  $j(".talk-page, a.page-show, a.page-hide").unbind("click");
  $j(".talk-page, a.page-show, a.page-hide").click(togglePage);

  $j(".talk-page .page-start, .talk-page .page-end").unbind("click");
  $j(".talk-page .page-start, .talk-page .page-end").click(scrollPage);

  $j(".popup-login").unbind("click");
  $j(".popup-login").click(function(e) {
    e.preventDefault();
    $j("#lightbox_login").lightbox();
    $j(".lb-login [name='user_name']").eq(0).focus();

    return false;
  });

  if ($j.browser.msie) {
    $j(".no-ol").unbind("focus");
    $j(".no-ol").focus(function(e) {
      this.hideFocus=true;

      return true;
    });
  }
}

$j(document).ready(function() {
  $j('#search').click(function(e) {
    e.preventDefault();
    $j('#search_form').submit();

    return false;
  });

  init_talks();

  var id = $j("#active_talk_id").val();
  if (0 < id && $j("#talk_" + id).length) {
    $j.activeTalk.activate($j("#talk_" + id));

    if ($j("#talk_id").val() != id) {
      var o = $j("#talk_" + id).offset();
      $j('html,body').animate({ scrollTop: o.top - 10 }, 1000);
    }
  }
});
