/*
Copyright (c) 2007, Tom Hughes-Croucher. All rights reserved.
Code licensed under the BSD License:
http://kid666.com/js/license.txt
version: 0.4
*/

/*
* Started using draggable YUI panel
*/

YAHOO_config = {

	//the "load" member of YAHOO_config is where we provide
	//information to the YUILoader Utility about what components
	//we want to load, where they exist, and what to do once
	//they're loaded.
	load: {
		require: ['dragdrop', 'container'], //what components?
		base: 'http://yui.yahooapis.com/2.3.0/build/', //where do they live?
		//loadOptional: true, //load all optional dependencies?
		//filter: "DEBUG", 	//use debug versions (or apply some
							//some other filter?

		//onLoadComplete is the function that YUILoader
		//should call when all components are successfully loaded.
		onLoadComplete: function(loader) {
		
			//Once the requested components and dependencies are on
			//the page, you can make use of them here:
			runLoader();
		}
	}
}


//getVersion was introduced in 2.2.0 which is when getListeners became stable so this check is effective 
if(typeof YAHOO!=="undefined" && typeof YAHOO.env!=="undefined" && YAHOO.env.getVersion("event")) {

	hLoaderScript = document.createElement('script');
	hLoaderScript.src = "http://yui.yahooapis.com/2.3.0/build/yuiloader/yuiloader-beta-min.js";
	hLoaderScript.type = "text/javascript";
	
	document.getElementsByTagName('head')[0].appendChild(hLoaderScript);


}
else if (typeof YAHOO!=="undefined" && typeof YAHOO.util.Event!=="undefined"){

	alert("Sorry, this script requires version 2.2.0 or greater of YUI core and event");
	document.getElementsByTagName('head')[0].removeChild(document.getElementById('__yuieventlistscript'));
}
else if (typeof YAHOO!=="undefined"){
	alert("Sorry, you don't appear to be using the YUI Event utility");
	document.getElementsByTagName('head')[0].removeChild(document.getElementById('__yuieventlistscript'));
}
else {
	alert("Sorry, you don't appear to be using the YUI library on this page");
	document.getElementsByTagName('head')[0].removeChild(document.getElementById('__yuieventlistscript'));
}


//once the container is available then do stuff
function runLoader() { 

	var myEvents = Array();
	//for storing original border of highlighted elements
	var origBorder = null;

	//add any listners attached to the document to myEvents
	if (YAHOO.util.Event.getListeners(document) !== null) {
		myEvents.push([YAHOO.util.Event.getListeners(document), document]);
	}

	//get all the elements in the document
	var myEls = document.getElementsByTagName('*');	
	//loops through all the elements
	for (i=0;i<myEls.length;i++) {
		var currEvent = YAHOO.util.Event.getListeners(myEls[i]);

		//if there are events attached to the current element add them to the myEvents
		if (currEvent !== null) {
			myEvents.push([currEvent, myEls[i]]);
		}
	}

	if (typeof console.log!=="undefined") {
		console.log(myEvents);
	}

	var hEventList = document.createElement('ul');
	var hEventItem;

	for (var i=0; i<myEvents.length; i++) {		

		hEventItem = document.createElement('li');

		if (myEvents[i][0].length > 1) {

			var hSubEventList = document.createElement('ul');
			for (var j=0; j<myEvents[i][0].length;j++) {
				hFunction = createEventNode(myEvents[i][0][j], myEvents[i][1]);
				hSubEventItem = document.createElement('li');
				hSubEventItem.appendChild(hFunction);
				hSubEventList.appendChild(hSubEventItem);
			}

			hEventItem.appendChild(hSubEventList);

		} 
		else {

			hFunction = createEventNode(myEvents[i][0][0], myEvents[i][1]);
			hEventItem.appendChild(hFunction);

		}
		hEventList.appendChild(hEventItem);

	}

	hEventList.style.height = "350px";
	hEventList.style.overflow = "auto";

	hTagline = document.createElement("a");
	hTagline.href = "http://kid666.com/blog/yui-event-list-bookmarklet/";
	hTagline.style.width = "100%";
	hTagline.style.display = "block";
	hTagline.style.paddingTop = "10px";
	hTagline.style.textAlign = "right";
	hTagline.appendChild(document.createTextNode("YUI Event List v0.4"));

	hContentWrapper = document.createElement("div");
	hContentWrapper.appendChild(hEventList);
	hContentWrapper.appendChild(hTagline);


	eventsPanel = new YAHOO.widget.Panel("yuiEventList", { width:"500px", visible:true, constraintoviewport:true, close:true});
	eventsPanel.setHeader("YUI Events");
	eventsPanel.setBody(hContentWrapper);
	
	panelWrapper = document.createElement('div');
	panelWrapper.setAttribute('class', 'yui-skin-sam');
	eventsPanel.render(panelWrapper);

	var leftDistance = getViewportWidth() /2 - 250;
	
	var docBody = document.getElementsByTagName("body")[0];
	docBody.insertBefore(panelWrapper, docBody.firstChild);
	
	document.getElementById("yuiEventList_c").style.top = "50px";
	document.getElementById("yuiEventList_c").style.left = leftDistance+"px";
}


// create an entry for the list of events displayed to the user
function createEventNode(eventObj, source) {
	var hLink = document.createElement('a');
	hLink.href = "javascript:alert(" + eventObj.fn + ");";

	//if this is an element node
	if(typeof source.nodeType !=="undefined" && source.nodeType === 1) {

		var hLinkText = source.tagName;

		if (source.id) {
			hLinkText += "#" + source.id;
		}

		if (source.class) {
			sClasses = source.class.split(" ");
			for (var i=0; i<sClasses.length();i++) {
				hLinkText += "." + sClasses[i];
			}
		}
		if (source.style.border != "") {
			origBorder = source.style.border;
		}
		else {
			origBorder = null;
		}

		//YAHOO.util.Event.addListener(hLink, "mouseover", );
		//YAHOO.util.Event.addListener(hLink, "mouseout", );

	}
	//if this is the document node
	else if (typeof source.nodeType !=="undefined" && source.	nodeType === 9) {
		hLinkText = "document";
	}
	//otherwise try to use the type as the name
	else {
		hLinkText = typeof eventObj;
	}

	hLinkText += " (" + eventObj.type + ")";

	hLink.appendChild(document.createTextNode(hLinkText));
	return hLink;
}

//Taken from YUI DOM component 2.3.0
// http://developer.yahoo.com/yui/dom
function getViewportWidth() {
	var width = self.innerWidth;  // Safari
	var mode = document.compatMode;

	if (mode || isIE) { // IE, Gecko, Opera
		width = (mode == 'CSS1Compat') ?
		document.documentElement.clientWidth : // Standards
		document.body.clientWidth; // Quirks
	}
	return width;
}