//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

var Client = {
  viewportWidth: function() {
    return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
  },

  viewportHeight: function() {
    return self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
  },
  
  viewportSize: function() {
    return { width: this.viewportWidth(), height: this.viewportHeight() };
  }
};

function hideJangBox() {
  $('overlay').style.display = 'none';
  $('jangbox').style.display = 'none';
}

function showJangbox(id) {
  var objOverlay = $('overlay');

  objOverlay.style.display = 'block';

  var arrayPageSize = getPageSizeWithScroll();
  var arrayPageScroll = getPageScroll();
  var objJangbox = $('jangbox');
  objJangbox.style.position= 'absolute';
	objJangbox.style.top = (arrayPageScroll[1] + ((Client.viewportHeight() - 35 - 200) / 2) + 'px');
	objJangbox.style.left = (((arrayPageSize[0] - 20 - 300) / 2) + 'px');

  objJangbox.style.display = 'block'; 

  // do the AJAX
  new Ajax('/sound_player.php?id='+id, {
    method: 'get',
    update: $('jangbox')
  }).request();

  objJangbox.effects().custom({
    'duration': 2500,
    'opacity': [0,1]
  });
}


function initJangbox() {
  var anchors = document.getElementsByTagName("a");

  for (var i=0; i<anchors.length; i++) {
      if (anchors[i].getAttribute("href") && 
              (anchors[i].className == "jangbox")) 
      {
          anchors[i].onclick = function() {
              showJangbox();
              return false;
          }
      }
  }

  /* 
  <div id="overlay">
      <a href="#" onclick="hideJangBox(); return false;"><imd id="loadingImage" /></a>
  </div>
  <div id="jangbox">
    // The thingy goes in here.
  </div>
  */

  var objBody = document.getElementsByTagName('body').item(0);


  var objOverlay = document.createElement("div");
  objOverlay.setAttribute('id', 'overlay');
  objOverlay.onclick = function() { hideJangBox(); return false; }
  objOverlay.style.display = 'none';
  objOverlay.style.position = 'absolute';
  objOverlay.style.top = '0';
  objOverlay.style.left = '0';
  objOverlay.style.zIndex = '100';
  objOverlay.style.width = '100%';
	
  var arrayPageSize = getPageSizeWithScroll();
	objOverlay.style.height = (arrayPageSize[1] + 'px');
  
  objBody.insertBefore(objOverlay, objBody.firstChild);

  var objJangbox = document.createElement('div');
  var arrayPageScroll = getPageScroll();
  objJangbox.setAttribute('id', 'jangbox');
  objJangbox.style.display = 'none';
  objJangbox.style.zIndex = '110';


  objBody.insertBefore(objJangbox, objOverlay.nextSibling);
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
  var oldonload = window.onload;
  if (typeof window.onload != 'function'){
      window.onload = func;
  } else {
    window.onload = function(){
    oldonload();
    func();
    }
  }
}

addLoadEvent(initJangbox);
