/************************************************************************
 * Copyright (C) 2009 Thomas Krehbiel
 * http://uvteksoftware.com, mailto:tom@uvteksoftware.com
 * Distributed under the terms of the GNU GPL version 3
 ************************************************************************/

function mapPath(s)
{
  // relpath is a global set by the server.
  // It should have a trailing slash.
  return relpath+s;
}

function staticPath(s)
{
  // staticpath is a global set by the server.
  // It should have a trailing slash.
  return staticpath+s;
}

// cookie code adapted from http://www.quirksmode.org/js/cookies.html
function getCookie(name) 
{
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0)
    {
    	s = c.substring(nameEQ.length,c.length);
    	// tek: Cookies appear to have spaces encoded as + signs.
    	// And sometimes they are uri-encoded (eg. %2F).
    	// I think it depends on the browser.
    	s = decodeURIComponent(s.replace('+', ' '));
    	return s;
    }
  }
  return null;
}

function getKeyCode(e)
{
  if(window.event) // IE
    return e.keyCode;
  else if(e.which) // Netscape/Firefox/Opera
    return e.which;
}

function showPost(id,query)
{
  var q = $('#post'+id);
  if(q)
  {
  	var queryparam = '';
  	if(query && query!='') queryparam = '&q='+encodeURIComponent(query);
    $('#load'+id).html('<img src="'+staticPath('images/load_spinner.gif')+'" alt="" width="16" height="16" border="0" />');
    $.get(mapPath('callback.php')+'?f=getpost&id='+id+queryparam,
      null, 
      function(data){ q.html(data); },
      'text'
    );
  }
}

// Deprecated comment box functionality

function showCommentBox(id)
{
  var elem = document.getElementById('commentbox'+id);
  if( elem )
  {
    $.get(mapPath('callback.php')+'?f=getcommentbox&id='+id,
      null, 
      function(data){
        elem.innerHTML = data;
        elem.style['display'] = 'block';
        var textelem = document.getElementById('txtc'+id);
        if( textelem ) { textelem.focus(); textelem.select(); }
        },
      'text'
    );
    return false;
  }
  return true;
}

function commentSubmit(id)
{
  var elem = document.getElementById('commentbox'+id);
  if( elem )
  {
    elem.style['display'] = 'none';
    showPost(id);
    return false;
  }
  return true;
}

function commentCancel(id)
{
  var elem = document.getElementById('commentbox'+id);
  if( elem )
  {
    elem.style['display'] = 'none';
    return false;
  }
  return true;
}

function commentKeyDown(e, id)
{
  var keyCode = getKeyCode(e);
  if( keyCode == 13 ) { // return
    commentSubmit(id);
    return false;
  }
  return true;
}

// This is loaded on every page.
$(document).ready(function()
{
	// Look for and hide any warnings about Javascript being enabled.
	// (Since, clearly, Javascript is enabled if we can run this code.)
	var q = $('#enablejs');
	if( q ) q.hide();
});

