<!--
// Functionality for RasPottery.com
// http://www.raspottery.com
// Copyright © 2005-2007

// set the browser type
var isNS = false;
var isIE = false;
var isOther = false;
var browser = navigator.appName;
var browserVersion = parseInt(navigator.appVersion);

isNS = (browser.indexOf('Netscape') >= 0);
isIE = (browser.indexOf('Microsoft') >= 0);
isOther = (!isNS && !isIE);

// constants
var YES = 1;
var NO = 0;

/**
 * Add an event listener (IE & FF compliant)
 * @param	string	event_name
 * @param	object	func
 */
function addEvent(event_name, func)
{
	if (window.attachEvent)
	{
		// IE
		window.attachEvent(event_name, func);
	}
	else
	{
		// other
		if (event_name.substring(0, 2) == 'on')
		{
			event_name = event_name.substring(2);
		}
		window.addEventListener(event_name, func, false);
	}
}

// returns true if the needle (string) is in the haystack (array)
function in_array(needle,haystack)
{
	var isin = false;

	for (var i=0; i<haystack.length; i++)
	{
		if (haystack[i] == needle) { isin = true; break; }
	}

	return isin;
}

// returns the array position of needle (string) in haystack (array)
function array_search(needle,haystack)
{
	var ret = -1;

	for (var i=0; i<haystack.length; i++)
	{
		if (haystack[i] == needle) { ret = i; break; }
	}

	return ret;
}

/**
* Return the number formatted as a float
* @param mixed num
* @return float
*/
function formatCurrency(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if (isNaN(num)) { num = 0; }
	var sign = (num==(num=Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	var cents = (num % 100);
	num = Math.floor(num / 100).toString();
	if (cents < 10) { cents = '0' + cents; }
	return (sign ? '' : '-') + num + '.' + cents;
}

function cash(amount, prefix_sign)
{
	amount = formatCurrency(amount) + '';
	x = amount.split('.');
	x1 = x[0];
	x2 = (x.length>1 ? '.' + x[1] : '');
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1))
	{
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return (prefix_sign ? '$' : '') + x1 + x2;
}

// find the Nth occurance of a string in another string (returns the index)
function strpos(str,fnd,n)
{
	var pos = -1;
	var totmatches = 0;
	var chr = '';

	for (var i=0; i<str.length; i++)
	{
		chr = str.substring(i,(i+1));
		if (chr == fnd)
		{
			totmatches++;
			if (totmatches == n) { pos = i; break; }
		}
	}

	return pos;
}

// return the Nth token in the string, separated by a separator
function token(str,sep,n)
{
	if (str.substring(0,1) != sep) { str = sep+str; }
	if (str.substring((str.length-1),1) != sep) { str = str+sep; }

	var pos1 = (n==1&&0?0:strpos(str,sep,n)+1);
	var pos2 = strpos(str,sep,(n+1));

	return str.substr(pos1,(pos2-pos1));
}

// returns true if the given date is valid (mm/dd/yyyy)
function validDate(check_date)
{
	var re = /^\d{1,2}\/\d{1,2}\/\d{2,4}$/;
	if (re.test(check_date))
	{
		var darr = check_date.split('/');
		var d = new Date(check_date);
		return (d.getMonth()+1 == darr[0] && d.getDate() == darr[1] && d.getFullYear() == darr[2]);
	}
	else { return false; }
}

// go to the given URL
function go(url) { document.location = url; }

/**
* Return 's' if the number isn't a 1
* @param	integer	num
* @return	string
*/
function s(num)
{
	return (num!=1 ? 's' : '');
}

function getMouseXY(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) { var e = window.event; }
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = (e.clientX + document.body.scrollLeft);
		posy = (e.clientY + document.body.scrollTop);
	}

	var pos = [posx,posy];

	return pos;
}

/**
* Return the number with the ordinal suffix added
* @param integer num
* @return string
*/
function addOrdinalSuffix(num)
{
	var mod = (num%10);
	if (mod == 1) { suffix = 'st'; }
	else if (mod == 2) { suffix = 'nd'; }
	else if (mod == 3) { suffix = 'rd'; }
	else { suffix = 'th'; }

	return num+suffix;
}

/**
* Open a window
* @param	string	url
* @param	integer	w
* @param	integer	h
*/
function openWindow(url, w, h, winname)
{
	var params = [
		'width=' + w,
		'height=' + h,
		'left=' + ((screen.width / 2) - (w / 2)),
		'top=' + ((screen.height / 2) - (h / 1.5)),
		'menubar=no',
		'scrollbars=yes',
		'status=yes'
	];
	var win = window.open(
		url,
		winname || 'openWin' + (new Date).getTime(),
		params.join(',')
	);
	checkOpenWin(win);
	return win;
}

/**
* Check if the given object is an open window or not
* @param object obj
*/
function checkOpenWin(obj)
{
	if (!obj)
	{
		alert('Unable to open the window.\nIf you have a popup blocker, please configure it to allow popups for this site.');
	}
}

/**
 * Open the large-size of an image
 * @param string filename
 */
function openFullImage(filename)
{
	openWindow('image.php?path=' + filename, 700, 600);
}

/**
* Is this a Gecko browser? (IE: firefox)
* @return boolean
*/
function isGecko()
{
	var agent = navigator.userAgent.toLowerCase();
	return (agent.indexOf('gecko')!=-1 || agent.indexOf('opera')!=-1);
}
var is_gecko = isGecko();

/**
* Return the index of the option with the given value
* @param	mixed	obj	<select> object (ID or reference)
* @param	integer	val
* @param	boolean	ignore_case
* @return	integer
*/
function findValueIndex(obj, val, ignore_case)
{
	obj = $(obj);
	if (obj.options.length)
	{
		opts = obj.options;
		for (var i=0; i<opts.length; i++)
		{
			if (val == opts[i].value || (ignore_case && val.toLowerCase() == opts[i].value.toLowerCase()))
			{
				return i;
			}
		}
	}

	return 0;
}

/**
 * Toggle the menu image
 * @param string  which
 * @param boolean state true=over, false=out
 */
var page_imgID = '';
function mo(obj)
{
	var imgID = obj.id;
	var dir = 'images/menu/';

	if (imgID == page_imgID) { return; }

	if (!obj.hover)
	{
		$(imgID).src = dir + 'sel_' + imgID + '.gif';
		obj.hover = true;
	}
	else
	{
		$(imgID).src = dir + imgID + '.gif';
		obj.hover = false;
	}
}

function onlyNumbers(evt)
{
	evt = evt || window.event;
	if (!evt) { return true; }

	code = evt.keyCode || evt.charCode;
	val = $F(evt.target || evt.srcElement);

	if ((code >= 48 && code <= 57) || code == 46) // a number or a period
	{
		return ((code==46 && val.indexOf('.') == -1) || code!=46);
	}
	else if (code == 13) // enter key
	{
		return true;
	}
	else if (code == 45) // '-'
	{
		// do not allow a hyphen unless it is the first character
		return !(val.length);
	}
	else
	{
		var allow = [8, 9, 46, 37, 38, 39, 40, 116];
		return (allow.indexOf(code) != -1)
	}
}

/************
* home page *
************/

var imgdir = '';
var rotate_every = 4000;
var timer = null;
var paths = []; // filenames in above directory
var images = []; // loaded images
var currentIDX = 0;

function loadNextImage()
{
	var nextIDX = (currentIDX + 1);
	if (nextIDX == paths.length)
	{
		nextIDX = 0;
	}

	currentIDX = nextIDX;
	preloadImage();
}

function preloadImage()
{
	if (!images[currentIDX])
	{
		var path = imgdir + paths[currentIDX];
		images[currentIDX] = new Image();
		images[currentIDX].onload = changeImage;
		images[currentIDX].src = path;
	}
	else
	{
		changeImage();
	}
}

function changeImage()
{
	if (!images[currentIDX] || !images[currentIDX].complete)
	{
		timer = setTimeout('changeImage()', 250);
		return 0;
	}

	clearTimeout(timer);
	var path = imgdir + paths[currentIDX];

	new Effect.Opacity(
		'revolve_container',
		{
			duration:0.5,
			from:1.0,
			to:0.0,
			afterFinish:function()
			{
				$('artwork').src = path;
				new Effect.Opacity(
					'revolve_container',
					{
						duration:0.5,
						from:0.0,
						to:1.0,
						afterFinish:function()
						{
							timer = setTimeout('loadNextImage()', rotate_every);
						}
					}
				);
			}
		}
	);
}

// -->
