function setNav(id,num) {
	console.log('setNav: ',arguments);
	var sec=Math.floor(num/10);
	$$('.subNav').each(function (item) {
		item.className='subNav';
	});
	$$('.thumbSetChange').each(function (item) {
		item.className='thumbSetChange';
	});
	$('navTo'+id).className+=' subactive';
	$('thumb'+(sec+1)).className+=' thumbactive';
	var w=Math.floor($(id).offsetWidth/670);
	for (i=1; i<3; i++) {
		$('thumb'+(i+1)).style.display=(i<w?'block':'none');
	}
	$('thumbLeftCtl').style.display=
	$('thumbRightCtl').style.display=
	$('thumb1').style.display=
	$('navSet').style.display=
	(w==1?'none':'');
	$$('.thumb').each(function (item) {
		item.style.border='1px solid black';
	});
	navTo.active=id;
	setTo.active=Math.floor(num/3);
	$(navTo.active+'_'+num).style.border='1px solid white';
}
function navTo (id,set) {
	console.log('navTo: ',arguments);
	if (typeof set=='undefined') set=true;
	var src,dest,i,fnc;
	if ($(id).firstChild == null) return false;
	$$('.thumbGroup').each(function (item) {
		item.style.marginLeft='0';
	});
	setNav(id,0);
	$(id).marginLeft='0';
	src=-parseInt($('thumbMover').style.marginTop);
	if (isNaN(src)) src=0;
	
	dest=$(id).firstChild.offsetTop-$('COMMERCIAL').firstChild.offsetTop;
	if (document.all) dest=$(id).offsetTop+src;
	navTo.startTime=(new Date()).getTime();
	if (navTo.run==false)
		navTo.run=new PeriodicalExecuter(navTo.doMove.bind($('thumbMover'),src,dest),1/15);
	setTo.lastDest=0;
	if (set) 
		showPic(navTo.active,0);
}
navTo.doMove = function (src,dst) {
	console.log('doMove: ',arguments);
	var elapsed=((new Date()).getTime()-navTo.startTime)/1000;
	if (elapsed>=1) {
		$('thumbMover').style.marginTop='-'+parseInt(dst)+'px';
		navTo.run.stop();
		navTo.run=false;
	} else {
		var pos=(Math.sqrt(elapsed)+elapsed)/2;
		$('thumbMover').style.marginTop='-'+Math.floor((dst-src)*pos+src)+'px';
	}
}
function setTo(set) {
	console.log('setTo: ',arguments);
	var src,dst,ael=$(navTo.active);
	src=setTo.lastDest;
	if (isNaN(src)) src=0;
	if (!isNaN(parseInt(set)))
		dst=670*(set-1);
	else {
		if (set.toLowerCase()=='left') 
			dst=src-670;
		else
			dst=src+670;
	}
	if (dst>=ael.offsetWidth) dst=0;
	if (dst<0) dst=Math.floor(ael.offsetWidth/670)*670;
	navs=$$('.thumbSetChange');
	for (i=0; i<navs.length; i++) 
		navs[i].className=navs[i].className.replace(/(^| )active( |$)/g,'');
	$('thumb'+(Math.floor(dst/670)+1)).className+=' active';
	setTo.startTime=(new Date()).getTime();
	if (setTo.run==false) 
		setTo.run=new PeriodicalExecuter(setTo.doMove.bind(ael,src,dst),1/15);
	setTo.current=Math.floor(dst/670);
	if (showPic.active<setTo.current*10 || showPic.active>setTo.current*10+9)
		showPic(navTo.active,Math.floor(dst/67));
	else
		showPic(navTo.active,showPic.active);
}
setTo.doMove = function (src,dst) {
	console.log('doMove: ',arguments);
	var elapsed=((new Date()).getTime()-setTo.startTime)/1000;
	if (elapsed>=1) {
		$(navTo.active).style.marginLeft='-'+dst+'px';
		setTo.lastDest=dst;
		setTo.run.stop();
		setTo.run=false;
	} else {
		var pos=(Math.sqrt(elapsed)+elapsed)/2;
		$(navTo.active).style.marginLeft='-'+Math.floor((dst-src)*pos+src)+'px';
	}
}
setTo.run=false;
setTo.current=0;
navTo.run=false;
function showPic(sec,i) {
	console.log('showPic: ',arguments);
	showPic.active=i=parseInt(i);
	if (i<setTo.current*10 || i>setTo.current*10+9) {
		setTo(Math.floor(i/10)+1);
		return;
	}
	
	
	setNav(sec,i);
	var img=$('imgLarge');	
	var pic=pics[sec][i];
	var lgpic=large[sec][i];
	var viewLarge=$('imgViewLarger');
	if (viewLarge) {
		viewLarge.href=lgpic.url;
		viewLarge.title=pic.title;
		viewLarge.rel="lightbox["+sec+"]";
	}	
	var counter = 0;
	var imgSetArray = new Array();
	var set=large[sec];
	for (myitem in set) {
		if (set[myitem].title) {
			tempItem = set[myitem].url.replace('thumbs','large');
	    	imgSetArray[counter] = "<a href='"+set[myitem].url+"' rel='lightbox["+sec+"]' title='"+set[myitem].title+"'></a>";
			counter++;			
		}
	}	
	var galleryOutput = '';
	var galleryOutputPrev = '';
	var j=0;
	for (j=i+1;j<parseInt(counter);j++) {
		galleryOutput += imgSetArray[j];
	}
	for (j=0;j<i;j++) {
		galleryOutput += imgSetArray[j];
	}
	var imgSetsDiv=$('imgSets');
	if (galleryOutput.length > 10)
		imgSetsDiv.innerHTML = galleryOutput;
	if (img.src==pic.url) return;
	
	
	showPic.startTime=(new Date()).getTime();
	if (showPic.run!=false) 
		showPic.run.stop();
	showPic.run=new PeriodicalExecuter(showPic.transDown.bind(img,pic),1/15);	
	window.location='#'+sec.toLowerCase()+'/'+(i+1);
	smartSlider.stop();
}
showPic.active=0;
showPic.next = function () {
	console.log('showPic.next: ',arguments);
	var set=pics[navTo.active];
	var ct=parseInt(showPic.active);
	showPic(navTo.active,showPic.active=(ct+1)%set.length);
}
showPic.play = function () {
	console.log('showPic.play: ',arguments);
	if (!smartSlider.active) {
		var set=pics[navTo.active];
		var ct=parseInt(showPic.active);
		showPic(navTo.active,showPic.active=(ct+1)%set.length);
	} else {
		smartSlider.stop();
	}
	smartSlider.active=!smartSlider.active;
	$('imgPlayCtl').src='images/photo_'+(smartSlider.active?'pause':'play')+'.gif';
	$('imgPlayCtl').title=smartSlider.active?'Stop Slideshow':'Slideshow';
}
showPic.prev = function () {
	console.log('showPic.prev: ',arguments);
	var set=pics[navTo.active];
	var ct=parseInt(showPic.active);
	showPic(navTo.active,showPic.active=(ct-1+set.length)%set.length);
}
showPic.transDown = function (pic) {
	console.log('showPic.transDown: ',arguments);
	var txt=$('imgText');
	
	var elapsed=((new Date()).getTime()-showPic.startTime)/500;
	var opa=Math.max(1-elapsed*elapsed,0);
	opacity(this,opa);
	opacity(txt,opa);
	showPic.transDown.timeout=false;
	if (elapsed>=1) {
		var img=document.createElement('img');
		img.src=pic.url;
		var tmp=showPic.onLoad.bind(img);
		Event.observe(img,'load',tmp);
		showPic.transDown.timeout=new PeriodicalExecuter(tmp,3);
		if (showPic.run != false) {
			showPic.run.stop();
			showPic.run=false;
		}
		img.id='imgLarge';
		img.width=pic.info[0];
		img.height=pic.info[1];
		img.style.marginLeft=-Math.floor(pic.info[0]/2);
		img.style.marginTop=-Math.floor(pic.info[1]/2);
		opacity(img,0);
		this.parentNode.replaceChild(img, this);
		txt.innerHTML=pic.title;
	}
}
showPic.onLoad = function () {
	console.log('showPic.onLoad: ',arguments);
	showPic.startTime=(new Date()).getTime();
	if (showPic.run != false) 
		showPic.run.stop();
	if (typeof arguments[0] !=='undefined' && !!arguments[0].stop)
		arguments[0].stop();
	showPic.run=new PeriodicalExecuter(showPic.transUp.bind(this),1/15);
	if (showPic.transDown.timeout!=false) {
		showPic.transDown.timeout.stop();
		showPic.transDown.timeout=false;
	}
}
showPic.transUp = function (pic) {
	console.log('showPic.transUp: ',arguments);
	var txt=$('imgText');
	var elapsed=((new Date()).getTime()-showPic.startTime)/500;
	var opa=Math.max(elapsed*elapsed,0);
	opacity(this,opa);
	opacity(txt,opa);
	if (elapsed>=1) {
		if (showPic.run != false) {
			showPic.run.stop();
			showPic.run=false;
		}
		if (smartSlider.active) {
			smartSlider.reset();
		}
	}
}
showPic.run=false;
function navWindowLoad() {
	if (typeof console=='undefined') console={};
	if ($('debug')===null || typeof console.log=='undefined') {
		console.log=function () {};
	}
	console.log('navWindowLoad: ',arguments);
	var s,i
	$$('.subNav').each(function (item) {
		s=item.id.replace(/^navTo/,'');
		item.observe('click',navTo.bind(item,s));
	});
	['thumb1','thumb2','thumb3','thumbLeftCtl','thumbRightCtl'].each(function (item) {
		s=item.replace(/^thumb/,'').replace(/Ctl$/,'');
		$(item).observe('click',setTo.bind(item,s));
	});
	$$('.thumb').each(function (item) {
		var sec=item.id.match(/(\w+)_(\d+)/);
		item.observe('click',showPic.bind(item,sec[1],sec[2]));
	});
	$('imgLeftCtl').observe('click',showPic.prev);
	$('imgRightCtl').observe('click',showPic.next);
	$('imgPlayCtl').observe('click',showPic.play);
	navWindowLoad.run=new PeriodicalExecuter(navWindowLoad.post.bind(this),0.50);
}
navWindowLoad.post = function() {
	console.log('navWindowLoad.post: ',arguments);
	var pos=window.location.toString().replace(/^[^\#]*/,'');
	var rex=/#(commercial|residential|portraits|artistchoice)\/(\d+)/i;
	if (pos.length==0 || !rex.test(pos)) 
		setNav('COMMERCIAL',0);
	else {
		pos=pos.match(rex);
		navTo(pos[1].toUpperCase(),false);
		setNav(pos[1].toUpperCase(),pos[2]-1);	
		showPic(pos[1].toUpperCase(),pos[2]-1);
	}
	navWindowLoad.run.stop();
	setTimeout(smartSlider.buildCache,1000);
}
function smartSlider() {
}
smartSlider.stop = function () {
	console.log('smartSlider.stop: ',arguments);
	if (smartSlider.run !=  false) {
		smartSlider.run.stop();
		delete smartSlider.run;
		smartSlider.run = false;
	}
}
smartSlider.start = function () {
	console.log('smartSlider.start: ',arguments);
	smartSlider.stop();
	smartSlider.run=new PeriodicalExecuter(smartSlider.delayedSlide,3);
	
}
smartSlider.reset = function () {
	console.log('smartSlider.reset: ',arguments);
	smartSlider.stop();
	smartSlider.start();
}
smartSlider.delayedSlide = function () {
	console.log('smartSlider.delayedSlide: ',arguments);
	var sets = ['COMMERCIAL','RESIDENTIAL','PORTRAITS','ARTISTCHOICE','ASSOCIATE'];
	var rev = {COMMERCIAL:0,RESIDENTIAL:1,PORTRAITS:2,ARTISTCHOICE:3,ASSOCIATE:4};
	var ns=showPic.active+1;
	smartSlider.stop();
	if (ns>=pics[navTo.active].length) {
		var nts=sets[(rev[navTo.active]+1)%sets.length];
		while (typeof pics[nts] == 'undefined' || pics[nts].length==0) {
			nts=sets[(rev[nts]+1)%sets.length];
		}
		navTo(nts);
		return;
	}
	showPic(navTo.active,ns);
}
smartSlider.buildCache = function () {
	for (var s in pics) {
		for (var i=0; i<pics[s].length; i++) {
			setTimeout((function (obj) {
				obj.cache = new Image();
				obj.cache.src=obj.url;
			}).curry(pics[s][i]),Math.random()*i*1250+100*i);
		}
	}
}
var onReady = function () {
	onReady.execute.apply(onReady,$A(arguments));
};
Object.extend(onReady,{
	repo: [],
	bound: false,
	ready: false,
	imReady: function () {
		if (onReady.ready) return;
		onReady.ready=true;
		for (i=0; i<onReady.repo.length; i++) {
			onReady.repo[i].apply();
		}	
	},
	register: function (fnc) {
		if (!onReady.ready)
			onReady.repo.push(fnc);
		else
			fnc.apply();
		return;
	}
});
switch (true) {
	case (document.addEventListener && !Prototype.Browser.Opera):
		onReady.execute = function (fnc) {
			if (!onReady.bound) {
				onReady.bound=true;				
				document.addEventListener('DOMContentLoaded', onReady.imReady, false);
			}
			onReady.register(fnc);
		};
	break;
	case (Prototype.Browser.IE):
		onReady.execute = function (fnc) {
			if (!onReady.bound) {
				onReady.bound=true;
				onReady.checkReady();
			}
			onReady.register(fnc);
		};
		onReady.checkReady = function () {
			try {
				document.documentElement.doScroll('left');
			} catch (error) {
				setTimeout(onReady.checkReady,50);
				return;
			}
			onReady.imReady();
		}
	break;
	case (Prototype.Browser.Opera):
		onReady.execute = function (fnc) {
			if (!onReady.bound) {
				onReady.bound=true;				
				document.addEventListener('DOMContentLoaded', function () {
					if (onReady.ready) return;
					for (var i=0; i<document.styleSheets.length; i++) 
						if (document.styleSheets[i].disabled) {
							setTimeout(arguments.callee,50);
							return;
						}
					onReady.imReady();
				}, false);
			}
			onReady.register(fnc);
		};
	break;
	case (Prototype.Browser.Safari):
		onReady.execute = function (fnc) {
			if (!onReady.bound) {
				onReady.bound=true;
				onReady.checkReady();
			}
			onReady.register(fnc);
		}
		onReady.checkReady = function () {
			if (document.readyState != 'loaded' && document.reradyState != 'complete') {
				setTimeout(arguments.callee, 50);
				return;
			}
			if (numStyles === undefined) {
				var numStyles = document.getElementsByTagName('style').length;
				var links = document.getElementsByTagName('link');
				for (var i=0; i<links.length; i++) {
					var t=links[i].getAttribute('rel');
					if (t!=null && t.toLowercase()=='stylesheet')
						numStyles++;
				}
			}
			if (document.styleSheets.length != numStyles) {
				setTimeout(arguments.callee, 50);
				return;
			}
			onReady.imReady();
		}
	break;
}

onReady(function () {});
Event.observe(window,'load',onReady.imReady);

smartSlider.run=false;
smartSlider.active=false;
//Event.observe(window,'load',navWindowLoad);
onReady(navWindowLoad);