/***********************************************************************/ /*spinner.js */ /*a handy utility for offering a rotating library of items, articles */ /*version 1: not entirely robust yet */ /*Authored in its entirety by Sam Kent: sam@htmelegance.com */ /*Free for public use on the condition that this header remains intact */ /*business use requires author's approval */ /***********************************************************************/ function thingy() { } var thing1 = new thingy(); thing1.itemHeight = 60; thing1.showItems = 1; thing1.myContainer = "greaterContainer"; var thing2 = new thingy(); thing2.itemHeight = 60; thing2.showItems = 1; thing2.myContainer = "greaterContainer2"; //this is how wide your items are and needs to correspond to the width in the .item style class //object for which way to adjust when scrolling scrollDirections = {"up":1,"down":-1}; thing1.scrollMax = {"up":-thing1.itemHeight,"down":-1}; thing2.scrollMax = {"up":-thing2.itemHeight,"down":-1}; scrollNext = {"up":-1,"down":1}; //for an interval var scrollInterval; //resets the scroll interval when a user clicks a link function resetScrollInterval() { window.clearInterval(scrollInterval); } //seconds between scrolling. Should be an integer, and positive //set to 0 if you do not want to scroll var secondsBetweenInterval = 4; //sets the interval for auto-scrolling function setScrollInterval() { scrollInterval = window.setInterval("shiftItemListUp();",secondsBetweenInterval*1000); } //initializes the scroller by counting the items //and adjusting heights of container elements function initScroller() { //alert("init"); //how tall are all items together? thing1.itemCount = document.getElementById(thing1.myContainer).childNodes.length; thing2.itemCount = document.getElementById(thing2.myContainer).childNodes.length; thing1.allItemHeight = thing1.itemCount * thing1.itemHeight; thing2.allItemHeight = thing2.itemCount * thing2.itemHeight; //alert(itemHeight); document.getElementById(thing1.myContainer).style.height = thing1.allItemHeight + "px"; document.getElementById(thing2.myContainer).style.height = thing2.allItemHeight + "px"; document.getElementById(thing1.myContainer).style.height = (thing1.itemHeight * thing1.showItems) + "px"; document.getElementById(thing2.myContainer).style.height = (thing2.itemHeight * thing2.showItems) + "px"; //safari doesn't scroll if(navigator.appVersion.indexOf("Safari") >= 0) { document.getElementById(thing1.myContainer).style.height = ((thing1.itemHeight * thing1.showItems) + thing1.itemHeight) + "px" document.getElementById(thing2.myContainer).style.height = ((thing2.itemHeight * thing2.showItems) + thing2.itemHeight) + "px" } //handle for not having enough items /*if(thingToShift.itemCount <= showItems) { shiftItemList = noAction; document.getElementById("shiftLinkDown").style.display = "none"; document.getElementById("shiftLinkUp").style.display = "none"; }*/ //and if you want, rotate the items periodically setScrollInterval(); } function noAction() { } var shifting = new Boolean(false); var move; var thingToShift = thing1; //function for shifting greaterContainer up and down function shift(direction, moveTo) { shifting = new Boolean(true); //capture first time here if(!moveTo) { //first time here if(direction == "up") { move = 0; } else { move = thingToShift.itemHeight * scrollDirections[direction]; } } //change the position document.getElementById(thingToShift.myContainer).style.marginBottom = move + "px"; //speed this up for FF on Mac var moveMultiplier = (navigator.appVersion.indexOf("Macintosh") >= 0) ? 3 : 1; //figure out how much to move to next time if(direction == "up") { if(move < -(thingToShift.itemHeight*0.8)) { move -= .5; } else if (move < -(thingToShift.itemHeight*0.6)) { move -= 1 * moveMultiplier; } else { move -= 2 * moveMultiplier; } } else { if(move < -(thingToShift.itemHeight*0.2)) { move += 3 * moveMultiplier; } else if (move < -(thingToShift.itemHeight*0.4)) { move += 2 * moveMultiplier; } else { move += 1; } } document.getElementById(thingToShift.myContainer).style.marginTop = move + "px"; //alert(move); if(move != thingToShift.scrollMax[direction]) { setTimeout("shift('" + direction + "'," + move + ");",1); } else { shifting = new Boolean(false); killElement(direction); thingToShift = (thingToShift == thing1) ? thing2 : thing1; } } function safariShift(direction) { //alert("this is SafariShift"); killElement(direction); } //change shift method for Safari if (navigator.appVersion.indexOf("Safari") >= 0) { shift = safariShift; } //function to kill the added element function killElement(direction) { //rename temp item's id with killed item's id //document.getElementById("tempID").id = exchangeID; //kill first item document.getElementById(thingToShift.myContainer).removeChild(thingToShift.itemDivs.item(0)); //reset the margin to account for killed item document.getElementById(thingToShift.myContainer).style.marginTop = "0px"; } //this will store the items in the rotater as pointer elements var itemDivs; //holder for the id parameter of the element to later be destroyed var exchangeID; //method for shifting the scroller one direction or another function shiftItemList(direction) { //not more than one shift at a time if(shifting == false) { //have to specify a direction if(!direction) { return; } //direction has to be up or down if(direction != "down" && direction != "up") { //alert("foobar"); return; } thing1.itemDivs = document.getElementById(thing1.myContainer).getElementsByTagName("div"); thing2.itemDivs = document.getElementById(thing2.myContainer).getElementsByTagName("div"); var newDiv = document.createElement("div"); newDiv.setAttribute("id","tempID"); newDiv.setAttribute("class","item"); newDiv.setAttribute("className","item"); //for IE newDiv.innerHTML = (direction == "up") ? thingToShift.itemDivs[0].innerHTML : thingToShift.itemDivs[thingToShift.itemDivs.length-1].innerHTML; exchangeID = thingToShift.itemDivs.item(0).id; //insert at end document.getElementById(thingToShift.myContainer).insertBefore(newDiv,thingToShift.itemDivs[thingToShift.itemDivs.length-1].nextSibling); //shift shift("up"); } } function shiftItemListUp() { shiftItemList("up"); } function up() { shiftItemListUp(); resetScrollInterval(); } function shiftItemListDown() { shiftItemList("down"); } function down() { shiftItemListDown(); resetScrollInterval(); }