jQuery.fn.PopUp = function(contenido,fnInit,autoClose,elementClose,ancho,alto){
  this.css('cursor','pointer');
  return this.click(function(){ newPopUp(contenido,fnInit,autoClose,elementClose,ancho,alto);  return false; });
}
jQuery.fn.PopUpLoadContent = function(urlToLoad,fnInit,autoClose,elementClose,ancho,alto){
  this.css('cursor','pointer');
  var posX=0,posY=0;
  this.click(function(e){ posX= e.pageX;posY=e.pageY; });
  return this.click(function(){newPopUpLoadContent(urlToLoad,fnInit,autoClose,elementClose,ancho,alto,posX,posY);  return false; });
}
jQuery.fn.PopUpExternalPage = function(urlToLoad,fnInit,autoClose,elementClose,ancho,alto){
  this.css('cursor','pointer');
  return this.click(function(){ newPopUpExternalPage(urlToLoad,fnInit,autoClose,elementClose,ancho,alto);  return false; });
}



function newPopUp(contenido,fnInit,autoClose,elementClose,ancho,alto){
  if(autoClose==undefined) autoClose = true;
  if(elementClose==undefined || elementClose=='') elementClose = null;
  initPopUp(contenido,fnInit,autoClose,elementClose,ancho,alto);
}
function newPopUpLoadContent(urlToLoad,fnInit,autoClose,elementClose,ancho,alto,posX,posY){
   $('body').css('cursor','wait');
    showLoading(posX,posY);
    var peticion = $.ajax({
                     url:        urlToLoad,
                     method:     'GET',
                     dataType:   'html',
                     success:    function(html){ $('body').css('cursor','auto'); newPopUp(html,fnInit,autoClose,elementClose,ancho,alto); },
                     error:      function(){ $('body').css('cursor','auto');  hideLoading(); alert('Error al cargar contenido.');  return false;}
     });
}
function newPopUpExternalPage(urlToLoad,fnInit,autoClose,elementClose,ancho,alto){
   var html = '<iframe width="100%" height="100%" border="0" src="'+urlToLoad+'"></iframe> ';
   newPopUp(html,fnInit,true,'',ancho,alto);
}

function showLoading(posX,posY){
  $('#popUpPreload').remove();
  $('body').append('<div id="popUpPreload">Loading...</div>');
  $('#popUpPreload').css('top',posY+'px');
  $('#popUpPreload').css('left',posX+'px');
  $('#popUpPreload').fadeIn();
}
function hideLoading(){
    $('#popUpPreload').fadeOut('slow',function(){$(this).remove();});
}

function initPopUp(contenido,fnInit,autoClose,elementClose,ancho,alto){
  if($.browser.msie)
    $('select').css('visibility','hidden');    // FIX IE6
  addPopUp(contenido,fnInit,ancho,alto);
  resizePopUpFnd();
  $(window).resize(resizePopUpFnd);
  divPopUpContent.fadeIn("slow");
  if(autoClose){
    divPopUpFnd.click(closePopUp);
   // divPopUpFnd.mousemove(function(e){posXmouse=e.pageX; posYmouse = e.pageY; });
   // divPopUpFnd.mouseout(function(){interval==undefined});
   // divPopUpFnd.mouseover(function(){if(interval==undefined)posClose();});
  }
  if(elementClose!=null)
    $(elementClose,divPopUp).click(function(){closePopUp(); return false;});
}
var posXmouse,posYmouse,interval;

function posClose(){
  var pos = divPopUpFnd.css('background');
  var p = pos.split('px');


  var X = (parseInt(p[0])*2+posXmouse)/3-10;
  var Y = (parseInt(p[1])+posYmouse)/2-5;
  if(!isNaN(X)){
   divPopUpFnd.css('background-position',X+'px '+Y+'px');
  }else{
   divPopUpFnd.css('background-position','0px 0px');
  }
  interval = setTimeout('posClose()',50);
}

function addPopUp(contenido,fnInit,ancho,alto){
  hideLoading();
  $('#popUp').remove();
  $('body').append(htmlPopUp);
  divPopUp = $('#popUp');
  divPopUpFnd = $('#popUpFnd');
  divPopUpContent = $('#popUpContent');
  if(!isNaN(ancho))divPopUpContent.width(ancho);
  if(!isNaN(alto))divPopUpContent.height(alto);
  divPopUpContent.html(contenido);
  try{
    if(fnInit!=undefined && fnInit!='' )
       eval(fnInit);
  }catch(e){

  }
}

function closePopUp(){
  divPopUpFnd.remove();
  divPopUpContent.fadeOut("slow",function(){
    divPopUp.remove();
    if(ifIE6())
      $('select').css('visibility','visible'); // FIX IE6
  });
}

function ifIE6(){
  if($.browser.msie&&parseInt($.browser.version)<7&&divPopUpContent.css('position')!='fixed')  return true;
  else return false;
}

function resizePopUpFnd(){
  divPopUpFnd.width($(document).width());
  divPopUpFnd.height($(document).height());
  centerPopUp();
}

function centerPopUp(){
   divPopUpContent.css('top',posY()+'px');
   divPopUpContent.css('left',posX()+'px');
}

function posY(){
  var popUpHeight  = divPopUpContent.height();
  var windowHeight = $(window).height();
  var y = 0;
  if(popUpHeight<windowHeight)
     y = (windowHeight-popUpHeight)/2;
  if(ifIE6())   /* IE6 fix */
     y += $(document).scrollTop();
  return parseInt(y);
}

function posX(){
  var popUpWidth  = divPopUpContent.width();
  var windowWidth = $(window).width();
  var x = 0;
  if(popUpWidth<windowWidth)
     x = (windowWidth-popUpWidth)/2;
  return parseInt(x);
}

var divPopUp,
    divPopUpFnd,
    divPopUpContent;


/*  HTML PopUp  */
var htmlPopUp = '';
htmlPopUp += '<div id="popUp">';
htmlPopUp += '  <div id="popUpContent">';
htmlPopUp += '  </div>';
htmlPopUp += '  <div id="popUpFnd">';
htmlPopUp += '  </div>';
htmlPopUp += '</div>';

