//-----------------------------------------------------------------------------
//	ImageCaptions v0.9
//	by Daniel Rubin - http://www.rubinary.com
//      http://www.rubinary.com/2008/03/24/script-image-captions/
//
//	Lecensed under Create Commons Attribution 2.5 License
//	http://creativecommons.org/licenses/by/2.5/
//
//	This script allows the owner of the site have the captions
//	added to the images of specified class. The caption text is
//	obtained from the alt attribute of the image.
//	
//	Installation is as simple as including this script in the
//	header of the web page.
//
//	You can specify the style in #caption section.
//	Proposed styling:
//
//	#caption {
//		text-align: center;
//		font: 13px Tahoma, "Trebuchet MS";
//		border: 0; padding: 2px 5px 3px 5px;
//		color: white; background: black;
//		opacity: .70; filter: alpha(opacity=70);
//		cursor: pointer;
//	}
//	
//	This script requires the Prototype framework.
//	http://www.prototypejs.org/
//
//-----------------------------------------------------------------------------
//
// Edit the line below to include the classes of the images that
// you want captioned.
// 
var imageClasses = ['centered','alignleft','alignright','aligncenter',
			'imagerow','imagerow2'];
//var imageClasses = [];
//
// Do not edit anything below this line. Unless you really want to that is.
//

var ImageCaption = Class.create();

ImageCaption.prototype = {

	initialize: function() {
		this.updateImageList();
                var objBody = document.getElementsByTagName("body").item(0);
                var objCaption = document.createElement("div");
                objCaption.setAttribute('id','drCaption');
                objCaption.style.visibility = 'hidden';
                objCaption.style.position = 'absolute';
                objCaption.style.zIndex = '10';
		if (document.all) {
			objCaption.onmouseover = function() {
				window.event.cancelBubble = true;
			};
		}
		this.objCaption = objCaption;
                objBody.appendChild(this.objCaption);
	},

	updateImageList: function() {
                if (!document.getElementsByTagName){ return; }
		var images = document.getElementById("posts")
				.getElementsByTagName('img');
                var objBody = document.getElementsByTagName("body").item(0);

		for (var i = 0; i < images.length; i++) {
			var image = images[i];
			if (image.className == 'wp-smiley' ||
				image.className.indexOf('size-large') != -1)
					 continue;
			className = image.className.split(" ");
			if (imageClasses.indexOf(className[0]) != -1) {
/*
				var prnt = image.parentNode.parentNode;
				var wrapper = document.createElement("div");
				wrapper.appendChild(
					image.parentNode.cloneNode(true));
				prnt.replaceChild(wrapper, image.parentNode);
				image = wrapper.childNodes[0].childNodes[0];
*/
				var wrapper = image.parentNode;
				wrapper.className += " " + className.shift();
				wrapper.style.position = 'relative';
				image.className = className.join(' ');

				wrapper.onmouseover = function() {
					myImageCaption.start(this);
				};
				wrapper.onmouseout = function(event) {
					myImageCaption.end(event);
				};
			}
			if (className[0] == 'iphone') {
 				var wrapper = image.parentNode;
                		var objPhone = document.createElement("div");
        	       		objPhone.className = 'iCaption';
				wrapper.appendChild(objPhone);
			}
		}
	},

	checkPointer: function(e) {
		if (!e) e = window.event;
		var pos = this.findPos(this.wrapper, 0, 0);
		var scrollTop = 
			(window.pageYOffset ? window.pageYOffset :
			 document.documentElement.scrollTop);
		var scrollLeft = 
			(window.pageXOffset ? window.pageXOffset :
			document.documentElement.scrollLeft);
		var mouseY = e.clientY + scrollTop;
		var mouseX = e.clientX + scrollLeft;
		if ((mouseY - 1) <= pos[0] || 
			(mouseY + 1) >= (pos[0] + this.wrapper.offsetHeight))
			return true;
		if ((mouseX - 1) <= pos[1] || 
			(mouseX + 1) >= (pos[1] + this.wrapper.offsetWidth))
			return true;
		return false;
	},

	end: function(e) {
		if (document.all && !this.checkPointer(e)) return;
                this.objCaption.style.visibility = 'hidden';
        },

	getPadding: function(obj) {
		if (obj.currentStyle) {
			return [
				parseInt(obj.currentStyle.paddingTop) + 
				parseInt(obj.currentStyle.borderTopWidth),
				parseInt(obj.currentStyle.paddingRight) + 
				parseInt(obj.currentStyle.borderRightWidth),
				parseInt(obj.currentStyle.paddingBottom) + 
				parseInt(obj.currentStyle.borderBottomWidth),
				parseInt(obj.currentStyle.paddingLeft) + 
				parseInt(obj.currentStyle.borderLeftWidth)
			       ];
		} else {
			var s = document.defaultView.getComputedStyle(obj, '');
			return [
				parseInt(s.getPropertyValue('padding-top')) +
				parseInt(s.getPropertyValue(
					'border-top-width')),
				parseInt(s.getPropertyValue('padding-right')) +
				parseInt(s.getPropertyValue(
					'border-right-width')),
				parseInt(s.getPropertyValue('padding-bottom'))+
				parseInt(s.getPropertyValue(
					'border-bottom-width')),
				parseInt(s.getPropertyValue('padding-left')) +
				parseInt(s.getPropertyValue(
					'border-left-width'))
			       ];
		}
	},

	start: function(which) {
		var image = which.childNodes[0];
		this.wrapper = which;
		which.appendChild(this.objCaption);
		//which.setAttribute('title','');
		image.setAttribute('title','');
		//var pos = this.findPos(which, 0, 0);
		var pad = this.getPadding(image);
		var objPad = this.getPadding(this.objCaption);
		var width = which.offsetWidth;
		this.objCaption.style.width = width - pad[1] - pad[3] -
			objPad[1] - objPad[3] + 'px';
		if (document.all) {
			this.objCaption.innerText = image.getAttribute('alt');
		} else {
			this.objCaption.textContent = 
					image.getAttribute('alt');
		}
		this.objCaption.style.left = (pad[3]) + 'px';
		this.objCaption.style.top =
			(which.offsetHeight - 
			pad[2] - this.objCaption.offsetHeight) + 'px';

                this.objCaption.style.visibility = 'visible';
	},

	findPos: function (obj, curtop, curleft) {
		if (obj.offsetParent) {
			return this.findPos(obj.offsetParent,
						curtop + obj.offsetTop,
						curleft + obj.offsetLeft
					   );
		} else {
			return [curtop + obj.offsetTop,
				curleft + obj.offsetLeft];
		}
	}
}

function initImageCaption() { myImageCaption = new ImageCaption(); }
Event.observe(window, 'load', initImageCaption, false);