/*
 * Michi Kono
 * michikono.com
 * Created December 28, 2006
 *
 *  Nsfw JS is released under the MIT license. For more information, visit michikono.com.
 *	Please keep this portion intact.
 */

var Nsfw = {
	version: 1.0,
	defaultWarning: 'The following content is potentially adult oriented and may be innapproriate depending on where you are currently. Are you sure you wish to continue?',
	defaultBackgroundColor: '#000000',
	defaultColor: '#000000', // should be the same as backgroundColor
	defaultImage: 'nsfw.gif', // relative to location of page being accessed

	promptImages: true, // should there even BE a "confirm" dialog for images?
	promptText: true, // should there even BE a "confirm" dialog for text (div/p)?
	
	initialize: function(message) {
		this.a(message);
		this.div(message);
		this.img();
	},
	
	// protects links from oops-clicks
	a: function(adultWarningMessage) {
		if((typeof adultWarningMessage) == 'undefined' || adultWarningMessage == '') {
			adultWarningMessage = this.defaultWarning;
		}
		
		var allLinks = document.getElementsByTagName('a');
		
		for(var i = 0; i < allLinks.length; i++) {
		
			//find all a.rel or a.className tags with the value 'adult' or 'nsfw'
			if(-1 != allLinks[i].rel.indexOf('adult') || -1 != allLinks[i].rel.indexOf('nsfw') || -1 != allLinks[i].className.indexOf('adult') || -1 != allLinks[i].className.indexOf('nsfw')) {	
				
				// this tag hasn't already been processed by this function, process it again
				if(-1 == allLinks[i].className.indexOf('adult-content')) {
				
					// take the old value of onclick so we don't overwrite it
					var theRest = '';
					if((typeof allLinks[i].onclick) != 'undefined' && allLinks[i].onclick) {
						theRest = allLinks[i].onclick;
						
						theRest = String(eval(theRest));
						theRest = theRest.substr(theRest.indexOf('{'));
					}
					
					// assign a custom class name for easy CSS integration. Don't assign this by hand because
					// this is used to determine if the tag has already been processed.
					allLinks[i].className = ' adult-content';
	
					// rewrite the onclick method to include a confirm dialog.
					eval('allLinks[i].onclick = function() {' + 
						'if(!confirm(adultWarningMessage)) { return false; }' + 
						theRest + 
						'this.onclick = function() { ' + theRest + ' };' +
					'}');
	
				}
			}
		}
	},
	
	// hides images
	img: function(replacement) {
		if((typeof replacement) == 'undefined') {
			replacement = this.defaultImage;
		}
			
		var allImages = document.getElementsByTagName('img');
		
		for(var i = 0; i < allImages.length; i++) {
				
			//find all img.rel tags with the value 'adult' or 'nsfw'
			if(-1 != allImages[i].className.indexOf('adult') || -1 != allImages[i].className.indexOf('nsfw')) {	
				
				// this tag hasn't already been processed by this function, process it again
				if(-1 == allImages[i].className.indexOf('adult-content')) {

					var original = allImages[i].src;
					allImages[i].src = replacement;

					// take the old value of onclick so we don't overwrite it
					var theRest = '';
					if((typeof allImages[i].onclick) != 'undefined' && allImages[i].onclick) {
						theRest = allImages[i].onclick;
						
						theRest = String(eval(theRest));
						theRest = theRest.substr(theRest.indexOf('{'));
					}
					
					// assign a custom class name for easy CSS integration. Don't assign this by hand because
					// this is used to determine if the tag has already been processed.
					allImages[i].className = ' adult-content';
	
					// rewrite the onclick method to include a confirm dialog.
					if(this.promptImages) {
							eval('allImages[i].onclick = function() {' + 
							'if(!confirm(\'' + this.defaultWarning + '\')) { return false; } else { this.src=\'' + original + '\'; }' + 
							theRest + 
							'this.onclick = function() { ' + theRest + ' };' +
						'}');
					}
					else {
						eval('allImages[i].onclick = function() {' + 
							'this.src=\'' + original + '\'; ' + 
							theRest + 
							'this.onclick = function() { ' + theRest + ' };' +
						'}');
					}
				}
			}
		}

	},

	// hides div and p tags
	div: function(adultWarningMessage) {
		if((typeof adultWarningMessage) == 'undefined' || adultWarningMessage == '') {
			adultWarningMessage = this.defaultWarning;
		}
		
		var allDivs = new Array();
		
		var temp = document.getElementsByTagName('p');
		for(var i = 0; i < temp.length; i++) {
			allDivs[i] = temp[i];
		}

		var temp = document.getElementsByTagName('div');
		var startLength = allDivs.length;
		for(var i = 0; i < temp.length; i++) {
			allDivs[startLength + i] = temp[i];
		}

		for(var i = 0; i < allDivs.length; i++) {
			//find all p.className|div.className tags with the value 'adult' or 'nsfw'
			if(-1 != allDivs[i].className.indexOf('adult') || -1 != allDivs[i].className.indexOf('nsfw')) {	

				// this tag hasn't already been processed by this function, process it again
				if(-1 == allDivs[i].className.indexOf('adult-content')) {

					var originalColor = allDivs[i].style.color;
					var originalBackground = allDivs[i].style.backgroundColor;
					allDivs[i].style.color = this.defaultColor;
					allDivs[i].style.backgroundColor = this.defaultBackgroundColor;

					// take the old value of onclick so we don't overwrite it
					var theRest = '';
					if((typeof allDivs[i].onclick) != 'undefined' && allDivs[i].onclick) {
						theRest = allDivs[i].onclick;
						
						theRest = String(eval(theRest));
						theRest = theRest.substr(theRest.indexOf('{'));
					}
					
					// assign a custom class name for easy CSS integration. Don't assign this by hand because
					// this is used to determine if the tag has already been processed.
					allDivs[i].className = ' adult-content';
					
					
					// rewrite the onclick method to include a confirm dialog.
					if(this.promptText) {
						eval('allDivs[i].onclick = function() {' + 
							'if(!confirm(\'' + this.defaultWarning + '\')) { return false; } else { this.style.color=\'' + originalColor + '\'; this.style.backgroundColor=\'' + originalBackground + '\'; }' + 
							theRest + 
							'this.onclick = function() { ' + theRest + ' };' +
						'}');
					}
					else {
						eval('allDivs[i].onclick = function() {' + 
							'this.style.color=\'' + originalColor + '\'; this.style.backgroundColor=\'' + originalBackground + '\';' + 
							theRest + 
							'this.onclick = function() { ' + theRest + ' };' +
						'}');
					}
				}
			}
		}

	}

}