/* JavaScript file for Scotto's Place
 * by Scott Weberg (except where noted).
 *****************************************************************/
 
/* functions to make the home page images dynamic                  */
/* --------------------------------------------------------------- */
function changeECSimage(imgid)
{	
	if (imgid.alt == "Essential Computer Skills Curriculum")
	{
		imgid.src="images/ECS_LogoShadow.jpg";
		imgid.alt="ECS Curriculum";
	}
	else
	{
		imgid.src="images/ECS_LogoNoShadow.jpg";
		imgid.alt="Essential Computer Skills Curriculum";
	}
}
function changeHRKimage(imgid)
{	
	if (imgid.alt == "Record Keeping Utilities")
	{
		imgid.src="images/HRK_LogoShadow.jpg";
		imgid.alt="Record Keeping";
	}
	else
	{
		imgid.src="images/HRK_LogoNoShadow.jpg";
		imgid.alt="Record Keeping Utilities";
	}
}
function changeFREEimage(imgid)
{	
	if (imgid.alt == "Free Stuff")
	{
		imgid.src="images/freeStuffShadow_sm.jpg";
		imgid.alt="Free Stuff Shadow";
	}
	else
	{
		imgid.src="images/freeStuffNoShadow_sm.jpg";
		imgid.alt="Free Stuff";
	}
}
function changePRODimage(imgid)
{	
	if (imgid.alt == "Our Products Page")
	{
		imgid.src="images/ourProductsShadow.jpg";
		imgid.alt="Our Products";
	}
	else
	{
		imgid.src="images/ourProductsNoShadow.jpg";
		imgid.alt="Our Products Page";
	}
}


/* functions to highlight images on the ECS page  */
/* ---------------------------------------------- */
function changeEOAimage(imgid)
{	
	if (imgid.alt == "Essential Office Applications curriculum")
	{
		imgid.src="images/EOA_Logo_1.jpg";
		imgid.alt="EOA Curriculum";
	}
	else
	{
		imgid.src="images/EOA_Logo_1dim2.jpg";
		imgid.alt="Essential Office Applications curriculum";
	}
}
function changeEWDimage(imgid)
{	
	if (imgid.alt == "Essential Website Development curriculum")
	{
		imgid.src="images/EWD_Logo_1.jpg";
		imgid.alt="EWD Curriculum";
	}
	else
	{
		imgid.src="images/EWD_Logo_1dim2.jpg";
		imgid.alt="Essential Website Development curriculum";
	}
}
function changeECKimage(imgid)
{	
	if (imgid.alt == "Essential Computer Knowledge curriculum")
	{
		imgid.src="images/ECK_Logo_1.jpg";
		imgid.alt="ECK Curriculum";
	}
	else
	{
		imgid.src="images/ECK_Logo_1dim2.jpg";
		imgid.alt="Essential Computer Knowledge curriculum";
	}
}


/* ----------------------------------------------------------------------------------------- */
/* JavaScript code for the slideshow of user comments on the ECS page                        */
/* ----------------------------------------------------------------------------------------- */
// Title: Tigra Fader
// URL: http://www.softcomplex.com/products/tigra_fader/
// Version: 1.0 (commented source)
// Date: 07/23/2007
// Notes: This script is free. Visit official site for further details.

function tFader (a_items, a_tpl) {

	// validate parameters and set defaults
	if (!a_items) return alert("items structure is missing");
	if (typeof(a_items) != 'object') return alert("format of the items structure is incorrect");
	if (a_items[a_items.length - 1] == null) return alert("last element of the items structure is undefined");
	if (!a_tpl) a_tpl = [];
	for (var i = 0; i < A_TSLIDEDEFS.length; i += 2)
		if (a_tpl[A_TSLIDEDEFS[i]] == null)
			a_tpl[A_TSLIDEDEFS[i]] = A_TSLIDEDEFS[i + 1];

	// save config parameters in the slider object
	this.a_tpl   = a_tpl;
	this.a_items = a_tpl.random ? tslide_randomize(a_items) : a_items;

	// initialize parameters, assign methods	
	this.n_currentSlide = 0;
	this.f_goto  = tslide_goto;
	this.f_run   = function () { this.b_running = 1; this.f_goto(); };
	this.f_stop  = function () { this.b_running = 0; clearTimeout(this.o_timerS); };
	this.f_fadeIn  = tslide_fadeIn;
	this.f_fadeOut = tslide_fadeOut;
	this.f_slideOp = tslide_slideOp;

	// register in the global collection	
	if (!window.A_SLIDES)
		window.A_SLIDES = [];
	this.n_id = window.A_SLIDES.length;
	window.A_SLIDES[this.n_id] = this;

	// generate control's HTML
	var s_attributes = ' '
		+ (a_tpl['css']    ? ' class="'  + a_tpl['css']    + '"' : '')
		+ (a_tpl['width']  ? ' width="'  + a_tpl['width']  + '"' : '')
		+ (a_tpl['height'] ? ' height="' + a_tpl['height'] + '"' : '')
		+ (a_tpl['alt']    ? ' title="'  + a_tpl['alt']    + '" alt="' + a_tpl['alt'] + '"' : '');

	this.a_imgRefs = [];
	document.write ('<img src="', this.a_items[0] , '"', s_attributes, ' name="tslide', this.n_id, '_0" />');
	this.a_imgRefs[0] = document.images['tslide' + this.n_id + '_0'];
	this.n_currentSlide = 0;

	// exit on old browsers
	if (!this.a_imgRefs[0] || !this.a_imgRefs[0].style || this.a_imgRefs[0].style.marginLeft == null)
		return;

	for (var i = 1; i < this.a_items.length; i++) {
		document.write('<img src="', this.a_items[i] , '"', s_attributes, ' name="tslide', this.n_id, '_', i, '" style="position:relative;z-index:-1;filter:alpha(opacity=100);" />');
		this.a_imgRefs[i] = document.images['tslide' + this.n_id + '_' + i];
		this.a_imgRefs[i].style.marginLeft = '-' + this.a_tpl.width + 'px';
		this.f_slideOp(i, 0);
		this.a_imgRefs[i].style.zIndex = i;
	}

	// calculate transition variables
	this.n_timeDec = Math.round(this.a_tpl['transtime'] * 1e3 / this.a_tpl['steps']);
	this.n_opacDec = Math.round(100 / this.a_tpl['steps']);

	// run this sucker
	this.f_run();
}

function tslide_goto (n_slide, b_now) {

	// cancel any scheduled transitions	
	if (this.o_timerS) {
		clearTimeout(this.o_timerS);
		this.o_timerS = null;
		if (this.n_nextSlide) {
			this.f_slideOp(this.n_nextSlide, 0);
			this.n_nextSlide = null;
		}
	}

	// determine the next slide
	this.n_nextSlide = (n_slide == null ? this.n_currentSlide + 1 : n_slide) % this.a_items.length;
	if (this.n_nextSlide == this.n_currentSlide) return;
	
	// schedule transition
	this.o_timerS = setTimeout('A_SLIDES[' + this.n_id + '].f_fade' + (this.n_nextSlide > this.n_currentSlide ? 'In' : 'Out') + '()', (b_now ? 0 : this.a_tpl['slidetime'] * 1e3));
}

function tslide_fadeIn (n_opacity) {
	// new transition
	if (n_opacity == null) {
		n_opacity = 0;
	}
	n_opacity += this.n_opacDec;
	// end of transition
	if (n_opacity > 99) {
		this.f_slideOp(this.n_nextSlide, 99);
		this.f_slideOp(this.n_currentSlide, 0);
		this.n_currentSlide = this.n_nextSlide;
		this.n_nextSlide = null;
		return this.f_run();
	}
	// set transparency
	this.f_slideOp(this.n_nextSlide, n_opacity);

	// cycle
	this.o_timerT = setTimeout('A_SLIDES[' + this.n_id + '].f_fadeIn(' + n_opacity + ')', this.n_timeDec);
}
function tslide_fadeOut (n_opacity) {
	// new transition
	if (n_opacity == null) {
		n_opacity = 99;
		this.f_slideOp(this.n_nextSlide, 99);
	}
	n_opacity -= this.n_opacDec;
	// end of transition
	if (n_opacity < 0) {
		this.f_slideOp(this.n_currentSlide, 0);
		this.n_currentSlide = this.n_nextSlide;
		this.n_nextSlide = null;
		return this.f_run();
	}
	// set transparency
	this.f_slideOp(this.n_currentSlide, n_opacity);

	// cycle
	this.o_timerT = setTimeout('A_SLIDES[' + this.n_id + '].f_fadeOut(' + n_opacity + ')', this.n_timeDec);
}

function tslide_slideOp (n_slide, n_opacity) {
	if (!n_slide) return;
	var e_slide = this.a_imgRefs[n_slide];
	tslide_setOpacity(e_slide, n_opacity);
}

function tslide_randomize (a_source) {
	var n_index,
		a_items = [];
	while (a_source.length) {
		n_index = Math.ceil(Math.random() * a_source.length) - 1;
		a_items[a_items.length] = a_source[n_index];
		a_source[n_index] = a_source[a_source.length - 1];
		a_source.length = a_source.length - 1;
	}
	return a_items;
}

// cross-browser opacity
var s_uaApp  = navigator.userAgent.toLowerCase();
if (s_uaApp.indexOf('opera') != -1 || s_uaApp.indexOf('safari') != -1)
	window.tslide_setOpacity = function (e_element, n_opacity) {
		e_element.style.opacity = n_opacity / 100;
	};
else if (s_uaApp.indexOf('gecko') != -1)
	window.tslide_setOpacity = function (e_element, n_opacity) {
		e_element.style.MozOpacity = n_opacity / 100;
	};
else if (s_uaApp.indexOf('msie') != -1)
	window.tslide_setOpacity = function (e_element, n_opacity) {
		try { e_element.filters.alpha.opacity = n_opacity } catch (e) {};
	};
else
	window.tslide_setOpacity = null;

// defaults
var A_TSLIDEDEFS = [
	'steps', 40,
	'css', '',
	'transtime', 0.5,
	'slidetime', 2
];


/* ----------------------------------------------------------------------------------------- */
/* JavaScript code for the breadcrumb trail at the top of the page                           */
/* ----------------------------------------------------------------------------------------- */
/*Path Breadcrumbs javascript
Version 0.1 beta
Created by Ted David
Created 5 September 2009
Copyright 2009, Yed David and UltiMate Music.
Bugs? email ted.david@verizon.net

Minor changes to comments for correctness by Scott Weberg for Scotto's Place use.
Note: The breadcrumb uses the page's <title> element.
*/

var cookieName = 'SessionPath';
var current_page;					// this holds the URL of the current page
var current_label;					// this holds the title of the current page
var seg_URLs = new Array();		// this holds the full set of navigated paths URLs
var seg_labels = new Array();		// this holds the labels to be used in the bread
var breadcrumbID = 'breadcrumbs'; // this is the id of the target element where breadcrumbs will be written for any page
var cookieValue;
var doc;
var tU = '<URL>'
var tUL = '</URL><LABEL>'
var tL = '</LABEL>'

// function getBreadcrumb() is called by <body onload="getBreadcrumb(document, document.location, document.title)" on each page that displays the breadcrumb trail.

function getBreadcrumb(D, U, L) 
{
	if(testSessionCookie() == false) // browser doesn't accept cookies, so no breadcrumbs
	{
		return;
	}
	doc = D;
	current_page = U;
	current_label = L;
	if(!cookieExists(cookieName))  // If no cookie yet, initialize the session cookie
	{
		cookieValue = tU + current_page + tUL + current_label + tL;
		writeSessionCookie(cookieName, cookieValue);
		seg_URLs[0] = current_page;
		seg_labels[0] = current_label;
		writeBreadcrumb(0);
	}
	else   // Modify the existing session cookie
	{
		modCookie();
	}
}
// END getBreadcrumb

function modCookie() // called by getBreadcrumb to modify the cookie which holds the breadcrumb
{
	var numSegs = -1;	// number of path segments in visited_path
	var seg;			// data for one segment
	var segSplit;		// index of the first instance of the tag group splitting the segment
	var visited_path;	// to retain processed cookieValue
	var seg_URL;		// the path value in one segment
	var seg_label;		// the label value in one segment
	var labelIndex;		// index into seg of the string ' LABEL '

	cookieValue = getCookieValue(cookieName);		// retrieve the cookie value
	visited_path = cookieValue;						// copy cookieValue for processing
	var segBegin= visited_path.indexOf(tU);			// index of the first instance of the opening tag of the segment
	var segEnd = visited_path.indexOf(tL);			// index of the first instance of the closing tag of the segment
	while(segBegin!= -1)							// extract all segments, starting with the last one
	{
		seg = visited_path.substring(segBegin+ 5, segEnd);		// split a segment from visited_path
		visited_path = visited_path.substr(segEnd + 8);			// remove same segment from visited_path
		segSplit = seg.indexOf(tUL);							// index of the tag splitting the seg
		seg_URL = seg.substring(0, segSplit);					// extract the path part
		seg_label = seg.substr(segSplit + 13);					// extract the label part
		numSegs++;												// increment the number of segments found
		seg_URLs[numSegs] = seg_URL;							// store path in an array
		seg_labels[numSegs] = seg_label;						// store label in an array
		if(visited_path.length > 0)
		{
			segBegin= visited_path.indexOf(tU);			// update segBeginof the trimmed strring visited_path
			segEnd = visited_path.indexOf(tL);
		}
		else
		{
			segBegin= -1;
		}
	}
	if (numSegs == 0) // cases: (a) linked outside of first page opened in site and back; (b) user selects a new page
	{
		if(current_label == seg_labels[0])  // user is back to first page
		{
			cookieValue = tU + current_page + tUL + current_label + tL; // reinitialize the cookie value;
			writeSessionCookie(cookieName, cookieValue);
			writeBreadcrumb(0);
		}
		else
		{
			cookieValue += tU + current_page + tUL + current_label + tL; // add the current path to cookieValue
			writeSessionCookie(cookieName, cookieValue);
			numSegs++;
			seg_URLs[numSegs] = current_page;
			seg_labels[numSegs] = current_label;
			writeBreadcrumb(numSegs);
		}
	}
	else if(numSegs > 0) // only need to look at the last two to do the testing
	{
		if(current_label == seg_labels[numSegs]) // user must have linked out of the site and is back to the same page - rewrite same array
		{
			writeBreadcrumb(numSegs);
		}
		else
		{
			if(current_label == seg_labels[0])  // user is back to first page
			{
				cookieValue = tU + current_page + tUL + current_label + tL; // reinitialize the cookie value;
				writeSessionCookie(cookieName, cookieValue);
				writeBreadcrumb(0);
			}
			else if(current_label == seg_labels[numSegs-1]) // user has clicked the back button - trim the last segment from the path list
			{
				seg_URLs[numSegs]='';
				seg_labels[numSegs]='';
				numSegs--;
				cookieValue = cookieValue.substring(0,cookieValue.lastIndexOf(tU)); // remove last segment from cookieValue
				writeSessionCookie(cookieName, cookieValue);
				writeBreadcrumb(numSegs);
			}
			else // this is a new segment being visited - append it
			{
				numSegs++;
				seg_URLs[numSegs] = current_page;
				seg_labels[numSegs] = current_label;
				cookieValue += tU + current_page + tUL + current_label + tL; // add the new current_page to cookieValue
				writeSessionCookie(cookieName, cookieValue);
				writeBreadcrumb(numSegs);
			}
		}
	}
//debug
//	var msg = 'Number of Segments = ' + numSegs + '\n';
//	for(var i = 0; i<=numSegs; i++)
//	{
//		msg+= '  Segment' + i+1 + ', URL = ' + seg_URLs[i] + ', Label = ' + seg_labels[i] + '\n';
//	}
//	alert(msg);
}
// END modCookie

function testSessionCookie()
{
  document.cookie ="testSessionCookie=Enabled";
  if (getCookieValue("testSessionCookie")=="Enabled")
    return true 
  else
    return false;
}
// END testSessionCookie

function cookieExists(cName)
{
	var exp = new RegExp(encodeURI(cName) +"=");
	if (exp.test(document.cookie))
	{
		return true;
	}
	else
	{
		return false;
	}
}
function getCookieValue(cName)
{
	var exp = new RegExp(encodeURI(cName) + "=([^;]+)");
  	if (exp.test(document.cookie + ";"))
  	{
    	exp.exec(document.cookie + ";");
    	return decodeURI(RegExp.$1);
  	}
 	else
 	{
 		return false;
 	}
}
// END getCookieValue

function writeSessionCookie(cName, cValue)
{
	if (testSessionCookie())
		{
    document.cookie = encodeURI(cName) + "=" + encodeURI(cValue) + "; path=/";
    return true;
  }
  else return false;
}
// END writeSessionCookie

function writeBreadcrumb(numSegs)
{
	var breadcrumb = '';
	for(var i=0; i<numSegs; i++)
	{
		breadcrumb+= '<a href=\"' + seg_URLs[i] + '\">' + seg_labels[i] + '</a> > '
	}
	breadcrumb+= seg_labels[numSegs];
	doc.getElementById(breadcrumbID).innerHTML = breadcrumb;
}
// END writeBreadcrumb


