// Pullquotes plugin by Avi Alkalay
// License LGPL
//
// http://avi.alkalay.net/2006/11/google-maps-plugin-for-wordpress.html
//
// $Id: pullquotes.js 2 2007-11-07 16:38:33Z aviram $
//



/**
 * Class to store all single pullquote related parameters: text, HTML element to
 * hook to, style etc.
 */
function PullquoteData(elem, defaultW, defaultH) {
	// DOM related stuff
	this.elem=elem;
	this.inheritedStyle=null;
	this.className=null;
	this.inheritedID=null;
	this.defaultW=defaultW;
	this.defaultH=defaultH;
	this.quote=null;
	this.float="right";

//	this.commandsAttributeName=commandsAttributeName;

	this.debug=0;
}


/**
 * Merges another pullquote into the caller's.
 */
PullquoteData.prototype.merge = function(quote) {
	this.quote+=quote.quote;
}


/**
 * This is the method that effectively creates the Pullquote based
 * on the PullquoteData object.
 */
PullquoteData.prototype.createPullquote = function() {
	var realMap;
	var quoteNode;

	quoteNode = document.createElement("div");

	if (this.className) quoteNode.className = "pullquote " + this.className;
	else quoteNode.className = "pullquote";

	if (this.inheritedStyle) quoteNode.style.cssText = this.inheritedStyle;
	if (this.float!="right") quoteNode.style.cssText += "; float: " + this.float + ";"


//	quoteNode.style.display = "block";
	if (quoteNode.style.visibility == "hidden")
		 quoteNode.style.visibility="inherit";


	if (this.defaultW) {
		if (this.defaultW.toString().indexOf("%")!=-1)
			quoteNode.style.width = this.defaultW;
		else quoteNode.style.width = this.defaultW + "px";
	}

	if (this.defaultH) {
		if (this.defaultH.toString().indexOf("%")!=-1)
			quoteNode.style.height = this.defaultH;
		else quoteNode.style.height = this.defaultH + "px";
	}

	if (this.inheritedID) quoteNode.id = this.inheritedID;
	else quoteNode.id = "pullquote-" + Math.ceil(10000*Math.random());

	quoteNode.innerHTML=this.quote;

	this.elem.parentNode.parentNode.insertBefore(quoteNode,this.elem.parentNode);

//	alert("Finished map with id=" + this.inheritedID);
}



/**
 * Parse stuff from title="" or rel="" attributes on XHTML elements.
 */
PullquoteData.prototype.parseCommands = function(commands) {
	if (commands == undefined) return 0;

	if (commands.indexOf("pullquote") == -1) return 0;

//	alert("This is a googlemap");

	var params=commands.split(";");
	var i;

	for (i=0; i<params.length; i++) {
//		alert("Parsing \"" + params[i] + "\"");
		if (params[i].indexOf("left")!=-1) {
			this.float="left";
		} else if (params[i].indexOf("w:")!=-1) {
			var val = params[i].split(':');
			this.defaultW=val[1];
		} else if (params[i].indexOf("h:")!=-1) {
			var val = params[i].split(':');
			this.defaultH=val[1];
		}
	}
	return 1;
}




/**
 * Parse an entire <span title="pullquote"> element.
 */
PullquoteData.prototype.parseNode = function () {
	// alert("parseNode: <" + this.elem.nodeName + " title='" + this.elem.getAttribute("title") + "'>");
	var name=this.elem.nodeName.toLowerCase();

	if (name=="span" || name=="a") {
		if (this.parseCommands(this.elem.getAttribute("title"))) {
//			alert("Found a <dl id='" + this.elem.id + "'>");
			if (this.elem.style.cssText) this.inheritedStyle=this.elem.style.cssText;
			if (this.elem.className) this.className=this.elem.className;
			if (this.elem.id) this.inheritedID=this.elem.id;
			this.quote=this.elem.innerHTML;
			return true;
		} else return false;
	}
	return false;
}


PullquotePlugin.prototype.consumeContainers = function() {
	var quoteContainers=["span","a"];

	var attributeForCommands="title";

	for (var i1=0; i1<quoteContainers.length; i1++) {
		var elems=document.getElementsByTagName(quoteContainers[i1]);
		var previousQuote=null;
		var cur=null;
		var i2=0;

//		alert("Found " + elems.length + " " + quoteContainers[i1] + " elements."); 

		while ((cur=elems.item(i2))) {
			var cmd=null;
			var quote = new PullquoteData(cur);

			if (quote.parseNode()) {
				if (previousQuote && quote.elem.parentNode==previousQuote.elem.parentNode) {
					previousQuote.merge(quote);
					delete quote;
				} else {
					this.pullquotes.unshift(quote);
					previousQuote=quote;
				}
			} else delete quote;   // release browser memory

// //			dump("Processing element " + cur.nodeName.toLowerCase() + ".\n");
// 			if (cur.nodeName.toLowerCase() == "a")
// 				cmd=cur.getAttribute(attributeForCommands);
// 			else cmd=cur.getAttribute("title");
// 
// 			if (cmd && cmd.indexOf("googlemap")!=-1) {
// 				var map = new MapData(cur,attributeForCommands,this.defaultW,this.defaultH);
// 
// 				if (map.parseNode()) this.maps.unshift(map);
// 				else {
// 					// Not a Google Map
// 					delete map;
// 				}
// 			}

			i2++;
		}

//		alert("Finished processing " + mapContainers[i1] + " elements.");
	}
}




/**
 * After parsing and creating all PullquoteData objects, efficiently create
 * the found pullquotes.
 */
PullquotePlugin.prototype.createPullquotes = function() {
	var quote;

	while ((quote=this.pullquotes.pop())) {
		quote.createPullquote();
		delete quote;     // don't need object in memory anymore
	}
}



function PullquotePlugin() {
	this.pullquotes=new Array;

	this.consumeContainers();
	this.createPullquotes();
}












function PullquotePluginInit() {
	var oldOnLoad = window.onload;
	var oldOnUnload = window.onunload;

	var instantiate = function() {
		new PullquotePlugin();
	}

	if (typeof window.onload != 'function')
		window.onload = instantiate;
	else window.onload = function() {
		oldOnLoad();
		instantiate();
	}
/*
	if (typeof window.onunload != 'function')
			window.onunload = GUnload();
	else window.onunload = function() {
		oldOnUnload();
		GUnload()
	}
*/
}
