// -----------------------------------------------------------------------------
// - Setup HDP namespace if not defined
// -----------------------------------------------------------------------------

if ( typeof HDP == 'undefined' ) var HDP = {};

// -----------------------------------------------------------------------------
// - Define Popup Class
// -----------------------------------------------------------------------------

HDP.Popup = function ()
  {
  var popup_type         = "system";
  var message            = "Popups make me a little more dead inside";
  var showed_popup       = 0;
  var did_onfocus        = 0;
  var suppress_popup     = 0;  
  var cookie_name        = 'count';
  var cookie_exp         = 365;
  var affiliate          = "";
  var tracking_id        = "";
  var offer_id           = "";
  var product            = "";
  var google_id			 = "";
  var internal_link_list = new Array( "orders.hdpublishing.net","nd-peoplesearch" );
  var history_init       = 0;
  var debug              = 0;
  var popup_url          = "https://orders.hdpublishing.net/cgi-bin/shop.cgi";
  var popup_params       = "width=410,height=381,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes";

  function set_message(i)            { message            = i ? i : message;            }
  function set_popup_type(i)         { popup_type         = i ? i : popup_type;         }
  function set_internal_link_list(i) { internal_link_list = i ? i : internal_link_list; }
  function set_affiliate(i)          { affiliate          = i ? i : affiliate;          }
  function set_tracking_id(i)        { tracking_id        = i ? i : tracking_id;        }
  function set_offer_id(i)           { offer_id           = i ? i : offer_id;           }
  function set_product(i)            { product            = i ? i : product;            }
  function set_popup_url(i)          { popup_url          = i ? i : popup_url ;         }
  function set_popup_params(i)       { popup_params       = i ? i : popup_params;       }
  function set_cookie_name(i)        { cookie_name        = i ? i : cookie_name;        }
  function set_cookie_exp(i)         { cookie_exp         = i ? i : cookie_exp;         }
  function set_debug(i)              { debug              = i ? i : debug;              }
  
  // ---------------------------------------------------------------------------
  // -
  // ---------------------------------------------------------------------------
  
  function is_internal_host( host )
    {
    for( var i = 0; i < internal_link_list.length; i++ )
      {
      if( host.toLowerCase().match( internal_link_list[i].toLowerCase() ) )
        {
        return true;  
        }
      }
      
    return false;
    }
    
  // ---------------------------------------------------------------------------
  // -
  // ---------------------------------------------------------------------------
  
  function mark_internal_links( )
    { 
    $("a").each( function()
      {
      if( is_internal_host( this.host ) )
        {
        if( debug ) { $(this).css( "border", "1px solid #ff0000"); }
        $(this).click( function(e) { suppress_popup = 1; } );   
        }
      });
    
    $("form").each( function()
      {
      if( is_internal_host( this.action ) )
        {
        if( debug ) { $(this).css( "border", "1px solid #00ff00"); }
        $(this).submit( function(e) { suppress_popup = 1;  }  );   
        }
      });
    
    }
  
  // ---------------------------------------------------------------------------
  // -
  // ---------------------------------------------------------------------------
  
  function display_popup_abandon() 
    {
    var url = popup_url + "?affiliate="   + affiliate 
                        + "&tracking_id=" + tracking_id
                        + "&offer_id="    + offer_id
                        + "&product="     + product;
                   
    if( !get_cookie_count() && suppress_popup != 1 )
      {
        set_cookie();  
        window.open(url, "", popup_params );
      }
    }
  
  function display_popup_system() 
    {       
	     
    if( !get_cookie_count() && suppress_popup != 1 )
      {
      showed_popup = 1;  
      set_cookie();  
      return message;
      }
    }
    
  // ---------------------------------------------------------------------------
  // -
  // ---------------------------------------------------------------------------
  
  function get_cookie_count()
    {
    return $.cookie( cookie_name );  
    }
    
  // ---------------------------------------------------------------------------
  // -
  // ---------------------------------------------------------------------------
  
  function set_cookie( )
    {
    var count = get_cookie_count();
    if( count != null )
      {
      $.cookie( cookie_name, 1, { expires: cookie_exp });  
      }
    else
      {
      $.cookie( cookie_name, -1);
      $.cookie( cookie_name,  1, { expires: cookie_exp }); 
      }
    }
   
  function on_cancel()
    {
    did_onfocus = 1;  
    var url = popup_url + "?affiliate="   + affiliate 
                        + "&tracking_id=" + tracking_id
                        + "&offer_id="    + offer_id
                        + "&product="     + product
						+ "&google_id"    + google_id;
	window.location = url; 
	}
  // ---------------------------------------------------------------------------
  // - Public variables and methods
  // ---------------------------------------------------------------------------
  
  return {
    
    init: function( product, offer_id, affiliate, tracking_id, popup_url, popup_params, cookie_name, cookie_exp, internal_link_list, debug, type, message )
      {
	  set_message( message );
      set_popup_type( type );  
      set_product( product );
      set_offer_id( offer_id );
      set_affiliate( affiliate );
      set_tracking_id( tracking_id );
      set_popup_url( popup_url );
      set_popup_params( popup_params );
      set_cookie_name( cookie_name );
      set_cookie_exp( cookie_exp );
      set_internal_link_list( internal_link_list );
      set_debug( debug );

      $(document).ready( function()
        {
        mark_internal_links();
        
        $(window).focus( function() 
          {
          if( did_onfocus == 0 && showed_popup == 1 )
            {
            on_cancel();
            }
          });
        
		    window.onbeforeunload = function() 
		     {
         if( popup_type == "system" )
           {
		   $(window).focus();
           return display_popup_system(); 
           }
         else
           {
           display_abandon_popup(); 
           }
		     };
		
        });
  

      }
    };
    
  }();