/*
$Id: jquery.custom_expose.js,v 1.0 2010/05/08 15:18:50 stan Exp $
vim: set ts=2 sw=2 sts=2 et:
*/

(function($) {
    
    $.fn.customExpose = function(settings) {

    // Settings arr
    settings = jQuery.extend({
      overlayBgColor: '#070252',
      overlayOpacity: 0.65
    },settings);

    var msg = this.html();

    function _start() {
      if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) {
        $('select').css({'visibility' : 'hidden'});
      }
        _set_interface();
    
      return true;
    }

    function __overlay_dimensions() {
      var headerHeight    = $("#header").outerHeight(true);
      var containerWidth  = $("#page-container").outerWidth(true);
      var rightBarWidth   = $("#right-bar").outerWidth(true);
      var authBoxHeight   = $("#customer-auth-box").outerHeight(true);
      var msgBoxWidth     = $("#jquery-message-box").width();
      var pageHeight      = $(document).height();
      var pageWidth       = $(document).width();
  
      $('#jquery-custom-overlay-center').css({
        backgroundColor:    settings.overlayBgColor,
        opacity:            settings.overlayOpacity,
        width:              containerWidth - rightBarWidth,
        height:             pageHeight - headerHeight,
        top:                headerHeight
      });

      $('#jquery-custom-overlay-right').css({
        backgroundColor:    settings.overlayBgColor,
        opacity:            settings.overlayOpacity,
        width:              rightBarWidth,
        height:             pageHeight - headerHeight - authBoxHeight,
        top:                headerHeight + authBoxHeight,
        left:               containerWidth - rightBarWidth
      });
      
      $("#jquery-message-box").css({
        left: (pageWidth - msgBoxWidth) / 2
      });
    }

    function _set_interface() {
      // Apply the HTML markup into body tag
      $('body').append('<div id="jquery-custom-overlay-center"></div><div id="jquery-custom-overlay-right"></div><div id="jquery-message-box"></div>');
      
      __overlay_dimensions();

      $('#jquery-custom-overlay-center,#jquery-custom-overlay-right').fadeIn();
      $("#jquery-message-box").html(msg).show();
      $(window).scrollTop(0);
              
      // Assigning click events in elements to close overlay
      $('#jquery-custom-overlay-center,#jquery-custom-overlay-right').click(function() {
        _finish();
      });

      // If window was resized, calculate the new overlay dimensions
      $(window).bind('resize.customExposeDim', function() { __overlay_dimensions(); });
    }

    function _finish() {
      $(window).unbind('resize.customExposeDim');

      $('#jquery-message-box').remove();
      $('#jquery-custom-overlay-center,#jquery-custom-overlay-right').fadeOut(function() {
        $(this).remove();
      });
      
      $('select').css({'visibility' : 'visible'});
    }

    return _start();
  };

})(jQuery); // Call and execute the function immediately passing the jQuery object


