var wsGalleryService = "wsGalleryService.php";
var wsGetCollections = wsGalleryService + "/getCollectionsList";
var wsGetCollectionTypes = wsGalleryService + "/getCollectionTypesList";

var qsArgs = PhotoClans.Utilities.Browser.QueryStringArgs();
var qsCurrent = (typeof(qsArgs.current) == "string") ? "&current=" + qsArgs.current : "";

var dsCollections = null;
var dsCollectionTypes = null;
var dsGalleries = null;

// var dsMyGalleries = null;

var theGalleryService = null;
var collectionPanels = new Array();

function addCollectionPanel(panelId, collectionId) {
	if (isFinite(panelId))
	{
		collectionPanels[parseInt(panelId)] = collectionId;
	}
}

function getCollectionTypes() {
	dsCollectionTypes = new Spry.Data.JSONDataSet(wsGetCollectionTypes, 
							{
								path: "d.collectionTypes", 
								useCache:false,
								distinctOnLoad:true
							});
    dsCollectionTypes.addObserver(
	        {
				onPostLoad: function() {
					Spry.$$("#collectionTypes").setStyle("display:block");
//					setTimeout(function() { dumpObject("collection types", dsCollectionTypes); },1000);
//					var loadingDiv = document.getElementById("contentLoading");
//					if (loadingDiv) {
//						loadingDiv.style.display = "none";
//					} else alert("no loading div?");
//					
//					var contentDiv = document.getElementById("contentContainer");
//					if (contentDiv) {
//						contentDiv.style.display = "block";
//					} else alert("no content div?");
					return;
				}
			}
		);
	dsCollectionTypes.loadData();
}

function getCollections() {
	dsCollections = new Spry.Data.JSONDataSet(wsGetCollections, 
							{
								path: "d.collections", 
								useCache:false,
								distinctOnLoad:true
							});
    dsCollections.addObserver(
	        {
				onPostLoad: function() {
					var loadingDiv = document.getElementById("contentLoading");
					if (loadingDiv) {
						loadingDiv.style.display = "none";
					} else alert("no loading div?");
					
					var contentDiv = document.getElementById("contentContainer");
					if (contentDiv) {
						contentDiv.style.display = "block";
					}
					return;
				}
			}
		);

		regPanelObserver = {};
		regPanelObserver.onPostUpdate = function()
            {
				InitSlidingPanels();
				return;
			};
	Spry.Data.Region.addObserver("contentContainer", regPanelObserver);

	dsGalleries = new Spry.Data.NestedJSONDataSet(dsCollections, "galleries");							

	dsCollections.loadData();
}


function panelToCollection(panelNumber) {
	if (isFinite(panelNumber)) {
		var panNum = parseInt(panelNumber);
		if ((panNum >=0) && (panNum < collectionPanels.length))
		{
			return collectionPanels[panNum];
		}
	}
	return '';
}
function showCollection(panelNumber) {
// var row = ds.getRowByRowNumber(10, true);
// update row.galleries	
	var collId = panelToCollection(panelNumber);
	if (collId.length) {
		sprySlidingPanels['collectionsPanel'].showPanel("collection_" + collId);
		sprySlidingPanels['galleriesPanel'].showPanel(panelNumber);
		removeSimpleViewer("ViewerContent") 
	}
}

function getCollectionGalleries(panelId, collectionId) {
	if (isFinite(panelId)) {
//		alert("request navbar " + collectionId);
//		var panNum = parseInt(panelNumber);	
		theGalleryService.getCollectionGalleryNavBar(collectionId, "ViewerContent", "galleryView.php",
			function(results){
			if (results.errorCode) {
				alert("Failed to create the gallery navigation area.\nReport this error code to the webmaster: " +
				results.errorCode +
				"\nMessage: " +
				results.errorMessage);
			}
			else {
// dumpObject("results for " + collectionId,results);
				var navBarId = "galleries_" + results.collectionId;
// alert("set inner html for " + results.collectionId + ":\n" + results.navBar);
				var navBar = document.getElementById(navBarId);
				if (navBar) {
					navBar.innerHTML = results.navBar;
				}
				else {
					alert("failed to find nav bar element " + navBarId + " on this page.");
				}
			}
			
		});
	}
}

function showGallery(collectionId, galleryId) {
	
}

function galleryImageListItem(galleryId, imageId, imageTitle, imageDescription, cs) {
   var imageListItem = "<li class=\"galleryThumb\" id=\"thmImgItem" + imageId + "\">"
								+ "<img id=\"thmImg" + imageId + "\" src=\"getImage.php?t=t"
								+ "&cs=" + cs
								+ "&g=" + galleryId
								+ "&i=" + imageId + "\""
								+ " title=\"" + imageTitle + "\""
								+ " onclick=\"return showImageDetailsEditor(event,'" + galleryId + "','" + imageId + "','" + escape(imageTitle) + "','" + escape(imageDescription) + "','" + cs + "');\""
								+ "/>"
//								+ "<br/><input onclick=\"window.status='clicked: ' + this.value;return true;\" name=\"thumbnail\" type=\"radio\" value=\"" + imageId + "\" />"
								+ "</li>";
	return imageListItem;
}


function insertGalleryImages(galleryId, images, cs) {
		var imageList = document.getElementById("photoList" + galleryId);
		if (imageList) {

			for (var i=0; i < images.length; i++) {
				image = images[i];
				imageList.innerHTML += galleryImageListItem(galleryId, image.id, image.title, image.description, cs);
			}
			CreateSquishableBySelector(".Squishable", squishableAttributes);
		}
		else alert("could not find imagelist?!");
}



function getGalleryImages(galleryId) {
	
	if (galleryId.length > 0) {
		communicationInProgress(true);	
		theGalleryService.getGalleryImages(myUserId, galleryId,
			function(results) {
				communicationInProgress(false);	
				if (results.errorCode) {
					alert("Error getting gallery images, Error code: " + results.errorCode + "\n" + results.errorMessage);
				}
				else {
					var numImages = results.images.length;
	//				alert("got back " + numImages + " images for gallery " + galleryId);
					insertGalleryImages(galleryId, results.images, results.cs);
					
				}
			});
	}
}


function communicationInProgress(inProgress) {
    // * This empties the log and shows the spinning indicator
    var log = $('log_res');
	
	if (inProgress) log.addClass('ajax-loading');
	else log.removeClass('ajax-loading');
	
	return true;
}



/*
**  Spry initializations
**
**
*/
var g_PublishDate;



	Spry.Utils.addLoadListener(function() {
		theGalleryService = new GalleryService();
		if (qsArgs['ct'] == undefined) {
			getCollectionTypes();
		}
		getCollections();

//		if (window.location.hash) {
//			var hashId = window.location.hash.substr(1);
//			if (spryPanels[hashId]) {
//				spryPanels[hashId].open();
//				window.location.hash = hashId;
//			}
//			setTimeout(function(){
//				Spry.$$(window.location.hash).forEach(function(a){
//					ss_scrollToElement(a, hashId)
//				});
//			}, 250);
//		}
		if (document.getElementById("ViewerContent")) {
			oldOnResize = document.body.onresize;
			document.body.onresize = function(){
				resizeViewer("ViewerContent");
			}
			resizeViewer("ViewerContent");
		}
});



function syncAnnouncementType()
{
    var row = dsMyGalleries.getCurrentRow();
    if (row) {
    var ansType = row["@type"];

//    var listbox = document.getElementById('mtgsType');
//    if (listbox) {
//        if (ansType) {
//            setSelectedIndex(listbox, ansType);
//            }
//        }
    }
}