if(typeof Prototype == 'undefined' || !Prototype.Version.match("1.6"))
	throw("Core popup library requires Prototype library >= 1.6.0");

var gHelpPopupWidth = 605;
var gHelpPopupHeight = 650;

var HelpPopup = Class.create({
	initialize: function(clickableElementId, windowContentUrl) {
		if($(clickableElementId)===null) return;
		this.windowContentUrl = windowContentUrl;
		this.clickableElementId = clickableElementId;
		var self = this;
		$(clickableElementId).observe("click", function(event) {
			// Fire an event to notify we're about to launch the popup window. This gives the parent a chance to modify the
			// url if client-side state has changed.
			$(clickableElementId).fire("helpPopup:popup",  { sender: self });
			
			var leftPosition = (screen.width - gHelpPopupWidth) / 2;
			var topPosition = (screen.height - gHelpPopupHeight) / 2;
			
			newWindow = window.open(self.windowContentUrl, "", "location=0, status=0, resizable=1, scrollbars=1, width="+ gHelpPopupWidth + ", height=" + gHelpPopupHeight + ", top=" + topPosition + ", left=" + leftPosition);
			newWindow.window.focus();
			
			event.stop();
		});
	}
});

var HelpPopups = Class.create({
	initialize: function() {
		$$("a.help_popup").each(function(clickableElement) {
			new HelpPopup(clickableElement.identify(), clickableElement.href);					
		});
	}
});


var gGenericWindowPopupWidth = 650;
var gGenericWindowPopupHeight = 600;

var GenericWindowPopup = Class.create({
	initialize: function(clickableElementId, windowContentUrl, enableBrowserFeatures) {
		if($(clickableElementId)===null) return;
		this.windowContentUrl = windowContentUrl;
		this.clickableElementId = clickableElementId;
		this.enableBrowserFeatures = false;
		if(enableBrowserFeatures != null)
			this.enableBrowserFeatures = enableBrowserFeatures;
		
		var self = this;
		$(clickableElementId).observe("click", function(event) {
			// Fire an event to notify we're about to launch the popup window. This gives the parent a chance to modify the
			// url if client-side state has changed.
			$(clickableElementId).fire("genericWindowPopup:popup", { sender: self });
			var newWindow = null;
			
			if(self.enableBrowserFeatures)
				newWindow = window.open(self.windowContentUrl, "", "width=" + gGenericWindowPopupWidth + ", height=" + gGenericWindowPopupHeight + ",status=1,resizable=1,scrollbars=1,toolbar=1,location=1,menubar=1,directories=1");
			else
				newWindow = window.open(self.windowContentUrl, "", "width=" + gGenericWindowPopupWidth + ", height=" + gGenericWindowPopupHeight + ",status=1,resizable=0,scrollbars=1,toolbar=0,location=0,menubar=0,directories=0");
			newWindow.window.focus();
			
			event.stop();
		});
	}
});

var gInlinePopupWidth = 585;
var gInlinePopupHeight = 310;

var InlinePopup = Class.create({
	initialize: function(clickableElementId, windowContentElementId) {
		if($(clickableElementId)===null) return;
		if(typeof Lightbox == 'undefined')
			throw("InlinePopup requires Lightbox library");
		
		Event.observe(window, 'load', function(event) {
			Lightbox.init();
		});
    		
		this.windowContentElementId = windowContentElementId;
		var self = this;
		$(clickableElementId).observe("click", function(event) {
			// Fire an event to notify we're about to launch the popup window. This gives the parent a chance to modify the
			// url if client-side state has changed.
			$(clickableElementId).fire("helpPopup:popup", { sender: self });
			
			Lightbox.showBoxByID(self.windowContentElementId, gInlinePopupWidth, gInlinePopupHeight);
			
			event.stop();
		});
	}
});

/*
 * Standard window load events
 */
Event.observe(window, 'load', function(event) {
	// Create help popups
	new HelpPopups();
});

