// addEvent as created by John Resig, http://ejohn.org/projects/flexible-javascript-events/
function addEvent( obj, type, fn ) {
	if ( obj.attachEvent ) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent('on'+type, obj[type+fn]);
	} else
		obj.addEventListener(type, fn, false);
}
function removeEvent(obj, type, fn) {
	if ( obj.detachEvent ) {
		obj.detachEvent( 'on'+type, obj[type+fn] );
		obj[type+fn] = null;
	} else
		obj.removeEventListener(type, fn, false);
}

// Insignificantly changed from the original script at http://simon.incutio.com/archive/2002/12/20/#blockquoteCitations
// Displays the content of the cite attribute in the blockquote element as a clickable source link if it's not empty
function display_blockquote_cite() {
	var quotes, cite;
	
  quotes = document.getElementsByTagName('blockquote');
  for (var i = 0; i < quotes.length; i++) {
    cite = quotes[i].getAttribute('cite');
    if ( (cite != null) && (cite.indexOf('http') != -1) ) {
			var link, div;
			
      link = document.createElement('a');
      link.setAttribute('href', cite);
      link.setAttribute('title', cite);
      link.appendChild(document.createTextNode('Source'));
      div = document.createElement('div');
      div.className = 'blockquotesource';
      div.appendChild(link);
      quotes[i].appendChild(div);
    }
  }
}

// Adds an additional class if a link is to another location
function display_link_location () {
	var i, x;
	
	for (i=0; x=document.links[i]; i++) {
		if (x.hostname != location.hostname) {
			x.className += ' foreign';
		}
	}
}
/*
// Enable flexible Sidenotes in browsers which don't support CSS3 media queries, written by Frans de Jonge
// if this browser is not Opera, apply the Javascript functions
// I couldn't think of a way to test wether the browser supports mediaqueries or not via Javascript and this is true for now anyway...
if (!window.opera) {
	addEvent(window, 'DOMContentLoaded', sidenotes);
	addEvent(window, 'resize', sidenotes);
}

function sidenotes() {
	// declare variables
	var cont, exec, notes, w_medium, w_large, width;
	
	// widths where switching should occur
	// only values you'd have to change on similar setups
	w_medium = 750;
	w_large= 950;
	
	// assign values to variables
	cont = document.getElementById('wrapper');
	notes = document.getElementsByTagName('small');
	
	for (var i=0; i<notes.length; i++) {
		if (notes[i].className == 'sidenote') {
			exec = true;
			return;
		}
	}
	
	if (exec == true) {
	
	// if Gecko (or Opera as you don't need mediaqueries) get the width this way
	if (window.outerWidth) {
		width = window.outerWidth;
	}
	// otherwise try the IE method
	else if (document.body.offsetWidth) {
		width = document.body.offsetWidth;
	}
	// if that doesn't work then either the browser will support mediaqueries, or use the float version
	else {
		width = 0;
	}
	
	if (width > w_medium) {
		if (width > w_large) {
			cont.style.marginRight = 'auto';
		}
		else {
			cont.style.marginRight = '200px';
		}
		for (var i=0; i<notes.length; i++) {
			if (notes[i].className == 'sidenote') {
				notes[i].className = 'sidenote sidenote2';
			}
		}
	}
	else {
		cont.style.marginLeft = 'auto';
		
		for (var i=0; i<notes.length; i++) {
			if (notes[i].className == ('sidenote sidenote2')) {
				notes[i].className = 'sidenote';
			}
		}
	}
	
	}
	
}
*/

// Inspired by the quickquote on http://my.opera.com/forums
var quote_text = '';
var quote_source = '';

function quote_selection_init() {
	if( window.getSelection || ( document.selection && document.selection.createRange ) ) {
		addEvent(document, 'mouseup', set_quote_text);
		
		var qq_possible_containers = document.getElementsByTagName('span');
		for (var i=0; i<qq_possible_containers.length; i++) {
			if (qq_possible_containers[i].className == 'permalink') {
				qq_possible_containers[i].innerHTML = '<a href="javascript:quote_selection(\'#' + qq_possible_containers[i].parentNode.parentNode.getAttribute('id') + '\')">Quote selection</a> &#183; ' + qq_possible_containers[i].innerHTML;
			}
		}
	}
}
function quote_selection(quote_source) {
	var comment = document.getElementById('comment');
	if (comment && (quote_text != '') ) {
		if (quote_source != '') quote_source = ' cite="http://' + window.location.hostname + window.location.pathname + quote_source + '"';
		comment.value += '<blockquote' + quote_source + '>' + quote_text + '</blockquote>\n';
		comment.focus();
	}
}
function set_quote_text() {
	var text = window.getSelection ? window.getSelection().toString() : document.selection.createRange().text;
	if (text.length > 0) quote_text = text;
}

// Checks if there is deleted text in post entries. Adds a Show or hide deleted text button if there is.
// Copyright © Frans de jonge 2009. Licensed under a Creative Commons Atrribution 2.0 license.
function AddShowHideDeletedTextButton() {
	var postEntries = document.getElementsByClassName('postentry');
	var postEntry = postEntries[0];
	if ( postEntries.length == 1 && postEntry.getElementsByTagName('del').length > 0 ) {
		var hideText = 'Hide deleted text', showText = 'Show deleted text';
		var button = document.createElement('input');
		button.setAttribute('type', 'button');
		button.setAttribute('value', hideText);
		button.addEventListener('click',
		function () {
			var e = postEntry.getElementsByTagName('del');
			for ( var i = 0; i<e.length; i++ ) {
				if (e[i].style.display == 'none') {
					e[i].style.display = 'inline';
					this.setAttribute('value', hideText);
				}
				else {
					e[i].style.display = 'none';
					this.setAttribute('value', showText);
				}
			}
		}
		,false);
		postEntry.insertBefore(button,postEntry.firstChild);
	}
}

// the events which will be executed
addEvent(window, 'DOMContentLoaded', display_blockquote_cite);
addEvent(window, 'DOMContentLoaded', display_link_location);
addEvent(window, 'DOMContentLoaded', quote_selection_init);
addEvent(window, 'DOMContentLoaded', AddShowHideDeletedTextButton);
