var NutBox = {

	init: function (options) {
		// init default options
		this.options = Object.extend({
			resizeDuration: 200,	// Duration of scale effects in ms. total is this number * 2.5
			initialWidth: 25,		// Initial width of the box (px)
			initialHeight: 25,		// Initial height of the box (px)
			defaultWidth: 550,		// Default width of the box (px)
			defaultHeight: 400,	// Default height of the box (px)
			defaultContentsElement: 'nutboxElem', //Default div element to copy HTML from for the content
			defaultHiddenElement: 'nutboxElemHidden' //Default div element to designate the hidden part of content (during special effects)
		}, options || {});


		this.transport=new Element('div').setProperty('id', 'nutboxTransport').setStyles({display: 'none'});

		this.overlay = new Element('div').setProperty('id', 'nutboxOverlay').injectInside(document.body);
		this.center = new Element('div').setProperty('id', 'nutboxCenter').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);

		this.bottomContainer = new Element('div').setProperty('id', 'nutboxBottomContainer').setStyle('display', 'none').injectInside(document.body);
		this.bottom = new Element('div').setProperty('id', 'nutboxBottom').injectInside(this.bottomContainer);
		//this.closeLink=new Element('a').setProperties({id: 'nutboxCloseLink', href: '#'}).injectInside(this.bottom).onclick = this.close.bind(this);
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: this.options.resizeDuration }).hide(),
			center: this.center.effects({duration: this.options.resizeDuration , transition: Fx.Transitions.sineInOut, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: this.options.resizeDuration/2 })
		};
		
		//Make ready for opeing 
		this.hookLinks();

	},

	hookLinks: function(){
		var c=0;
		$A($$('a')).each(function(el){
			if(el.rel && el.href){
				//Hook open buttons
				if(el.rel.test('^nutboxOpen', 'i')){
					c++;
					el.addEvent('click', function (e) {
						e = new Event(e);
						e.stop();
						this.clickOpen(el);
					}.bind(this));
				}
				 //Hook close buttons
				else if(el.rel.test('^nutboxClose', 'i')){
					c++;
					el.addEvent('click', function (e) {
						e = new Event(e);
						e.stop();
						this.clickClose(el);
					}.bind(this));
				}
			}
		}, this);
	},

	hideStuff: function(st,n){
		if(null==this.hiddenStuff){
			this.hiddenStuff=[];
			var c=0;
			$A($$('div')).each(function(el){
				//Find hidden stuff
				if(el.className && el.className.test(n, 'i')){
					c++;
					this.hiddenStuff.push(el);
				}
			}, this);
			//alert("Found "+c+" hidden elements");
		}
		var newState=st?'none':'';
		var j;
		var c=0;
		for(j=0;j<this.hiddenStuff.length;j++){
			var el=this.hiddenStuff[j];
			el.style.display=newState;
			c++;
		}
		//alert("Set "+c+" hidden elements to " + st);
	},


	clickOpen: function(link) {
		this.open (link.href, link.title, link.rel);
		return false;
	},

	clickClose: function(link) {
		this.close();
		return false;
	},
	
	setup: function(){
		var aDim = this.rel.match(/[0-9a-zA-Z\-\_]+/g);
		//Skipping first token
		if(aDim){
			this.options.contentsWidth=	(aDim[1] > 0) ? aDim[1] : this.options.defaultWidth;
			this.options.contentsHeight=	(aDim[2] > 0) ? aDim[2] : this.options.defaultHeight;
			this.options.contentsElement=	(null!=aDim[3]) ? aDim[3] : this.options.defaultContentsElement;
			this.options.hiddenElement=	(null!=aDim[4]) ? aDim[4] : this.options.defaultHiddenElement;
		}
	},

	open: function(sLinkHref, sLinkTitle, sLinkRel) {
		this.href = sLinkHref;
		this.title = sLinkTitle;
		this.rel = sLinkRel;
		this.overlay.setStyles({'top': (window.getScrollTop()-1000)+'px', 'height': (((document.height !== undefined) ? document.height : document.body.offsetHeight)+1000) /*window.getHeight() */+'px'});
		this.setup();
		
		var useDom=true;
		
		var virgin=false;
		//Make sure we have the player node (hopefully called only once)
		if(null==this.player){
			/*
			//Handle the hidden element
			var el=document.getElementById(this.options.hiddenElement);
			if((el)&&(this.options.defaultHiddenElement !=el)){
				//Get hold of it
				this.hidden=el;
			}
			else alert("Could not get hidden part of player");
			*/
			//this.hideStuff(true,this.options.hiddenElement);
			//Handle the main element
			var el=document.getElementById(this.options.contentsElement);
			if((el)&&(this.options.defaultElement !=el)){
				//Get hold of it
				this.player=el;
				//Show hidden stuff
				this.hideStuff(false,this.options.hiddenElement);
				//Make it visible
				this.player.style.display='';
				//Measure it
				var temp = new Element('div').setProperty('id', 'tempDiv').setStyles({width: 'auto', height: 'auto'}).injectInside(document.body);
				temp.setHTML(el.innerHTML);
				this.ww= Math.max(document.getElementById ('tempDiv').offsetWidth,this.options.contentsWidth);
				this.hh= Math.max(document.getElementById ('tempDiv').offsetHeight,this.options.contentsHeight);
				document.body.removeChild (temp);
				temp.style.display = 'none';
				temp.setHTML(' ');
				//Hide hidden stuff
				this.hideStuff(true,this.options.hiddenElement);
				//Detatch it
				el.parentNode.removeChild(el);
				//Hide hidden stuff
				//if(null!=this.hidden)this.hidden.style.display='none';
				//Log
//				alert("Got player: [" + this.ww + ", "+ this.hh + "] ");
				virgin=true;
			}
			else alert("Could not get player");
		}
		//Hide the hidden part
		//if(null!=this.hidden)this.hidden.style.display='none';
		this.hideStuff(true,this.options.hiddenElement);
		//Show the main part
		if(null!=this.player)this.center.appendChild(this.player);
		this.top = Window.getScrollTop() + (Window.getHeight() / 15);
		this.center.setStyles({top: this.top+'px', display: ''});
		this.fx.overlay.start(0.8);
		this.step = 1;
		//this.fx.center.start({'height': [this.options.contentsHeight]});
		this.fx.center.start({'height': [this.hh]});
		return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.fx.center.start({'width': [this.options.contentsWidth], 'marginLeft': [this.options.contentsWidth/-2]});
			break;
			this.step++;
		case 2:
			//this.center.setHTML(this.ht) ;
			//Show the hidden part
			//if(null!=this.hidden)this.hidden.style.display='';
			this.hideStuff(false,this.options.hiddenElement);

			this.bottomContainer.setStyles({top: (this.top + this.center.clientHeight)+'px', height: '0px', marginLeft: this.center.style.marginLeft, width: this.options.contentsWidth+'px',display: ''});
			{
				this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
				break;
			}
			this.bottomContainer.style.height = '';
			this.step++;
		}
	},

	close: function(){
		this.fx.overlay.start(0);
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.step=1;
		this.center.setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'});
		if(null!=this.player)this.center.removeChild(this.player);
		return false;
	}

};


window.addEvent('domready', NutBox.init.bind(NutBox));
