//
// Helper functions
// ------------------------------------

// Close popup and do simple postback on necessary level
function lw2_close_pb_get( is_top, url ) {

  var par = lw2.parent_get( is_top );
  if( null == par || 'function' != typeof( par.__4pb_glob ) ) {
    alert( RSRC.ld( "err.internal" ) );
    return;
  } // if

  if( !url ) url = '';

  // Perform postback
  par.__4pb_glob( '', '', url );
} // lw2_close_pb_get

// Close popup and do postback for page mode on necessary level
function lw2_close_pm_get( act, arg, tgt, is_top ) {

  var par = lw2.parent_get( is_top );
  if( null == par || 'function' != typeof( par.__4pb_glob ) ) {
    alert( RSRC.ld( "err.internal" ) );
    return;
  } // if

  // Perform postback
  par.__4pb_glob( act, arg, tgt );
} // lw2_close_pm_get

// Close popup and go to URL on necessary level
function lw2_close_url_get( url, is_top ) {

  var par = lw2.parent_get( is_top );
  if( null == par ) {
    alert( RSRC.ld( "err.internal" ) );
    return;
  } // if

  // Go to url at parent window
  par.lw2.go2url( par, url );
} // lw2_close_url_get

// Close popup by popup Id on necessary level
function lw2_close_get( popup_id, is_top ) {

  var par = lw2.parent_get( is_top );
  if( null == par ) {
    alert( RSRC.ld( "err.internal" ) );
    return;
  }

  par.lw2.lw_close( popup_id, true );
} // lw2_close_get

// Close popup 'popup_id', then open new one, with ID, width, height and url
function lw2_close_open_get( popup_id, id, url, w, h, is_top ) {

  var par = lw2.parent_get( is_top );
  if( null == par ) {
    alert( RSRC.ld( "err.internal" ) );
    return;
  }

  // Options
  var optz = lw2.opts_grp_cmm( w, h, url, id );

  // Close old, open new one
  par.lw2.close_open( popup_id, optz );
} // lw2_close_open_get

//
// Class implementing LightBox
// ------------------------------------


var lw2 = {

//  // More common call of function for create new parameters
//  opts_grp_cmm : function( sz_width, sz_height, url, cntr_id ) {
//    return lw2.opts_grp( false, null, true, 0, 0, sz_width, sz_height, true, true, 0, url, true, cntr_id, null );
//  },

  // More common call of function for create new parameters
  opts_grp_cmm : function( sz_width, sz_height, url, cntr_id, timeout ) {
    if( lw2.is_null( timeout ) || timeout < 1 )
      timeout = 0;
    return lw2.opts_grp( false, null, true, 0, 0, sz_width, sz_height, true, false, timeout, url, true, cntr_id, null );
  },

  // create default lightbox object from parameters set
  opts_grp : function( debug, err_msg, is_center, pos_x, pos_y, sz_width, sz_height, bg_has,
                       bg_is_click_close, timeout, url, flash_is_hide, cntr_id, on_before_popup ) {

    var lw_opts = {
      // Debug parameters
      'debug' : lw2.is_null( debug ) ? lw2.__opts_def.debug : debug,
      'err_msg' : lw2.is_null( err_msg ) ? lw2.__opts_def.err_msg : err_msg,

      // Default position
      'is_center' : lw2.is_null( is_center ) ? lw2.__opts_def.is_center : is_center,
      'pos_x' : lw2.is_null( pos_x ) ? lw2.__opts_def.pos_x : pos_x,
      'pos_y' : lw2.is_null( pos_y ) ? lw2.__opts_def.pos_y : pos_y,
      'sz_width' : lw2.is_null( sz_width ) ? lw2.__opts_def.sz_width : sz_width,
      'sz_height' : lw2.is_null( sz_height ) ? lw2.__opts_def.sz_height : sz_height,

      // Background defaults
      'bg_has' : lw2.is_null( bg_has ) ? lw2.__opts_def.bg_has : bg_has,
      'bg_is_click_close' : lw2.is_null( bg_is_click_close ) ? lw2.__opts_def.bg_is_click_close : bg_is_click_close,

      // Timeout
      'timeout' : lw2.is_null( timeout ) ? lw2.__opts_def.timeout : timeout,

      // url
      'url' : lw2.is_null( url ) ? lw2.__opts_def.url : url,

      // flash hide
      'flash_is_hide' : lw2.is_null( flash_is_hide ) ? lw2.__opts_def.flash_is_hide : flash_is_hide,

      // container id
      'cntr_id' : lw2.is_null( cntr_id ) ? lw2.__opts_def.cntr_id : cntr_id,

      // call-back last_check( opts_prm )
      'on_before_popup' : lw2.is_null( on_before_popup ) ? lw2.__opts_def.on_before_popup : on_before_popup,

      // Cookie pair
      'cookie_key' : null,
      'cookie_val' : null

    } // lw_opts

    return lw_opts;

  }, // opts_grp

  // new instance of options
  opts_mk : function() {
    var lw_opts = {
      // Debug parameters
      'debug' : lw2.__opts_def.debug,
      'err_msg' : lw2.__opts_def.err_msg,

      // Default position
      'is_center' : lw2.__opts_def.is_center,
      'pos_x' : lw2.__opts_def.pos_x,
      'pos_y' : lw2.__opts_def.pos_y,
      'sz_width' : lw2.__opts_def.sz_width,
      'sz_height' : lw2.__opts_def.sz_height,

      // Background defaults
      'bg_has' : lw2.__opts_def.bg_has,
      'bg_is_click_close' : lw2.__opts_def.bg_is_click_close,

      // Timeout
      'timeout' : lw2.__opts_def.timeout,

      // url
      'url' : lw2.__opts_def.url,

      // flash hide
      'flash_is_hide' : lw2.__opts_def.flash_is_hide,

      // container id
      'cntr_id' : lw2.__opts_def.cntr_id,

      // call-back last_check( opts_prm )
      'on_before_popup' : lw2.__opts_def.on_before_popup,

      // Cookie pair
      'cookie_key' : null,
      'cookie_val' : null
    }

    return lw_opts;
  }, // new_inst

  // show lightbox
  lw_call : function( opts_prm ) {
    var opts = lw2.__opts_def;

    if( false == lw2.is_null( opts_prm ) )
      opts = opts_prm;

    if( lw2.is_null( opts.cntr_id ) )
      return;

    if( null == window.lightbox )
      window.lightbox = {};

    window.lightbox[ opts.cntr_id ] = opts;

    if( opts.timeout > 0 ) {
      setTimeout( "lw2.__popup( '" + opts.cntr_id + "' )", opts.timeout );
      return;
    }

    // Show lightbox indirect
    this.__lightbox( opts );
  }, // lw_call

  // show lightbox for advertisement purposes only
  lwa_call : function( opts_prm, cookie_key, cookie_val ) {

    if( false == lwa_cookie_chk( cookie_key, cookie_val ) )
      return;

    opts_prm.cookie_key = cookie_key;
    opts_prm.cookie_val = cookie_val;

    opts_prm.on_before_popup = function( opts_prm ) {
      try {
        var len = 0;
        var lb = window.lightbox;
        if( false == lw2.is_null( lb ) ) {
          for( i in window.lightbox )
            len++;
        }

        if( len > 1 )
          return false;

      } catch( e ) {
        return false;
      }

      lwa_cookie_set( opts_prm.cookie_key, opts_prm.cookie_val );
      return true;
    }

    this.lw_call( opts_prm );
  }, // lwa_call



  // Gets sign indicating is object defined or null
  is_null : function( obj ) {
    return 'undefined' == typeof( obj ) || null == obj;
  }, // is_null

  // Close lightwindow by container Id
  lw_close : function( /* string */ cntr_id, is_top ) {

    if( false == is_top )
      d = window.document;
    else
      d = window.top.document;

    if( true == lw2.is_null( d ) ) {
      alert( 'Internal error: cannot detect document' );
      return;
    }

    try {
      var el = d.getElementById( cntr_id + '_bg' )
      if( false == lw2.is_null( el ) ) {
        d.body.removeChild( el );
      }

      el = d.getElementById( cntr_id );
      if( false == lw2.is_null( el ) ) {
        d.body.removeChild( el );
      }
    } catch( e ) {
      return;
    }

  }, // lw_close

  // Close opener, then open another popup
  close_open : function( cntr_id, opts_prm ) {
    lw2.lw_close( cntr_id );
    lw2.lw_call( opts_prm );
  }, // close_open

  // Go to specified URL
  go2url : function( par, url ) {
    par.location.href = url;
  }, // go2url

  // Get parent window
  parent_get : function( is_top ) {
    if( false == is_top )
      return window.parent;

    return window.top;
  }, // parent_get


  //
  // Private methods
  //

  __popup : function( cntr_id ) {
    this.__lightbox( window.lightbox[ cntr_id ] );
  }, // __popup

  // Main function, private
  __lightbox : function( opts_prm ) {

    // Stage 0, check is allow popup
    if( false == lw2.is_null( opts_prm.on_before_popup ) && 'function' == typeof( opts_prm.on_before_popup ) ) {
      if( false == opts_prm.on_before_popup( opts_prm ) )
        return;
    }

    // Integers
    var l, t, w, h, len;
    // Elements
    var div_ovl, div_cntr, div_hdr, span_ttl, div_wrk, iframe1;

    // Stage 1, validate necessary elements
    var d = window.document;
    if( lw2.is_null( d ) || lw2.is_null( d.body ) )
      return;
    var body = d.body;

    // Stage 2, prepare
    try {

      l = opts_prm.pos_x;
      t = opts_prm.pos_y;
      w = opts_prm.sz_width;
      h = opts_prm.sz_height + 25; // only 'close' button div height

      if( opts_prm.flash_is_hide )
        this.__flash_off( 'hidden' );

      var _div = d.getElementById( opts_prm.cntr_id );
      if( false == lw2.is_null( _div ) ) {

        body.removeChild( _div );

        var _bg = d.getElementById( opts_prm.cntr_id + '_bg' );
        if( false == lw2.is_null( _bg ) )
          body.removeChild( _bg );
      }

      len = 0;
      for( i in window.lightbox )
        len++;

      len = ( len * 2 * 100 ) + 100;

      if( true == opts_prm.bg_has ) {
        div_ovl = d.createElement( 'div' );
        div_ovl.className = 'lw_bg';
        div_ovl.style.zIndex = len;
        div_ovl.id = opts_prm.cntr_id + '_bg';

        var h2 = d.documentElement.clientHeight - 1;
        if( body.clientHeight > h2 ) h2 = body.clientHeight - 1;
        div_ovl.style.height = (h2*3) + "px";

        if( true == opts_prm.bg_is_click_close ) {

          div_ovl.onclick = function( e ) {
            e = e || window.event;
            if( !e )
              return;
            var d = e.target || e.srcElement;

            // Close window
            lw2.lw_close( opts_prm.cntr_id, false );

            // Show flash
            if( opts_prm.flash_is_hide )
              lw2.__flash_off( 'visible' );
          }
        }
        body.appendChild( div_ovl );
      }

      // container div
      div_cntr = d.createElement( 'div' );
      div_cntr.id = opts_prm.cntr_id;
      div_cntr.className = 'lw_cntr';
      div_cntr.style.zIndex = len + 100;

      body.appendChild( div_cntr );

      // header div
      div_hdr = d.createElement( 'div' );
      div_hdr.className = 'lw_hdr';
      div_cntr.appendChild( div_hdr );

      // span ttl [close]
      span_ttl = d.createElement( 'span' );
      span_ttl.className = 'lw_close';
      span_ttl.id = 'lw_close_' + opts_prm.cntr_id;
      span_ttl.onclick = function() {

        lw2.lw_close( opts_prm.cntr_id, false );

        if( opts_prm.flash_is_hide )
          lw2.__flash_off( 'visible' );
      }

      span_ttl.appendChild( d.createTextNode( RSRC.ld( "cmm.btn.close" ) ) );
      div_hdr.appendChild( span_ttl );

      // content div
      div_wrk = d.createElement( 'div' );
      div_wrk.className = 'content';
      div_cntr.appendChild( div_wrk );

      // content iframe

      iframe1 = d.createElement( 'iframe' );
      iframe1.setAttribute( 'scrolling', 'auto' );
      iframe1.setAttribute( 'frameBorder', '0' );
      iframe1.setAttribute( 'name', 'lw_iframe' );
      iframe1.setAttribute( 'src', opts_prm.url );
      iframe1.setAttribute( 'marginWidth', '0' );
      iframe1.setAttribute( 'marginHeight', '0' );
      iframe1.id = 'iframe_cont';
      iframe1.className = 'content';
      iframe1.style.width = opts_prm.sz_width + "px";
      iframe1.style.height = opts_prm.sz_height + "px";
      div_wrk.appendChild( iframe1 );

      var left_pos, top_pos;
      if( false == opts_prm.is_center ) {
        left_pos = l;
        top_pos = t;
      } else {

        var cur_w = d.documentElement.clientWidth;
        var cur_h = d.documentElement.clientHeight;
        if( body.clientWidth < cur_w )
          cur_w = body.clientWidth;
        if( body.clientHeight < cur_h )
          cur_h = body.clientHeight;

        left_pos = ( ( w / 2 ) - w ) + ( cur_w / 2 );
        top_pos = ( h /2 ) - h + ( cur_h / 2 );

        if( left_pos < 10 ) left_pos = 20;
        if( top_pos < 10 )  top_pos = 20;
      }

      div_cntr.style.left = left_pos + "px";
      div_cntr.style.top = top_pos + "px";
      div_cntr.style.width = w + "px";
      div_cntr.style.height = h + "px";

      // Scroll to up
      window.scrollTo( 0, 0 );

    } catch( ex ) {
      alert( 'Exception: ' + ex.name + '\nMessage: ' + ex.message );
      return;
    } // try


    // Stage 3. Show
    try {

      if( true == opts_prm.bg_has ) {
        div_ovl.style.display = 'block';
        div_ovl.style.visibility = 'visible';
      }

      div_cntr.style.display = 'block';

    } catch( ex ) {
      alert( 'Exception: ' + ex.name + '\nMessage: ' + ex.message );
    }

  }, // __lightbox()

  // hide/show flash on page
  __flash_off : function( visibility ) {
    var objects = document.getElementsByTagName( 'object' );
    for( i = 0; i != objects.length; i++ ) {
      objects[ i ].style.visibility = visibility;
    }
    var embeds = document.getElementsByTagName( 'embed' );
    for( i = 0; i != embeds.length; i++ ) {
      embeds[ i ].style.visibility = visibility;
    }
    var iframes = document.getElementsByTagName('iframe');
    for (i = 0; i != iframes.length; i++) {
      if( 'iframe_cont' == iframes[ i ].id )
        continue;

      iframes[i].style.visibility = visibility;
    }

  }, // __flash_off()

  // Default options for Lightbox
  __opts_def : {

    // Debug parameters
    'debug' : true,
    'err_msg' : 'Internal error...',

    // Default position
    'is_center' : true,
    'pos_x' : 128,
    'pos_y' : 128,
    'sz_width' : 400,
    'sz_height' : 400,

    // Background defaults
    'bg_has' : true,
    'bg_is_click_close' : true,

    // Timeout
    'timeout' : 0,

    // url
    'url' : null,

    // flash hide
    'flash_is_hide' : true,

    // container id
    'cntr_id' : 'lw_cntr',

    // call-back last_check( opts_prm )
    'on_before_popup' : null,

    // Cookie pair
    'cookie_key' : null,
    'cookie_val' : null

  } // lw2.__opts_def




} // lw2 namespace