﻿/*/
/* A3 Dialog Box 1.0
/* Requires jQuery 1.3.2 
/* By Lars Michael Astrom (http://www.lars-astrom.com)
/* Copyright (c) 2010 A3 IT Solutions
/* Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
/* Tested in IE7, IE8, Firefox 3.5, and Chrome 4.0
/*/

/*/
/* Implementation
/* Suggested implementation is to set options as a variable and pass it into the plug in
/* so that if the "open" command is used, you still get the options.

/* Sample implementation:
/* var dialogOptions = {
/*   autoOpen: true, 
/*   alignment: "right", 
/*   offset: 175 
/* };
/* $(document).ready(function(){ $("#Dialog").A3OptInDialog(dialogOptions); });

/* To open the dialog box with an event (such as onclick) pass "open" to the plug in:
/* $(document).ready(function(){ $("#Dialog").A3OptInDialog("open", dialogOptions); });
/*/

(function($){
  $.A3OptInDialog = {
    //Set the default values  
    defaults: {
      autoOpen: false, // auto open on page load
      alignment: "left", // left or right aligned
      delay: 500, // delay between page loading and dialog being shown (milliseconds)
      speed: 1000, // speed at which the dialog appears (milliseconds)
      offset: 0, // horizontal offset (px)
      hideAfter: 30000 // hide dialog after (milliseconds)
    }
  };
  
  $.fn.A3OptInDialog = function(action, options){
    // make sure options are passed even if action is used
    if (typeof(action) == "object"){ options = action; }
  
    var o = $.extend({}, $.A3OptInDialog.defaults, options);
    var s = $(this).selector; // selector
    var dt, st; // timer objects
    var f = false; // iframe focus check
    var xpos = (($(window).width() - ($(s).outerWidth())) / 2);
    xpos = Math.round((o.alignment == "left") ? xpos + (o.offset / 2) : xpos - (o.offset / 2)) + "px";
    
    return this.each(function(){
      // add dialog box holder and move the dialog outside the viewable area
      $(this).wrap('<div id="DialogContainer"></div>');
  
      $("#DialogContainer").css({
        "width": "100%",
        "height": "100%",
        "overflow": "hidden",
        "position": "absolute",
        "top": "0px",
        "left": "0px",
        "display": "none"
      });
      
      // fix relative z-index bug in IE
      if($.browser.msie){ $("#DialogContainer").css({"z-index": "1"}); }

      if (o.alignment == "right"){ $(s).css({ marginRight: (($(s).outerWidth() + 20) * -1) + "px" }, o.speed); }
      else{ $(s).css({ marginLeft: (($(s).outerWidth() + 20) * -1) + "px" }, o.speed);}
      
      // Initialize the script and bind events
      $.fn.A3OptInDialog.init = function(){
        // cookie functions
        $.cookie("IHFCNewsletterOptIn", "true", { expires: 1});
      
        // delay dialog box pop up
        dt = setTimeout(function(){ $.fn.A3OptInDialog.ShowDialog(); }, o.delay);
        
        // bind hover events
        $(s).hover(function(){
          $.fn.A3OptInDialog.StopHideTimer();
        }, function(){
          if (!f){ $.fn.A3OptInDialog.StartHideTimer(0); };
        });
        
        // if iframe is loaded, check to see if any input element has focus
        $(s + " iframe").load(function(){
          $(this).contents().find(":input").each(function(){
            $(this).focus(function (){
              $.fn.A3OptInDialog.StopHideTimer();
              f = true;
            }).blur(function (){ f = false; });
          });
        });
        
        // handle close button
        $(s + " a.close").click( function (){ $.fn.A3OptInDialog.HideDialog(); });
      };
      
      // show dialog function
      $.fn.A3OptInDialog.ShowDialog = function(){
        // set position
        if (o.alignment == "right"){ $(s).show().animate({ marginRight: xpos }, o.speed); }
        else{ $(s).show().animate({ marginLeft: xpos }, o.speed);}
        
        $("#DialogContainer").show();
        $.fn.A3OptInDialog.StartHideTimer(o.speed); // start the hide timer
        clearTimeout(dt); // clear the delay timer
      }
      
      // hide dialog function
      $.fn.A3OptInDialog.HideDialog = function(){
        // animate the hide
        $(s).fadeOut(250, function(){ 
          if (o.alignment == "right"){ $(s).css({ marginRight: (($(s).outerWidth() + 20) * -1) + "px" }, o.speed); }
          else{ $(s).css({ marginLeft: (($(s).outerWidth() + 20) * -1) + "px" }, o.speed);}
          $("#DialogContainer").hide();
        });
        
        $.fn.A3OptInDialog.StopHideTimer(); // stop hide timer
      }
      
      // hide timer function
      $.fn.A3OptInDialog.StartHideTimer = function(m){ st = setTimeout(function(){ $.fn.A3OptInDialog.HideDialog(); }, (o.hideAfter + m)); }
      
      // cancel the hide timer function
      $.fn.A3OptInDialog.StopHideTimer = function(){ clearTimeout(st); }
      
      // if the "open" action is enter, show dialog regardless of cookie
      if (action == "open"){ $(s).A3OptInDialog.init(); }
      
      // check auto open
      if (o.autoOpen){
        // check for cookie
        if ($.cookie("IHFCNewsletterOptIn") != "true"){ 
          $(s).A3OptInDialog.init(); 
        }
      }
    });
  }
})(jQuery);
