if(typeof LITHIUM == "undefined") { var LITHIUM={}; };
if(typeof LITHIUM.Dom == "undefined") { LITHIUM.Dom={}; };

LITHIUM.Dom.CLICK = "click";
LITHIUM.Dom.MOUSEOVER = "mouseOver";

LITHIUM.Dialog = Class.create({
	initialize: function(element, url, windowName, options, className) {		
		this.element = element;
		this.originalUrl = url;
		this.url = url;
		this.windowName = windowName;
		this.className = className || false;
		this.options = options || "width=300,height=400,scrollbars=yes,resizable=yes";		
		if(this.className != false) {
			this.element = $(this.element).down("." + className);
		}		
		$(this.element).addClassName("dialog-link");
		this.beforeOpenMethod = false;
		Event.observe(this.element, 'click', this.openWindow.bind(this));	
	},
	openWindow: function(event) {
		Event.stop(event);		
		if (this.beforeOpenMethod != false) {
			this.beforeOpenMethod(this);
		}
		if (this.url != false) {
			//LIA-8696
			//When a component is re-rendered via Ajax, its id comes back with a '-' rather than a '_' as its delimiter;
			//IE doesn't like an '-' in a window name passed to window.open[!]; hence, if that delimiter is in place, we replace
			//it with the non-ajax-rendered delimiter, and all is well. Considered handling this server-side, but this
			//seems like a client-side quirk, and thus can be addressed here as such.
			var formattedName = this.windowName.gsub(" ", "");
			formattedName = formattedName.replace('-', '_');
			window.open(this.url, formattedName, this.options.toString());
		}		
	},
	setBeforeOpenMethod: function(method) {
		this.beforeOpenMethod = method;
	}
});

LITHIUM.Warning = Class.create({
	initialize: function(error) {
		alert(error);
	}
});

/**
 * This code is used to mimic the core tapestry JS that hides all elements with the CSS class of "t-invisible" then removes
 * the class name. This enables us to then do a Element#show() and Element#hide() as needed through JS and not have the 
 * element intially show up during page load.
 */
document.observe("dom:loaded", function() {$$(".t-invisible").each(
	function(element) {
		element.hide();
		element.removeClassName("t-invisible");
	});
});

/**
 * The methods Element#getOffsetParent and Element#viewportOffset cause a bug in IE7.  Updating these methods
 * per the patch listed here: http://dev.rubyonrails.org/attachment/ticket/11473/getOffsetParent_viewportOffset_fix.diff.
 * TODO: This should be removed once prototype 1.6.0.3 is released.
 */
Element.addMethods({ 
	getOffsetParent: function(element) { 
		element = $(element);   
	 	var op = element.offsetParent, body = document.body, docEl = document.documentElement;   
	 		 
	 	/* IE with strict doctype may try to return documentElement as offsetParent   
	 	   on relatively positioned elements, we will return body instead */   
	 	if (op && op !== docEl) return $(op);   
	 	if (op === docEl || element === docEl || element === body) return $(body);   
	 	      
	 	while ((element = element.parentNode) && element !== body)   
			if (Element.getStyle(element, 'position') != 'static') 
				return $(element); 
		 
	 	return $(body); 
	}, 
	
  viewportOffset: function(forElement) {
    forElement = $(forElement);
    var element = forElement, valueT = 0, valueL = 0
    do {
      valueT += element.offsetTop  || 0;
      valueL += element.offsetLeft || 0;

      // Safari fix
      if (element.offsetParent == document.body &&
        Element.getStyle(element, 'position') == 'absolute') break;

    } while (element = element.offsetParent);

    element = forElement;
    do {
      
        valueT -= element.scrollTop  || 0;
        valueL -= element.scrollLeft || 0;
      
    } while ((element = element.getOffsetParent()) != document.body);

    return Element._returnOffset(valueL, valueT);
  }
});