/* ##########################################################################
Copyright 2007 	Daniel Skinner
Contact: www.destiny-denied.co.uk/contact/

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
########################################################################### */
var ImageViewer = Class.create();
ImageViewer.prototype = {
	prefix: "image_viewer_",
	initialize: function (c,options) {
		this.setOptions(options);
    if (this.options.CSSPath) document.getElementsByTagName('head')[0].appendChild(Builder.node('link',{'rel':'stylesheet','type':'text/css','href':this.options.CSSPath}));
		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');
			if(image.parentNode.getAttribute('href')) {  image.parentNode.setAttribute('href','#') || image.parentNode.removeAttribute('href');  } //prevent links
			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: '',
			CSSPath:''
		},(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'},'close');
			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) {
		 var ScrollTop =document.body.scrollTop|| document.documentElement.scrollTop;
	  $(this.prefix+'wrap').style.height=document.body.scrollHeight+"px";
		$(this.prefix+'container').style.top=ScrollTop+50+"px";
		var p = this.prefix;
		this.buildViewer(image);
		if (!this.isOpening) {
			this.isOpening=true;
			var tidyUp = function() {
  			this.isOpening=false;
  			this.isClosing=false;
			}
			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.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);
		}		

	}
};
