// frames.js (version 0.1a alpha)
//
// A <frame>-like framework with fading effects. Requires prototype.js and scriptaculous.js effects module.
//
// Copyright (c) 2007 Modern Webspace, Inc. (http://modernwebspace.com, http://bgok.net)
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice, associated urls and this permission notice
// shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// History:
// 0.1a alpha - Orginal version (little testing, but it seems to work in IE6 & 7, FF2, Opera 9.21 & Safari for Windows 3.0.2)
// 0.2a alpha - Added support for framesets
//
// Future features
// ---------------
// - use ajax to source the frame contents
// - accept raw HTML as content
// - clean up the frame registration code. It's a little schizo right now. Is it better to register the frame information
//   with classes or with a function? Or both?
// - Should the frame name be decoupled from the div id?
// - CLASS-ify, OBJECT-ify
// - Add click tracking option
// - Build PHP backend for simplified editing and serving content - A simple CMS!

var activeFrames = new Array;  // The array selector is the name of a frameset
var effectSpeed = 0.2;
var frames = new Array;  // The array selector is the frame name

function prepareFrame(el, frameSet) {
	// Frames need to be reqistered. This is primarily so that the div can be hidden and the frameset can be set.
	// Does it make sense to set the frameset with an HTML class?
	el=$(el);
	el.hide();
	if (typeof(frameSet) == 'undefined') frameSet = 'main';
	frames[el.id] = {element: el, frameSet: frameSet};
}

function showFrame(el) {
	el = frames[$(el).id].element;
	var frameSet = frames[el.id].frameSet;
	
	if  ((typeof(activeFrames[frameSet]) != 'object') || (el.id != activeFrames[frameSet].id)) {
		if (typeof(activeFrames[frameSet]) == 'object') {
			if (Prototype.Browser.WebKit)
				$(activeFrames[frameSet]).hide();
			else
				new Effect.Fade(activeFrames[frameSet], {duration: effectSpeed, queue: {position: 'end', scope: frameSet}});
		}
		activeFrames[frameSet] = el;
		if (Prototype.Browser.WebKit)
			el.show();
		else
			new Effect.Appear(el, {duration: effectSpeed, queue: {position: 'end', scope: frameSet}});
	}
}

function addFrameActivateEventToLinks(frameClass) {
	$A(document.getElementsByClassName(frameClass)).each(function(el) {
		$A(document.getElementsByClassName(el.id+'-link')).each(function(el1) {
			Event.observe(el1, 'click', function(ev) {
				showFrame(el.id);
				Event.stop(ev);
				return false;
			});
		});
	});
}
		

function activateFrames() {
	// Add event to each link to the specials frame
	addFrameActivateEventToLinks('frame');

	var params = (new String(window.location)).toQueryParams();
	if (typeof(params.frame) != 'undefined') {
		if (typeof(params.frame) == 'string') {
			showFrame(params.frame);
		} else {
			params.frame.each(function(frameName) { showFrame(frameName) });
		}
	} else {
		document.getElementsByClassName('default').each(function(frameName) { showFrame(frameName) });
	}
}
