/* *******
Attach this event to a link, and clicking on that link will load the 
contents of the linked page into a hovering, draggable, closable window. 
******** */
function displayAddHoverWindow(ClickEvent)
	{
	// Create a mootools wrapper for the event
	ClickEvent = new Event(ClickEvent); 
	ClickEvent.stop(); 
	var clickedLink = ClickEvent.target;

	if($(clickedLink.hoverWindowDiv))
		{
		$(clickedLink.hoverWindowDiv).remove(); 
		return;
		}

	// Get the point clicked
	var x = ClickEvent.page.x;	
	var y = ClickEvent.page.y;	
	
	// Create a new window with a random ID
	var windowID = $random(1000000,9999999);
	var hoverWindow = new Element("div", 
		{
		'styles': 
			{
			'top': y,
			'left': x
			},
		'class': 'hoverwindow',
		'id': 'HoverWindow' + windowID
		});
	
	// Add the window to the page
	hoverWindow.injectAfter(clickedLink); 

	// Set up the title bar
	var hoverTitle = new Element("div",
		{
		'id': 'HoverWindowDragger' + windowID,
		'class': 'dragbar'
		}).injectInside(hoverWindow);
	hoverTitle.innerHTML = "Drag here"; 
	
	// Create the Close box
	var closeHoverWindowTitle = new Element('div',
		{
		'id': 'HoverWindowCloser' + windowID, 
		'class': 'box'
		}).injectInside(hoverTitle);
	closeHoverWindowTitle.innerHTML = "<img src=\"/img/close.gif\">"; 
	
	closeHoverWindowTitle.addEvent("click", function()
		{
		hoverWindow.remove(); 
		}); 

	// Create the box for the window content (the Reply or Edit box,for example)
	new Element("div",
		{
		'id': 'HoverWindowContent' + windowID,
		'class': 'content'
		}).injectInside(hoverWindow);

	// Make the title bar a draggable div that will move the window
	new Drag.Move('HoverWindow' + windowID, {handle: 'HoverWindowDragger' + windowID}); 
	//hoverTitle.makeDraggable({'el':'HoverWindow' + windowID}); 
	
	// add some information so that the Close box will know which window to close
	clickedLink.hoverWindowDiv = hoverWindow.id; 

	// Get the content for the hover window
	var getHoverWindowWindow = new Ajax(clickedLink.href,  {method: 'get',evalScripts: true, update:'HoverWindowContent'+windowID}).request(); 
	}

function initialize()
	{
	document.getElementsBySelector(".replylink").each(function(link) {
		link.addEvent('click', displayAddHoverWindow); 
		}); 
	document.getElementsBySelector(".editlink").each(function(link) {
		link.addEvent('click', displayAddHoverWindow); 
		}); 

	}

(function () {
	window.addEvent('domready', initialize); 
	})();

/* ***************************************
Fx.Morph from Mootools Demos
***************************************** */

Fx.Morph = Fx.Styles.extend({
 
	start: function(className){
 
		var to = {};
 
		$each(document.styleSheets, function(style){
			var rules = style.rules || style.cssRules;
			$each(rules, function(rule){
				if (!rule.selectorText.test('\.' + className + '$')) return;
				Fx.CSS.Styles.each(function(style){
					if (!rule.style || !rule.style[style]) return;
					var ruleStyle = rule.style[style];
					to[style] = (style.test(/color/i) && ruleStyle.test(/^rgb/)) ? ruleStyle.rgbToHex() : ruleStyle;
				});
			});
		});
		return this.parent(to);
	}
 
});
 
Fx.CSS.Styles = ["backgroundColor", "backgroundPosition", "color", "width", "height", "left", "top", "bottom", "right", "fontSize", "letterSpacing", "lineHeight", "textIndent", "opacity"];
 
Fx.CSS.Styles.extend(Element.Styles.padding);
Fx.CSS.Styles.extend(Element.Styles.margin);
 
Element.Styles.border.each(function(border){
	['Width', 'Color'].each(function(property){
		Fx.CSS.Styles.push(border + property);
	});
});

		
/*** ****
A convenience function, used in the LoginAccordion (inline script in common.php)
to adjust the appearance of the accordion toggler bars
**** ***/
function toggleToggler(toggler, toClass)
	{
	var Morpher = toggler.FxMorpher; 
	if(!Morpher)
		Morpher = toggler.FxMorpher = new Fx.Morph(toggler, {wait: false}); 
	Morpher.start(toClass); 
	}

