$(document).ready(function() {	
	// 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);
		}
	});
	
	// Resize images on pageload
	resizeImages();
	
	// Resize images on redraw
	$(window).resize(function() {
		resizeImages();
	});
	
	// Make off-site links pop up in new window
	$('a:not([href^=.])').attr('target', '_blank');
		
	// TODO: Convert to jquery
	// Add onclick calls to forum and topic rows
	var dts = document.getElementsByTagName('dt');
	for (i=0; i<dts.length; i++) {
		if (dts[i].parentNode.parentNode.className.substr(0,3) == 'row') {
			if (dts[i].parentNode.parentNode.parentNode.className.substr(0,9) == 'topiclist') {
				dts[i].onclick = topicListRowClick;
			}
		}
	}
	
	// Add JS functions to spoiler boxes
	// TODO: Convert to jquery
	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';
		}
	}
})

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

function topicListRowClick(evt) {
		if (!evt) {
				var evt = window.event;
		}
		if (checkModifierKeys(evt)) {
				if (window.location.href.indexOf('viewforum') == -1) {
						var as = this.getElementsByTagName('a');
						window.location.href = as[0].href;
				} else if (this.title == 'New posts') {
						window.location.href = this.firstChild.href;
				} else if (this.title == 'No new posts') {
						var dds = this.parentNode.getElementsByTagName('dd');
						var as = dds[2].firstChild.getElementsByTagName('a');
						window.location.href = as[1].href;
				} else {
						var as = this.getElementsByTagName('a');
						window.location.href = as[0].href;
				}
		}
}

function checkModifierKeys(evt) {
		if ((evt.ctrlKey == false) && (evt.shiftKey == false) && (evt.metaKey == false) && (evt.altKey == false) && (evt.button == 0)) {
				return true;
		} else {
				return false;
		}
}

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;
}
