/***
	Component Name: Notifications
	Author: Carl Danley
	Version: 1.0
	Description:
		A javascript component written to provide an easy-to-use means of notifying users.
	Supported Browsers:
		Internet Explorer, Chrome, Opera, Safari, Firefox
*/
(function (H, I) {
    var J = H.document;

    function note() {
        var j = this;
        j.settings = {
            'dependencies': ['http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'],
            'spacing': {
                'x': 10,
                'y': 10
            }
        };
        j.notes = [];
        var k = function () {
                var b = j.settings.dependencies;
                var c = b.length;
                j.settings.dependencies_loaded = 0;
                var e = (function (d, t, a) {
                    return function () {
                        d++;
                        if (d === t) {
                            a()
                        }
                    }
                })(j.settings.dependencies_loaded, c, o);
                for (var i = 0; i < c; i++) {
                    b[i] += '?time=' + (new Date()).getTime();
                    m(b[i], e, true)
                }
            };
        var l = function (a, b, c) {
                if (H.addEventListener) {
                    b.addEventListener(a, c, false)
                } else if (J.attachEvent) {
                    b.attachEvent('on' + a, c)
                }
            };
        var m = function (a, b, d) {
                var e = J.getElementsByTagName('head')[0];
                var g = J.createElement('script');
                g.type = 'text/javascript';
                g.src = a;
                var i = (function (h, s, c, r) {
                    return function () {
                        if (c !== I) {
                            c()
                        }
                        if (r === I || r === true) {
                            h.removeChild(s)
                        }
                    }
                })(e, g, b, d);
                g.onload = i;
                g.onreadystatechange = (function (f, s) {
                    return function () {
                        if (s.readyState === 'complete' || s.readyState === 'loaded') {
                            f()
                        }
                    }
                })(i, g);
                //e.appendChild(g)
            };
        var o = function () {
                B()
            };
        var p = function (a, b) {
                if (b.currentStyle) {
                    return b.currentStyle[a]
                } else {
                    return J.defaultView.getComputedStyle(b, null).getPropertyValue(a)
                }
            };
        var q = function () {
                var c = false;
                var d = [];
                var f = this;
                var g = function () {
                        if (c) {
                            return
                        }
                        try {
                            J.documentElement.doScroll("left")
                        } catch (error) {
                            setTimeout((function (a) {
                                return function () {
                                    a()
                                }
                            })(g), 1);
                            return
                        }
                        h()
                    };
                var h = function () {
                        if (c) {
                            return
                        }
                        for (var i = 0, len = d.length; i < len; i++) {
                            d[i]()
                        }
                        c = true
                    };
                f.ready = function (a) {
                    d[d.length] = a
                };
                f.listen = function () {
                    if (J.readyState === "complete") {
                        return on_ready()
                    }
                    if (J.addEventListener) {
                        l('DOMContentLoaded', J, (function (a) {
                            return function () {
                                a()
                            }
                        })(h));
                        l('load', H, (function (a) {
                            return function () {
                                a()
                            }
                        })(h))
                    } else if (J.attachEvent) {
                        l('load', H, (function (a) {
                            return function () {
                                a()
                            }
                        })(h));
                        l('readystatechange', J, (function (a) {
                            return function () {
                                a()
                            }
                        })(h));
                        var b = false;
                        try {
                            b = H.frameset === null
                        } catch (e) {};
                        if (J.documentElement.doScroll && b) {
                            g()
                        }
                    }
                }
            };
        var u = function (a, b) {
                b.setAttribute('class', a);
                b.setAttribute('className', a)
            };
        var v = function () {
                var a = J.body.scrollTop;
                if (a === 0) {
                    if (H.pageYOffset) {
                        a = H.pageYOffset
                    } else {
                        a = (J.body.parentElement) ? J.body.parentElement.scrollTop : 0
                    }
                }
                return a
            };
        var z = function () {
                var w;
                var h;
                if (typeof H.innerWidth !== 'undefined') {
                    w = H.innerWidth;
                    h = H.innerHeight
                } else if (typeof J.documentElement !== 'undefined' && typeof J.documentElement.clientWidth !== 'undefined' && J.documentElement.clientWidth !== 0) {
                    w = J.documentElement.clientWidth;
                    h = J.documentElement.clientHeight
                } else {
                    w = J.getElementsByTagName('body')[0].clientWidth;
                    h = J.getElementsByTagName('body')[0].clientHeight
                }
                return [w, h]
            };
        var A = function (a) {
                var b = '';
                for (var i in a) {
                    b += i + ': ' + a[i] + '; '
                }
                return b
            };
        var B = function () {
                l('resize', H, (function (a) {
                    return function (e) {
                        a()
                    }
                })(C));
                setInterval((function (a, b) {
                    return function () {
                        if (a() === true) {
                            b()
                        }
                    }
                })(G, C), 50);
                /*if (J.domain !== 'marketplace.codebyter.com') {
                    j.show('This notification module has been pirated by the owner of <b>' + J.domain + '</b>. Please contact <a href="mailto:carldanley@gmail.com">Carl Danley</a> to report this blatant act of piracy.', 'error', {
                        'anchor': 'br',
                        'autoremove': false,
                        'closeable': false
                    })
                }*/
                $demo.init()
            };
        j.show = function (b, c, d) {
            c = (c !== I || c === '') ? c : 'info';
            if (d === I) {
                d = {
                    'anchor': 'br',
                    'width': 250,
                    'autoremove': true,
                    'lifetime': 4000,
                    'closeable': true
                }
            } else {
                d.anchor = (d.anchor !== I) ? d.anchor : 'br';
                d.width = (d.width !== I) ? d.width : 250;
                d.autoremove = (d.autoremove !== I) ? d.autoremove : true;
                d.lifetime = (d.lifetime !== I) ? d.lifetime : 4000;
                d.closeable = (d.closeable !== I) ? d.closeable : true
            }
            d.type = c;
            var e = D(c, b, d);
            var f = z();
            e.settings = d;
            j.notes.push(e);
            J.body.appendChild(e);
            F(e, c, d.anchor);
            C();
            var g = (function (a, n) {
                return function () {
                    a(n)
                }
            })(j.hide, e);
            var h = (function (r, a) {
                return function () {
                    jQuery(a).animate({
                        'opacity': '0.00'
                    }, 3000, r)
                }
            })(g, e);
            if (d.autoremove === true) {
                setTimeout(h, d.lifetime)
            }
        };
        j.hide = function (a) {
            var b;
            for (var i = 0, len = j.notes.length; i < len; i++) {
                b = j.notes[i];
                if (a === b) {
                    j.notes.splice(i, 1);
                    break
                }
            }
            J.body.removeChild(a);
            for (var i = 0, len = j.notes.length; i < len; i++) {
                b = j.notes[i];
                if (b.settings.anchor === a.settings.anchor) {
                    F(b, b.settings.type, b.settings.anchor)
                }
            }
        };
        var C = function () {
                var a = z();
                var b, pos;
                for (var i = 0, len = j.notes.length; i < len; i++) {
                    b = j.notes[i];
                    pos = F(b, b.settings.type, b.settings.anchor)
                }
            };
        var D = function (a, b, c) {
                var d = J.createElement('div');
                var e = J.createElement('div');
                e.style.cssText = 'float: left; display: inline;';
                d.appendChild(e);
                var f = J.createElement('div');
                f.style.cssText = 'float: left; display: inline;';
                f.innerHTML = b;
                d.appendChild(f);
                var g = J.createElement('div');
                g.style.cssText = 'clear: both;';
                d.appendChild(g);
                if (c.autoremove === false && c.closeable === true) {
                    var i = J.createElement('div');
                    u('note_close', i);
                    l('click', i, (function (h, n) {
                        return function () {
                            h(n)
                        }
                    })(j.hide, d));
                    d.appendChild(i)
                }
                if (a === 'info') {
                    u('note_container note_info', d);
                    u('note_icon_info', e);
                    u('note_text_info', f)
                } else if (a === 'error') {
                    u('note_container note_error', d);
                    u('note_icon_error', e);
                    u('note_text_error', f)
                } else if (a === 'warning') {
                    u('note_container note_warning', d);
                    u('note_icon_warning', e);
                    u('note_text_warning', f)
                } else if (a === 'confirm') {
                    u('note_container note_confirm', d);
                    u('note_icon_confirm', e);
                    u('note_text_confirm', f)
                } else {
                    u('note_container note_info', d);
                    u('note_icon_info', e);
                    u('note_text_info', f)
                }
                if(jQuery.browser.msie) {
                    d.style.position = 'absolute';
                } else {
                    d.style.position = 'fixed';
                }
                d.style.width = c.width + 'px';
                f.style.maxWidth = (c.width - 50) + 'px';
                return d
            };
        var E = function (a, b, c) {
                var d = [],
                    tmp, y = 0;
                for (var i = 0, len = j.notes.length; i < len; i++) {
                    tmp = j.notes[i];
                    if (tmp === a) {
                        break
                    }
                    if (tmp.settings.anchor === c) {
                        d.push(tmp)
                    }
                }
                if (d.length === 0) {
                    return y
                }
                var e = j.settings.spacing.y;
                for (var i = 0, len = d.length; i < len; i++) {
                    tmp = d[i];
                    if (c === 'tl' || c === 'tr' || c === 'tm') {
                        y += tmp.offsetHeight + e
                    } else {
                        y -= tmp.offsetHeight + e
                    }
                }
                return y
            };
        var F = function (a, b, c) {
                var d = z();
                var e = v();
                var x = 0,
                    y = 0;
                var f = E(a, b, c);
                y += f;
                var g = j.settings.spacing;
                if (c === 'br') {
                    x += d[0] - g.x - a.offsetWidth;
                    x -= (G() === true) ? 20 : 0;
                    y += e + d[1] - g.y - a.offsetHeight
                } else if (c === 'tr') {
                    x += d[0] - g.x - a.offsetWidth;
                    x -= (G() === true) ? 20 : 0;
                    y += e + g.y
                } else if (c === 'bl') {
                    x += g.x;
                    y += e + d[1] - g.y - a.offsetHeight
                } else if (c === 'tl') {
                    x += g.x;
                    y += e + g.y
                } else {
                    x += d[0] - g.x - a.offsetWidth;
                    x -= (G() === true) ? 20 : 0;
                    y += e + d[1] - g.y - a.offsetHeight
                }
                a.style.left = x + 'px';
                a.style.top = y + 'px'
            };
        var G = function () {
                return (jQuery(J).height() > jQuery(H).height()) ? true : false
            };
        j.dom = new q();
        j.dom.listen();
        j.dom.ready((function (a) {
            return function () {
                a()
            }
        })(k))
    };
    H.$note = new note()
})(window);
