﻿function $(id) {
	return document.getElementById(id);
}

function bindPhotoNav(obj, curphotoid, showpath)
{
	var items = eval("(" + obj + ")").items;
	var length = items.length;
	var photoindex = 1;
	var photonav = $("photonav");
	if (items.length < 1)
	{
		photonav.innerHTML = "暂时无法显示导航条";
		return;
	}

	var html = "";
	html += "<table cellpadding='1'><tr>";
	for (var i in items)
	{
		var border = "";
		var size = 88;
		if (items[i].photoid == curphotoid)
		{
			photoindex = new Number(i) + 1;
			border = " class=\"activeimg\"";
			size = 76;
		}
		html += "<td><a href='"+showpath+"&id=" + items[i].photoid + "'><div" + border + "><img title='" + items[i].title + "' alt='" + items[i].title + "' src='" + items[i].square + "' width='" + size + "' height='" + size + "' border='0' /></div></a></td>";
	
	}
	html += "</tr></table>";
	photonav.innerHTML = html;
	_attachEvent(window, "load", function(){										  
		//scrolling
		if (photoindex > 4) {
			$('photonav').scrollLeft = (photoindex-4) * 90;
		}

		//disable prevImg
		if (photoindex == 1) {
			$("prevImg").style.display = 'none';
			if (1 == length) {
				$("nextImg").style.display = 'none';
				$("nextImgOnPhoto").href = showpath+'&id=' + items[0].photoid;
			}
			else{
				$("nextImgOnPhoto").href = showpath+'&id=' + items[photoindex].photoid;
				$("nextImg").href = showpath+'&id=' + items[photoindex].photoid;
			}
		}

		//disable nextImg
		else if (photoindex == length) {
			$("nextImg").style.display = 'none';
			$("nextImgOnPhoto").href = showpath+'&id=' + items[0].photoid;
			$("prevImg").href = showpath+'&id=' + items[photoindex - 2].photoid;
		}
		else {
			$("prevImg").href = showpath+'&id=' + items[photoindex - 2].photoid;
			$("nextImg").href = showpath+'&id=' + items[photoindex].photoid;
			$("nextImgOnPhoto").href = showpath+'&id=' + items[photoindex].photoid;
		}
		$("photoIndex").innerHTML = '<em>' + photoindex + '</em>/' + length;
	});

}

function resizePhoto(img)
{
	if (img.width > 685)
	{
		img.width = 685;
	}
}

function ShowFormatBytesStr(bytes)
{
	if(bytes > 1073741824)
	{
		document.write((Math.round((bytes/1073741824)*100)/100).toString()+' G');
	}
	else if(bytes > 1048576)
	{
		document.write((Math.round((bytes/1048576)*100)/100).toString()+' M');
	}
	else if(bytes > 1024)
	{
		document.write((Math.round((bytes/1024)*100)/100).toString()+' K');
	}
	else
	{
		document.write(bytes.toString()+' Bytes');
	}
}

function scrollHiddenDiv(div, scrollwidth)
{
	div.scrollLeft += scrollwidth;
}

function _attachEvent(obj, evt, func) {
	if(obj.addEventListener) {
		obj.addEventListener(evt, func, false);
	} else if(obj.attachEvent) {
		obj.attachEvent("on" + evt, func);
	}
}
