/**
 *  @content 'class popupBox'; 'class url'
 *
 *  === 'class popupBox' ===
 *  @description Class for create JavaScipt popup box (and load content with Ajax)
 *  @author Michal Brauner
 *  @last_change  2008-04-22
 *  @version  1.1
 *
 *  === 'class url' ===
 *  @description Class for create JavaScipt popup box (and load content with Ajax)
 *  @author Michal Brauner
 *  @last_change  2008-04-22
 *  @version  1.1
 *  @required ajax.js
 *  @required utilites.js
 */


function popupBox()
{
  /* instance of 'url' class */
  this.contentUrl         = null;

  this.ajax               = null;

  /* in this variable is stored instance of popupBox class - must global variable */
  this.globalVariableName = 'box';

  this.zIndex             = 1000000;

  this.mainDivId          = 'popupBox';
  this.innerDivId         = 'popupBoxIn';
  this.closeDivId         = 'popupBoxClose';

  this.mainDiv            = null;
  this.innerDiv           = null;
  this.closeDiv           = null;

  /*   params of popup box */
  this.closeButton        = true;

  /* size in px */
  this.width              = 700;
  this.height             = 500;

  /* position in px (if null, is generated automatic - center in screen) */
  this.top                = null;
  this.left               = null;

  /* padding in px */
  this.paddingLeft        = 0;
  this.paddingRight       = 0;
  this.paddingTop         = 0;
  this.paddingBottom      = 0;


  this.loadContent  = function( urlTxt )
  {
    if ( this.mainDiv==null )
      this.createBox();

    if ( this.mainDiv==null )
      return;

    this.contentUrl = new url( urlTxt );

    this.requestAjax();
  }


  /* create box */
  this.createBox      = function()
  {
    this.createMainDiv();
    this.createInnerDiv();

    if ( this.closeButton )
      this.createCloseButton();
  }

  /* destroy box */
  this.destroyBox       = function()
  {
    mainDiv = this.getMainDiv();

    if ( this.innerDiv!=null )
    {
      remove_element( this.innerDiv );
      this.innerDiv = null;
    }

    if ( this.closeDiv!=null )
    {
      remove_element( this.closeDiv );
      this.closeDiv = null;
    }

    if ( this.mainDiv!=null )
    {
      remove_element( this.mainDiv );
      this.mainDiv = null;
    }


  }

  /* show box */
  this.show           = function()
  {
    /* we must first create a box */
    if ( this.mainDiv==null )
      this.createBox();

    this.mainDiv.style.display      = 'block';
    this.mainDiv.style.visibility   = 'visible';
  }

//   /* only hidde box */
//   this.hidde2          = function()
//   {
//     this.mainDiv.style.display      = 'none';
//     this.mainDiv.style.visibility   = 'hidden';
//   }


  /* send Ajax request */
  this.requestAjax    = function()
  {
    if ( this.contentUrl==null )
      return;

    /* Ajax init */
    if ( this.ajax==null)
      this.ajax       = new sack();
    else
      this.ajax.reset();

    this.ajax.requestFile   = this.contentUrl.getNoParamUrl();

    var params              = this.contentUrl.getParamsArr();
    var paramName, paramValue;

    for ( var i=0; i<params.length; i++)
    {
      if ( !this.isArray(params[i]) || params[i].length<2 )
        continue;

      paramName  = params[i][0];
      paramValue = params[i][1];

      this.ajax.setVar( paramName, paramValue );
    }

    var boxVar                   = eval( ' ' + this.globalVariableName + ';' );

    this.ajax.onCompletion       = boxVar.responseAjax;
    this.ajax.runAJAX();
  }


  /* process Ajax */
  this.responseAjax   = function()
  {
    ajax = this;

    if ( ajax==null )
      return;

    var response              = ajax.response;

    // !!! DODELAT
    var innerDiv  = document.getElementById( 'popupBox3In' );

    if ( innerDiv==null )
      innerDiv  = document.getElementById( 'popupBox2In' );

    if ( innerDiv==null )
      innerDiv  = document.getElementById( 'popupBoxIn' );

    if ( innerDiv!=null )
    {
      innerDiv.innerHTML = response;
    }
  }

  this.setCloseButton = function( closeButton )
  {
    this.closeButton  =  closeButton==true ? true : false;
  }

  /*  Function for construct box */

  this.createMainDiv  = function()
  {
    var paramsDiv     = new Array();

    paramsDiv[0]      = new Array();
    paramsDiv[0][0]   = 'id';
    paramsDiv[0][1]   = this.mainDivId;

    this.mainDiv        = createNewElement( 'div',   paramsDiv, document.body, true );

    if ( this.mainDiv )
    {
      this.mainDiv.style.position   = 'absolute';
      this.mainDiv.style.zIndex     = this.zIndex;
      this.mainDiv.style.display    = 'none';
      this.mainDiv.style.visiblity  = 'hidden';

      this.mainDiv.style.width      = this.width + 'px';
      this.mainDiv.style.height     = this.height + 'px';

      this.mainDiv.style.padding    = '0px';
      this.mainDiv.style.margin     = '0px';

      var left                      = 0;
      var top                       = 0;

      var windowWidth               = winWidth();
      var windowHeight              = winHeight();

      if ( this.left==null && windowWidth )
      {
          left  = (windowWidth>>1) - (this.width>>1);
          if ( left<0 )
            left = 0;
      }
      else {
        left                        = this.left;
      }



      if ( this.top==null && windowHeight )
      {
        top                         = (windowHeight>>1) - (this.height>>1);
        if ( top<0 )
          top = 0;
      }
      else {
        top                         = this.top;
      }

      this.mainDiv.style.left       = left + 'px';
      this.mainDiv.style.top        = top + 'px';


    }
  } // end of function createMainDiv

  this.getMainDiv = function()
  {
    var boxVar = eval( ' ' + this.globalVariableName + ';' );

    mainDiv = eval( ' document.getElementById(\'' + boxVar.mainDivId + '\');' );
    return mainDiv;
  }

  this.getInnerDiv = function()
  {
    var boxVar = eval( ' ' + this.globalVariableName + ';' );

    innerDiv = eval( ' document.getElementById(\'' + boxVar.innerDivId + '\');' );
    return innerDiv;
  }

  this.getCloseDiv = function()
  {
    var boxVar = eval( ' ' + this.globalVariableName + ';' );

    mainDiv = eval( ' document.getElementById(\'' + boxVar.closeDiv + '\');' );
    return mainDiv;
  }


  this.createInnerDiv = function()
  {
    if ( this.mainDiv==null )
      this.createMainDiv();

    if ( this.mainDiv==null )
      return;

    var paramsDiv     = new Array();

    paramsDiv[0]      = new Array();
    paramsDiv[0][0]   = 'id';
    paramsDiv[0][1]   = this.innerDivId;

    this.innerDiv      = createNewElement( 'div',   paramsDiv, this.mainDiv );

    if ( this.innerDiv )
    {
      this.innerDiv.style.position = 'relative';

      var width       = this.width  - (this.paddingLeft + this.paddingRight);
      var height      = this.height - (this.paddingTop + this.paddingBottom);

      var left        = this.paddingLeft;
      var top         = this.paddingTop;

      this.innerDiv.style.width    = width + 'px';
      this.innerDiv.style.height   = height + 'px';

      this.innerDiv.style.left     = left + 'px';
      this.innerDiv.style.top      = top + 'px';

      this.innerDiv.style.padding  =   '0px';

    }
  } // end of function createInnerDiv


  this.createCloseButton  = function()
  {
    if ( this.mainDiv==null )
      this.createMainDiv();

    if ( this.mainDiv==null )
      return;

    var paramsDiv     = new Array();

    paramsDiv[0]      = new Array();
    paramsDiv[0][0]   = 'id';
    paramsDiv[0][1]   = this.closeDivId;

    this.closeDiv     = createNewElement( 'a',   paramsDiv, this.mainDiv );

    if ( this.closeDiv )
    {
      this.closeDiv.style.position  = 'absolute';
      this.closeDiv.style.display   = 'block';

      this.closeDiv.alt             = 'Zavřít';

      var width       = 15
      var height      = 15;

      var left        = this.width - width;
      var top         = 0;

      this.closeDiv.style.width    = width + 'px';
      this.closeDiv.style.height   = height + 'px';

      if ( document.body && document.body.clientWidth && !window.innerWidth )
      {
        left -= 4;
      }

      this.closeDiv.style.left     = left + 'px';
      this.closeDiv.style.top      = top + 'px';

      this.closeDiv.title           = this.globalVariableName;

      boxVar = eval( ' ' + this.globalVariableName + ';' );

      if (this.closeDiv.attachEvent  )
        this.closeDiv.attachEvent('onclick', function() { var globalVariableName=event.srcElement.title; var boxVar = eval( ' ' + globalVariableName + ';' );  if (typeof(boxVar)!='undefined') {boxVar.destroyBox();} } );
      else
        this.closeDiv.setAttribute( 'onClick', 'if (typeof('+this.globalVariableName+')!=\'undefined\') {boxVar='+this.globalVariableName+'; boxVar.destroyBox();} return false;' );
    }
  }
}


/**
 *  @description Class for work with url
 *  @author Michal Brauner
 *  @last_change  2008-03-06
 *  @version  1.0
 */
function url( url )
{
  this.urlNoParam           = '';
  this.urlAll               = '';
  this.params               = new Array();

  this.separatorParams      = '&';
  this.separatorFirstParam  = '?';


  /**
   *  Ziska url adresu bez jakychkoliv parametru
   */
  this.getNoParamUrl  = function()
  {
    return this.urlNoParam;
  }


  /**
   *  Vrati pole s parametry
   *  @return array
   */
  this.getParamsArr  = function( )
  {
    if ( !this.isArray(this.params) )
      return null;

    return this.params;
  }


  /**
   *  Vrati hodnotu parametru
   *  @param paramName String ... jmeno parametru
   *  @return String
   */
  this.getParamName  = function( paramName )
  {
    if ( !this.isArray(this.params) )
      return '';

    return this.params[paramName];
  }

  /**
   *  Prida novy parametr do pole parametru
   *  @param paramName String ... jmeno parametru
   *  @param paramValue String ... hodnota parametru
   */
  this.addParam   = function( paramName, paramValue )
  {
    if ( !this.isArray(this.params) )
      return;

    this.params[paramName]  = paramValue;

    var item      = new Array();

    item[0]       = paramName;
    item[1]       = paramValue;

    this.params.push(item);
  }


  /**
   *  Smaze parametr z pole parametru
   *  @param paramName String
   */
  this.deleteParam   = function( paramName )
  {
    if ( !this.isArray(this.params) )
      return;

    var paramsNew         = new array();
    var paramNameOriginal = '';

    for ( var i=0; i<this.params.length; i++ )
    {
      if ( this.isArray(this.params[i]) )
      {
        paramNameOriginal = this.params[i][0];

        if ( paramNameOriginal!=paramName )
        {
          var item      = new Array();

          item[0]       = paramName;
          item[1]       = this.params[i][1];

          paramsNew.push(item);
        }
      }
    }

    this.params       = paramsNew;
  }



  /**
   *  Nacte url adresu do datovych struktur tridy (parametru apod...)
   *  @param url String ... url adresa
   */
  this.parseUrl   = function( url )
  {
    if ( url.length<=0 )
      return false;

    this.urlAll         = url;

    var parts           = url.split( this.separatorFirstParam );

    if ( this.isArray(parts) && parts.length>0 )
    {
      this.urlNoParam   = parts[0];

      // load all params
      if ( parts.length>1 )
      {
        /* save params into array paramName=paramValue */
        var params  = parts[1].split( this.separatorParams );

        var param       = '';
        var paramName   = '';
        var paramValue  = '';
        var paramArr    = null;

        // start loading params
        if ( this.isArray(params) )
        {
          for ( var i=0; i<params.length; i++ )
          {
            param     = params[i];
            paramArr  = param.split('=');

            if ( this.isArray(paramArr) )
            {
              if ( paramArr.length>=1 ) paramName  = paramArr[0];  else paramName   = '';
              if ( paramArr.length>=2 ) paramValue = paramArr[1];  else paramValue  = '';

              this.params[i]      = new Array();
              this.params[i][0]  = paramName;
              this.params[i][1]  = paramValue;

            }
          }
        } // end loading params
      } // end load all params
    }
    else
    {
      this.urlNoParam   = url;
    }

  } // end of function parseUrl

  this.parseUrl( url );
}



/**
 *  Prototypes of functions
 */
url.prototype.isArray     = function( value )
{
  return ( value instanceof Array );
}

popupBox.prototype.hidde     = function()
{
  if ( this.mainDiv )
  {
    this.mainDiv.style.display      = 'none';
    this.mainDiv.style.visibility   = 'hidden';
  }
}
popupBox.prototype.isArray     = function( value )
{
  return ( value instanceof Array );
}

popupBox.prototype.isSet        = function( variable )
{
  return( typeof( variable ) != 'undefined' );
}



