// generic functions and variables--from template.javascript - should not be modified.
var speed=100000;
var t=null;

function setCookie(name, value, expire ){
	c=name + "=" + escape(value);
	if (expire != null) c += ";expires=" + expire.toGMTString();
	c += ";path=/;";
	document.cookie = c;
}

function getExpireDate(){
	var expires = new Date();
	expires.setTime((new Date().getTime()) + 1000*60*60*24*365);
	return expires;
}

function GetCookie (name) {
	begin = document.cookie.indexOf(name+"="); 
	if (begin != -1) {
		begin += name.length+1; 
		end = document.cookie.indexOf(";", begin);
		if (end == -1) end = document.cookie.length;
		return unescape(document.cookie.substring(begin, end));
	}
	return null;
}

function delCookie (NameOfCookie) {
// The function simply checks to see if the cookie is set.
// If so, the expiration date is set to Jan. 1st 1970.
	if (GetCookie(NameOfCookie)) {
		document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function SlideShow(){
	if (speed==0) return;
	imgnum = parseInt(imgnum + 1);
	if (imgnum > (numthumbs-1)) imgnum=0;
	setpic(imgnum);
	t = setTimeout('SlideShow()', speed);
}

function SlideShowDelay(delay){
	speed=delay*1000;
	clearTimeout(t);
	SlideShow();
}

function SwitchBanner(){
	setTimeout('SwitchBanner()', 6500);
	banner=GetCookie("JayspixBanner");
	banner=banner-1+2;
	if (banner >= numbanners) banner=0;
	document.images['bannerimg'].alt=banners[banner][1];
	document.images['bannerimg'].src=banners[banner][2];
	setCookie("JayspixBanner", banner, getExpireDate());
}

function FollowLink(){
//	document.location = banners[banner][0];
	window.open(banners[banner][0]);
}

function findPosY(obj) {
	curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function findPosX(obj) {
	curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curtop += obj.x;
	return curtop;
}

function thumbrow() {
	var me,tn,x=cur;
	for (var i=1;i<=perpage;i++) {
		me = document.getElementById(i);
		if (x<thumbs.length) {
			me.style.backgroundImage="url('"+thumbs[x][1]+"')";
			me.style.cursor="pointer";
			tn=document.getElementById('tn'+i);
			tn.alt=thumbs[x][2];
			if (thumbs[x][4]>old) {
				tn.src=tnoutnew;
			} else {
				tn.src=tnoutline;
			}
			x++;
		} else {
			me.style.backgroundImage="url('"+blankimg+"')";
			tn=document.getElementById('tn'+i);
			tn.alt='';
			tn.src=blankimg;
			me.style.cursor="auto";
		};
	};
	if (cur==0) {
		me=document.getElementById('psi');
		me.src=blankimg;
		me.width=pi.width;
		me.style.cursor="auto";
	} else {
		me=document.getElementById('psi');
		me.src=previmg;
		me.width=pi.width;
		me.style.cursor="pointer";
	}
	if (x==thumbs.length) {
		me=document.getElementById('nsi');
		me.src=blankimg;
		me.width=ni.width;
		me.style.cursor="auto";
	} else {
		me=document.getElementById('nsi');
		me.src=nextimg;
		me.width=ni.width;
		me.style.cursor="pointer";
	}
};

function savepic() {
	document.location='/savefile.php?file=' + encodeURIComponent(thumbs[imgnum][0]) + '&forcedownload=1&path=' + href.substr(0,href.lastIndexOf("/")+1);
//	document.location='/savefile/' + encodeURIComponent(thumbs[imgnum][0]) + '?forcedownload=1&path=' + href.substr(0,href.lastIndexOf("/")+1);
//	var w=open('/savefile/' + encodeURIComponent(thumbs[imgnum][0]) + '?forcedownload=1&path=' + href.substr(0,href.lastIndexOf("/")+1), 'external_viewer', 'width=240,height=240');
}

function makepic(i) { //check for 
	var num=parseInt(i)+cur-1;
	if (num>=thumbs.length) return;
	if (num<0) return;
	if (thumbs[num][3]!='') {  // should replicate this for 'save as...' code to allow people to save pics.--opener should auto-open image files for saving...
//		var href=window.location.href;
		open('/opener.php?path=' + href.substr(0,href.lastIndexOf("/")+1) + '&file=' + encodeURIComponent(thumbs[num][0]), 'external_viewer', 'width=240,height=240');
		num=parseInt(num+1);
	}
	setpic(num);
}

function setpic(num) {
	if (num>=thumbs.length) return;
	if (num<0) return;
	if (numthumbs==0) return;
	if (thumbs[num][3]!='') {
		setpic(parseInt(num+1));
		return;
	}
	myImage.src=thumbs[num][0];
//size isn't known if image loads slowly--maybe prefetch prev/next images &/or thumbrow.
// - using "setInterval" in start function to solve this problem.
	imgnum=num;
	window.status=thumbs[imgnum][2];
	cf.replaceChild(document.createTextNode('Photo '+(imgnum+1)+' of '+thumbs.length+'     '+thumbs[imgnum][2]),cf.childNodes[0]);
	cf.style.fontSize="16px";
	if (imgnum==0) {
		me=document.getElementById('pii');
		me.src=blankimg;
		me.width=pi.width;
		me.style.cursor="auto";
	} else {
		me=document.getElementById('pii');
		me.src=previmg;
		me.width=pi.width;
		me.style.cursor="pointer";
	}
	if ((imgnum+1)>=thumbs.length) {
		me=document.getElementById('nii');
		me.src=blankimg;
		me.width=ni.width;
		me.style.cursor="auto";
	} else {
		me=document.getElementById('nii');
		me.src=nextimg;
		me.width=ni.width;
		me.style.cursor="pointer";
	}
	mp.src=thumbs[imgnum][0];
	sizeme();
}

function sizeme() {
	var width,height;
	var maxwidth=findPosX(document.getElementById('rightmost'))-windowpadding;
//	mp.src=thumbs[imgnum][0];
	height=myImage.height;
//	height=mp.height;
	if ((width=myImage.width) > maxwidth) {
//	if ((width=mp.width) > maxwidth) {
		var myheight=myImage.height;
		var mywidth=myImage.width;
		var ratio=maxwidth/mywidth;
		width=maxwidth;
		height=parseInt(myheight*ratio);
	}
	mp.width=mt.width=width;
	pf.style.height=mp.style.height=mt.style.height=height+"px";
//	mt.height=height;
	rd.style.top=height+"px";
}

function prev() {
	setpic(imgnum-1);
}

function next() {
	setpic(imgnum+1);
}

function prevset() {
	cur = cur - perpage;
	if (cur <0 ) cur = 0;
	thumbrow();
}

function nextset() {
	if (cur+perpage < thumbs.length) cur = cur + perpage;;
	thumbrow();
}

function start() {
	var d = new Date();
	var timestamp = d.getTime()/1000;
	old=parseInt(timestamp-(marknew*86400));
	mt=document.getElementById('maintable');
	mp=document.getElementById('mainpic');
	pd=document.getElementById('picdiv');
	pf=document.getElementById('picframe');
	cf=document.getElementById('caption');
	rd=document.getElementById('rest');
	if (numthumbs) {
		pf.style.top=pd.style.top;
		thumbrow();
		setpic(0);
		setInterval("sizeme()", 100); // just in case images load slowly
	}
	if (numbanners>=1) SwitchBanner();
}

var href=window.location.href;
var framewidth=2;
var windowpadding=20+framewidth;
var cur=0;
var imgnum=0;
var myImage = new Image;
var mt,mp,pd,pf;
var pi=new Image;
var ni=new Image;
var bi=new Image;
var old;
if (typeof(window['previmg']) != "undefined") pi.src=previmg;
if (typeof(window['previmg']) != "undefined") ni.src=nextimg;
if (typeof(window['previmg']) != "undefined") bi.src=blankimg;
if (numthumbs) {
	var srch=window.location.search;
	var arry=srch.split('?');
	if(arry[1]) {
		cur=parseInt(arry[1]);
	}
}

banner=GetCookie("JayspixBanner");
banner=banner-1;
if (banner >= numbanners || banner < 0) banner=0;
delCookie("JayspixBanner");
delCookie("JayspixBanner");
setCookie("JayspixBanner", banner, getExpireDate());

//  setting window.onload doesn't work if the javascript is included.. instead use <body onload="start()">
//window.onload=start;