SupportFunctions =
{
	getSize: function () 
	{
		if (window.innerHeight != undefined && window.scrollMaxY  != undefined) {// Firefox
			var yWithScroll = window.innerHeight + window.scrollMaxY;
			var xWithScroll = window.innerHeight + window.scrollMaxX;

		}else if(window.innerHeight  && document.body.scrollHeight > document.body.offsetHeight){
			var yWithScroll = (window.innerHeight >document.body.scrollHeight)?window.innerHeight:document.body.scrollHeight;
			var xWithScroll = (window.innerHeight >document.body.scrollWidth)?window.innerHeight:document.body.scrollWidth;

		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			var yWithScroll = document.body.scrollHeight;
			var xWithScroll = document.body.scrollWidth;

		} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			var yWithScroll = document.body.offsetHeight;
			var xWithScroll = document.body.offsetWidth;
		}

		return [xWithScroll,yWithScroll];
	},

	buildResourceListEntry: function(toShow)
	{
		if (toShow.length > 0)
		{
			res = toShow.shift();
	
			var id = res.getElementsByTagName("entryID")[0].getElementsByTagName("value")[0].childNodes[0].nodeValue;
			var url = path + '/resourcesAJAX/getSingleResourceSniplet:' + id;
	
			new Ajax.Request(url,
			{
				method: 'get',
				onSuccess: function(transport)
				{
					var resp = transport.responseText;
	
					var resDiv = $(document.createElement('div'));
					resDiv.id = 'resource-container-' + id;
					resDiv.addClassName('resource available');
					resDiv.innerHTML = resp;
					resDiv.setStyle('display: none;');
	
					$('resourcees-fieldset').appendChild(resDiv);
					resDiv.appear();

					AdminActions.init();

					SupportFunctions.buildResourceListEntry(toShow);
				}
			});
		}
	},

	showOverlay: function ()
	{
		var body = $(document.getElementsByTagName("body")[0]);

		/* create, add style and attach the underlay */
		var divUnderlay = $( document.createElement('div') );
		divUnderlay.id = 'underlayDiv';
		divUnderlay.addClassName('underlay');
		divUnderlay.setStyle({'height': SupportFunctions.getSize()[1] + 'px'});
		divUnderlay.setOpacity(0.6);

		body.appendChild(divUnderlay);

		/* createm add style and attach the overlay container */
		var divOverlay = $( document.createElement('div') );
		divOverlay.id = 'overlayDiv';
		divOverlay.addClassName('overlay');

		body.appendChild(divOverlay);

		// hide all elements that have a tendency to be shown above the underlay
		SupportFunctions.hideForUnderlay();

		return divOverlay;
	},

	closeOverlay: function ()
	{
		var body = $(document.getElementsByTagName("body")[0]);

		SupportFunctions.showAfterUnderlay();

		body.removeChild( $('underlayDiv') );
		body.removeChild( $('overlayDiv') );
		//body.stopObserving('click');

		overlay = 0;
	},

	AJAXError: function (msg)
	{
		/* show overlay and get container to add data to */
		container = SupportFunctions.showOverlay();

		/* get current measurments to properly position this window */
		var scrollX = document.viewport.getScrollOffsets()[0];
		var scrollY = document.viewport.getScrollOffsets()[1];

		var viewportW = document.viewport.getWidth();
		var viewportH = document.viewport.getHeight();

		/* add content */
		var innerForm = $( document.createElement("div") );
		innerForm.addClassName("errorInfo");

		var label = $( document.createElement("h1") );
		label.innerHTML = "An error has occured.";

		var labelSecondary = $( document.createElement("p") );
		labelSecondary.innerHTML = msg;

		var cancel = $( document.createElement("div") );
		cancel.addClassName("button");
		cancel.innerHTML = "OK";

		cancel.observe
		(
			'click',
			SupportFunctions.closeOverlay
		)

		/* empty div to force IE to expand the innerForm container while accounting for 'cancel' container bottom margin */
		var spacer = $( document.createElement("div") );

		innerForm.appendChild(label);
		innerForm.appendChild(labelSecondary);
		innerForm.appendChild(cancel);
		innerForm.appendChild(spacer);

		container.appendChild(innerForm);

		/* position the container */
		var xPos = scrollX + viewportW/2 - 275;
		var yPos = scrollY + viewportH/2 - container.getHeight()/2;

		container.setStyle("top: " + yPos + "px; left: " + xPos + "px;");
	},

	getLoading: function(container, desiredID, desiredH, desiredW)
	{
		var layer = $(document.createElement('div'));
		layer.addClassName('sF loading');

		if (desiredID != null)
			layer.id = desiredID;
		else
			layer.id = 'loading-icon';

		if (desiredW == null)
			desiredW = 100;

		if (desiredH == null)
			desiredH = 100;

		// get positioning - based on container dimensions
		var topPos = container.getHeight()/2 - desiredH/2;
		var leftPos = container.getWidth()/2 - desiredW/2;

		layer.setStyle('top: ' + topPos + 'px; left:' + leftPos + 'px;');
		layer.setStyle('height: ' + desiredH + 'px; width: ' + desiredW + 'px;');
		layer.setStyle('border: 1px solid #cecece; position: absolute; background-color: #fff;');

		return layer;
	},

	hideForUnderlay: function()
	{
		SupportFunctions.hideObjects();
		SupportFunctions.hideSelects();
	},

	showAfterUnderlay: function()
	{
		SupportFunctions.showObjects();
		SupportFunctions.showSelects();
	},

	hideObjects: function ()
	{
		$$("object").each
		(
			function (object)
			{
				object.setStyle('visibility: hidden;');
			}
		);
	},

	showObjects: function()
	{
		$$("object").each
		(
			function (object)
			{
				object.setStyle('visibility: visible;');
			}
		);
	},

	hideSelects: function()
	{
		$$("select").each
		(
			function (object)
			{
				object.setStyle('visibility: hidden;');
			}
		);
	},

	showSelects: function()
	{
		$$("select").each
		(
			function (object)
			{
				object.setStyle('visibility: visible;');
			}
		);
	}
}
