/*
Copyright (c) 2008, James Punteney All rights reserved.
Code licensed under the BSD License:
http://www.punteney.com/does/license/
*/
if (typeof JP == "undefined" || !JP) { var JP = {};}

/******************
* This code provides for quick javascript notifices
* For more details go to: 
* http://www.punteney.com/does/javascript/notices/
*******************/
JP.Notices = {  
    // Set configuration elements here
    config: {
        notices_div: 'jp-notices',
        duration: 2500,
        show_close: true,
        icon_path: ''
    },
    // End Config
    
    notices_div: false,
    
    display: function(msg, css_class, duration, config) {
        if (!config) {
            config = {};   
        }
        config = YAHOO.lang.merge(JP.Notices.config, config); 
        config.icon_path = config.icon_path || '';
        if (config.icon_path !== '' && config.icon_path.charAt(config.icon_path.length - 1) != '/') {
            config.icon_path += '/';
        }
            
        if (!JP.Notices.notices_div) {
            // Check for the config value or set to default
            var notices_div = config.notices_div ? config.notices_div : 'jp-notices';
            notices_div = YAHOO.lang.isString(notices_div) ? YAHOO.util.Dom.get(notices_div) : notices_div;
            if (!notices_div) {
                notices_div = document.createElement('div');
                notices_div.id = config.notices_div ? config.notices_div : 'jp-notices';
                document.body.appendChild(notices_div);
            }
            JP.Notices.notices_div = notices_div;
        }
         
        var notice = document.createElement('div');
        var id = YAHOO.util.Dom.generateId(notice);
        YAHOO.util.Dom.addClass(notice, 'jp-notice');
        YAHOO.util.Dom.addClass(notice, css_class);
        if (config.show_spinner) {
            notice.innerHTML = '<img src="'+config.icon_path+'ajax-loader.gif" class="jp-spinner"> ' + msg; 
        } else {
            notice.innerHTML = msg;
        }
        JP.Notices.notices_div.appendChild(notice);
        
        var show_close = config.show_close ? config.show_close : config.show_close;
        if (typeof show_close == "undefined") {
            show_close = true;
        }
        if (show_close) {
            var close_icon = config.close_icon || 'close.gif';
            var notice_close = document.createElement('div');
            YAHOO.util.Dom.addClass(notice_close, 'notice-close');
            var notice_close_link = document.createElement('a');
            notice_close_link.setAttribute('href','#');
           
            notice_close_link.innerHTML = '<img src="'+config.icon_path+close_icon+'">';
            notice.appendChild(notice_close);
            notice_close.appendChild(notice_close_link);
            YAHOO.util.Event.addListener(notice_close_link, 'click', JP.Notices.close);
        }
        
        
        // Check and see if it positioned fixed if it is check for ie6 as it doesn't support fixed position
        if (YAHOO.util.Dom.getStyle(notices_div, 'position') == 'fixed') {
            if (!window.XMLHttpRequest) {
                // IE6, older browsers
                var scrollY = 0;
                if( document.documentElement && document.documentElement.scrollTop ) {
                    scrollY = document.documentElement.scrollTop;
                }
                else if( document.body && document.body.scrollTop ) {
                    scrollY = document.body.scrollTop;
                }
                else if( window.pageYOffset ) {
                    scrollY = window.pageYOffset;
                }
                else if( window.scrollY ) {
                    scrollY = window.scrollY;
                }
                YAHOO.util.Dom.setStyle(notices_div, 'position', 'absolute');
                YAHOO.util.Dom.setY(notices_div, scrollY);
            }
        }
        
        if(!duration) {
            if (duration !== 0) {
                duration = config.duration || 2500;
            }
        }
        if (duration  > 0) {
            setTimeout(function() { JP.Notices.remove(id); }, duration);
        }
        return id;
    },

    process_notice: function(msg) {
        return JP.Notices.display(msg, 'processing', 0, { show_spinner: true });
    },
        
    success_notice: function(msg, duration) {
        return JP.Notices.display(msg, 'success', duration);
    },

    error_notice: function(msg, duration) {
        return JP.Notices.display(msg, 'error', duration);
    },

    close: function(e) {
        YAHOO.util.Event.preventDefault(e);
        var close_link = YAHOO.util.Event.getTarget(e);
        JP.Notices.remove(YAHOO.util.Dom.getAncestorByClassName(close_link, 'jp-notice'));
    },
    
    remove_el: function(el) {
        var child_count = el.childNodes.length;
        for (var i=0; i<child_count; i++) {
            if (el.childNodes[i]) {
                JP.Notices.remove_el(el.childNodes[i]);   
            }
        }
        if (el.innerHTML) {
            el.innerHTML = '';   
        }
        var p = el.parentNode;
        p.removeChild(el);        
    },
       
    remove: function(notice) {
        notice = YAHOO.lang.isString(notice) ? YAHOO.util.Dom.get(notice) : notice;
        if(notice) {
            if (YAHOO.util.Anim) {
                var anim_hide = new YAHOO.util.Anim(notice, { height: {to: 0} }, .25);
                anim_hide.onComplete.subscribe(function(){JP.Notices.remove_el(notice);});  
                anim_hide.animate();
            } else {
                JP.Notices.remove_el(notice); 
            }
        }
    }
};
