/* -*- coding: utf-8 -*- If this: € 
   isn't a Euro sign, your editor is not UTF-8 safe! */

var globalDebug;

var langToAppropriateClassMap = {
    "tg" : "lang_tg",
    "zh" : "lang_zh",
    "ja" : "lang_ja",
    "ar" : "lang_ar",
    "fa" : "lang_fa"
};

/* Because the OS X fonts treat Han characters, correctly, as being twice
   the average size of their Roman-alphabet counterparts, the special
   handling we do to enforce this under Windows and Unix should not be
   done. As such, we need to work out if the platform is OS X before
   applying that handling.  */ 

var langToAppropriateOSXClassMap = {
    "tg" : "lang_tg",
    "ar" : "lang_ar",
    "zh" : "lang_zh_osx",
    "ja" : "lang_ja_osx",
    "fa" : "lang_fa"
};

var imagesToPreload = [ "/images/preview-watermark.png" ];

var spanWithTitleClass = "span_with_title";

function isOSX() {
    var idex = navigator.plugins.length;
    for (--idex; idex > -1; --idex) {
	if (navigator.plugins[idex].name.indexOf
	    ("Default Plugin Carbon.cfm") > -1 
	    || navigator.plugins[idex].name.indexOf("OS X") > -1) {
	    return true;
	}
    }
    return false;
}

var deliciousURLMap = {
  "delicious-network-fans" : "http://del.icio.us/aidan",
  "delicious-network-add" : "http://del.icio.us/network?add=aidan",
  "delicious-network-people" : "http://del.icio.us/network/aidan",
  "delicious-network-username" : "http://del.icio.us/aidan"
};

var deliciousIMGMap = {
  "delicious-network-fans" : "/images/clear.small.gif",
  "delicious-network-add" : "/images/add.small.gif",
  "delicious-network-people" : "/images/clear.small.gif",
  "delicious-network-username" : "/images/delicious.small.gif"
};
/* Instead of applying CSS using language tags, map those tags to normal
   classes, since the latter technique works with MSIE */

function workWithSpanTags ()
{
    var spannende = document.getElementsByTagName("span"); 

    for (var s in spannende) {
	if (spannende[s].getAttribute) {
	    var spanLang = spannende[s].getAttribute("lang");
	    var spanTitle = spannende[s].getAttribute("title");
	    var className = spannende[s].className;

	    if (spanLang) {
		if (langToAppropriateClassMap[spanLang]) {
		    spannende[s].className += " " + 
			langToAppropriateClassMap[spanLang];
		} 
		if (spanTitle) {
		    spannende[s].className += " " + 
			spanWithTitleClass;
		}
	    }

	    if (className.match(/delicious-network/)) {
		var links;
		/* Make sure the images are always there. */
		if (deliciousURLMap[className] && 
		    0 == spannende[s].getElementsByTagName("img").length) {
		    spannende[s].innerHTML = '<a href="' 
			+ deliciousURLMap[className] + '"><img src="' 
			+ deliciousIMGMap[className]
			+ '" alt="del.icio.us" border="0"></a> '
			+ spannende[s].innerHTML; 
		}
		links = spannende[s].getElementsByTagName("a");
		for (var l in links) {
		    links[l].className += " delicious-network-badge";
		}
	    }
	}
    }
}

function handleClickAndRewrite (ev)
{
    var passReferer = "";
    /* No need to separately track loading URLs on this server. */
    if (!(this.hostname.match(/parhasard\.net$/i) || 
	  /* And subscription links shouldn't be rewritten at all. */
	  (this.rel && "subscribe" == this.rel) || 
	  /* Don't rewrite for right clicks, that's particularly annoying,
	     since URLs are rewritten before being copied. */
	  (ev && ev.which && (2 == ev.which || 3 == ev.which)) ||
	  (ev && ev.button && (2 == ev.button || 3 == ev.button)))) {
	/* Avoid passing referer information to our referers; if referer
	   spammers get any (left) clicks from me, they won't know about
	   it. Mozilla doesn't quite do this correctly.  */
	if (this.rel && "citation" == this.rel) {
	    passReferer = "noref=1&";
	}
	this.href = "/redirect.php?" + passReferer + "url="
	    + encodeURIComponent(this.href);
    } else {
	this.onmousedown = null;
    }
    return true;
} 

function workWithFormSubmits ()
{
    var forming = document.getElementsByTagName("form");

    for (var i in forming) {
	if (forming[i].getAttribute) {
	    forming[i].onsubmit = function () {
		var inputs = document.getElementsByTagName("input");
		for (var ipt in inputs) {
		    if (inputs[ipt].getAttribute && 
			"submit" == inputs[ipt].getAttribute("type")) {
			/* We can't disable the form submit buttons, because
			   that means their values don't go through to the
			   PHP. We can make it look like they've been
			   disabled, though--that's what the "disabled"
			   class in the PHP is there for.  */
			inputs[ipt].onclick = function () { 
			    return false; 
			};
			inputs[ipt].className += " disabled";
		    }
		}
		return true;
	    }
	}
    }
}

var requestObject;

function showEntryComments (ev)
{
    var entryCommentsURL = this.href.replace(/\/entry\//, "/entry-comment/");
    if (!requestObject) {
	requestObject = new XMLHttpRequest();
    }
    
    requestObject.open("GET", entryCommentsURL, true);

    
}

/* Pass any clicked links to a script on our server that records where the
   user's going, what page they went from, and then passes them there.

   Also, make sure that MSIE doesn't display a square block for zero width
   spaces in links (which are added to help them wrap).  */

function workWithLinkTags ()
{
    var reffing = document.getElementsByTagName("a");
    for (var r in reffing) {
	if (reffing[r]) { 
	    if (reffing[r].innerHTML) {
		reffing[r].innerHTML = reffing[r].innerHTML.replace(/​/g, 
		    "<span class=\"MSIE_good_coverage\">​</span>");
	    }
	    if (reffing[r].rel && "child" == reffing[r].rel 
		&& reffing[r].href) {
		reffing[r].onmousedown = showEntryComments;
	    } else if (reffing[r].href) {
		reffing[r].onmousedown = handleClickAndRewrite;
	    }
	}
    }
} 

window.onload = function ()
{
    var worthIt = document.getElementById;
    var tmpImage = false;

    globalDebug = -1 != window.location.href.indexOf("archive.php");

    if (isOSX()) {
	langToAppropriateClassMap = langToAppropriateOSXClassMap;
    }

    if (worthIt) {
	workWithSpanTags();
	workWithLinkTags();
	workWithFormSubmits();
    }

    /* Preload any images we want to. */
    tmpImage = new Image();
    for (var i in imagesToPreload) {
	tmpImage.src = imagesToPreload[i];
    }

    
}

/* BEGIN addEvent.js */

inArray = function (ourArray, value)
{
    for (var i in ourArray) { 
	if (value === ourArray[i]) {
	    return true;
	}
    }
    return false;
};

function addEvent(obj, type, fn) 
{
    if (obj.addEventListener) {
	obj.addEventListener(type, fn, false);
	EventCache.add(obj, type, fn);
    } else 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] );
	EventCache.add(obj, type, fn);
    } else {
	obj["on"+type] = obj["e"+type+fn];
    }
}
	
var EventCache = function() 
{
    var listEvents = [];

    return {
	listEvents : 
	listEvents, add : function(node, sEventName, fHandler)
	    {
		listEvents.push(arguments);
	    },
	    flush : function()
	    {
		var i, item;
	    for(i = listEvents.length - 1; i >= 0; i = i - 1){
		item = listEvents[i];
		if(item[0].removeEventListener){
		    item[0].removeEventListener(item[1], item[2], item[3]);
		}
		if(item[1].substring(0, 2) != "on"){
		    item[1] = "on" + item[1];
		}
		if(item[0].detachEvent){
		    item[0].detachEvent(item[1], item[2]);
		}
		item[0][item[1]] = null;
	    }
	}
    };
} ();

addEvent(window, 'unload', EventCache.flush);

/* END addEvent.js */
/* BEGIN sweetTitles.js */
/* Sweet Titles (c) Creative Commons 2005
   http://creativecommons.org/licenses/by-sa/2.5/
   Author: Dustin Diaz | http://www.dustindiaz.com */
var sweetTitles = { 
    xCord : 0, // @Number: x pixel value of current cursor position
    yCord : 0, // @Number: y pixel value of current cursor position

    tipElements : ['a','abbr','acronym', 'span'], 
	// @Array: Allowable elements that/ can have the toolTip

    obj : Object, // @Element: That of which you're hovering over
    tip : Object, // @Element: The actual toolTip itself
    active : 0, // @Number: 0: Not Active || 1: Active
    init : function() 
    {
	if (!document.getElementById ||
	    !document.createElement ||
	    !document.getElementsByTagName) {
	    return;
	}
	var i,j;
	this.tip = document.createElement('div');
	this.tip.id = 'toolTip';
	document.getElementsByTagName('body')[0].appendChild(this.tip);
	this.tip.style.top = '0';
	this.tip.style.visibility = 'hidden';
	for (i in this.tipElements) {
	    var current = document.getElementsByTagName(this.tipElements[i]);
	    for (j in current) {
		if (current[j].getAttribute && 
		    current[j].getAttribute("title")) {
		    addEvent(current[j],'mouseover',this.tipOver);
		    addEvent(current[j],'mouseout',this.tipOut);
		    current[j].setAttribute('tip',current[j].title);
		    current[j].removeAttribute('title');
		}
	    }
	}
    },
    updateXY : function(e) 
    {
	if (document.captureEvents) {
	    sweetTitles.xCord = e.pageX;
	    sweetTitles.yCord = e.pageY;
	} else if ( window.event.clientX ) {
	    sweetTitles.xCord = 
		window.event.clientX+document.documentElement.scrollLeft;
	    sweetTitles.yCord = 
		window.event.clientY+document.documentElement.scrollTop;
	}
    },
    tipOut: function() 
    {
	if (window.tID) {
	    clearTimeout(tID);
	}
	if (window.opacityID) {
	    clearTimeout(opacityID);
	}
	sweetTitles.tip.style.visibility = 'hidden';
    },
    checkNode : function() 
    {
	var trueObj = this.obj;
	if (inArray(this.tipElements, trueObj.nodeName.toLowerCase())) {
	    return trueObj;
	} else {
	    return trueObj.parentNode;
	}
    },
    tipOver : function(e) 
    {
	sweetTitles.obj = this;
	tID = window.setTimeout("sweetTitles.tipShow()",500);
	sweetTitles.updateXY(e);
    },
    tipShow : function() 
    {
	var scrX = Number(this.xCord);
	var scrY = Number(this.yCord);
	var tp = parseInt(scrY+15);
	var lt = parseInt(scrX+10);
	var anch = this.checkNode();
	var addy = '';
	var access = '';
	if (anch.nodeName.toLowerCase() == 'a') {
	    addy = (anch.href.length > 35 ? 
		    anch.href.toString().substring(0,35)+"…" : anch.href);
	    var access = (anch.accessKey ? 
			  ' <span>['+anch.accessKey+']</span> ' : '' );
	} 
	this.tip.innerHTML = "<p>"+anch.getAttribute('tip')+"<em>"+access+addy+"</em></p>";
	if (parseInt(document.documentElement.clientWidth+document.documentElement.scrollLeft) < parseInt(this.tip.offsetWidth+lt)) {
	    this.tip.style.left = parseInt(lt-(this.tip.offsetWidth+10))+'px';
	} else {
	    this.tip.style.left = lt+'px';
	}
	if (parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) < parseInt(this.tip.offsetHeight+tp)) {
	    this.tip.style.top = parseInt(tp-(this.tip.offsetHeight+10))+'px';
	} else {
	    this.tip.style.top = tp+'px';
	}
	this.tip.style.visibility = 'visible';
	this.tip.style.opacity = '.1';
	this.tipFade(10);
    },
    tipFade: function(opac) 
    {
	var passed = parseInt(opac);
	var newOpac = parseInt(passed+10);
	if (newOpac < 88) {
	    this.tip.style.opacity = '.'+newOpac;
	    this.tip.style.filter = "alpha(opacity:" + newOpac + ")";
	    opacityID = window.setTimeout("sweetTitles.tipFade('"
					  + newOpac + "')", 20);
	} else { 
	    this.tip.style.opacity = '.88';
	    this.tip.style.filter = "alpha(opacity:88)";
	}
    }
};

function pageLoader() 
{
    sweetTitles.init();
}

addEvent(window, 'load', pageLoader);
/* END sweetTitles.js */

/* Local Variables: */
/* c-indentation-style: "stroustrup" */
/* End: */

