var iPhotoPlay = GetCookie("photoplay");
var iPhotoDir = parseInt(GetCookie("photodir"));
var vPhotoTimer = null;
var iPhotoDelay = 5000;

if (isNaN(iPhotoDir) || (iPhotoDir <=0) || (iPhotoDir > 2)) {
	iPhotoDir = 2;
}

//alert(iPhotoPlay);

if (iPhotoPlay==1) {
	if ((iPhotoDir==1) &&  (photo_id_prev==0)) {
		alert("已經到達第一張");
		photo_stop();
	} else if ((iPhotoDir==2) &&  (photo_id_next==0)) {
		alert("已經到達最後一張");
		photo_stop();
	} else {
		vPhotoTimer = setTimeout("photo_play()",iPhotoDelay);
	}
}
	
function photo_direct(s)
{
	iPhotoDir = s;
	SetCookie("photodir",s);
	photo_play();
}

function photo_play()
{
	var expir = new Date();
	expir.setTime(expir.getTime()+iPhotoDelay+3000);
	SetCookie("photoplay", 1, expir);
	
	//alert(iPhotoDir+" "+photo_id_prev+" "+photo_id_next);
	if (vPhotoTimer) {
		clearTimeout(vPhotoTimer);
	}
	if (iPhotoDir==1) {
		if (photo_id_prev > 0) {
			// 往前
			location.replace("photo.php?id="+photo_id_prev);
		} else {
			alert("目前已經是第一張了");
		}
	} else if (iPhotoDir==2) {
		if (photo_id_next > 0) {
			// 往後
			location.replace("photo.php?id="+photo_id_next);
		} else {
			alert("目前已經是最後一張了");
		}
	}
}

function photo_stop()
{
	iPhotoPlay = 0;
	SetCookie("photoplay",0);
	if (vPhotoTimer) {
		clearTimeout(vPhotoTimer);
	}
}


function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}
function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
		((path == null) ? "" : ("; path=" + path)) +
		((domain == null) ? "" : ("; domain=" + domain)) +
		((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
