

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/
 * Gallery Scroller script, v1.0
 * (c) 2004 timallen.com
 * created by bivia of bivia.com, August 26 2004
 * $Id: galleryScroller.js,v 1.12 2005/01/31 21:10:59 bcurtis Exp $
 *
 *----*/


window.bv_PopWin = false;
function bv_setPic(E,D,B,H) {
	if (!E || E.href == "") return true;
	else {
		var WxH, Dim, ImgParts, ImgSz;
		var Link = '';
		var PopImg = '';
		WxH = '';
		if (D) WxH = (Dim = D.match(/\b(\d+)x(\d+)\b/i)) ? ' width="'+ Dim[1] +'" height="'+ Dim[2] +'"' : '';
		if (B && H) {
			ImgSz = (ImgParts = B.match(/\b(\d+)x(\d+)\b/i)) ? ImgParts[1] +','+ ImgParts[2] : '';
//			Link = (ImgParts = E.href.match(/^(.*)\/300\/(.*)$/i)) ? '<a href="'+ ImgParts[1] +'/big/'+ ImgParts[2] +'" target="pop_viewer" onclick="bv_popViewer(this.href, '+ ImgSz +'); return false;">' : '';
			Link = '<a href="'+ H +'" target="taviewer" onclick="bv_popViewer(this.href, '+ ImgSz +'); return false;">';
			PopImg = '<img class="popper" src="/gallery/img/kumo/enlarge.gif" height="15" width="15" alt="" border="0" />';
		}
		document.getElementById("bigPic").innerHTML = Link +'<img src="'+ E.href +'"'+ WxH +' alt="" />'+ PopImg;
		document.getElementById("picCaption").innerHTML = (E.title) ? E.title : "";
	}
	return false;
}
function bv_popViewer(L,W,H) {
	if (window.bv_PopWin && !window.bv_PopWin.closed) window.bv_PopWin.resizeTo(W +35,H +35);
	window.bv_PopWin = window.open('/_popviewer.php?i='+L, 'taviewer', 'width='+ W +', height='+ H +', resizable');
	window.bv_PopWin.focus();
}



function bv_setBandWidth(To,From) {
	var Bw = (To == "hi") ? 1 : 0;
	var E  = new Date();
	var Ex = new Date(E.getTime() + 1000*60*60*24*30*6);
	document.cookie = "bndchoice="+ Bw +"; path=/; expires="+ Ex.toGMTString() +";";
	bv_setBandWidthToggles();
}
function bv_setBandWidthToggles() {
	var HiTo = (document.cookie.indexOf("bndchoice=0") == -1) ? "on" : "off";
	var LoTo = (HiTo == "on") ? "off" : "on";
	if (bv_flip) {
		var Hi = document.getElementById("hiBandButton");
		var Lo = document.getElementById("loBandButton");
		if (!Hi.orig_off || !Hi.off) {
			bv_preload(Hi, "off", "on");
			bv_preload(Lo, "off", "on");
			Hi.orig_off = Hi.off;
			Lo.orig_off = Lo.off;
		}
		Hi.off = (HiTo == "on") ? Hi.on : Hi['orig_off'];
		Lo.off = (LoTo == "on") ? Lo.on : Lo['orig_off']; 
		bv_flip(Hi, HiTo);
		bv_flip(Lo, LoTo);
	}
}
function bv_setVideo(E) {
	document.getElementById("picCaption").innerHTML = (E.title) ? E.title : "";
	return true;
}


var bv_ScrollerPrefs = {
		gap : 0,
		Stage_width : 750
	};
function setScrollerPrefs(Obj) {
	for (var Prop in Obj) {
		switch (Prop.toLowerCase()) {
			case "gap" : bv_ScrollerPrefs.gap = Obj[Prop]; break;
			case "pos" : bv_setMouseTrackAxisPosition(Obj[Prop][0],Obj[Prop][1],Obj[Prop][2]); break;
			case "width" : bv_ScrollerPrefs.Stage_width = Obj[Prop]; break;
		}
	}
// hide the png strip mask from IE 5
	if (	     navigator.platform == "Win32" 
			&&   navigator.appName == "Microsoft Internet Explorer" 
			&&   parseFloat(navigator.appVersion.match(/MSIE (\d+\.\d+)/, '')[1]) < 5.5
		) document.write('<style type="text/css">#picStripMask{display:none;}</style>');
}










/*---------------------------------------------------/
 * Shuffler: moves "cards" in a train between "decks"
 * 
 * Imagine two stacks of cards:   =   Cards are flying
 * between each stack as you      =   shuffle them. They
 * can fly to the left deck from  ----  the right, or to
 * the right from the left. As each  =  gets added to its
 * destination, the destination      =  gets bigger. As
 * each gets taken from the source,  =  that deck gets
 * smaller.
 *
 * On top of that, you cheat. Each card is hooked so that
 * once the previous cards flies a certain distance from
 * the source, it "grabs" the next card.
 *
 *----------*/

function Shuffler() {
	if (bv_Scroller.StartSpeed) {
		var MoveBy = bv_Scroller.StartSpeed;
		bv_Scroller.Carryover = 0;
	} else {
		var Diff = bv_Scroller.Speed /bv_Scroller.Rate; // (px/s) / (fps) = px/f
		var MoveBy = Math.round(Diff);
		bv_Scroller.Carryover += Diff - MoveBy;
		if (Math.abs(bv_Scroller.Carryover) < 0.01) bv_Scroller.Carryover = 0;
		if (Math.abs(bv_Scroller.Carryover) > 1.0) {
			MoveBy += parseInt(bv_Scroller.Carryover);
			bv_Scroller.Carryover -= parseInt(bv_Scroller.Carryover);
		}
	}
	var First = bv_Scroller.Fliers[0];
	var Last = bv_Scroller.Fliers[bv_Scroller.Fliers.length -1];
	for (var xx=0; xx < bv_Scroller.Fliers.length; xx++) // nudge them all in the direction of movement
		bv_Scroller.Fliers[xx].move(MoveBy);
	
	First.clip(First.grabL - First.getX(), First.width);
	Last.clip(0,Last.stopR - Last.getX());
	
	if (MoveBy < 0) { // if they are moving to the left
		// the initial slide might be happening;
		if (bv_Scroller.StartSpeed && First.getX() < (First.stopR - First.stopL) /4) { // if more than 3/4 of the way...
			bv_Scroller.Speed = bv_Scroller.StartSpeed *bv_Scroller.Rate;                // ...leave it some momentum...
			bv_Scroller.StartSpeed = 0;                                                  // ...and stop the initial jolt
		}
		if (Last.getX() < Last.grabR) { // if the last card in the hand is past the grabbing point
			bv_Scroller.Fliers.push(Last.sibR);  // grab the next card
		// move the new last card in the hand to the right side of the prev last card:
			Last.sibR.moveTo(Last.getX() + Last.width);
			Last.sibR.clip(0,Last.sibR.stopR - Last.sibR.getX());
		}
		if (First.getX() < First.stopL) {  // if the first card in the hand is past the stopping point
//			First.moveTo(First.stopL);      // place it correctly for the left deck
			bv_Scroller.Fliers.shift();     // ...and then knock it from the top of the hand
		}
	} else if (MoveBy > 0) {      // if they are moving to the right
		if (Last.getX() > Last.stopR) {  // if the last card in the hand is past the stopping point
//			Last.moveTo(Last.stopR);      // place it correctly for the right deck
			bv_Scroller.Fliers.pop();     // ...and then knock it from the bottom of the hand
		}
		if (First.getX() > First.grabL) {  // if the first card in the hand is past the grabbing point
			bv_Scroller.Fliers.unshift(First.sibL); // grab the next card
		// move the new first card in the hand by the amount overshot to the right:
			First.sibL.moveTo(First.getX() - First.sibL.width);
		}
	}
}

function bv_moveDivTo(x) {
	this.style.left = x +"px";
	return this.style.left;
}
function bv_moveDivBy(x) {
	this.style.left = (this.getX() +x) +"px";
	return this.style.left;
}
function bv_getDivX() {
	if (!this.style.left) this.style.left = 0 +"px";
	return parseInt(this.style.left);
}
function bv_clipDivLR(L,R) {
	if (R > this.style.width) R = this.style.width;
	this.style.clip = "rect(0 "+ R +"px auto "+ L +"px)";
}


function bv_mouseTrackListener(x,y) {
	bv_Scroller.F = Math.round(  Math.sin( Math.atan2(x,y) ) *x  ) * (x / Math.abs(x));
//	bv_Scroller.F = Math.ceil(8* x / (Math.pow(y /4,2) +1));
//	bv_Scroller.F = x;
}
// adjust Push based current mouse position
function getForce() {
	var Force = 
			(bv_Scroller.F > +98) ? -8 :
			(bv_Scroller.F > +95) ? -7 :
			(bv_Scroller.F > +90) ? -6 :
			(bv_Scroller.F > +82) ? -5 :
			(bv_Scroller.F > +70) ? -4 :
			(bv_Scroller.F > +55) ? -3 :
			(bv_Scroller.F > +40) ? -2 :
			(bv_Scroller.F > +20) ? -1 :
			(bv_Scroller.F < -98) ? +8 :
			(bv_Scroller.F < -95) ? +7 :
			(bv_Scroller.F < -90) ? +6 :
			(bv_Scroller.F < -82) ? +5 : 
			(bv_Scroller.F < -70) ? +4 :
			(bv_Scroller.F < -55) ? +3 :
			(bv_Scroller.F < -40) ? +2 :
			(bv_Scroller.F < -20) ? +1 : 
			0; // therefore, 0.05 > X > -0.05
	return Force;
}
// determine Speed based on Force and Friction
function setSpeed() {
	bv_Scroller.Speed += getForce() * Math.ceil(bv_Scroller.Speed_Max / bv_Scroller.Friction) *2;
	var Fr = Math.ceil(Math.abs(bv_Scroller.Speed) * (bv_Scroller.Friction /100));
	bv_Scroller.Speed -= (bv_Scroller.Speed < 0) ? -1 * Fr : Fr;
	bv_Scroller.Speed = Math.max(-1 * bv_Scroller.Speed_Max, Math.min(bv_Scroller.Speed, bv_Scroller.Speed_Max));
}


var bv_Scroller = new Object();
function bv_galleryScroller() {
	bv_Scroller.Rate = 45;        // essentially, fps
	bv_Scroller.Speed = -2000;    // initial starting speed; 0=stopped, 1000=1000px/s; negative is right-to-left
	bv_Scroller.Speed_Max = 6000; // px/s; significantly less than c
	bv_Scroller.Friction = 60;    // % Speed loss per second; not at all like physical friction
	bv_Scroller.StartSpeed = -40;
	bv_Scroller.Carryover = 0;
	bv_Scroller.F = 0;

	GrabWidth = 2 + bv_ScrollerPrefs.gap;
	bv_Scroller.Hand = document.getElementById("picStrip").getElementsByTagName("div");

	for (var Card = 0; Card < bv_Scroller.Hand.length; Card++) {
		bv_Scroller.Hand[Card].move   = bv_moveDivBy;
		bv_Scroller.Hand[Card].moveTo = bv_moveDivTo;
		bv_Scroller.Hand[Card].getX   = bv_getDivX;
		bv_Scroller.Hand[Card].clip   = bv_clipDivLR;
		bv_Scroller.Hand[Card].width  = bv_Scroller.Hand[Card].getElementsByTagName("img")[0].width +GrabWidth;
		bv_Scroller.Hand[Card].grabL  = 0;
		bv_Scroller.Hand[Card].stopL  = 0 - bv_Scroller.Hand[Card].width;
		bv_Scroller.Hand[Card].stopR  = bv_ScrollerPrefs.Stage_width;
		bv_Scroller.Hand[Card].grabR  = bv_ScrollerPrefs.Stage_width - bv_Scroller.Hand[Card].width;
		bv_Scroller.Hand[Card].sibL   = (Card != 0) ? bv_Scroller.Hand[Card -1] : bv_Scroller.Hand[bv_Scroller.Hand.length -1];
		bv_Scroller.Hand[Card].sibR   = (Card != bv_Scroller.Hand.length -1) ? bv_Scroller.Hand[Card +1] : bv_Scroller.Hand[0];
		bv_Scroller.Hand[Card].style.width = bv_Scroller.Hand[Card].width +"px";
		bv_Scroller.Hand[Card].moveTo(bv_Scroller.Hand[Card].stopR);
		bv_Scroller.Hand[Card].clip(0,0);
	}

	bv_Scroller.Fliers = new bv_Array();
	bv_Scroller.Fliers[0] = bv_Scroller.Hand[0];

	bv_Scroller.TimerSpeed = setInterval("setSpeed()",500);
	bv_Scroller.TimerShuffle = setInterval("Shuffler()", Math.floor(1000 /bv_Scroller.Rate));

}



function bv_Array() {
	var Arr = new Array();
	if (!Arr.push) {
		Arr.push = function (o) {
				this[this.length] = o;
				return this.length;
			}
	}
	if (!Arr.pop) {
		Arr.pop = function ()  {
				var Last = this[this.length -1];
				this.length--;
				return Last;
			}
	}
	if (!Arr.shift) {
		Arr.shift = function () {
				var First = this[0];
				for (var xx=0; xx < this.length -1; xx++)
					this[xx] = this[xx +1];
				this.length--;
				return First;
			}
	}
	if (!Arr.unshift) {
		Arr.unshift = function (o) {
				for (var xx=this.length; xx > 0; xx--)
					this[xx] = this[xx -1];
				this[0] = o;
				return this.length;
			}
	}
	return Arr;
}




