// {{{ Image Tester / Design Enhancer
var testImage = document.createElement('img');
testImage.src = themePath + 'styles/images/transparent.gif';
testImage.id = 'images-enabled-tester';
testImage.alt = '';

document.getElementById('footer').appendChild(testImage);

var imageCheckerThread = setInterval("checkImageTester()", 25);

var numTests = 0;

function checkImageTester() {
  numTests++;
  
  if (testImage.complete) {
    clearInterval(imageCheckerThread);
    
    if (testImage.offsetWidth == 2) {
      //images are enabled
      toggleEnhance();
    }
    
    //clean up our mess
    document.getElementById('footer').removeChild(testImage);
    testImage = null;
  }
  
  //let's scale back our repeat rate gradually, then give up
  if (numTests == 20) { //after .5 sec, go to 100ms
    clearInterval(imageCheckerThread);
    imageCheckerThread = setInterval("checkImageTester()", 100);
  } else if (numTests == 30) { //after 1.5 sec, go to 250ms
    clearInterval(imageCheckerThread);
    imageCheckerThread = setInterval("checkImageTester()", 250);
  } else if (eval('numTests ' + String.fromCharCode(62) + ' 40')) { //after 4 sec, give up
    clearInterval(imageCheckerThread);
    document.getElementById('footer').removeChild(testImage);
    testImage = null;
  }
}

function toggleEnhance() {
  var body = document.getElementsByTagName('body')[0];
  var c = body.className;
  
  if (c.search(/enhance/) != -1) {
    body.className = c.replace(/enhance/, '');
  } else {
    body.className = c + ' enhance';
  }
}
// }}} Image Tester / Design Enhancer


// {{{ Determine User-Agent
function getUserAgent() {
  var ua = navigator.userAgent.toLowerCase();
  var makeVersion = function(result) {
    return (parseInt(result[1]) * 1000) + parseInt(result[2]);
  };
  
  if (ua.indexOf("opera") != -1) {
    return "opera";
  } else if (ua.indexOf("webkit") != -1) {
    return "safari";
  } else if (ua.indexOf("msie") != -1) {
    var result = /msie ([0-9]+)\.([0-9]+)/.exec(ua);
    if (result && result.length == 3) {
      if (makeVersion(result) >= 6000) {
        return "ie6";
      }
    }
  } else if (ua.indexOf("gecko") != -1) {
    var result = /rv:([0-9]+)\.([0-9]+)/.exec(ua);
    if (result && result.length == 3) {
      if (makeVersion(result) >= 1008) {
        return "gecko1_8";
      }
      return "gecko";
    }
    return "unknown";
  }
}

var ua = getUserAgent();
// }}} Determine User-Agent


// {{{ Re-style image-block images
$(document).ready(function() {
  $('img.image-210').each(function(i) {
    var caption = $(this).attr('title');
    var parent = $(this).parent().get(0);
    
    if (parent.tagName == 'p' || parent.tagName == 'P') {
      //we want to break it out of the paragraph
      $(this).insertBefore(parent);
    }
    
    var div = document.createElement('div');
    $(div).attr('class', $(this).attr('class'));
    $(this).attr('title', $(this).attr('alt')).wrap(div);
    if (caption) {
      $(this).after('<p>' + caption + '</p>');
    }
    
  });
  
  $('img.image-455').each(function(i) {
    var caption = $(this).attr('title');
    var parent = $(this).parent().get(0);
    
    if (parent.tagName == 'p' || parent.tagName == 'P') {
      //we want to break it out of the paragraph
      $(this).insertBefore(parent);
    }
    
    var div = document.createElement('div');
    $(div).attr('class', $(this).attr('class'));
    $(this).attr('title', $(this).attr('alt')).wrap(div);
    if (caption) {
      $(this).after('<p>' + caption + '</p>');
    }
    
  });
  
  $('img.image-700').each(function(i) {
    var caption = $(this).attr('title');
    var parent = $(this).parent().get(0);
    
    if (parent.tagName == 'p' || parent.tagName == 'P') {
      //we want to break it out of the paragraph
      $(this).insertBefore(parent);
    }
    
    var div = document.createElement('div');
    $(div).attr('class', $(this).attr('class').replace('left', '').replace('right', ''));
    $(this).attr('title', $(this).attr('alt')).wrap(div);
    if (caption) {
      $(this).after('<p>' + caption + '</p>');
    }
    
  });
  
  // Set the class name 'active' on <a> tags inside primary and secondary
  // navigation links in the active-trail.  CSS alone isn't quite cutting it
  // in a cross-browser-friendly way with the image replacements and all.
  // This way, the CSS can simply focus on <a> with the 'active' class.
  $('div#nav-secondary ul li.active-trail > a, div#nav-primary ul li.active-trail > a').addClass('active');
  
});
// }}} Re-style image-block images


$(document).ready(function() {
  // {{{ Flash Presentation Embed
  $('div#presentation').each(function(i) {
    var flashvars = {
      xmlfile:"data/index.xml"
    };
    
    var params = {
      base:basepath,
      wmode:"window",
      menu:false
    };
    
    var attributes = {
      id:"presentation"
    }
    
    var showPresentation = false;
    var swfFile = 'poplar_forest-jefferson.swf';
    
    
    if ($(this).attr('class') == 'flash-archeology') {
      params.base = params.base + '/landscape';
      showPresentation = true;
    } else if ($(this).attr('class') == 'flash-jefferson') {
      params.base = params.base + '/biography';
      showPresentation = true;
    } else if ($(this).attr('class') == 'flash-restoration') {
      params.base = params.base + '/restoration';
      showPresentation = true;
    } else if ($(this).attr('class') == 'flash-main') {
      params.base = params.base + '';
      showPresentation = true;
      swfFile = 'exampleIntro.swf';
    }
    
    if (showPresentation) {
      swfobject.embedSWF(basepath + "/assets/swf/" + swfFile, "presentation", "700", "426", "9.0.0", basepath + "/swf/expressinstall.swf", flashvars, params, attributes);
    }
  });
  // }}} Flash Presentation Embed
  
  // {{{ Column Height Adjuster
  /**
   * contentHeight shall be the height from the bottom of the
   * secondary nav bar to the bottom of the longest vertical divider.
   *
   * There are several candidates for content height:
   *  = height(div#nav-section) + 35
   *  = height(div#content-head) + 25 + height(div#content-main-100)
   *  = height(div#content-head) + 25 + height(div#content-main-010)
   *  = height(div#content-head) + 25 + height(div#content-main-001)
   *  = height(div#content-head) + 25 + height(div#content-main-110)
   *  = height(div#content-head) + 25 + height(div#content-main-111)
   *
   * After determining the maximum candidate height, the following
   * blocks are assigned new heights according to the following calculations:
   *  height(div#nav-section) = maxCandidateHeight - 35
   *  height(div#content-main-100) = maxCandidateHeight - 25 - height(div#content-head)
   *  height(div#content-main-010) = maxCandidateHeight - 25 - height(div#content-head)
   *  height(div#content-main-001) = maxCandidateHeight - 25 - height(div#content-head)
   *  height(div#content-main-110) = maxCandidateHeight - 25 - height(div#content-head)
   *  height(div#content-main-111) = maxCandidateHeight - 25 - height(div#content-head)
   */
  var heightNames = [
    'nav-section',
    'content-head',
    'content-main-100',
    'content-main-010',
    'content-main-001',
    'content-main-110',
    'content-main-111'
  ];
  
  var heightElements = new Object();
  var heights = new Object();
  
  for (var i = 0; i < heightNames.length; ++i) {
    heightElements[heightNames[i]] = document.getElementById(heightNames[i]);
    if (heightElements[heightNames[i]] != null) {
      heights['_' + heightNames[i]] = heightElements[heightNames[i]].offsetHeight;
    } else {
      heightElements[heightNames[i]] = new Object();
      heightElements[heightNames[i]].style = new Object();
      heights['_' + heightNames[i]] = 0;
    }
  }
  
  var candidateHeights = [
    heights['_nav-section']  + 35,
    heights['_content-head'] + 25 + heights['_content-main-100'],
    heights['_content-head'] + 25 + heights['_content-main-010'],
    heights['_content-head'] + 25 + heights['_content-main-001'],
    heights['_content-head'] + 25 + heights['_content-main-110'],
    heights['_content-head'] + 25 + heights['_content-main-111']
  ];
  
  var maxCandidateHeight = candidateHeights[0];
  for (var i = 1; i < candidateHeights.length; ++i) {
    if (candidateHeights[i] > maxCandidateHeight) {
      maxCandidateHeight = candidateHeights[i];
    }
  }
  
  if (ua != 'safari') {
    heightElements['nav-section'].style.height      = (maxCandidateHeight - 35) + 'px';
    heightElements['content-main-100'].style.height = (maxCandidateHeight - 25 - heights['_content-head']) + 'px';
    heightElements['content-main-010'].style.height = (maxCandidateHeight - 25 - heights['_content-head']) + 'px';
    heightElements['content-main-001'].style.height = (maxCandidateHeight - 25 - heights['_content-head']) + 'px';
    heightElements['content-main-110'].style.height = (maxCandidateHeight - 25 - heights['_content-head']) + 'px';
    heightElements['content-main-111'].style.height = (maxCandidateHeight - 25 - heights['_content-head']) + 'px';
  } else {
    heightElements['nav-section'].style.minHeight      = (maxCandidateHeight - 35) + 'px';
    heightElements['content-main-100'].style.minHeight = (maxCandidateHeight - 25 - heights['_content-head']) + 'px';
    heightElements['content-main-010'].style.minHeight = (maxCandidateHeight - 25 - heights['_content-head']) + 'px';
    heightElements['content-main-001'].style.minHeight = (maxCandidateHeight - 25 - heights['_content-head']) + 'px';
    heightElements['content-main-110'].style.minHeight = (maxCandidateHeight - 25 - heights['_content-head']) + 'px';
    heightElements['content-main-111'].style.minHeight = (maxCandidateHeight - 25 - heights['_content-head']) + 'px';
  }
  // }}} Column Height Adjuster
});


