/*          FOLDER NAMES                */
/*          customizable                */
var folders = {
  cmd: 'cmd',
  frm: 'frm',
  pg: 'pg',
  tbl: 'tbl',
  pu: 'pu'
};/*          /FOLDER NAMES                */

/*
 *  Silence script errors in IE when using console
 */
if(!window.console){
   window.console = {
      log: function(){},
      error: function(){},
      info : function(){},
      warn : function(){},
      debug : function(){}
   }
}

$(document).ready(function() {
  $(this).keydown(function(event) {
    if(event.which == 27 && puUtils.currentOptions)
      closePopunder();
  });
});

var puUtils = {
  shadowDiv: null,
  popunderDiv: null,
  currentOptions: null
};

function popunder(cOptions) {
  closePopunder();
  puUtils.currentOptions = jQuery.extend({
    /*Simple popup*/
    title: null,
    message: null,
    /*Specific content*/
    content: null,
    /*Load content from ajax*/
    contentPg: null,
    /*Paramaters used when fetching contentPg*/
    paras: null,
    /*Buttons*/
    bttn1: null,
    bttn2: null,
    /*Metadata passed to actions assigned to buttons*/
    metadata: null,
    /*Is the popunder closable with esc?*/
    abortable: false
  }, cOptions);

  puUtils.shadowDiv = document.createElement('div');
  $(puUtils.shadowDiv).click(function() {
    if(puUtils.currentOptions.abortable)
      closePopunder();
  }).addClass('popunderShadow');

  puUtils.popunderDiv = document.createElement('div');
  var popJQ = $(puUtils.popunderDiv).addClass('popunder');

  if(puUtils.currentOptions.bttn1 || puUtils.currentOptions.bttn2) {
    var bttnDiv = document.createElement('div');
    bttnDiv.className = 'buttons';
    if(puUtils.currentOptions.bttn1) {
      var tBttn1 = $(document.createElement('input'));
      tBttn1.attr('type','button');
      tBttn1.attr('value',puUtils.currentOptions.bttn1.value);
      tBttn1.click(function() {
        if(puUtils.currentOptions.bttn1.action)
          $.executeAction(puUtils.currentOptions.bttn1.action, puUtils.currentOptions.metadata);
        closePopunder();
      });
      $(bttnDiv).append(tBttn1);
    }
    if(puUtils.currentOptions.bttn2) {
      var tBttn2 = $(document.createElement('input'));
      tBttn2.attr('type','button');
      tBttn2.attr('value',puUtils.currentOptions.bttn2.value);
      tBttn2.click(function() {
        if(puUtils.currentOptions.bttn2.action)
          jQuery.executeAction(puUtils.currentOptions.bttn2.action, puUtils.currentOptions.metadata);
        closePopunder();
      });
      $(bttnDiv).append(tBttn2);
    }
    popJQ.append(bttnDiv);

    //initDynamicObjects(popJQ);
  }

  if(puUtils.currentOptions.content)
    popJQ.prepend(puUtils.currentOptions.content);
  else if(puUtils.currentOptions.contentPg) {
    var paras = puUtils.currentOptions.paras;
    console.log(folders.pu + '/' + puUtils.currentOptions.contentPg + '.jsp' + ((paras)?'?' + paras:''));
    $.ajax({
      url: folders.pu + '/' + puUtils.currentOptions.contentPg + '.jsp' + ((paras)?'?' + paras:''),
      success: function(data) {
        console.log('popunder success');
        popJQ.prepend(data);
       // initDynamicObjects(popJQ);
        centerPopunder(popJQ);
      },
      error: function(req, stat, thr) {
        console.log('popunder error', req, stat, thr);
      },
      complete: function(req, stat) {
        console.log('popunder complete', req, stat);
      }

    });
  } else
    popJQ.prepend('<h1>' + puUtils.currentOptions.title + '</h1><p>' + puUtils.currentOptions.message + '</p>');

  

  $('body').append(puUtils.shadowDiv).append(puUtils.popunderDiv);

  centerPopunder(popJQ);

}

function centerPopunder(popJQ) {
  popJQ.css('margin-top', ((popJQ.height()/2)*-1) + 'px');
  popJQ.css('margin-left', ((popJQ.width()/2)*-1) + 'px');
}

function resizePopunder(width, height) {
  var popJQ = $(puUtils.popunderDiv);
  if(width)
    popJQ.css('width', width);
  if(height)
    popJQ.css('height', height);
  centerPopunder(popJQ);
}

function closePopunder() {
  if(puUtils.shadowDiv) {
    puUtils.shadowDiv.parentNode.removeChild(puUtils.shadowDiv);
    puUtils.shadowDiv = null;
  }
  if(puUtils.popunderDiv) {
    puUtils.popunderDiv.parentNode.removeChild(puUtils.popunderDiv);
    puUtils.popunderDiv = null;
  }
}
