//Load dojo 1.4 from the Google CDN
google.load("dojo", "1.4");

combinationGallery = {
	glightbox: null,
	slideshowInterval: null,
	connections: [],
	galleryContainer: null,
	gallery: null,
	dataStore: null,
	closeInfo: null,
	
	startSlideshow: function(e)
	{
		dojo.stopEvent(e);
		combinationGallery.connections.push(dojo.connect(combinationGallery.glightbox.nextNode, "onclick", combinationGallery.cancelSlideshow));
		combinationGallery.connections.push(dojo.connect(combinationGallery.glightbox.prevNode, "onclick", combinationGallery.cancelSlideshow));
		combinationGallery.connections.push(dojo.connect(combinationGallery.glightbox.closeNode, "onclick", combinationGallery.cancelSlideshow));
		combinationGallery.connections.push(dojo.connect(document.documentElement,"onkeypress",function(e) {
			var key = (e.charCode == dojo.keys.SPACE ? dojo.keys.SPACE : e.keyCode);
			switch(key){
				case dojo.keys.ESCAPE:
				case dojo.keys.DOWN_ARROW:
				case dojo.keys.RIGHT_ARROW:
				case 78:
				case dojo.keys.UP_ARROW:
				case dojo.keys.LEFT_ARROW:
				case 80:
					combinationGallery.cancelSlideshow();
			}
		}));
		combinationGallery.glightbox.show({group:"gallery",href:combinationGallery.dataStore._arrayOfAllItems[combinationGallery.gallery.slideShow.imageIndex].imageUrl,title:combinationGallery.dataStore._arrayOfAllItems[combinationGallery.gallery.slideShow.imageIndex].title});
		combinationGallery.slideshowInterval = setInterval(function() {
			combinationGallery.glightbox._nextImage();
		}, 8000);
		return false;
	},//startSlideshow
	
	cancelSlideshow: function()
	{
		while (combinationGallery.connections.length>0) dojo.disconnect(combinationGallery.connections.pop());
		if (combinationGallery.slideshowInterval !== null) {
			clearInterval(combinationGallery.slideshowInterval);
		}
		combinationGallery.slideshowInterval=null;
	},//cancelSlideshow
	
	createDataStore: function()
	{
		combinationGallery.dataStore = new dojo.data.ItemFileReadStore({data:album});
	},//createDataStore
	
	createGallery: function()
	{
		combinationGallery.gallery = new dojox.image.Gallery();
		combinationGallery.gallery.setDataStore(combinationGallery.dataStore,{query:{}});
		combinationGallery.gallery.placeAt(combinationGallery.galleryContainer);
		
		combinationGallery.gallery.thumbPicker.size = 675;
		combinationGallery.gallery.thumbPicker.thumbWidth = 119;
		combinationGallery.gallery.thumbPicker.useLoadNotifier = false;
		
		//Clear the alt and title attributes of the thumbs since there is some HTML in it
		//which is how we are passing the title and the caption of the image
		var thumbs = dojo.query("> div img",combinationGallery.gallery.thumbPicker.thumbsNode);
		for(var i=0; i<thumbs.length; i++)
		{
			thumbs[i].alt = "";
			thumbs[i].title = "";
		}
		
		//Subscribe to a Dojo topic that is published to when a thumbnail is clicked.
		dojo.subscribe(combinationGallery.gallery.thumbPicker.getClickTopicName(),function(image){
			//Reset the styles for all the images in the thumbnail picker
			var thumbContainers = dojo.query("> div",combinationGallery.gallery.thumbPicker.thumbsNode);
			
			for(var i=0; i<thumbContainers.length; i++)
			{
				if(dojo.hasClass(thumbContainers[i],"highlight"))
					dojo.removeClass(thumbContainers[i],"highlight");
			}
			
			//Get a reference to the clicked image container and set the highlight on it
			var queryString = "div#img_dojox_image_ThumbnailPicker_0_" + Number(image.index);
			var imgNode = dojo.query(queryString,combinationGallery.gallery.thumbPicker.thumbsNode)[0];
			dojo.addClass(imgNode,"highlight");
		});
		
		//Make clicks on the image information hide itself
		dojo.query(".slideShowTitle")
			.connect("onclick",function(){
			dojo.toggleClass(this,"hide");
		});
		
		//The thumbnail resize function doesn't take margins into account.
		combinationGallery.gallery.thumbPicker.resize = function(dim){
			var sizeParam = this.isHorizontal ? "w": "h";
			var total = 0;
			var fudge;
	
			if(this._thumbs.length > 0 && dojo.marginBox(this._thumbs[0]).w == 0){
				// Skip the resize if the widget is not visible
				return;
			}
	
			// Calculate the complete size of the thumbnails
			if ((this._thumbs.length > 0) && (this._thumbs[0].hasChildNodes())) {
				//fudge=dojo.marginBox(this._thumbs[0])[sizeParam]-dojo.marginBox(this._thumbs[0].firstChild)[sizeParam];
				fudge = 20;
			} else {
				fudge= 20;
			}
	
			dojo.forEach(this._thumbs, dojo.hitch(this, function(imgContainer){
				var mb = dojo.marginBox(imgContainer.firstChild);
				var size = mb[sizeParam];
				total += (Number(size) + fudge);
	
				if(this.useLoadNotifier && mb.w > 0){
					dojo.style(imgContainer.lastChild, "width", (mb.w - 4) + "px");
				}
				dojo.style(imgContainer, "width", mb.w + "px");
			}));
	
			dojo.style(this.thumbsNode, this._sizeProperty, total + "px");
			this._updateNavControls();
		};
		
	}//createGallery
};

//This callback fires when page load is complete
google.setOnLoadCallback(function() {
	if (album) {
		dojo.require("dijit._Widget");
		dojo.require("dojo.data.ItemFileReadStore");
		dojo.require("dojox.image.Gallery");
		dojo.require("dojox.image.ThumbnailPicker");
		dojo.require("dojox.image.SlideShow");
		dojo.require("dojox.image.Lightbox");
			 
		function init()
		{
			document.body.className='soria';
			
			//Instantiate the lightbox
			combinationGallery.glightbox=new dojox.image.LightboxDialog({});
			combinationGallery.glightbox.startup();
			for(var i=0;i<album.items.length;i++)
			{
				combinationGallery.glightbox.addImage({title:album.items[i].title,href:album.items[i].imageUrl},"gallery");
			}
			
			//Handle the start slideshow link
			dojo.connect(dojo.byId('startslideshow'),'onclick',combinationGallery.startSlideshow);
			
			//Get a reference to the photo gallery container
			combinationGallery.galleryContainer =  dojo.byId("combination-gallery");
			
			//Create the data store
			combinationGallery.createDataStore();
			
			//Create the photo gallery
			combinationGallery.createGallery();
			
		}//init
	
		dojo.addOnLoad(init);
	}	
});//google.setOnLoadCallback