/* Copyright 2008-2009, Jon Maken
 * Revision: 3:46 PM 2/12/2009
 *
 * TODO compress with http://developer.yahoo.com/yui/compressor/ for production
 */

// Global variables
var selectedBtn = null;
var capsPanel = null;
var preloadFlag = false;

/* Return the target of an event; works with W3C or IE events
 */
function getTarget(e)
{
	e = e || window.event;
    return e.target || e.srcElement;
}

function winPop(url, w, h, pad)
{
	var fopen = window.open;
	fopen(url, "_blank", 
		"directories=no,location=no,menubar=no,status=no,toolbar=no,resizable=no,width=" +
		(w + pad) + 
		",height=" +
		(h + pad) + "\"");
}

/* Takes a variable number of string pathname arguments preloads the images
 */
function preloadImages()
{
	if (arguments)
	{
		for (var i = 0; i < arguments.length; i++)
		{
			ni = new Image();
			ni.src = arguments[i];
		}
		preloadFlag = true;
	}
}

function setTransparentBkgndImage(el, src, x, y, repeat)
{
	var style = el.style;
	var ua = window.navigator.userAgent;
	if ('filter' in style && ua.indexOf('MSIE 7') == -1)
	{
		style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +
						src + '", sizingMethod="crop")';
	}
/* TODO cleanup after testing with IE 6	
	else if (ua.indexOf('WebKit') != -1)
	{
		alert(ua.indexOf('WebKit'));
		style.setProperty('background-image', "'url(' + src + ')'");
		style.setProperty('background-position', '' + x + ' ' + y);
		style.setProperty('background-repeat', repeat);
	}
	else
	{
		style.backgroundImage = 'url(' + src + ')';
		style.backgroundPosition = '' + x + ' ' + y;
		style.backgroundRepeat = repeat;
	}
*/
}
	
/* Takes a path string similar to "http://some/path/to/myfile.gif" and returns
 * an object that has properties corresponding to the parts of the path string
 */
function getPathParts(path) {
	var lastSlash = path.lastIndexOf("/");
	var file = path.substring(lastSlash + 1);
	var filePath = path.substring(0, lastSlash + 1);
		
	// assumes filename has "basename_other.ext" format
	var lastUnder = file.lastIndexOf("_");
	var baseFile = file.substring(0, lastUnder);
	var ext = file.substring(file.lastIndexOf("."));
		
	return {
		"path" : filePath, 
		"file" : file,
		"baseFile" : baseFile,
		"ext" : ext };
}

/* Swaps an image element's current source; exits early if the element is already
 * "on" to stop unnecessary image swaps
 */
function swapImg(el, action) {
	if (el.src.search("_on") != -1) return;
	el.src = el[action];
}
	
// Add image metadata to the element to ease image rollover and click effects
function addImgMeta(el) {
	var p = getPathParts(el.src);
	el.off = p.path + p.baseFile + "_off" + p.ext;
	el.on = p.path + p.baseFile + "_on" + p.ext;
	el.over = p.path + p.baseFile + "_over" + p.ext;
}

/* Swaps the existing content of an element with new content with cusomizable
 * fade out and fade in speeds given in millisecods
 */
function swapContent(el, newContent, outSpeed, inSpeed) {
	function callback() {
		el.html(newContent);
		el.fadeIn(inSpeed);
	}
	
	el.fadeOut(outSpeed, callback);
	// TODO cleanup after testing with IE 6
	//el.hide();
	//el.html(newContent);
	//el.fadeIn(inSpeed);
}
	
function swapSummary(el) {
	if (el.src.search("_on") != -1) return;
		
	var p = getPathParts(el.src);
		
	// toggle selected buttons, saving reference to new selected button
	selectedBtn.src = selectedBtn.off;
	el.src = el.on;
	selectedBtn = el;
		
	// swap content summary
	swapContent(capsPanel, Summary[p.baseFile], 200, 900);
}
