var Class = {create: function() {return function() {this.initialize.apply(this, arguments);}}}
Function.prototype.bind = function(object) {var __method = this;return function() {__method.apply(object,arguments);}}
function $(oName) { return document.getElementById(oName); }
Array.prototype.each = function(func){for(var i=0; i<this.length; i++) func(this[i], i);}

// Auxiliary functions / / / / / / / / / /

function getElementsByClassName(classname, node)  {
    a = []; els = document.body.getElementsByTagName("*");
    var re = new RegExp('\\b' + classname + '\\b');
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [scrOfX, scrOfY];
}

// DarkRoomING / / / / / / / / / /

function mkdark() {
  o = document.createElement('div');
  o.id = 'dark';
  o.style.height = document.body.offsetHeight + 'px';
  document.body.appendChild(o);
  return o;
}

function rmdark() {
	if (!($('dark'))) return;
	document.body.removeChild($('dark')); 
}

DarkRoom = Class.create();
DarkRoom.prototype = {
	initialize: function() {
		this.pics = getElementsByClassName('image', 'span');
		for (i=0; i<this.pics.length; i++) {
			this.pics[i].onclick = this.enlarge.bind(this.pics[i]);
		}
	},
	
	enlarge: function() {
		mkdark();
		s = $('dark');
		p = this.getElementsByTagName('IMG');
		pSrc = (p[0].src).match(/src=(.*)/);
		
		c = document.createElement('span'); 
		c.id = 'picture'; 
		c.style.visibility = 'hidden';
		c.style.top = getScrollXY()[1] + 100 + 'px';
		ci = document.createElement('img'); ci.src = pSrc[1];
		cleg = document.createElement('strong'); 
		clegTxt = document.createTextNode(
			this.getElementsByTagName('STRONG')[0].childNodes[0].nodeValue);  
		
		wait = document.createElement('span');
		wait.id = 'wait';
		wait.style.top = getScrollXY()[1] + 220 + 'px';
		waitImg = document.createElement('img');
		waitImg.src = 'http://lab.playgreen.org/pub/skins/indigo/includes/waitingSign.gif';
		
		sh = document.createElement('div');
	  sh.id = 'shadow';
		sh.style.height = document.body.offsetHeight + 'px';
 		s.appendChild(wait).appendChild(waitImg);
		s.appendChild(sh);
		
		ci.onload = function(){
			s.removeChild(wait);
			c.appendChild(ci);
			c.appendChild(cleg).appendChild(clegTxt);
			s.appendChild(c);
			c.style.width = ci.offsetWidth + 'px';
			c.style.visibility = 'visible';
		}
		
		ci.onclick = function(){rmdark();}
	}
}

Toggle = Class.create();
Toggle.prototype = {
	initialize: function(rootNode) {
		this.a = ($(rootNode)).getElementsByTagName('SPAN');		
		for (i=0; i<this.a.length; i++) {
			this.a[i].onclick = this.flip.bind(this.a[i]); 
		} 
	},
	
	flip: function() {
		p = this.parentNode.getElementsByTagName('DIV')[0];
		if (p.className == '') p.className = 'off';
		else p.className = '';
	}
}

// Main function / / / / / / / / / /

window.onload = function() {
	//relocate();
	var box = new Toggle('toolbox');
	var gallery = new DarkRoom();
}
