/**********************************************************************

  LICENSE FOR USE
  The contents of this file are copyright ©2008 Phil DeJarnett
  It is licensed for exclusive use by The Pentier Group on the
  website domains pentier.com and thepentiergroup.com, and any
  subdomains for those domains.
  
  The right to use the code listed below, in part or in full, in any
  other context is fully reserved.
  
  ANY AND ALL OTHER USE BY ANY PARTIES IS RESTRICTED WITHOUT EXPRESS
  CONSENT OF PHIL DEJARNETT.
  
  If you are interested in the using part or all of this code you may
  contact the author at http://www.overzealous.com/

**********************************************************************/
GalleryViewer = {
	
	isShowing: false,
	imagePreview: null,
	
	init: function() {
		var list = $("gallery-images-list");
		var items = list.childElements();
		for(var i=0; i<items.length; i++) {
			var item = items[i];
			var link = item.firstDescendant();
			var id = item.id.substr(6);
			link.observe('click', this.imageSelected.bindAsEventListener(this, id));
		}
		ip = $('image-preview');
		this.imagePreview = ip;
		
		if(ip.hasClassName('empty')) {
			ip.hide();
			ip.removeClassName('empty');
			// see if we should load in an image
			if(document.location.hash) {
				var id = parseInt(document.location.hash.substr(7));
				if(id > 0) {
					this.imageSelected(null, id);
				}
			}
		} else {
			this.isShowing = true;
		}
	},
	imageSelected: function(event, id) {
		if(event != null) {
			Event.stop(event);
		}
		
		var items = $("gallery-images-list").childElements();
		for(var i=0; i<items.length; i++) {
			items[i].removeClassName('selected');
		}
		$('image_'+id).addClassName('selected');
		
		new Ajax.Updater( this.imagePreview,
					'gallery-view.php',
					{
					 	method: 'get',
						parameters: { itemid: id },
						onComplete: this.imageDisplayed.bind(this, id)
					 });
	},
	imageDisplayed: function(id, response) {
		if(!this.isShowing) {
			// slide in preview
			new Effect.BlindDown(this.imagePreview, {
							duration: .5,
							scaleContent: false
						});
			this.isShowing = true;
		}
		document.location.href = '#image_'+id;
	},
	newImageChanged: function(effect) {
		this.imagePreview.update(this.fakeImagePreview.innerHTML);
		this.imagePreview.setOpacity(1);
		this.imagePreview.show();
		this.fakeImagePreview.setOpacity(0);
		this.fakeImagePreview.setStyle({
				height: 'auto',
				visibility: 'hidden'
			});
	}
}

Event.observe(document, 'dom:loaded', GalleryViewer.init.bind(GalleryViewer));
