if( typeof(_tnn) == "undefined")
	_tnn = {};
if( typeof(_tnn.wsg) == "undefined")
	_tnn.wsg = {};

var WebsiteGalleryPhotoDiv	= document.getElementById("WebsiteGalleryPhoto");
var PhotoThumbsObj	= document.getElementById("PhotoThumbs");
var PhotoThumbsListObj		= document.getElementById("PhotoThumbsList");
var PhotoThumbsLIObjs		= new Array();
var CurPageType	= false;

var AllowDisplay = true;
if(
	(RequireLogin && !StatusLogin) ||
	(RequireEmail && !StatusEmail)
	)
	AllowDisplay = false;

var PhotosCount = PicNums.length;

_tnn.wsg.SwitchPageType = function( PageType) {
	var Display	= ["WebsiteGalleryPhoto", "AddToProfile", "PhotoThumbsList"];
	var Thumbs	= ["PhotoThumbs"];
	CurPageType = PageType;
	switch( PageType) {
		case "Display":
			var Show = Display;
			var Hide = Thumbs;
		break;
		case "Thumbs":
			var Show = Thumbs;
			var Hide = Display;
		break;
	}
	for( var i in Hide) if( document.getElementById( Hide[ i ] ) ) document.getElementById( Hide[ i ] ).style.display = "none";
	for( var i in Show) if( document.getElementById( Show[ i ] ) ) document.getElementById( Show[ i ] ).style.display = "block";
}

/* --------- : Start of Photo directory : --------- */

_tnn.wsg.UpdateDirPaging = function( BaseID, CurPage ) {

	var BasePlace = document.getElementById( BaseID );
	BasePlace.innerHTML = "";
	BasePlace.className = "PagingLinks";

	var PagingUL = _tnn.AddElement( BasePlace,	"appendChild", "UL", {}, {} );
	var PagingLI = _tnn.AddElement( PagingUL,	"appendChild", "LI", Array(), Array() );

	var Pages = _tnn.CalculatePaging({
		"Limit"		: 7,
		"Per"		: PerPage,
		"ECount"	: PicCount,
		"Cur"		: CurPage,
		"Links"		: Array()
	});

	if( Pages["first"] !== false){
		var PagingLI = _tnn.AddElement( PagingUL, "appendChild", "LI", {}, {} );
		var EntryLink = _tnn.AddElement(
			PagingLI,
			"appendChild",
			"A",
			{	"href":"/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/Page.0.html",
				"onclick":"CurPage = 0; return _tnn.wsg.ShowThumbs();"
			},
			{}
		);
		EntryLink.innerHTML = "1...";
	}

	for( var i in Pages["pages"]) {
		var PagingLI = _tnn.AddElement( PagingUL, "appendChild", "LI", {}, {} );
		if( CurPage == Pages["pages"][ i ]) {
			PagingLI.innerHTML = Pages["pages"][ i ] + 1;
			continue;
		}
		var EntryLink = _tnn.AddElement( PagingLI, "appendChild", "A", { "href":"/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/Page." + Pages["pages"][ i ] + ".html", "onclick":"CurPage = " + Pages["pages"][ i ] + "; return _tnn.wsg.ShowThumbs();" }, {} );
		EntryLink.innerHTML = Pages["pages"][ i ] + 1;
	}

	if( Pages["last"] !== false){
		var PagingLI = _tnn.AddElement( PagingUL, "appendChild", "LI", {}, {} );
		var EntryLink = _tnn.AddElement( PagingLI, "appendChild", "A", { "href":"/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/Page." + Pages["last"] + ".html", "onclick":"CurPage = " + Pages["last"] + "; return _tnn.wsg.ShowThumbs();"	}, {} );
		EntryLink.innerHTML = "..." + (Pages["last"] + 1);
	}

	if( Pages["prev"] !== false){
		var PagingLI = _tnn.AddElement( PagingUL, "appendChild", "LI", {}, {} );
		var EntryLink = _tnn.AddElement( PagingLI, "appendChild", "A", { "href":"/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/Page." + Pages["prev"] + ".html", "onclick":"CurPage = " + Pages["prev"] + "; return _tnn.wsg.ShowThumbs();"	}, {} );
		EntryLink.innerHTML = "&lt; Previous";
	}

	if( Pages["next"] !== false){
		var PagingLI = _tnn.AddElement( PagingUL, "appendChild", "LI", {}, {} );
		var EntryLink = _tnn.AddElement( PagingLI, "appendChild", "A", { "href":"/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/Page." + Pages["next"] + ".html", "onclick":"CurPage = " + Pages["next"] + "; return _tnn.wsg.ShowThumbs();"	}, {} );
		EntryLink.innerHTML = "Next &gt;";
	}

}

_tnn.wsg.ShowThumbs = function () {

	_tnn.wsg.SwitchPageType( "Thumbs");

	PhotoThumbsObj.innerHTML = "";

	var Disps		= 0;
	var StartOffset	= parseInt( CurPage * PerPage );
	var CurOffset	= 0;

	_tnn.wsg.UpdateDirPaging( "PagingTop", CurPage );
	_tnn.wsg.UpdateDirPaging( "PagingBottom", CurPage );

	var ThumbsTable = _tnn.AddElement(
		PhotoThumbsObj,
		"appendChild",
		"TABLE",
		{	"id":"WebsiteGalleryPhotoDir",
			"class":"ListTable",
			"border":0,
			"cellpadding":4,
			"cellspacing":1
		},
		{}
	);

	var ThumbsTBody = _tnn.AddElement( ThumbsTable, "appendChild", "TBODY", {}, {} );

	for( var ImageSlot in PicNums ) {
		if( CurOffset++ < StartOffset )
			continue;
		if( Disps >= PerPage && PerPage > 0)
			break;
		var ImageNum = PicNums[ ImageSlot ];
		if( Disps % PageCols == 0)
			var ThumbRow = _tnn.AddElement( ThumbsTBody, "appendChild", "TR", {"class":((Disps % 2) ? "One" : "Two")}, {} );
		Disps++;
		var ThumbCell = _tnn.AddElement( ThumbRow, "appendChild", "TD", {}, {} );

		var tmp = new Array();
		tmp["href"] = "/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/" + ImageNum + ".html";
		if( AllowDisplay)
			tmp["onclick"] = "return _tnn.wsg.ShowPic(" + ImageNum + ");";
		var ThumbLink = _tnn.AddElement(
			ThumbCell,
			"appendChild",
			"A",
			tmp,
			{}
		);

		var ThumbImage	= _tnn.AddElement(
			ThumbLink,
			"appendChild",
			"IMG",
			{	"src":"/RRN/dyn/wsg/" + IDPath + "/photos/" + ThumbPath + "/" + ImageNum + ".jpg"
			},
			{}
		);
	}

	return false;
}

/* --------- : End of Photo directory : --------- */


/* --------- : Start of Photo display : --------- */

_tnn.wsg.ImageLoadCheck = function( ImageID ) {
	var ImgSrc = document.getElementById( ImageID );
	if( !ImgSrc)
		return false;
	return (ImgSrc.width > 0) ? true : false;
}

_tnn.wsg.LoginCheck = function() {
	var ImageSuccess = _tnn.wsg.ImageLoadCheck( "WebsiteGalleryPhotoIMG" );
	if( ImageSuccess == false) {
		if( CurPageType == "Display")
			document.location.href = "/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/" + CurPicNum + ".html";
		else
			document.location.href = "/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/Page." + CurPage + ".html";
	}
}

_tnn.wsg.FindNextPrev = function( PicNum ) {
	NextPicNum	= 0;
	PrevPicNum	= 0;

	var LastPicNum	= 0;
	var NextFound	= false;
	var PrevFound	= false;
	var PicCount	= 0

	for( var PcN in PicNums ) {
		PicCount++;
		if( PicNums[ PcN ] == PicNum) {
			PrevPicNum	= LastPicNum;
			NextSlot	= parseInt(PcN) + 1;
			PrevFound	= true;
			if( PicNums[ NextSlot ]) {
				NextPicNum	= PicNums[ NextSlot ];
				NextFound	= true;
				break;
			}
		}
		LastPicNum = PicNums[ PcN ];
	}
	if( PerPage < 1)
		var tmp = 0;
	else
		var tmp = parseInt( (PicCount-1) / PerPage );
	return {
		"CurPage":tmp
	};
}

_tnn.wsg.CreatePhotoThumbsList = function() {

	var Disps		= 0;
	var StartOffset	= 0;
	var CurOffset	= 0;

	for( var n in PicNums ) {
		if( PicNums[ n ] == CurPicNum )
			break;
		StartOffset++;
	}

	StartOffset -= parseInt( ListThumbCount / 2);

	if( (StartOffset + ListThumbCount) > PhotosCount)
		StartOffset = PhotosCount - ListThumbCount;

	if( !window.PhotoThumbsListULObj) {
		PhotoThumbsListObj.innerHTML = "";
		PhotoThumbsListULObj = _tnn.AddElement(
			PhotoThumbsListObj,
			"appendChild",
			"UL",
			{},
			{}
		);
	}

	for( var ImageSlot in PicNums ) {
		if( (CurOffset++ < StartOffset) || (Disps >= ListThumbCount) ) {
			if( PhotoThumbsLIObjs[ CurOffset ])
				PhotoThumbsLIObjs[ CurOffset ].style.display = "none";
			continue;
		}

		var ImageNum = PicNums[ ImageSlot ];
		Disps++;

		if( PhotoThumbsLIObjs[ CurOffset ]) {
			PhotoThumbsLIObjs[ CurOffset ].style.display = "inline";
			PhotoThumbsLIObjs[ CurOffset ].className = ( ( (CurPicNum == ImageNum) ) ? "Current" : "" );
			continue;
		}

		var tmp = new Array();
		tmp["id"] = "WSGListThumb_" + CurOffset;
		if( (CurPicNum == ImageNum) )
			tmp["class"] = "Current";

		var AfterFound = false;
		FindAfter:
		for( var i in PhotoThumbsLIObjs) {
			if( i > CurOffset) {
				if( AfterFound > 0) {
					if( i < AfterFound)
						var AfterFound = i;
				} else
					var AfterFound = i;
			}
		}

		if( AfterFound > 0) {
			PhotoThumbsLIObjs[ CurOffset ] = _tnn.AddElement(
				PhotoThumbsListULObj,
				"insertBefore",
				"LI",
				tmp,
				{},
				PhotoThumbsLIObjs[ AfterFound ]
			);
		} else {
			PhotoThumbsLIObjs[ CurOffset ] = _tnn.AddElement(
				PhotoThumbsListULObj,
				"appendChild",
				"LI",
				tmp,
				{}
			);
		}

		var tmp = new Array();
		tmp["href"] = "/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/" + ImageNum + ".html";
		if( AllowDisplay)
			tmp["onclick"] = "return _tnn.wsg.ShowPic(" + ImageNum + ");";

		var ThumbLink = _tnn.AddElement(
			PhotoThumbsLIObjs[ CurOffset ],
			"appendChild",
			"A",
			tmp,
			{}
		);

		var ThumbImage	= _tnn.AddElement(
			ThumbLink,
			"appendChild",
			"IMG",
			{	"src":"/RRN/dyn/wsg/" + IDPath + "/photos/" + ListThumbPath + "/" + ImageNum + ".jpg"
			},
			{}
		);

	}

	return false;
}

_tnn.wsg.UpdateDisplayPaging = function( BaseID, CurPh, CurPicNum ) {
	var BasePlace = document.getElementById( BaseID );
	BasePlace.innerHTML = "";
	BasePlace.className = "PageLinks";

	LinkNums = _tnn.wsg.FindNextPrev( CurPh, CurPicNum );

	BasePlace.PagingElems = new Array();
	BasePlace.PagingElems["UL"]		= _tnn.AddElement(	BasePlace,						"appendChild",	"UL",	{},	{} );
	BasePlace.PagingElems["PrevLI"]	= _tnn.AddElement(	BasePlace.PagingElems["UL"],	"appendChild",	"LI",	Array(),	Array()	);

	if( PrevPicNum > 0) {
		var tmp = new Array();
		tmp["href"] = "/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/" + PrevPicNum + ".html";
		if( AllowDisplay)
			tmp["onclick"] = "return _tnn.wsg.ShowPic(" + PrevPicNum + ");";

		BasePlace.PagingElems["PrevA"] = _tnn.AddElement(
			BasePlace.PagingElems["PrevLI"],
			"appendChild",
			"A",
			tmp,
			{}
		);
		BasePlace.PagingElems["PrevA"].innerHTML = "Prev &laquo;";
	}

	BasePlace.PagingElems["PageLI"]	= _tnn.AddElement( BasePlace.PagingElems["UL"], "appendChild", "LI", Array(), Array() );
	BasePlace.PagingElems["PageA"]	= _tnn.AddElement(
		BasePlace.PagingElems["PageLI"],
		"appendChild",
		"A",
		{	"href":"/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/Page." + LinkNums["CurPage"] + ".html",
			"onclick":"CurPage = " + LinkNums["CurPage"] + "; return _tnn.wsg.ShowThumbs();"
		},
		{}
	);
	BasePlace.PagingElems["PageA"].innerHTML = "View page";

	BasePlace.PagingElems["NextLI"] = _tnn.AddElement( BasePlace.PagingElems["UL"], "appendChild", "LI", {}, {} );

	if( NextPicNum > 0) {
		var tmp = new Array();
		tmp["href"] = "/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/" + NextPicNum + ".html";
		if( AllowDisplay)
			tmp["onclick"] = "return _tnn.wsg.ShowPic(" + NextPicNum + ");";
		BasePlace.PagingElems["NextA"] = _tnn.AddElement(
			BasePlace.PagingElems["NextLI"],
			"appendChild",
			"A",
			tmp,
			{}
		);
		BasePlace.PagingElems["NextA"].innerHTML = "&raquo; Next";
	}
}

_tnn.wsg.ShowPic = function( PicNum ) {
	if( !AllowDisplay)
		return true;
	if( PicNum == 0)	return false;
	if( !WebsiteGalleryPhotoDiv)	return false;

	_tnn.wsg.SwitchPageType( "Display");

	WebsiteGalleryPhotoDiv.innerHTML = "";

	CurPicNum = PicNum;

	_tnn.wsg.UpdateDisplayPaging( "PagingTop", PicNum );
	_tnn.wsg.UpdateDisplayPaging( "PagingBottom", PicNum );

	AddToProfileObj = document.getElementById("AddToProfile");
	if( AddToProfileObj) {
		AddToProfileObj.innerHTML = "";
		var tmp = _tnn.AddElement(	AddToProfileObj,	"appendChild",	"UL",	{},	{} );
		var tmp	= _tnn.AddElement(	tmp,				"appendChild",	"LI",	Array(),	Array()	);
		var tmp = _tnn.AddElement(
			tmp,
			"appendChild",
			"A",
			{	"href":"/RRN/accounts/profile/?IDX=photo.wsg_copy&WebsiteGalleryID=" + WebsiteGalleryID + "&PicNum=" + PicNum
			},
			{}
		);
		tmp.innerHTML = "Add photo my profile";
	}

	if( NextPicNum ) {
		var tmp = new Array();
		tmp["href"] = "/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/" + NextPicNum + ".html";
		if( AllowDisplay)
			tmp["onclick"] = "return _tnn.wsg.ShowPic(" + NextPicNum + ");";
		var Attribs = tmp;
	} else {
		var Attribs = {
			"href":"/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/Page." + CurPage + ".html"
		};
	}

	var PicLink = _tnn.AddElement( WebsiteGalleryPhotoDiv, "appendChild", "A", Attribs, {} );

	_tnn.AddElement(
		PicLink,
		"appendChild",
		"IMG",
		{	"src":"/RRN/wsg/" + HrefTitle + "." + WebsiteGalleryID + "/" + PicNum + ".jpg",
			"onload":"_tnn.wsg.LoginCheck();",
			"id":"WebsiteGalleryPhotoIMG"
		},
		{}
	);

	_tnn.wsg.CreatePhotoThumbsList();

	return false;
}

/* --------- : End of Photo display : --------- */

top.window.focus();
document.body.onkeydown = function(){
	if( event.ctrlKey) {
		switch( event.keyCode) {
			case 37:
				if( CurPage > 0 ) {
					CurPage--;
					_tnn.wsg.ShowThumbs( CurPage );
				}
			break;
			case 39:
				if( CurPage < PageCount-1 ) {
					CurPage++;
					_tnn.wsg.ShowThumbs( CurPage );
				}
			break;
		}
	}
};