﻿/* ****************************************************************
	site.js
	Carfagno site js helpers
	author: Jesse Hemingway
	copyright (c) 2008 Jirosoft	
**************************************************************** */

// navPop object template
// ********************************************************************************
function NavButton(elemID, reverse)
{
	this.elemID = elemID;
	this.reverse = reverse;
	
	this.mouseIn = false;
	this.direction = 0;
	this.nSteps = 0;
	this.iStep = 0;
}



var navButtons = new Array(new NavButton('navPopLeft1', true), new NavButton('navPopLeft2', true),
						new NavButton('navPopLeft3', true),
						new NavButton('navPopRight1', true), new NavButton('navPopRight2', true),
						new NavButton('navPopRight3', true));


var travel = 30;
var timer = null;

function navPop(iButton)
{

	if (navButtons[iButton].direction <= 0) {
		navButtons[iButton].nSteps = 8;
		if (navButtons[iButton].direction == 0)
			navButtons[iButton].iStep = 0;
		else
			navButtons[iButton].iStep = (16 - navButtons[iButton].iStep) / 2;
		navButtons[iButton].direction = 1;
		if (timer) {
			clearTimeout(timer);
			timer = null;
		}
		navPopAdvance();
	}
	navButtons[iButton].mouseIn = true;
}

function navDrop(iButton)
{
	navButtons[iButton].mouseIn = false;
}

function navPopAdvance()
{
	var nInMotion = 0;
	for (var i = 0; i < 6; i++) {
		if (navButtons[i].nSteps) {
			var elem = getElement(navButtons[i].elemID);
			var uStep = navButtons[i].iStep / (navButtons[i].nSteps - 1);

			var uMove = Math.min(1, uStep);
			var position = 0;
			if (navButtons[i].direction == 1)
				position = 	-travel + Math.pow(uMove, 1/2) * travel;
			else if (navButtons[i].direction == -1)
				position = 0 - Math.pow(uMove, 2) * travel;
			if (navButtons[i].reverse)
				position = -travel + 28 - position;
			elem.style.backgroundPosition = position.toString() + 'px 0px';

			navButtons[i].iStep++;
			if (uStep > 1) {
				if (navButtons[i].direction > 0 && (!navButtons[i].mouseIn && uStep > 1.4)) {
					navButtons[i].direction = -1;
					navButtons[i].nSteps = 16;
					navButtons[i].iStep = 0;
				} else {
					if (navButtons[i].direction > 0)
						navButtons[i].direction = 0.8;
					else {
						navButtons[i].direction = 0;
						navButtons[i].nSteps = 0;
						navButtons[i].iStep = 0;
					}
				}
			}
		}
	}
	
//	if (!timer)
		timer = setTimeout("navPopAdvance()", 40);		
}


// gallery support
// ********************************************************************************
var curFile = null;
var nCurChips = 0;
var imageIndex = 0;

function showImage(file, nChips, index, chipIndex)
{
	imageIndex = index;
	curFile = file;
	nCurChips = nChips;
	getElement('largePicImg').src = file;
	
	splitwords = file.lastIndexOf('/');
	splitpath = 'ovGfx/lightbox/';
	filename = file.substr(splitwords+1);
	splitindex = filename.lastIndexOf('.');
	lightbox_imgname = filename.slice(0,splitindex); 
	lightbox_imgname = lightbox_imgname.concat('_lightbox.jpg');
	getElement('largePicImg').setAttribute('href',splitpath.concat(lightbox_imgname));
	
	iCut = file.lastIndexOf('.')
	if (iCut > 0) {
		for (var i = 1; i <= nChips; i++) {
			fileChip = file.slice(0, iCut - 1) + i + '_chip' + file.slice(iCut);
			getElement('chip' + i).src = fileChip;
			revealLayer('chipDiv' + i);
		}
	

	
		for (i; i <= 10; i++)
			hideLayer('chipDiv' + i);
			
		var text = eval('galText_' + imageIndex + '_' + chipIndex);
		
		if (text)
			writeLayer('galText', text);
			
			
	}
}

function showSubImage(chipIndex)
{
	iCut = curFile.lastIndexOf('.');
	if (iCut > 0) {
		curFile = curFile.slice(0, iCut - 1) + chipIndex + curFile.slice(iCut);
		showImage(curFile, nCurChips, imageIndex, chipIndex);
	}
}


// ovScrollerStart object template
// ********************************************************************************
var ovScrollers = new Array();
var scrollTimer = null;

function ovScroller(layer, speed, direction)
{
	this.layer = layer;
	this.items = new Array();
	this.timer = null;
	this.speed = 0;
	this.motion = '';
	this.preFreezeMotion = '';
	this.maxSpeed = speed;
	this.direction = direction;
	this.uPos = 0;
	this.pageDir = 0;

//	this.start = ovScrollerStart;
//	this.stop = ovScrollerStop;
	this.advance = ovScrollerAdvance;
	this.freeze = ovScrollerFreeze;
	this.pageOver = ovScrollerPageOver;
	
	ovScrollers.push(this);
}

function ovStartScrollers()
{
	if (!scrollTimer)
		scrollTimer = setTimeout("ovScrollerAdvanceAll()", 50);
}

function ovScrollerFreeze(freezeIt)
{
	if (freezeIt) {
		this.preFreezeMotion = this.motion;
		this.motion = 'stop';
		this.speed = 0;
	} else
		this.motion = this.preFreezeMotion;
}

function ovScrollerAdvance()
{
	var obj = getElement(this.layer);
	var dif = obj.scrollWidth - obj.offsetWidth;
	if (this.direction == -1 && !this.motion) {
		obj.scrollLeft = this.uPos = dif;
		this.motion = 'run';
	}
	if (dif > 0 && this.motion != 'stop') {	
		if (this.motion == 'slow')
			this.speed -= this.speed / 4;
		else if (this.speed < this.maxSpeed)
			this.speed += (this.maxSpeed - this.speed) / 10;
		else if (this.speed > this.maxSpeed)
			this.speed -= (this.speed - this.maxSpeed) / 4;
		
		if (this.pageDir) {
			this.speed = this.maxSpeed + 6;
			this.direction = this.pageDir;
		}

		var accel = this.maxSpeed / 20;
		var bumperWidth = this.maxSpeed / accel / 2;
		if (this.motion == 'slow' && (this.uPos <= 0.5 || this.uPos >= dif - 0.5 || this.speed < 0.1)) {
			this.direction = -this.direction;
			this.speed = 0;
			this.motion = 'run';
		}
		if (this.direction > 0) {
			this.uPos = Math.min(dif, this.uPos + this.speed);
			if (!this.pageDir && (this.uPos >= dif - bumperWidth))
				this.motion = 'slow';
		} else {
			this.uPos = Math.max(0, this.uPos - this.speed);
			if (!this.pageDir && (this.uPos < bumperWidth))
				this.motion = 'slow';
		}
		obj.scrollLeft = Math.round(this.uPos);
	}
}

function ovScrollerPageOver(clickDown, direction)
{
	var obj = getElement(this.layer);
	var dif = obj.scrollWidth - obj.offsetWidth;
	
	if (clickDown)
		this.pageDir = direction;
	else
		this.pageDir = 0;
}

function ovScrollerAdvanceAll()
{
	for (i in ovScrollers) {
		ovScrollers[i].advance();
	}
	this.scrollTimer = setTimeout("ovScrollerAdvanceAll()", 30);
}
