                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                /*
 * Copyright (c) 2009-2010 JS-Kit <support@js-kit.com>. All rights reserved.
 * You may copy and modify this script as long as the above copyright notice,
 * this condition and the following disclaimer is left intact.
 * This software is provided by the author "AS IS" and no warranties are
 * implied, including fitness for a particular purpose. In no event shall
 * the author be liable for any damages arising in any way out of the use
 * of this software, even if advised of the possibility of such damage.
 * $Id: wrapper.js 23112 2010-04-29 12:38:12Z jskit $
 */



if(!window.JSKitLib) JSKitLib = {vars:{}};





JSKitLib.map = function(f, arr) {
	if(arr) for(var i = 0; i < arr.length; i++) f(arr[i], i, arr);
	return arr;
}

JSKitLib.filter = function(f, arr) {
	var newArr = [];
	if(arr)
		for(var i = 0; i < arr.length; i++)
			if(f(arr[i], i, arr))
				newArr.push(arr[i]);
	return newArr;
}

JSKitLib.lookup = function(f, arr){
	return JSKitLib.filter(f, arr).shift();
}

JSKitLib.fmap = function(o,f) {
	var r, a = [], l = o.length;
	if(l > 0 || l === 0)
		for(var i = 0; i < l; i++) {
			r = f.call(this,o[i],i,arguments);
			if(r !== undefined) a.push(r);
		}
	else
		for(var i in o)
			if(o.hasOwnProperty(i)) {
				r = f.call(this,o[i],i,arguments);
				if(r !== undefined) a.push(r);
			}
	return a;
}

JSKitLib.foldl = function(acc,o,f) {
	var r, l = o.length;
	if(l > 0 || l === 0)
		for(var i = 0; i < l; i++) {
			r = f.call(this,o[i],acc,i);
			if(r != undefined) acc = r;
		}
	else
		for(var i in o)
			if(o.hasOwnProperty(i)) {
				r = f.call(this,o[i],acc,i);
				if(r != undefined) acc = r;
			}
	return acc;
}

JSKitLib.intersperse = function(f) {
	return JSKitLib.foldl([], this, function(e, acc, i) {
		if(acc.length) acc.push(f);
		acc.push(e);
	});
}

JSKitLib.merge = function() {
	return Array.prototype.concat.apply([], arguments);
}

JSKitLib.cloneObject = function(obj) {
	return JSKitLib.foldl({}, obj, function(value, acc, key) { acc[key] = value; });
}





JSKitLib.setEventHandler = function(obj, eventNames, eventHandler) {
	JSKitLib.fmap(eventNames, function(eventName) {
		obj["on" + eventName] = function(){
			eventHandler();
			return false;
		}
	});
}

JSKitLib.resetEventHandler = function(obj, eventNames) {
	JSKitLib.fmap(eventNames, function(eventName) {
		obj["on" + eventName] = function(){};
	});
}

JSKitLib.addEventHandler = function(obj, eventNames, eventHandler, capture) {
	JSKitLib.fmap(eventNames, function(e) {
		if (obj.addEventListener) {
			obj.addEventListener(e, eventHandler, !!capture);
		} else if (obj.attachEvent) {
			if (capture) {
				if (capture === true) capture = obj;
				capture.setCapture();
				capture.attachEvent('onlosecapture', eventHandler);
			}
			obj.attachEvent('on' + e, eventHandler);
		}
	});
}

JSKitLib.removeEventHandler = function(obj, eventNames, eventHandler, capture) {
	JSKitLib.fmap(eventNames, function(e) {
		if (obj.removeEventListener) {
			obj.removeEventListener(e, eventHandler, !!capture);
		} else if (obj.detachEvent) {
			if (capture) {
				if (capture === true) capture = obj;
				capture.detachEvent('onlosecapture', eventHandler);
				capture.releaseCapture();
			}
			obj.detachEvent('on' + e, eventHandler);
		}
	});
}

JSKitLib.setMouseEvent = function(obj, eventName, eventHandler) {
	var normalize = function(pr_event){
		e = pr_event || window.event;
		if (!e.target)
			e.target = e.srcElement || document;
		if (e.target.nodeType == 3)
			e.target = e.target.parentNode;
		if (!e.relatedTarget && e.fromElement)
			e.relatedTarget = (e.fromElement == e.target) ? e.toElement : e.fromElement;
		return e;
	};
	obj["onmouse" + eventName] = function(pr_event) {
		var e = normalize(pr_event);
		if (e.relatedTarget == obj || JSKitLib.isChildNodeOf(obj, e.relatedTarget)) return false;
		eventHandler(e);
	};
}

JSKitLib.stopEventPropagation = function(e) {
	if (!e) e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

JSKitLib.preventDefaultEvent = function(e) {
  if (!e) e = window.event;
  e.returnValue = false;
  if (e.preventDefault) e.preventDefault();
}

JSKitLib.deferCall = function(func, onlyIE) {
	if (!JSKitLib.vars.windowOnLoadFired && (!onlyIE || (onlyIE && JSKitLib.isIE() && !window.$JSKitNoDeferCallIfIE))) {
		JSKitLib.addEventHandler(window, ['load'], func);
	} else {
		func();
	}
}

JSKitLib.addHandlers = function(obj, moveHandler, upHandler, capture) {
	JSKitLib.addEventHandler(obj, ['mousemove'], moveHandler, capture);
	JSKitLib.addEventHandler(obj, ['mouseup'], upHandler, capture);
}

JSKitLib.removeHandlers = function(obj, moveHandler, upHandler, capture) {
	JSKitLib.removeEventHandler(obj, ['mousemove'], moveHandler, capture);
	JSKitLib.removeEventHandler(obj, ['mouseup'], upHandler, capture);
}

JSKitLib.notDraggable = function(element) {
	element.onselectstart = function(ev) { JSKitLib.stopEventPropagation(ev); return true; }
	element.onmousedown = JSKitLib.stopEventPropagation;
	return element;
}

JSKitLib.getMousePosition = function(e) {
	if (!e) var e = window.event;
	if (e.clientX || e.clientY) {
		return {x:e.clientX, y:e.clientY};
	} else {
		return {x:e.pageX, y:e.pageY};
	}
}

JSKitLib.preventSelect = function(element, exceptions) {
	var browser = JSKitLib.getBrowser();
	var prevent = function() {
		if (browser == 'IE' || browser == 'safari') {
			element.onselectstart = function() { return false; }
		} else if (browser == 'gecko') {
			JSKitLib.addClass(element, 'js-nsgecko');
		}
	}
	if (typeof exceptions == 'object') {
		var include = exceptions.include || [];
		var exclude = exceptions.exclude || [];
		// Do not handle for certain browsers
		if (exclude.length) {
			for (var i=0; i < exclude.length; i++) {
				if (exclude[i] != browser) {
					prevent();
				}
			}
		}
		// Handle for certain browsers
		if (include.length) {
			for (var i=0; i < include.length; i++) {
				if (include[i] == browser) {
					prevent();
				}
			}
		}
	} else {
		prevent();
	}
}

JSKitLib.timedRetry = function(obj) {
	if(obj.pred()) {
		obj.onSuccess();
	} else {
		obj.currentRetries = (obj.currentRetries || 0) + 1;
		if(obj.currentRetries > obj.maxRetries) {
			if(obj.onFailure) obj.onFailure();
		} else {
			if(obj.onRetry) obj.onRetry();
			setTimeout(function(){
					JSKitLib.timedRetry(obj);
				}, obj.timeout);
		}
	}
}

JSKitLib.addDOMLoadedListener = function(callback) {
	window.JSK$DOMLoadedCallbacks = window.JSK$DOMLoadedCallbacks || [];
	window.JSK$DOMLoadedCallbacks.push(callback);
	if (window.JSK$DOMLoadedCallbacks.length > 1)
		return;
	var totalListener = function() {
		JSKitLib.fmap(window.JSK$DOMLoadedCallbacks, function(c) { c(); });
	}
	switch (JSKitLib.getBrowser()) {
		case 'gecko':
		case 'opera':
			document.addEventListener("DOMContentLoaded", totalListener, false);
			break;
		case 'IE':
			var temp = document.createElement('div');
			(function() {
				try {
					temp.doScroll('left');
				} catch (e) {
					setTimeout(arguments.callee, 100);
					return;
				}
				totalListener();
			})();
			break;
		case 'safari':
			(function() {
				if (document.readyState != 'complete') {
					setTimeout(arguments.callee, 100);
					return;
				}
				totalListener();
			})();
			break;
		default:
			JSKitLib.addEventHandler(window, ['load'], totalListener);
	}
}






JSKitLib.isPreIE7 = function() {
	if (document.body && document.body.filters && parseInt(navigator.appVersion.split("MSIE") [1]) < 7)
		return true;
}

JSKitLib.isPreIE8 = function() {
	if (document.body && document.body.filters && parseInt(navigator.appVersion.split("MSIE") [1]) < 8)
		return true;
}

JSKitLib.isIE = function() {
	if (document.body && document.body.filters && navigator.appVersion.match(/MSIE/))
		return true;
}

JSKitLib.getBrowser = function() {
	if (JSKitLib.vars.browser) return JSKitLib.vars.browser;
	if (document.body && document.body.filters && navigator.appVersion.match(/MSIE/)) {
			JSKitLib.vars.browser = "IE";
	} else if ((navigator.appCodeName.toLowerCase()=="mozilla") 
		&& (navigator.appName.toLowerCase()=="netscape") 
		&& (navigator.product.toLowerCase()=="gecko") 
	) {
		if (navigator.userAgent.toLowerCase().indexOf("safari")!=-1) {
			JSKitLib.vars.browser = "safari";
		} else if (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1) {
			JSKitLib.vars.browser = "gecko";
		}
	} else if (navigator.product && navigator.product.toLowerCase()=="gecko") {
		JSKitLib.vars.browser = "gecko";
	} else if (navigator.appName.match(/Opera/)) { 
		JSKitLib.vars.browser = "opera"; 
	}
	return JSKitLib.vars.browser;
}

JSKitLib.isFF3 = function() {
	return (navigator.userAgent.indexOf("Firefox/3") != -1);
}

JSKitLib.isGChrome = function() {
	return (navigator.userAgent.toLowerCase().indexOf('chrome') != -1);
}

JSKitLib.isSafari = function() {
	if (navigator.appVersion.match(/Safari/)) {
		return true;
	}
}

JSKitLib.isOpera = function() {
	if (navigator.appName.match(/Opera/)) {
		return true;
	}
}



if (!window.JSKHL) JSKHL = {};

JSKHL.ref = function(domain) {
	var wl = window.location;
	return wl.protocol + "//" + domain + wl.pathname;
}

JSKHL.uniq = function(thread) {
	return thread.replace(/\W/g, '_').substr(0, 50);
}

JSKHL.permalink = function(uniq, haloLink) {
	var config = window.JSKitConfig || {};
	if (config['comments-permalink'])
		return config['comments-permalink'];

	// Blogger-specific extraction
	var e = haloLink.parentNode.firstChild;
	while (e && e.nodeType == 3) e = e.nextSibling;
	e = e && e.lastChild;
	if (e && e.tagName == 'A' && e.getAttribute('title') == 'permanent link') {
		return e.getAttribute('href');
	} else {
		var domain = (config['comments-domain'] || window.location.host);
		return 'http://js-kit.com/api/static/pop_comments?ref='
			+ encodeURIComponent(JSKHL.ref(domain)) + '&path=' + encodeURIComponent('/' + uniq);
	}
}

JSKHL.title = function(haloLink) {
	var config = window.JSKitConfig || {};
	if (config['comments-page-title'])
		return config['comments-page-title'];

	// Blogger-specific extraction
	var e = haloLink.parentNode.parentNode;
	if (e && e.className == 'post') {
		e = e.firstChild;
		while (e && e.tagName != 'H3') e = e.nextSibling;
		if (e && e.tagName == 'H3' && e.className == 'post-title') {
			var a = e.firstChild;
			while (a && a.nodeType == 3) a = a.nextSibling;
			return a ? a.innerHTML : e.innerHTML;
		}
	}
	return document.title;
}

JSKHL.commentsLabel = function(count) {
	if (typeof window.JSKitCommentsCountFilter == 'function') {
		return JSKitCommentsCountFilter(count);
	} else {
		if (count) return 'Comments (' + count + ')';
		else return 'Comments';
	}
}

JSKHL.replaceWithEcho = function(uniq, haloLink) {
	var config = window.JSKitConfig || {};
	var div = document.createElement('div');
	div.className = 'js-kit-comments';
	div.style.display = 'none';
	var config = {'uniq': uniq, 'editable': 'yes', 'backwards': config['comments-backwards'] || 'no',
		'permalink': JSKHL.permalink(uniq, haloLink), 'page-title': JSKHL.title(haloLink)};
	JSKitLib.fmap(config, function(v, k) { div.setAttribute(k, v); });
	div.jsk$haloLink = haloLink;

	var a = haloLink.cloneNode(false);
	if (a.className) a.className += ' ';
	a.className += 'js-CommentsPopupLink';
	a.setAttribute('href', 'javascript:void(0);');
	a.removeAttribute('onclick');
	a.innerHTML = '{LinkLabel}';
	div.appendChild(a);

	haloLink.parentNode.insertBefore(div, haloLink);
}

JSKHL.appendEchoScript = function() {
	if (window.JSCC) return;
	if (!document.body){
		alert("Enclose the script in a <BODY></BODY> tag!");
		return;
	}
	var sc = document.createElement('script');
	sc.src = 'http://cdn.js-kit.com/scripts/comments.js';
	sc.type ='text/javascript';
	sc.charset = 'utf-8';
	document.body.appendChild(sc);
}

function postCount(thread) {
	var uniq = JSKHL.uniq(thread);
	window.jsk$halo_id = window.jsk$halo_id || 0;
	var id = 'jsk_' + uniq + '_' + window.jsk$halo_id;
	window.jsk$halo_id++;
	document.write('<span id="' + id + '">' + JSKHL.commentsLabel(0) + '</span>');
	var haloLink = document.getElementById(id).parentNode;
	JSKHL.replaceWithEcho(uniq, haloLink);

	if (!window.jsk$setDOMLoadedHandler) {
		JSKitLib.addDOMLoadedListener(JSKHL.appendEchoScript);
		window.jsk$setDOMLoadedHandler = true;
	}
}

function postCountTB(thread) {
	// Removing " | Trackbacks"
	var uniq = JSKHL.uniq(thread);
	window.jsk$halo_tb_id = window.jsk$halo_tb_id || 0;
	var id = 'jsk_tb_' + uniq + '_' + window.jsk$halo_tb_id;
	window.jsk$halo_tb_id++;
	document.write('<span id="' + id + '"></span>');
	setTimeout(function() {
		var tb = document.getElementById(id).parentNode;
		var pipe = tb.previousSibling;
		if (pipe && pipe.nodeType == 3) pipe.nodeValue = ' ';
		tb.parentNode.removeChild(tb);
	}, 0);
}

function HaloScan(thread) { void(0); }

function HaloScanTB(thread) { void(0); }

if (!window.jsk$wrapper_loaded) {
	(function() {
		var config = window.JSKitConfig || {};
		window.JSKitEvents = window.JSKitEvents || [];
		JSKitEvents.push({subscribe: 'comments-count-updated', callback: function(eventName, eventParams) {
			// Hiding old links rendered by postCount()
			var e = $JCA[eventParams.jcaIndex].target.jsk$haloLink;
			if (e) e.parentNode.removeChild(e);
		}});
	})();
	window.jsk$wrapper_loaded = true;
}
