
/* ----------------------------------------------------------------------------
Copyright (c) 2009 Steve Goeckler. All Rights Reserved.

FileName : ss.js
Author   : Steve Goeckler
Abstract : JavaScript Library for slideshow functionality
History  : Born									10.27.09		!sg

--------------------------------------------------------------------------- */

var gnActiveImageIdx = 0;
var gsActiveImage = "";

var gbSwitching = false;
var gbSS = false;
var gidTimer = null;
var giSSInterval = 5000;

var giSSFrameCounter = 0;
var MAX_FRAMES = -1;		// 3x loop * 7 frames per loop == max slide show run

var gBaseImgPath = "images/dyn/jcover/index.pperl?source="
var gBaseLinkPath = "book.php?isbn="

var garrDvImgs =   new Array ("#dvImg1", "#dvImg2", "#dvImg3", "#dvImg4", "#dvImg5");
var garrSSCtrlIds = new Array ("ssCtrl_1", "ssCtrl_2", "ssCtrl_3", "ssCtrl_4", "ssCtrl_5");
var garrSSCtrlOfs = new Array ("0px", "-19px", "-38px", "-57px", "-76px");

var garrTitles =   new Array ();
var garrAuthors =   new Array ();
var garrImages =   new Array ();
var garrCaptions =   new Array ();
var garrLinks =   new Array ();

	garrTitles[0] = "Harvey K. Littleton: A Life in Glass";
	garrAuthors[0] = "Harvey Littleton and Joan Falconer Byrd";
	garrImages[0] = new Image();
	garrImages[0].src = "http://www.randomhouse.com/images/dyn/jcover/?source=9780847838189&amp;height=348&trans=border:cccccc:1&amp;maxwidth=348";
	garrCaptions[0] = " The son of a Corning Glass Works scientist, Harvey Littleton (born 1922) first studied physics and industrial design, before becoming a teaching ceramicist. In the late...";
	garrLinks[0] = "9780847838189";
	garrTitles[1] = "The Mastery of Time";
	garrAuthors[1] = "Dominique Flechon; Foreword by Franco Cologni";
	garrImages[1] = new Image();
	garrImages[1].src = "http://www.randomhouse.com/images/dyn/jcover/?source=9782080200808&amp;height=348&trans=border:cccccc:1&amp;maxwidth=348";
	garrCaptions[1] = " More than a simple chronology, this volume explores the technical resources used to measure time&mdash;solar, hydraulic, mechanical, or electrical&mdash;just as it explains the key factors behind...";
	garrLinks[1] = "9782080200808";
	garrTitles[2] = "The Garden Source";
	garrAuthors[2] = "Andrea Jones, Foreword by James Van Sweden";
	garrImages[2] = new Image();
	garrImages[2].src = "http://www.randomhouse.com/images/dyn/jcover/?source=9780847837595&amp;height=348&trans=border:cccccc:1&amp;maxwidth=348";
	garrCaptions[2] = " This is a must-have for anyone looking for garden design ideas. Hundreds of full-color images are cleverly arranged to offer thousands of possible design solutions and...";
	garrLinks[2] = "9780847837595";
	garrTitles[3] = "Spectacular Alaska";
	garrAuthors[3] = "Produced and edited by Dana Levy & Letitia Burns O'Connor with essays by";
	garrImages[3] = new Image();
	garrImages[3].src = "http://www.randomhouse.com/images/dyn/jcover/?source=9780789324290&amp;height=348&trans=border:cccccc:1&amp;maxwidth=348";
	garrCaptions[3] = " With its vast diversity of land, people, and wildlife, Alaska is the last true American frontier. For the armchair traveler, those planning and dreaming of a...";
	garrLinks[3] = "9780789324290";
	garrTitles[4] = "French Bistro";
	garrAuthors[4] = "Bertrand Auboyneau; Fran&ccedil;ois Simon; Photographs by Christian Sarramon";
	garrImages[4] = new Image();
	garrImages[4].src = "http://www.randomhouse.com/images/dyn/jcover/?source=9782080200884&amp;height=348&trans=border:cccccc:1&amp;maxwidth=348";
	garrCaptions[4] = " The French bistro provides an irresistible dining experience, combining fresh, traditional dishes with a friendly atmosphere. With its checkered tablecloths, chalkboard menus brandishing the plats du...";
	garrLinks[4] = "9782080200884";
								
	//--------------------------------------------------------------------------------

	$(document).ready(function(){
		ssInit();
	});

	//--------------------------------------------------------------------------------
	// preload covers 
	function ssPreload() {
		for ($i=0;$i<5;$i++) {
			$('body').append('<img src="' + garrImages[$i].src + '" style="display:none;" />');
		}
	}
	//--------------------------------------------------------------------------------
	// switch to the 1st section and then start the slide show
	function ssInit() 
	{
		ssPreload();
		switchSlide("1");
		ssStart(true); 
	}
	
	//--------------------------------------------------------------------------------
	//setup flags, timer and buttons states, then start the slide show
	function ssStart(bDelaySwitch)  {
	
		if (gbSS) return;
		gbSS = true;
	
		updatePlayPause(true);
		$("#dvSSImages").slideDown("slow");
		if (bDelaySwitch==true)
			gidTimer = setTimeout (slideShow, giSSInterval)
		 else {
			slideShow();
		 }
	}

	//--------------------------------------------------------------------------------
	//reset flags, kill timer and buttons states, update state to current section
	function ssStop()  {
	
		if (!gbSS) return;
		
		clearTimeout(gidTimer);
		gbSS = false;
		
		updatePlayPause(false);
		switchSlide(garrBtnIds[gnActiveImageIdx]);
	}

	//--------------------------------------------------------------------------------
	//play pause slideshow functionality
	function playPause()  {
	
		if (gbSS) 
			ssStop();
		else
			ssStart(false);
	}

	//--------------------------------------------------------------------------------
 	//goto next frame set timeout if not 3x loop through slide show
	function slideShow()  {
	
		next();
		if (MAX_FRAMES < 0) { // no frame display limit
			gidTimer = setTimeout (slideShow, giSSInterval)
			return;
		}
		if (++giSSFrameCounter < MAX_FRAMES)
			gidTimer = setTimeout (slideShow, giSSInterval)
		else
			gbSS = false;
	}
	
	//--------------------------------------------------------------------------------
	//map gnActiveImageIdx to a imgId, then call switchSlide()
	function next() {
	
	  var imgId = ""
		switch(gnActiveImageIdx) {
			case 0: imgId = "2"; break;
			case 1: imgId = "3"; break;
			case 2: imgId = "4"; break;
			case 3: imgId = "5"; break;
			case 4: imgId = "1"; break;
		}
		switchSlide(imgId);
	}

	//--------------------------------------------------------------------------------
	//map gnActiveImageIdx to a imgId, then call switchSlide()
	function prev() {
	
	  var imgId = ""
		switch(gnActiveImageIdx) {
			case 0: imgId = "5"; break;
			case 1: imgId = "1"; break;
			case 2: imgId = "2"; break;
			case 3: imgId = "3"; break;
			case 4: imgId = "4"; break;
		}
		switchSlide(imgId);
	}
	
	//--------------------------------------------------------------------------------
	function switchSlide(imgId, bClick)
	{
		if (gbSwitching == true) return;
		gbSwitching = true;

		//if a click called this fn, kill slideshow
		if (bClick==true) {		
			clearTimeout(gidTimer);
			gbSS = false;
			giSSFrameCounter = 0;
			updatePlayPause(false);
		}
		
		var idx = 0;
		switch(imgId) {
			case "1": idx = 0; break;
			case "2": idx = 1; break;
			case "3": idx = 2; break;
			case "4": idx = 3; break;
			case "5": idx = 4; break;
		}
		
		if (gsActiveImage != "")
			$(gsActiveImage).hide();
			
		//setup new image
		gnActiveImageIdx = idx;
		gsActiveImage = garrDvImgs[idx];
	
		var opts = {};
		var sTitleLink, sImageHTML, sMoreLink 
		sTitleLink = '<a href="' + gBaseLinkPath + garrLinks[idx] + '" title="' + garrTitles[idx] + " " + garrAuthors[idx] + '">' + garrTitles[idx] + '</a>'
		sImageHTML = '<a href="' + gBaseLinkPath + garrLinks[idx] + '" title="' + garrTitles[idx] + " " + garrAuthors[idx] + '"><img src="' + garrImages[idx].src + '" alt="' + garrTitles[idx] + " " + garrAuthors[idx] + '" /></a>'
//		sImageHTML = '<a href="' + gBaseLinkPath + garrLinks[idx] + '" title="' + garrTitles[idx] + " " + garrAuthors[idx] + '"><img src="' + gBaseImgPath + garrImages[idx] + '&height=350" alt="' + garrTitles[idx] + " " + garrAuthors[idx] + '" /></a>'
		sCaptions = '<p id="pSSCaption">' + garrCaptions[idx] + '</p>'
		sMoreLink = '<p id="pMoreLink" class="arwMore"><a href="' + gBaseLinkPath + garrLinks[idx] + '" title="' + garrTitles[idx] + " " + garrAuthors[idx] + '">More</a></p>'

		$("#hSSTitle").html(sTitleLink);
		//$("#pSSAuthor").html(garrAuthors[idx]);
		if (gsActiveImage != "")
			$(gsActiveImage).html(sImageHTML);
		//$("#pMoreLink").html(sMoreLink);
			
		if ($.browser.msie)
			$(gsActiveImage).fadeIn(3000);
		else 
			$(gsActiveImage).fadeIn(3000);

		updateCtrlPanel();
		
		//$("#dvSSCaptionFrame").css("height", "120px");
		$("#dvSSCaptionFrame").hide("fast", function() {
											$("#dvSSCaptionFrame").html(sCaptions + sMoreLink);
											$("#dvSSCaptionFrame").fadeIn(3000);
											gbSwitching = false;
										});
 	}

	//--------------------------------------------------------------------------------
	function updateCtrlPanel(){
	
		//update ctrl panel 
		for (i= 0; i< garrSSCtrlOfs.length; i++)
			$("#"+garrSSCtrlIds[i]).css("background-position", garrSSCtrlOfs[i] + " 1px");	
		$("#"+garrSSCtrlIds[gnActiveImageIdx]).css("background-position", garrSSCtrlOfs[gnActiveImageIdx] + " -14px");	
	}
	
	//--------------------------------------------------------------------------------
	function updatePlayPause(bPlay){
	
		if (bPlay) {
			$("#ssCtrl_play").attr("title", "Pause slideshow");
			$("#ssCtrl_play").css("background-position", "-95px 1px");	
		}
		else {
			$("#ssCtrl_play").attr("title", "Resume slideshow");
			$("#ssCtrl_play").css("background-position", "-114px 1px");	
		}
	}
	
	//--------------------------------------------------------------------------------
	var windowOnLoad = function(){
		//ssInit();
	}
	
	if (window.addEventListener)
		window.addEventListener('load', windowOnLoad, false);
	else
		window.attachEvent('onload', windowOnLoad);


