/************************************************************************/
/* Nelsou.com                                                           */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2007 by Nelson (nelsou.com@gmail.com)                  */
/*                                                                      */
/* =========================                                            */
/* $Id: javascripts/nels_box.js,v 1.0 Nelson Exp $                      */
/* File Last Updated:                                                   */
/* 17:02 19/09/2007                                                     */
/************************************************************************/

gl_wiindow = {};
gl_wiindow.height = null;
gl_wiindow.width = null;

function show_image(path, callback)
{
  var photo = new Image();
  photo.src = path + "?" + new Date().getTime();

  var W, H;
  var tmp = screen_size();
  W = tmp[0];
  H = tmp[1];
  callback = (callback ? callback : "hide_image");
  show_overlay(tmp[3], tmp[2], callback);
  $('html').style.overflow = 'hidden';

  var load = document.createElement('div');
  load.className = "loading";
  load.innerHTML = "Loading ....";
  load.id = "loading_image";
  document.body.appendChild(load);

  photo.onload = function()
  {
    if ($('overlay').style.display != "block")
      return ;
    var coefX = photo.width / (W - 100);
    var coefY = photo.height / (H - 100);
    var coef = ((coefX > coefY) ? coefX : coefY);

    var newWidth = photo.width / ((coef < 1) ? 1 : coef);
    var newHeight = photo.height / ((coef < 1) ? 1 : coef);
  	var image = document.createElement('img');
    image.id = "image_box";
  	image.src = path;
  	image.style.height = newHeight + 'px';
  	image.style.width = newWidth + 'px';
  	image.style.marginTop = (newHeight / -2) + 'px';
  	image.style.marginLeft = (newWidth / -2) + 'px';
  	image.onclick = window[callback];
  	if ($('loading_image'))
      document.body.removeChild($('loading_image'));
    document.body.appendChild(image);
  }
  return true;
}

function hide_image()
{
  hide_overlay();
	$('html').style.overflow = 'auto';
	if ($('loading_image'))
	 document.body.removeChild($('loading_image'));
	if ($('image_box'))
	 document.body.removeChild($('image_box'));
  return true;
}

function show_wiindow(height, width)
{
	gl_wiindow.height = height;
	gl_wiindow.width = width;

	init_wiindow(height, width + 40);
	showWindowLoading();
	$('wiindow').style.display = 'block';
	$('overlay').style.display = 'block';
	$('html').style.overflow = 'hidden';
  return true;
}

function hide_wiindow()
{
  hide_overlay();
	$('wiindow').style.display = 'none';
	$('html').style.overflow = 'auto';
  return true;
}

function show_overlay(pageHeight, pageWidth, callback)
{
  if (callback)
    $('overlay').onclick = window[callback];
  $('overlay').style.display = "block";
	$('overlay').style.width = pageWidth + 'px';
	$('overlay').style.height = pageHeight + 'px';
}

function hide_overlay()
{
  $('overlay').style.display = "none";
  $('overlay').innerHTML = "";
}


function init_wiindow(height, width)
{
	var tmp = screen_size();
	var windowWidth = tmp[0];
  var windowHeight = tmp[1];
	var pageWidth = tmp[2];
  var pageHeight = tmp[3];

  if (windowWidth < width)
    width = windowWidth - 40;
  if (windowHeight < height)
    height = windowHeight - 40;
	var newPosX = (windowWidth / 2) - (width / 2);
	var newPosY = (windowHeight / 2) - (height / 2);

  //Morphing($('wiindow'), {left: newPosX, top: newPosY, width: width, height: height}, 0);

	$('wiindow').style.left = newPosX + 'px';
	$('wiindow').style.top = newPosY + 'px';
	$('wiindow').style.width = (width - 10) + 'px';
	$('wiindow').style.height = height + 'px';

  show_overlay(pageHeight, pageWidth, "hide_wiindow");
  /*
	$('overlay').onclick = hide_wiindow;
	$('overlay').style.width = pageWidth + 'px';
	$('overlay').style.height = pageHeight + 'px';
	*/
  return true;
}
