//
// zykloid_effects.js - (C) 2007 Zykloid Software.
//
// Requires: Effects.js
//


//	T A B S

var gCurrentTab = "overview"; // The default loaded tab on the page
var sTabTag = "-tab";
var sContentTag = "-tab-content";


// Select the specified tab
function selectTab (tab, target) {

	//	collect statistics...
	_uacct = "UA-548967-1";
	urchinTracker (target);

	if (gCurrentTab == tab) return;
	lastSection = gCurrentTab;
	gCurrentTab = tab;

	sectionTab = gCurrentTab + sTabTag;
	document.getElementById(sectionTab).className = "active";
	if (lastSection) {
		lastTab = lastSection + sTabTag;
		document.getElementById(lastTab).className = "inactive";
	}

	sectionContent = gCurrentTab + sContentTag;
	new Effect.Appear(sectionContent);
	if (lastSection) {
		lastContent = lastSection + sContentTag;
		new Effect.Fade(lastContent);
	}
}


function scrollLeft (content_id, container_id) {
	var content = document.getElementById(content_id);
	var x_pos = parseInt (content.style.left);
	x_pos = (isNaN (x_pos)) ? 0 : x_pos;
	var container_width = $(container_id).getDimensions().width;
	var move_width = ((x_pos + container_width) <= 0) ? container_width : Math.abs(x_pos);
	new Effect.Move (content_id, { x:move_width, y:0 })
	// console.log ("x-pos:" + x_pos + ", width: " + move_width);
}


function scrollRight (content_id, container_id) {
	var content = document.getElementById(content_id);
	var x_pos = parseInt (content.style.left);
	x_pos = (isNaN (x_pos)) ? 0 : x_pos;
	var content_width = $(content_id).getDimensions().width;
	var container_width = $(container_id).getDimensions().width;
	var min_x = -(content_width - container_width);
	var move_width = ((x_pos - container_width) < min_x) ? Math.abs(min_x - x_pos) : container_width;
	new Effect.Move (content_id, { x:-move_width, y:0 })
	//console.log ("x-pos:" + x_pos + ", width: " + move_width);
}


//	R O L L O V E R S

/**
 * Add a rollover effect to the specified image, by adding event
 * handlers to switch the image to the specified URL while the
 * mouse is over the image.
 *
 * If the image is specified as a string, search for an image with that
 * string as its id or name attribute.
 * 
 * This method sets the onmouseover and onmouseout event handler properties
 * of the specified image, overwriting and discarding any handlers previously
 * set on those properties.
 */
function addRollover (img, rolloverURL) {
    if (typeof img == "string") {  // If img is a string,
        var id = img;              // it is an id, not an image
        img = null;                // and we don't have an image yet.

        // First try looking the image up by id
        if (document.getElementById) img = document.getElementById(id);
        else if (document.all) img = document.all[id];

        // If not found by id, try looking the image up by name.
        if (!img) img = document.images[id];
        
        // If we couldn't find the image, do nothing and fail silently
        if (!img) return;
    }

    // If we found an element but it is not an <img> tag, we also fail
    if (img.tagName.toLowerCase() != "img") return;

    // Remember the original URL of the image
    var baseURL = img.src;

    // Preload the rollover image into the browser's cache
    (new Image()).src = rolloverURL;

    img.onmouseover = function() { img.src = rolloverURL; }
    img.onmouseout = function() { img.src = baseURL; }
}


/**
 * Find all <img> tags in the document that have a "ze:src" attribute on them. 
 * Use the value of this attribute as the URL of an image to be displayed when the 
 * mouse passes over the image, and set appropriate event handlers to create the 
 * rollover effect. 
 * The ze: namespace prefix should be mapped to the URI "http://zykloid.com/jsns/zykloid_effects/1.0/" 
 */
function initRollovers () {
	var images = document.getElementsByTagName("img");
	for (var i = 0; i < images.length; i++) {
		var image = images[i];
		//var rolloverURL = image.getAttributeNS (initRollovers.xmlns, "rollover");
		var rolloverURL = image.getAttribute("ze:rollover");
		if (rolloverURL) {
			addRollover(image, rolloverURL);
		}
	}
}

// This is a made-up namespace URI for our "ze:" 'zykloid effects' namespace.
initRollovers.xmlns = "http://zykloid.com/jsns/zykloid_effects/1.0/";


//	 G E N E R A L
	
function initZykloidEffects () {
	initRollovers ();
}


//console.log("Zykloid Effects Initialized");
