/* ##############################################
Copyright 2006 	Daniel Skinner
Email: skinner@destiny-denied.co.uk
################################################ */
var ImageViewer = Class.create();
ImageViewer.prototype = {
	prefix: "image_viewer_",
	initialize: function (c,options) {
		this.setOptions(options);
		this.createViewer();
		this.closeEvent=this.close.bindAsEventListener(this);
		this.openEvent= function(image) { return this.display.bindAsEventListener(this, image); };
		$A($$("img."+c)).each(function(image) {
			var temp={};
			temp.caption = (image.getAttribute('title') || image.getAttribute('alt')) || this.options.caption;
			temp.src=image.parentNode.getAttribute('href') || image.getAttribute('src');
			temp.ele=image;
			var theImage = new Image();
			Event.observe(theImage,'load',this.applyBehaviours.bindAsEventListener(this,temp,theImage));	
			theImage.src=temp.src;	
		}.bind(this));	
	},
	//Private Initialisation
	setOptions: function(options) {
		//provide default options if not otherwise specified
		this.options = Object.extend({
			openEffect: 'appear',
			closeEffect:'fade',
			caption: 'default-caption'
		},(options || {}));
	},
	createViewer: function() {
		var p = this.prefix;
		if (!$(p+"container")) {
			var image = Builder.node('img',{'id':p+'image', 'style':'display:block;'});	
			var caption = Builder.node('span',{'class':'caption','id':p+'caption'});	
			var close = Builder.node('div',{'class':'close_button','id':p+'close_button'});
			var text = Builder.node('div',{'class':'text','id':p+'text', 'style':'display:none;'},[caption,close]);
			var container = Builder.node('div',{'class':'container','id':p+'container',style:'display:none'},[Builder.node('div',{'class':'content','id':p+'content'},[image,text])]);			
			document.body.appendChild(container);
		}
		if (!$(p+'wrap')) document.body.appendChild(Builder.node('div',{'class':'wrap','id':p+'wrap','style':'display:none;'}));
	},
	applyBehaviours: function(e,image,theImage) {
		image.width = theImage.width;
		image.height = theImage.height;
		var e = image.ele;
		e.style.cursor='pointer';
		e.style.cursor='hand';
		Event.observe(e.parentNode, 'click', this.openEvent(image));
	},
	buildViewer: function(image) {
		var p = this.prefix,px = 'px';
		$(p+"caption").innerHTML = image.caption;
		$(p+"image").src = image.src;
		$(p+"text").style.width = image.width+px;
		$(p+"content").style.width = image.width+px;
	},
	display: function(e,image) {
		Event.stopObserving(image.ele.parentNode, 'click', this.openEvent(image));
		var p = this.prefix;
		this.buildViewer(image);
		if (!this.isOpening) {
			this.isOpening=true;
			var tidyUp = function() {
				this.isOpening=false;
				this.isClosing=false;
				Event.observe(image.ele.parentNode, 'click', this.openEvent(image));
			}
			new Effect.Appear($(p+'wrap'),{duration:0.4, to:0.5, queue:{position: 'start', scope:'sc1',limit:3}});
			switch (this.options.openEffect) {
			case "slide":
				new Effect.SlideDown($(p+'container'),{to:1.0, queue:{position: 'end', scope:'sc1',limit:3}});
				break
			case "blind":
				new Effect.BlindDown($(p+'container'),{to:1.0, queue:{position: 'end', scope:'sc1',limit:3}});
				break
			default:
				new Effect.Appear($(p+'container'),{to:1.0, queue:{position: 'end', scope:'sc1',limit:3}});
				break
			}
			new Effect.Appear($(p+"text"),{to:0.6, afterFinish:tidyUp.bind(this), queue:{position: 'end', scope:'sc1',limit:3}});
		}
		Event.stopObserving( $(p+'wrap'), 'click', this.closeEvent);
		Event.stopObserving( $(p+'close_button'), 'click', this.closeEvent);
		Event.observe( $(p+'wrap'), 'click', this.closeEvent);
		Event.observe( $(p+'close_button'), 'click', this.closeEvent);
		Event.stop(e); //prevent default action - i.e. the hyperlink
	},
	close: function(e) {
		var p = this.prefix;
		if (!this.isClosing && !this.isOpening) {
			this.isClosing=true;
			var tidyUp = function() {
				$(p+"text").style.display="none";
				this.isClosing=false;
				this.isOpening=false;
			}
			switch (this.options.closeEffect) {
			case "slide":
				new Effect.SlideUp($(p+'container'),{fps:60, queue:{position:'start', scope:'sc1', limit:3}});
				break	
			case "blind":
				new Effect.BlindUp($(p+'container'),{fps:60, to:1.0, queue:{position: 'end', scope:'sc1',limit:3}});
				break
			case "fold":
				new Effect.Fold($(p+'container'),{fps:100, queue:{position:'start', scope:'sc1', limit:3}});
				break	
			case "shrink":
				new Effect.Shrink($(p+'container'),{queue:{position:'start', scope:'sc1', limit:3}});
				break	
			default:
				new Effect.Fade($(p+"text"),{fps:60, duration:0.4, queue:{position:'start', scope:'sc1', limit:3}});
				new Effect.Fade($(p+'container'),{fps:60, duration:0.6, queue:{position:'end', scope:'sc1', limit:3}});	
				break
			}
			new Effect.Fade($(p+'wrap'),{fps:60, duration:0.4, afterFinish:tidyUp.bind(this),queue:{position:'end', scope:'sc1', limit:3}});
			Event.stopObserving( $(p+'wrap'), 'click', this.closeEvent);
			Event.stopObserving( $(p+'close_button'), 'click', this.closeEvent);
		}		

	}
};