createDomNode = function(type, id, classname)
{
  var domnode = document.createElement(type);
  if (id)
    domnode.id = id;
  if (classname !== null && classname != undefined)
    domnode.className = classname;
  return domnode;
}

var z_background = null;
var z_modal = null;
var z_file = null;
var z_width = null;
var z_height = null;
var z_callback = null;

// width, height are the sizeX and sizeY of the light box.
// if width and height are null, the size is automatically calculated (ONLY FOR AN IMAGE: type = 1,
//           For any other case, width and height MUST be set)
// type = 1: image,
//        2: video,
//        3: flash,
//        4: user defined, into the call back
// file: is the file to load (case 1,2,3), with full web path
// callback: is the function to call when the light box is loaded
//            (800,  600,     5,   'ZGIKae7rMHg', null)
function zoom(width, height, type, file, callback)
{
  z_width = width;
  z_height = height;
  z_type = type;
  z_file = file;
  z_callback = callback;

  // 1. put the modal background
  if (!z_background)
  {
    z_background = createDomNode('div', 'lightbg', 'modalbg');
    z_background.style.zIndex = 10;
    document.body.appendChild(z_background);
    eventManager.registerEvent(z_background, 'click', closezoom, true);
  }
  z_background.style.display = 'block';
  z_background.style.height = browser.getDocumentHeight() + 'px';

  if (!z_modal)
  {
    z_modal = createDomNode('div', 'light', 'modal');
    z_modal.style.zIndex = 100;
    z_background.appendChild(z_modal);
  }

  browser.setInnerHTML(z_modal, '<div id="modalloader" style="padding: 15px; background-color: white; width: 120px; height: 30px; position: absolute; left: -60px; top: -30px; z-index: 1000;"><img src="/pics/ajax-loading.gif" style="vertical-align: middle;" alt="" title="" /> <b>Cargando...</b></div><div id="modalcontainer" style="background-color: white; position: absolute; visibility: hidden; overflow: auto; opacity: 0; filter: alpha(opacity: 0); z-index: 1100;">'+(type==1?'<img id="modalimg" src="'+file+'" alt="" title="" />':'')+'</div><div id="modalclose" style="background-color: black; color: white; width: 30px; height: 30px; text-align: center; font-size: 24px; font-weight: bold; position: absolute; top: 0px; left: 0px; cursor: pointer;">X</div>');

  adjustsize();
}

function adjustsize()
{
  var modalimg = document.getElementById('modalimg');
  if (z_type == 1 && (z_width == null || z_height == null))
  {

	//var width = browser.getNodeWidth($('modalimg'));
    var width = browser.getNodeWidth(modalimg);
    if (width == 0)
    {
      setTimeout(adjustsize, 100);
      return;
    }
  }

  if (z_width == null)
  {
    var widthmax = browser.getWindowWidth() - 100;
    //z_width = browser.getNodeWidth($('modalimg'));
    z_width = browser.getNodeWidth(modalimg);
    if (z_width > widthmax)
    {
      z_width = widthmax;
    }
  }

  if (z_height == null)
  {
    var heightmax = browser.getWindowHeight() - 100;
    //z_height = browser.getNodeHeight($('modalimg'));
    z_height = browser.getNodeHeight(modalimg);
    if (z_height > heightmax)
    {
      z_height = heightmax;
    }
  }

  var fct = null;
  if (z_type == 1) fct = finalImage;
  if (z_type == 2) fct = finalVideo;
  if (z_type == 3) fct = finalFlash;
  if (z_type == 4) fct = finalCallback;
  if (z_type == 5) fct = finalYoutube;

  fct();
//  animManager.createSprite('zoom', 'modalloader', fct, {autostart:true, loop:false, chain: [{type:'move',time:400,xinit:-60,xend:-60,yinit:-30,yend:-(z_height/2),winit:120,wend:120,hinit:30,hend:z_height-30}, {type:'move',time:300,xinit:-60,xend:-(z_width/2),yinit:-(z_height/2),yend:-(z_height/2),winit:120,wend:z_width-30,hinit:z_height-30,hend:z_height-30}]});

  var modalcontainer = document.getElementById('modalcontainer');

  modalcontainer.style.width = z_width+'px';
  modalcontainer.style.height = z_height+'px';
  modalcontainer.style.left = -(z_width/2)+'px';
  modalcontainer.style.top = -(z_height/2)+'px';
  $('modalclose').style.left = -(z_width/2+30)+'px';
  $('modalclose').style.top = -(z_height/2)+'px';
  eventManager.registerEvent($('modalclose'), 'click', closezoom, true);

  /*
  $('modalcontainer').style.width = z_width+'px';
  $('modalcontainer').style.height = z_height+'px';
  $('modalcontainer').style.left = -(z_width/2)+'px';
  $('modalcontainer').style.top = -(z_height/2)+'px';
  */
}

function finalImage()
{

  var modalcontainer = document.getElementById('modalcontainer');

  animManager.createSprite('zoom1', 'modalloader', null, {autostart:true, loop:false, chain: [{type:'move', time:500, tinit:100, tend: 0}]});
  animManager.createSprite('zoom2', 'modalcontainer', null, {autostart:true, loop:false, chain: [{type:'move',time:500, tinit:0, tend:100}]});
  //$('modalcontainer').style.visibility = '';

  modalcontainer.style.opacity = 1;
  modalcontainer.style.filter = "alpha(opacity: '100')";
  
  modalcontainer.style.visibility = '';

	if (z_callback)
	  z_callback();
}


function finalVideo()
{
  var modalcontainer = document.getElementById('modalcontainer');

   modalcontainer.style.opacity = 1;
  modalcontainer.style.filter = "alpha(opacity: '100')";


//  animManager.createSprite('zoom1', 'modalloader', null, {autostart:true, loop:false, chain: [{type:'move', time:500, tinit:100, tend: 0}]});
//  animManager.createSprite('zoom2', 'modalcontainer', null, {autostart:true, loop:false, chain: [{type:'move',time:500, tinit:0, tend:100}]});
  //$('modalcontainer').style.visibility = '';
  modalcontainer.style.visibility = '';

  var s1 = new SWFObject('/flash/player-viral.swf','player','800','600','9');
  s1.addParam('allowfullscreen','true');
  s1.addParam('allowscriptaccess','always');
  s1.addParam('flashvars','file='+z_file+'&autostart=true');
  s1.addVariable('plugins', 'gapro-1');
  s1.addVariable('gapro.accountid', 'UA-6748263-1');
  s1.write('modalcontainer');

	if (z_callback)
	  z_callback();
}

function finalFlash()
{
  animManager.createSprite('zoom1', 'modalloader', null, {autostart:true, loop:false, chain: [{type:'move', time:500, tinit:100, tend: 0}]});
  animManager.createSprite('zoom2', 'modalcontainer', null, {autostart:true, loop:false, chain: [{type:'move',time:500, tinit:0, tend:100}]});
  var modalcontainer = document.getElementById('modalcontainer');
  //$('modalcontainer').style.visibility = '';
  modalcontainer.style.visibility = '';
  
  var s1 = new SWFObject(z_file,'player','800','600','9');
  //s1.addParam('allowscriptaccess','always');
  s1.write('modalcontainer');

  modalcontainer.style.opacity = 1;
  modalcontainer.style.filter = "alpha(opacity: '100')";

	if (z_callback)
	  z_callback();
}

function finalCallback()
{
	var modalcontainer = document.getElementById('modalcontainer');

	animManager.createSprite('zoom1', 'modalloader', null, {autostart:true, loop:false, chain: [{type:'move', time:500, tinit:100, tend: 0}]});
  animManager.createSprite('zoom2', 'modalcontainer', null, {autostart:true, loop:false, chain: [{type:'move',time:500, tinit:0, tend:100}]});
  //$('modalcontainer').style.visibility = '';
	modalcontainer.style.visibility = '';

	if (z_callback)
	  z_callback();
}

function finalYoutube()
{
  var modalcontainer = document.getElementById('modalcontainer');
  modalcontainer.style.visibility = '';  
  modalcontainer.style.opacity = 1;
  modalcontainer.style.filter = "alpha(opacity: '100')";

  modalcontainer.innerHTML = '<iframe id="youtube1" width="100%" height="100%" src="http://www.youtube.com/embed/' + z_file + '?hl=es&fs=1&autoplay=1" frameborder="0" allowfullscreen></iframe>';
  
  if (z_callback)
	  z_callback();
}



function closezoom(e)
{
	var modalcontainer = document.getElementById('modalcontainer');

  // check coords fuera de la modalcontainer parapoder cerrar
  var x = browser.getCursorX(e);
  var y = browser.getCursorY(e);
  /*
  var l = browser.getNodeDocumentLeft($('modalcontainer'));
  var t = browser.getNodeDocumentTop($('modalcontainer'));
  var w = browser.getNodeWidth($('modalcontainer'));
  var h = browser.getNodeHeight($('modalcontainer'));*/

  var l = browser.getNodeDocumentLeft(modalcontainer);
  var t = browser.getNodeDocumentTop(modalcontainer);
  var w = browser.getNodeWidth(modalcontainer);
  var h = browser.getNodeHeight(modalcontainer);

  if ( x < l || x > l + w || y < t || y > t + h )
  {
    z_background.style.display = 'none';
    // empty the modal
    browser.setInnerHTML(z_modal, '');
  }
}

