fullSizel = 0;
fullSizer = 0;
currentMargin = 0;
var bOokcase;

// thanks prototype
function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

function wSize() {
  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;
  }
  //var retArr = new Array(myWidth, myHeight);
  return myWidth; //retArr;
};

function getElementsByName_iefix(tag, name) {
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

function resizeIt(x) {
/*	if(typeof(olf) == 'undefined' || ! olf)
		olf = 0;*/
	//   r  =   x   /  y
	//   x  =  ry
	//   y  =  x/r
	var rto = 1234 / 790;
	var nY = Math.round(x / rto);
	var nX = Math.round(nY * rto);
	
	//pSprs = document.getElementsByName('spreadward');
	//pSbrBdrs = document.getElementsByName('spreadward_border');
	pSprs = getElementsByName_iefix('img', 'spreadward');
	pSbrBdrs = getElementsByName_iefix('div', 'spreadward_border');

	currentMargin = wSize() - nX;
	if(currentMargin < 0)
		currentMargin = 0;

//  	alert('x: '+nX+' y: '+nY);
	for (var jj = 0; jj < pSprs.length; jj++)
	{
		tiddle(jj, nX, nY);
		pSbrBdrs[jj].style.left = Math.round(currentMargin / 2)+'px';
	}
	return false;
};

function tiddle (jj, nX, nY) {
	if (typeof(pSprs) == 'undefined' || typeof(pSbrBdrs) == 'undefined')
		return;

	pSprs[jj].style.display = 'block';
	pSprs[jj].width = nX;
	pSprs[jj].height = nY;

/*	if (typeof(pSbrBdrs[jj].style) != 'undefined')
	{*/
	pSbrBdrs[jj].style.display = 'block';
	pSbrBdrs[jj].style.width = nX + 'px';
	pSbrBdrs[jj].style.height = nY + 'px';
	pSbrBdrs[jj].style.top = (jj * nY) + 'px';
// 	}


};

function tw_size (twidel, typof) { //width,
	if (! $('library') )
	{
		window.location = 'http://lingua.phobo.us';
		return false;
	}

// 	if(typeof(width) == 'undefined' || ! width)
// 		width = 1234;
	if(typof == 'spread'){
		switch(fullSizel) {//the easy one
			case 1: fullSizel = 0;
				resizeIt(wSize()-20);	
				$('minmax_'+typof).src = '/images/mag_'+typof+'_sm.png';
				$('minmax_'+typof).title = 'full size';
				$('pageup').style.display = 'none';
				$('pagedown').style.display = 'none';
				$('footer').style.position = 'static'; 
			break;
			case 0: resizeIt(1234);//fullsize
				fullSizel = 1;
				$('minmax_'+typof).src = '/images/min_'+typof+'_sm.png';		
				$('minmax_'+typof).title = 'fit to screen';
	
			break;
		};
	}
	else
	{
		switch(fullSizer) {
			case 2: fullSizer = 0;
				resizeIt(wSize()-20);
				//turn off
				var cht = $('library').library.library[0].style.height.match(/^(\d+)px$/);
				$('library').library.viewPage = 1;
				$('library').style.height = ($('library').library.spreads * cht[1]) + 'px';
				document.body.style.overflowX = 'auto';
				document.releaseEvents(Event.CLICK);
				document.onkeypress = function () { return true; };
				$('pageup').style.display = 'none';
				$('pagedown').style.display = 'none';
				$('footer').style.position = 'static'; 
			break;
			case 1:	fullSizer = 2;
				resizeIt((wSize()-20)*2);
				$('library').library.styleInit(1);
			break;
			
			case 0: ///lots of paging logic ahead
				fullSizer = 1;
				resizeIt((wSize() -160)*2);
				if (typeof(bOokcase) == 'undefined')
					bOokcase = new oLibrary();
				$('library').library.styleInit();
			break;
		};
	}
	return false;
};


function twiddle (me) {
	if (typeof(me.src) != 'undefined' && typeof(me.alt) != 'undefined')
	{
		var srcH = me.src;
		me.src = me.alt;
		me.alt = srcH;
	}
	return false;
};

function getKey(keyStroke) {
	var keyCode = (document.layers) ? keyStroke.which : ( keyStroke.keyCode ? keyStroke.keyCode : window.event.keyCode );
	var keyString = String.fromCharCode(keyCode).toLowerCase();
	
	switch(keyCode) {
		case 33: $('library').library.pageUp();
			return false;
			break;
		case 34: $('library').library.pageDown();
			return false;
			break;
	};
	//alert(keyCode + ' | ' + keyString);
	return true;
};


function oLibrary () {
	//tie into control buttons:
	this.oPup = $('pageup'); this.oPup.library = this;
	this.oPown = $('pagedown'); this.oPown.library = this;
	//save yourself
	$('library').library = this;
	//finish tying
 	this.oPup.onclick = function () { this.library.pageUp(); return false; }
 	this.oPown.onclick = function () { this.library.pageDown(); return false; }

	this.library = new Array ();
	for(var k = 0; k < $('library').childNodes.length; k++)
		this.library.push($('library').childNodes[k]);
/*	library: array of spreads...
So...
Page		image				pos
1	this.library[0].firstChild	    x:0, y:0
2	this.library[0].firstChild	    x:-617, y:0
3	this.library[1].firstChild	    x:0, y:0
*/
	this.viewPage = 1;
	this.spreads = this.library.length;
};

oLibrary.prototype.styleInit = function (flag) {
	//macros for paged view...
	if (document.layers) { document.captureEvents(Event.KEYPRESS); }
	document.onkeypress = getKey;

	document.body.style.overflowX = 'hidden';
	$('library').style.overflow = 'hidden';
	$('pageup').style.display = 'inline';
	$('pagedown').style.display = 'inline';	

	for ( var k in this.library )
	{
		this.library[k].style.position = 'absolute';
		this.library[k].style.left = Math.round(currentMargin / 2)+'px';
		this.library[k].style.top = '0px';
		this.library[k].style.display = 'none';
	}

	var tph = this.library[0].style.height.match(/([0-9]+)px/);
	$('library').style.height = Math.round((parseInt(tph[1])+40)/2)+'px';
 	$('footer').style.position = 'absolute'; 
 	$('footer').style.top = ((parseInt(tph[1])+40))+'px';

	this.scroll(flag);
	return false;
};
oLibrary.prototype.pageUp = function() {
	this.viewPage--;
	if(this.viewPage < 1)
		this.viewPage = 1;
	this.scroll();
	return false;
};
oLibrary.prototype.pageDown = function() {
	this.viewPage++;
	if(this.viewPage >= (this.spreads * 2))
		this.viewPage = (this.spreads * 2);
	this.scroll();
	return false;
};
oLibrary.prototype.scroll = function(flag) {
	for(var j = 0; j <= this.spreads; j++)
	{	
		if (! this.library[j])
			continue;		
		
		//go to spread
		if(j == parseInt((this.viewPage-1) / 2))
		{
			this.library[j].style.display = 'block';
			//go to page
			if (this.viewPage % 2) //odd number e.g. left side
				this.library[j].style.left = 
					(fullSizer == 1
						? 
					(70 + Math.round(currentMargin / 2))
						:
					0) + 'px';
			else
				this.library[j].style.left = 
					'-' +  
					(fullSizer == 1
						?
					((wSize()-240) + Math.round(currentMargin / 2)) 
						:
					(wSize()-20) )
					+ 'px'; 
		}
		else
		{
			this.library[j].style.display = 'none';
		}
	}
	if (! flag)
		window.scrollTo(0,0);
};


