// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function corners_top (item) {
  item.style.position = 'relative';

  var left = document.createElement('div');
  Element.extend(left);
  left.addClassName("rc rc-tl");

  var right = document.createElement('div');
  Element.extend(right);
  right.addClassName("rc rc-tr");

  item.appendChild(left);
  item.appendChild(right);
}

function corners_bottom (item) {
  item.style.position = 'relative';

  var left = document.createElement('div');
  Element.extend(left);
  left.addClassName("rc rc-bl");

  var right = document.createElement('div');
  Element.extend(right);
  right.addClassName("rc rc-br");

  item.appendChild(left);
  item.appendChild(right);
}

var sir;
Element.observe(document, 'dom:loaded', function () {

  $$(".rc-top").each(function (item) {
    corners_top(item);
  });
  $$(".rc-bottom").each(function (item) {
    corners_bottom(item);
  });
  $$(".rc-both").each(function (item) {
    corners_top(item);
    corners_bottom(item);
  });
	
  $$("table.list tr:nth-child(even)").each(function (item) {
    item.addClassName('even')
  });
  $$("table.list tr:nth-child(odd)").each(function (item) {
    item.addClassName('odd')
  });
  
  /* Pop links to articles around images with no link */
  $$('div.section h2').each(function (e) {
	  var d = e.firstDescendant();
	  var cu = '';
	  if (d && d.href) {
		  cu = d.href;
	  }

	  if (cu) {
		  pa = e.up()
		  if (pa && pa.getElementsByTagName('h2').length ==1) {
			pa.getElementsBySelector('h2 + p + div.entry img').each(function(img) {
				if (!img.up().href) {
					img.wrap('a', { 'href': cu });
				}
			});
		  }
	  }
  });
  
  set_footer_position();
});


Event.observe(window, 'load', function() {
  set_footer_position();
});

function set_footer_position () {
  if ($('footer-wrapper').style.position == 'fixed') return;

  var footer_position = $('footer-wrapper').positionedOffset();
  var footer_top = footer_position[1];
  var footer_bottom = $('footer-wrapper').getHeight()+footer_top;
  if (document.viewport.getHeight() > footer_bottom) {
    $('footer-wrapper').style.position = 'fixed';
  }
}

// SIFR config
var exuberanceb = {
    src: '/images/exuberance-bold.swf'
};
var exuberance = {
    src: '/images/exuberance-regular.swf'
};

sIFR.activate(exuberance, exuberanceb);

sIFR.replace(exuberanceb, {
  selector: 'h2.main',
  wmode: "transparent"
  ,css: [
    '.sIFR-root { color: #605D8B; leading: -25; font-size: 24px;}'
  ]
});

sIFR.replace(exuberance, {
  selector: 'div.author h2',
  wmode: "transparent"
  ,css: [
    '.sIFR-root { color: #FFFFFF; leading: -25; font-size: 18px;}'
  ]
});

sIFR.replace(exuberance, {
  selector: 'div#column-1 div.strap',
  wmode: "transparent"
  ,css: [
    '.sIFR-root { color: #605D8B; leading: -25; font-size: 18px;}'
  ]
});

sIFR.replace(exuberance, {
  selector: 'div.comments h2',
  wmode: "transparent"
  ,css: [
    '.sIFR-root { color: #605D8B; leading: -25; font-size: 18px;}'
  ]
});

sIFR.replace(exuberance, {
  selector: 'div.write-comment h2',
  wmode: "transparent"
  ,css: [
    '.sIFR-root { color: #605D8B; leading: -25; font-size: 18px;}'
  ]
});

sIFR.replace(exuberance, {
  selector: 'div#column-2 div.strap',
  wmode: "transparent"
  ,css: [
    '.sIFR-root { color: #605D8B; font-size: 14px;}'
  ]
});
