/* DAO jquery MyNotice JavaScript Document
 *
 * jQuery JavaScript Notification 
 *
 * Copyright 2011, Keio Kwan
 * Dual licensed under the MIT or GPL Version 2 licenses. -- not yet, but want to do it :D
 * klkkeio@hotmail.com
 *
 * Use with jquery.js
 * http://jquery.org/
 * Copyright 2011, John Resig
 * Released under the MIT or GPL Version 2 licenses.
 *
 * Date: Thrus May 19 11:11:00 2011 +0800
 * 
 * Script parameters
 * 
 *
 *
 */
 
(function($) {
    $.fn.showMyNotice = function(HTMLMessage, options) {
        var defaults = {
            CloseMethod: 'click', // if 'click', click div or it's control to close; if 'autoclose', notice div will be auto close with delay timer; if 'alwayson'
            AddCloseButton: true, // If CloseMethod='click' and this is true, it will auto add a close button; if CloseSelector is existed, will use CloseSelector insert of add a close button; otherwise, click the html to close
            CloseSelector: '', // this selector will override Auto added Close Button; if AddCloseButton = false, CloseSelector will be hide; otherwise, will be show;
            HideAndShowEffect: 'fade', // options: 'fade', 'slide', 'none'
            EffectTimer: 1000,
            ConsequencyEffect: true, // if true, effects will be queued; otherwise, will do all together
            DelayTimer: 0, // If ClickToClose = false, auto close delay with this timer
            NoticeCssClass: 'myNotice', // Notice Div Class, you can customize the css to make it what you like to show
            CloseButtonCssClass: 'myNoticeClose', // css class for auto added close button
            UseOverlay: false,
            OverlayCssClass: 'myNoticeOverlay',
            InsertBeforeSelector: '#s_wrap_main'
        };

        var opts = $.extend({}, defaults, options);

        return this.each(function() {

            var $obj = $(this);

            var $msg = $(HTMLMessage);
            // create close button
            var $closeButton = $('<a>');
            //var $closeButton = $('<a>').addclass(opts.CloseButtonCssClass).attr({href:'#', title:'close'}).text('close');
            $closeButton.addClass(opts.CloseButtonCssClass);
            $closeButton.attr({ href: '#', title: 'close' });
            $closeButton.text('close');
            // create notice
            var $notice = $('<div>');
            //var $notice = $('<div>').addclass(opts.NoticeCssClass).hide();
            $notice.addClass(opts.NoticeCssClass);
            //
            // add message into notice tag
            $notice.append($msg);

            if (opts.CloseSelector != '') // if customized close button, change close button to selector
            {
                $closeButton = $(opts.CloseSelector, $notice);
            }
            else // else, add the close button into notice
            {
                $notice.append($closeButton);
            }

            var _npos = $notice.position();

            // add the notice into object and make it show (display:none as default in NoticeCssClass)
            $notice.hide();
            //$obj.append($notice);
            if (opts.InsertBeforeSelector == '')
                $obj.append($notice);
            else
                $notice.insertBefore(opts.InsertBeforeSelector);
            $notice.fadeIn(opts.EffectTimer);

            var _top = parseInt(_npos.top);
            var _height = parseInt($notice.height());
            var _ptop = parseInt($notice.css('padding-top'));
            var _pbottom = parseInt($notice.css('padding-bottom'));

            var _tHeight = _top + _height + _ptop + _pbottom;

            // reset close position
            var _hClose = parseInt($closeButton.height() / 2);
            var _reposClose = parseInt((_tHeight / 2) - _hClose);

            // vertical align middle - close button
            if (opts.CloseSelector == '')
                $closeButton.css('top', _reposClose + 'px');
            /*if (_reposClose <= parseInt($msg.css('padding-right')))
            $closeButton.css('right',_reposClose+'px');*/

            //add padding to fix the hide area
            var _IsFixed = $notice.is(function() {
                return $(this).css('position') === 'fixed';
            });

            if (_IsFixed) {
                $obj.css('padding-top', _tHeight + 'px');
            }

            if (opts.UseOverlay) {
                var $overlayDiv = $('<div class="' + opts.OverlayCssClass + '">&nbsp;</div>');
                $overlayDiv.css('height', _tHeight + 'px');
                $overlayDiv.insertBefore($msg);
                $msg.css({ 'position': 'absolute', 'left': 0, 'top': 0, 'widht': '100%', 'display': 'block' });
            }

            $closeButton.bind('click', function(e) {
                $notice.fadeOut(opts.EffectTimer, function() {
                    if (_IsFixed) {
                        $obj.css('padding-top', '0px');
                    }
                });
                e.preventDefault();
            });

        });
    };
})(jQuery);
