~lss-team/lilsoftstats/trunk

« back to all changes in this revision

Viewing changes to js/jquery/jquery.usermode.js

  • Committer: Nick
  • Date: 2011-11-14 04:10:28 UTC
  • Revision ID: nick@little-apps.org-20111114041028-cvmpwq6z6hx3pkya
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @author trixta
 
3
 */
 
4
(function($){
 
5
        $.userMode = (function(){
 
6
                var userBg, 
 
7
                        timer, 
 
8
                        testDiv,
 
9
                        boundEvents = 0;
 
10
                
 
11
                function testBg(){
 
12
                        testDiv = testDiv || $('<div></div>').css({position: 'absolute', left: '-999em', top: '-999px', width: '0px', height: '0px'}).appendTo('body');
 
13
                        var black = $.curCSS( testDiv.css({backgroundColor: '#000000'})[0], 'backgroundColor', true),
 
14
                                white = $.curCSS( testDiv.css({backgroundColor: '#ffffff'})[0], 'backgroundColor', true),
 
15
                                newBgStatus = (black === white || white === 'transparent');
 
16
                        if(newBgStatus != userBg){
 
17
                                userBg = newBgStatus;
 
18
                                $.event.trigger('_internalusermode');
 
19
                        }
 
20
                        return userBg;
 
21
                }
 
22
                
 
23
                function init(){
 
24
                        testBg();
 
25
                        timer = setInterval(testBg, 3000);
 
26
                }
 
27
                
 
28
                function stop(){
 
29
                        clearInterval(timer);
 
30
                        testDiv.remove();
 
31
                        testDiv = null;
 
32
                }
 
33
                
 
34
                $.event.special.usermode = {
 
35
                        setup: function(){
 
36
                                (!boundEvents && init());
 
37
                                boundEvents++;
 
38
                                var jElem = $(this)
 
39
                                        .bind('_internalusermode', $.event.special.usermode.handler);
 
40
                                //always trigger
 
41
                                setTimeout(function(){
 
42
                                        jElem.triggerHandler('_internalusermode');
 
43
                                }, 1);
 
44
                return true;
 
45
            },
 
46
                        teardown: function(){
 
47
                boundEvents--;
 
48
                                (!boundEvents && stop());
 
49
                                $(this).unbind('_internalusermode', $.event.special.usermode.handler);
 
50
                return true;
 
51
            },
 
52
            handler: function(e){
 
53
                e.type = 'usermode';
 
54
                                e.disabled = !userBg;
 
55
                                e.enabled = userBg;
 
56
                return jQuery.event.handle.apply(this, arguments);
 
57
            }
 
58
                };
 
59
                
 
60
                return {
 
61
                        get: testBg
 
62
                };
 
63
                
 
64
        })();
 
65
        
 
66
        $.fn.userMode = function(fn){
 
67
                return this[(fn) ? 'bind' : 'trigger']('usermode', fn);
 
68
        };
 
69
        
 
70
        $(function(){
 
71
                $('html').userMode(function(e){
 
72
                        $('html')[e.enabled ? 'addClass' : 'removeClass']('hcm');
 
73
                });
 
74
        });
 
75
})(jQuery);