
function popBook(tPictureHref, Width, Height){
	
	if( typeof(Bookbox.center)=='undefined' ) Bookbox.init(); 
	Width = bookGetVisible(0)-100;
	Height = bookGetVisible(1)-40;
	
	currentImgSrc = tPictureHref;
	currentImgDescription = '';
	currentImgWidth = Width;
	currentImgHeight = Height;
	Bookbox.click();
}

function bookGetVisible(Mode) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if(Mode==0) return myWidth;
  if(Mode==1) return myHeight;
}



var Bookbox = {

	init: function(options){
		this.options = $extend({
			resizeDuration: 400,
			resizeTransition: false,	// default transition
			initialWidth: 450,
			initialHeight: 250,
			animateCaption: true,
			showCounter: true
		}, options || {});

		this.anchors = [];
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
    	this.eventPosition = this.position.bind(this);
		
    	this.overlay = new Element('div', {'id': 'lbOverlay'}).injectInside(document.body);
		this.overlay.onclick = this.close.bind(this);
			
		this.center = new Element('div', {'id': 'lbCenter', 'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight, 'marginLeft': -(this.options.initialWidth/2), 'display': 'none'}}).injectInside(document.body);
		this.image = new Element('iframe', {'id': 'lbImage', 'frameborder':'0'}).injectInside(this.center);
		
		/*
		this.bottomContainer = new Element('div', {'id': 'lbBottomContainer', 'styles': {'display': 'none'}}).injectInside(document.body);
    	this.bottom = new Element('div', {'id': 'lbBottom'}).injectInside(this.bottomContainer);
		this.caption = new Element('div', {'id': 'lbCaption'}).injectInside(this.bottom);
		
		this.closeLink = new Element('a', {'id': 'lbCloseLink', 'href': '#'}).injectInside(this.bottom);
		this.closeLink.onclick = this.close.bind(this);
		this.closeLink.innerHTML = (picClose);
		*/
		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
    		overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			resize: this.center.effects($extend({duration: this.options.resizeDuration, onComplete: nextEffect}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {})),
			image: this.image.effect('opacity', {duration: 500, onComplete: nextEffect})
    		//bottom: this.bottom.effect('margin-top', {duration: 400, onComplete: nextEffect})
		};

	},

	click: function(){
	   return this.show(currentImgSrc, currentImgDescription)
	},

	show: function(url, title){
		return this.open([[url, title]], 0);
	},

	open: function(images, imageNum){
		this.images = images;
    	this.position();
		this.setup(true);
		this.top = (window.getScrollTop() + (window.getHeight()-currentImgHeight) / 2);
		this.center.setStyles({top: this.top, display: ''});
    	this.fx.overlay.start(0.8);
		return this.changeImage(imageNum);
	},
	
	position: function(){
    	this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()});
		this.top = (window.getScrollTop() + (window.getHeight()-currentImgHeight) / 2);
		//this.image.style.width = currentImgWidth+'px';
		var wH = window.getHeight() - 40;
		this.image.style.height = wH + 'px';
		this.center.style.height = wH + 'px';
		var wW = window.getWidth() - 100;
	
		/*
		if( wW!=currentImgWidth ){
			this.image.style.width = wW + 'px';
			this.center.style.width = wW + 'px';
			this.center.style.marginLeft = -this.image.offsetWidth/2  + 'px';
		}
		*/
    },

	setup: function(open){
		var elements = $A(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(window.ie ? 'select' : 'embed'));
		elements.each(function(el){
			if (open) el.lbBackupStyle = el.style.visibility;
			el.style.visibility = open ? 'hidden' : el.lbBackupStyle;
		});
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;
	},

	keyboardListener: function(event){
		switch (event.keyCode){
			case 27: case 88: case 67: this.close(); break;
		}
	},

	changeImage: function(imageNum){
		if (this.step || (imageNum < 0) || (imageNum >= this.images.length)) return false;
		this.step = 1;
		this.activeImage = imageNum;

		this.fx.image.hide();
		this.center.className = 'lbLoading';
    	//this.bottomContainer.style.display = 'none';
		
		this.nextEffect();
		return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.center.className = '';
			this.image.src = this.images[this.activeImage][0];
			this.image.style.width = currentImgWidth+'px';
			this.image.style.height = currentImgHeight+'px';

			if (this.center.clientHeight != this.image.offsetHeight){
				this.fx.resize.start({height: this.image.offsetHeight});
				break;
			}
			this.step++;
		case 2:
			if (this.center.clientWidth != this.image.offsetWidth){
				this.fx.resize.start({width: this.image.offsetWidth, marginLeft: -this.image.offsetWidth/2});
				break;
			}
			this.step++;
		case 3:
			//this.bottomContainer.setStyles({top: this.top + 10 + this.center.clientHeight, height: 0, marginLeft: this.center.style.marginLeft, display: ''});
			this.fx.image.start(1);
			break;
		case 4:
			/*
			if (this.options.animateCaption){
				this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
				break;
			}
			*/
			//this.bottomContainer.style.height = '';
		case 5:
			if (this.activeImage) this.prevLink.style.display = '';
			if (this.activeImage != (this.images.length - 1)) this.nextLink.style.display = '';
			this.step = 0;
		}
	},

	close: function(){
		if (this.step < 0) return;
		this.step = -1;
		if (this.preload){
			this.preload.onload = Class.empty;
			this.preload = null;
		}
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display = 'none';
		//this.bottomContainer.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0)
		return false;
	}
};

