var baseUrl = 'http://www.geekology.co.za/blog/wp-content/themes/geekology/ajax/';


/* Send a JSON-based AJAX request: */
function sendJSONRequest(requestAction, requestData, requestTarget)
{
  requestData = JSON.stringify(requestData);
  
  $.post(baseUrl + requestAction + '.php', { data: requestData }, function(data) { requestTarget(data) }, 'json');
}


/* Request search suggestions: */
function searchSuggestions(keywords)
{
  if (keywords != '')
  {
    var requestData = Object();
    
    eval("requestData.keywords = '" + keywords + "'");
    
    var requestTarget = function(data)
                        {
                          var results = '';
                          
                          if ((JSON.stringify(data)) != '["none"]')
                          {
                            for (var category in data)
                            {
                              results = results + '<div class="suggestionCategory">' + category + '</div>\n';
                              
                              for (var categoryResult in data[category])
                              {
                                results = results + '<a href="' + eval('data[category][categoryResult].link') + '" title="' + eval('data[category][categoryResult].title') + '">';
                                results = results + '<div class="suggestionResult">';
                                /*results = results + '<div class="suggestionDate">' + eval('data[category][categoryResult].date') + '</div>';*/
                                results = results + '<div class="suggestionEntry"><strong>' + eval('data[category][categoryResult].title') + '</strong><br /><em>' + eval('data[category][categoryResult].description') + '</em></div>';
                                results = results + '</div>';
                                results = results + '</a>';
                              }
                            }
                          }
                          
                          searchSuggestionsShow(results);
                        }
    
    sendJSONRequest('searchBox', requestData, requestTarget);
  }
  else
  {
    searchSuggestionsHide();
  }
}

function searchSuggestionsShow(results)
{
  if (results != '')
  {
    $('#searchSuggestions').html(results);
    $('#searchSuggestions').show();
  }
  else
  {
    searchSuggestionsHide();
  }
}

function searchSuggestionsHide()
{
  $('#searchSuggestions').html('');
  $('#searchSuggestions').hide();
}


function loadGeekGa()
{
  $.geekGaTrackPage('UA-7147362-1');
  
  $("a[href='http://www.geekology.co.za/blog/feed/']").each(function() {
    $(this).click(function() {
      $.geekGaTrackEvent('feed', 'click', 'RSS 2.0', 'articles');
    });
  });
  
  $("a[href='http://www.twitter.com/willemvzyl/']").each(function() {
    $(this).click(function() {
      $.geekGaTrackEvent('feed', 'click', 'Twitter', 'willemvzyl');
    });
  });
  
  $("a[href='http://www.reddit.com/user/willemvzyl/']").each(function() {
    $(this).click(function() {
      $.geekGaTrackEvent('feed', 'click', 'Reddit', 'willemvzyl');
    });
  });
  
  $("a[href='http://digg.com/users/willemvzyl']").each(function() {
    $(this).click(function() {
      $.geekGaTrackEvent('feed', 'click', 'Digg', 'willemvzyl');
    });
  });
  
  $("a[href='http://delicious.com/willemvzyl']").each(function() {
    $(this).click(function() {
      $.geekGaTrackEvent('feed', 'click', 'Delicious', 'willemvzyl');
    });
  });
  
  $("a[href='http://technorati.com/blogs/http%3A%2F%2Fwww.geekology.co.za%2Fblog']").each(function() {
    $(this).click(function() {
      $.geekGaTrackEvent('feed', 'click', 'Technorati', 'geekology');
    });
  });
  
  $("a[href='http://www.geekology.co.za/blog/']").each(function() {
    $(this).click(function() {
      $.geekGaTrackEvent('page', 'click', 'Home', '');
    });
  });
  
  $("a[href='http://www.geekology.co.za/blog/about/']").each(function() {
    $(this).click(function() {
      $.geekGaTrackEvent('page', 'click', 'About', '');
    });
  });
  
  $("a[href='http://www.geekology.co.za/blog/software/']").each(function() {
    $(this).click(function() {
      $.geekGaTrackEvent('page', 'click', 'Software', '');
    });
  });
  
  $("a[rel='bookmark']").each(function() {
    $(this).click(function() {
      $.geekGaTrackEvent('post', 'click', $(this).html(), '');
    });
  });
}


/* Load some jQuery plugins and other things: */
function geekOut()
{
  $(document).ready( function() {
    //flinch a bit, then educate people using IE:
    if ($.browser.msie)
    {
      showIeWarning();
    }

    
    //load non-ie specific features:
    if (!$.browser.msie)
    {
      //guess which browser doesn't support the rounded corners plugin?
      $('.rounded').corners();
      
      //add events to the search box for suggestions:
      $('.keywords').bind('focus change keypress', function() { searchSuggestions(this.value); });
      $('.keywords').blur(function () { setTimeout('searchSuggestionsHide();', 1000); });
    }
    
    
    //enable the Lava Lamp menu plugin:
    $(".lavaLamp").lavaLamp({ fx: "backout", speed: 500 });
    
    
    //enable the Lightbox plugin on post images with links to the full image:
    $('#contentPosts p a img').each(function() {
      $(this).parent().attr('rel', 'lightbox');
    });
    
    //enable the Lightbox plugin on post galleries:
    $('#contentPosts .gallery a img').each(function() {
      $(this).parent().attr('rel', 'lightbox');
    });
    
    $('a[rel*=lightbox]').lightBox();
    
    
    //load Google Analytics page and event tracking:
    loadGeekGa();
  });
}


/* Show a warning if someone visits with IE: */
function showIeWarning()
{
  ieWarnings = new Array('Using <strong>Internet Explorer<\/strong>, eh? You should really <a href="http://www.geekology.co.za/blog/2009/04/campaign-to-bring-down-ie6/" target="_blank" title="The campaign to Bring Down IE6">read this<\/a> and <a href="http://www.geekology.co.za/blog/2009/02/performance-of-popular-web-browsers-in-acid3-test/" target="_blank" title="The performance of popular web browsers in the Acid3 test">this<\/a>, then <a href="http://www.mozilla.com/en-US/firefox/personal.html" target="_blank" title="Mozilla Firefox">download this<\/a>.',
                         'Ouch... <strong>Internet Explorer<\/strong>. You should really <a href="http://www.geekology.co.za/blog/2009/04/campaign-to-bring-down-ie6/" target="_blank" title="The campaign to Bring Down IE6">read this<\/a> and <a href="http://www.geekology.co.za/blog/2009/02/performance-of-popular-web-browsers-in-acid3-test/" target="_blank" title="The performance of popular web browsers in the Acid3 test">this<\/a>, then <a href="http://www.mozilla.com/en-US/firefox/personal.html" target="_blank" title="Mozilla Firefox">download this<\/a>.',
                         'Using <strong>Internet Explorer<\/strong>, eh? You can\'t blame them for trying, but you should really <a href="http://www.geekology.co.za/blog/2009/04/campaign-to-bring-down-ie6/" target="_blank" title="The campaign to Bring Down IE6">read this<\/a> and <a href="http://www.geekology.co.za/blog/2009/02/performance-of-popular-web-browsers-in-acid3-test/" target="_blank" title="The performance of popular web browsers in the Acid3 test">this<\/a>, then <a href="http://www.mozilla.com/en-US/firefox/personal.html" target="_blank" title="Mozilla Firefox">download this<\/a>.',
                         'HTML rendering error. The rendering engine needs an <a href="http://www.geekology.co.za/blog/2009/02/performance-of-popular-web-browsers-in-acid3-test/" target="_blank" title="The performance of popular web browsers in the Acid3 test">oil change and new spark plugs</a>. Alternatively, you could <a href="http://www.geekology.co.za/blog/2009/04/campaign-to-bring-down-ie6/" target="_blank" title="The campaign to Bring Down IE6">switch</a> from <strong>Internet Explorer</strong> to a <a href="http://www.mozilla.com/en-US/firefox/personal.html" target="_blank" title="Mozilla Firefox">standards-compliant browser</a>.');
  
  warningToUse = (Math.round((Math.random()*3)));
  
  $('div#ieWarning').html(ieWarnings[warningToUse]);
  $('div#ieWarning').fadeIn('normal');
}
