function resizeImages() {
	if (window.location.href.indexOf('viewtopic') != -1) {
		// .not([src*="dmbtabs.com"])
		
		$('div.content img').each(function() {
				$(this).scale();
		});
			
/*		$('img[src*="http"]').each(function() {
			$(this).scale();
		}); */
	}
}

function toggleSpoiler(evt) {
		var spoiler_text_div = this.parentNode.parentNode.childNodes[1];
		if (spoiler_text_div.style.display == 'none') {
				spoiler_text_div.style.display = 'block';
				var new_text = document.createTextNode('HIDE');
				this.replaceChild(new_text, this.firstChild);
		} else {
				spoiler_text_div.style.display = 'none';
				var new_text = document.createTextNode('REVEAL');
				this.replaceChild(new_text, this.firstChild);
		}
		return false;
}

$(document).ready(function() {		
	// TODO: Convert to jQuery
	// Add JS functions to spoiler boxes
  var divs = document.getElementsByTagName('div');
  for (i=0; i<divs.length; i++) {
      if (divs[i].className == 'spoiler-div') {
          var inter_text = document.createTextNode(' :: ');
          var reveal_link = document.createElement('a');
          reveal_link.onclick = toggleSpoiler;
          reveal_link.href = '#';
          var reveal_text = document.createTextNode('REVEAL');
          reveal_link.appendChild(reveal_text);
          divs[i].firstChild.appendChild(inter_text);
          divs[i].firstChild.appendChild(reveal_link);
          divs[i].childNodes[1].style.display = 'none';
          divs[i].childNodes[1].style.color = '#000';
      }
  }
	
	// Resize images on pageload
	resizeImages();
	
	// Resize images on redraw
	$(window).resize(function() {
		resizeImages();
	});
	
	// Add affiliate tag to amazon links
	$('a[href*=amazon.com]').each(function() { 
		var href = $(this).attr('href');
		var re = /\/([A-Z0-9]{10})(\/|\?|\b)/i
		var asin = re.exec(href);
		if (asin[1]) {
			var newurl = 'http://www.amazon.com/gp/product/'+asin[1]+'?tag=dmbtabs-20';
			$(this).attr('href', newurl);
		}
	});
	
});
