var d;
/**
 * Our global object. For information on this pattern (Douglas Crockford's module pattern), see:
 * http://www.wait-till-i.com/2007/07/24/show-love-to-the-module-pattern/
 * http://www.wait-till-i.com/2007/08/22/again-with-the-module-pattern-reveal-something-to-the-world/
 */
var Gatech = Gatech || {
  'version' : '1.1'
}

/**
 * DropDown handler -- not your average dropdown menu. This bad boy supports peeking on mouseover. 
 * Peeking! Does that give you the shivers or what?
 */
Gatech.dropDown = function(dropdown, trigger, anchor) {
  var version = '0.3';
  this.dropdown = '#' + dropdown;
  this.trigger = '#' + trigger;
  this.anchor = '#' + anchor;
};
  
Gatech.dropDown.prototype.hide = function(event) {
  //console.log('hide');
  var self = this;
  if (event) { event.preventDefault(); }
  $(this.dropdown).slideUp(500, function() { self.cleanup(); });
  try { $(this.trigger).blur(); }
  catch(err) { }
};

Gatech.dropDown.prototype.show = function(event) {
  //console.log('show');
  var self = this;
  event.preventDefault();
  if ($(this.dropdown + ':hidden') || $(this.dropdown).hasClass('peek')) {
    $(this.dropdown).hide();
    $(this.dropdown).removeClass('peek');
    $(this.trigger).unbind();
    try { $(this.trigger).blur(); }
    catch(err) { }
    $(this.dropdown).slideDown(500, function() { self.invert(); });
  }
};

Gatech.dropDown.prototype.peek = function() {
  //console.log('peek');
  if ($(this.dropdown + ':hidden')) {
    $(this.dropdown).css('overflow', 'hidden');
    $(this.dropdown).addClass('peek');
    $(this.dropdown).show();
  }
};

Gatech.dropDown.prototype.unpeek = function() {
  //console.log('unpeek');
  $(this.dropdown).hide();
  $(this.dropdown).removeClass('peek');
};

Gatech.dropDown.prototype.cleanup = function() {
  //console.log('cleanup');
  var self = this;
  $(this.trigger).unbind();
  $(this.trigger).mouseout(function() { self.unpeek() });
  $(this.trigger).mouseover(function() { self.peek() });
  $(this.anchor).unbind('click');
  $(this.anchor).click(function(e) { self.show(e) });
};

Gatech.dropDown.prototype.invert = function() {
  //console.log('invert');
  var self = this;
  $(this.anchor).unbind('click');
  $(this.anchor).click(function(e) { self.hide(e) });
}

/**
 * Feature Rotator. Yeah.
 *  target = the id of the rotator div
 *  indicators = the id of the div containing progress indicators
 *  generator = the url of the script generating json for the rotator
 *  interval = the interval, in milliseconds, between images
 */
Gatech.featureRotator = function(target, indicators, generator, interval) {
  var version = '0.2';
  
  // configuration variables
  this.featureCount = 7;
  this.indicators = {
    el : '#' + indicators,
    dir : '/images/' + indicators,
    next : 'Next.gif',
    prog : 'Progress.gif'
  };
  this.js = '/js/';
  
  // initialization
  this.index = Gatech.getUrlVars()['n'] || 0;
  this.target = '#' + target;
  this.generator = generator;
  this.interval = interval;
  this.featureStack = [];
  this.indicatorStack = [];
  this.populateStack();
  this.indicatorStack[0].src = this.indicators.dir + this.indicators.prog;
  this.start();
}

Gatech.featureRotator.prototype.populateStack = function() {
  //console.log('populateStack');
  this.indicatorStack = $(this.indicators.el + ' img');
  for (i = 0; i < this.featureCount; i++) { this.requestFeature(i); }
}
    
Gatech.featureRotator.prototype.requestFeature = function(i) {
  //console.log('requestFeature');
  var self = this;
  $.getJSON(
    this.generator, 
    'n=' + i + '&JSON=1',
    function(data) {
      self.featureStack[i] = data;
      Gatech.preloadImages(data.feature.image);
    }
  );
}

Gatech.featureRotator.prototype.start = function() {
  //console.log('start');
  var self = this;
  $.ajax({
     url: '/js/jquery.timers.js',
     dataType: 'script',
     success: function(){
       $(this.target).everyTime(self.interval, function() { self.displayNextFeature(); });
     },
     cache: true
   });
}

Gatech.featureRotator.prototype.displayNextFeature = function() {
  //console.log('displayNextFeature');
  var self = this;
  if (this.featureStack[(parseInt(this.index) + 1) % 7]) {
    this.indicatorStack[this.index].src = this.indicators.dir + this.indicators.next;
    this.indicatorStack[(parseInt(this.index) + 1) % 7].src = this.indicators.dir + this.indicators.prog;

    this.index++;
    this.index = this.index % 7;

    var imageTarget = $(this.target + ' img')[0];
    $(imageTarget).fadeOut(500, function() {
      imageTarget.src = self.featureStack[self.index].feature.image;
      $(imageTarget).fadeIn(500);
    });

    imageTarget.parentNode.href = this.featureStack[this.index].feature.link;
    imageTarget.alt = this.featureStack[this.index].feature.head;
    $(this.target + ' h1')[0].firstChild.nodeValue = this.featureStack[this.index].feature.head;
    $(this.target + ' p')[0].firstChild.nodeValue = this.featureStack[this.index].feature.summary;
  } else { // in case the db barfed
    this.indicatorStack[0].src = this.indicators.dir + this.indicators.next;
  }
  this.Counter++;
  if (parseFloat(navigator.userAgent.indexOf('6.0')) > 0 && navigator.appName == "Microsoft Internet Explorer" && this.Counter == 5) { this.exit(); }
  else if (this.Counter == 25) { this.exit(); }
}

Gatech.featureRotator.prototype.displayArbitraryFeature = function(f) {
  //console.log('displayArbitraryFeature');
  this.indicatorStack[this.index].src = this.indicators.dir + this.indicators.next;
  try { this.indicatorStack[this.index].blur(); }
  catch(err) { }
  this.index = f == 0 ? 6 : (f - 1) % 7;
  $(this.target).stopTime();
  this.displayNextFeature();
  this.start();
}
    
Gatech.featureRotator.prototype.exit = function() {
  this.indicatorStack[0].src = this.indicators.dir + this.indicators.next;
  $(this.target).stopTime();
}

Gatech.preloadImages = function() {
  var i = new Image;
  $(arguments).each(function(s) { i.src = s; });
}

/**
 * getUrlVars -- because we don't really want to load jQuery's URL parser just to grab one value.
 * Returns a string-indexed array.
 */
Gatech.getUrlVars = function() {
  var vars = [], hash;
  var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  for(var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
  }
  return vars;
}

$(document).ready(function() {
  // audience nav
  if ($('#home').length > 0) {
      $('#audienceNav').removeClass('hide');
      $('#closeAudienceNav').click(function(e) { Gatech.audienceNav.hide(e); });
  } else {
      if ($('#audienceNav').length > 0) {
        $('#audienceNav').hide();
        $('#audienceNav').removeClass('hide');
        $('#techForYouAnchor').click(function(e) { Gatech.audienceNav.show(e); });
        $('#techForYou').mouseout(function() { Gatech.audienceNav.unpeek(); });
        $('#techForYou').mouseover(function() { Gatech.audienceNav.peek(); });
        $('#closeAudienceNav').click(function(e) { Gatech.audienceNav.hide(e); });
      }
  }
    
  // Fire up the feature rotator, if necessary.
  if ($('#featureRotator').length > 0) {
    var myFeatureRotator = new Gatech.featureRotator('featureRotator', 'featureIndicators', '/inc/generateFeature.php', 10000);

    $('#featureIndicators a').click(function() {
      myFeatureRotator.displayArbitraryFeature(parseInt(this.href.slice(this.href.indexOf('?n=') + 3)));
      return false;
    });
  }

  // search box
  if ($('#q').length > 0) { 
    $('#q').focus(function() { $('#q').val(''); }); 
    $('#q').blur(function() { if ($('#q').val() == '') { $('#q').val('Search'); }}); 
  }
});

Gatech.audienceNav = new Gatech.dropDown('audienceNav', 'techForYou', 'techForYouAnchor');

if (!window.console || !console.firebug) { // prevent errors from debugging debris
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    window.console = {};
    for (var i = 0; i < names.length; ++i) { window.console[names[i]] = function() {}; }
}
