jQuery.newClass('letram.tofou.TwoStateButton', {
    init:       function () {
        this.element = null;
        this.selected = false;
        this.currentState = false;
        this.part = jQuery.classes.letram.tofou.TwoStateButton.DEFAULT_PART;
        this.state = {
            normal:     {
                on:     null,
                off:    null
            },
            selected:   {
                on:     null,
                off:    null
            }
        };
        
        this.sourceType = jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_IMAGE;
        this.suffix = {
            normal:     {
                on:     jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_NORMAL_ON,
                off:    jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_NORMAL_OFF
            },
            selected:   {
                on:     jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_SELECTED_ON,
                off:    jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_SELECTED_OFF
            }
        };
        
        this.listeners = [];
    },
    statics:      {
        DEFAULT_PART:           3,
        
        IMAGE_NORMAL_OFF:       0,
        IMAGE_NORMAL_ON:        1,
        IMAGE_SELECTED_OFF:     2,
        IMAGE_SELECTED_ON:      3,
        
        MODE_GENERATE_URL:      0,
        MODE_ALL_DEFINED:       1,
        
        SOURCE_TYPE_IMAGE:      0,
        SOURCE_TYPE_BACKGROUND: 1,
        
        SUFFIX_NORMAL_OFF:      'a',
        SUFFIX_NORMAL_ON:       'b',
        SUFFIX_SELECTED_OFF:    'c',
        SUFFIX_SELECTED_ON:     'd',
        TOGGLE_IMAGE:           function () {
            return this.$.classes.TwoStateButton.toggleImage();
        },
        TOGGLE_STATE:           function () {
            return this.$.classes.TwoStateButton.toggleState();
        },
        isSelectedSource:       function (src) {
            var fi = src.getUrlInfo('-');
            if (fi === null) {
                return false;
            };
            return fi.suffix == jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_SELECTED_ON
                || fi.suffix == jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_SELECTED_OFF;
        },
        subscribes:             function (element, settings) {
            var self = element;
            var options = jQuery.extend({
                mode        : jQuery.classes.letram.tofou.TwoStateButton.MODE_GENERATE_URL,
                url         : null,
                part        : jQuery.classes.letram.tofou.TwoStateButton.DEFAULT_PART,
                source      : jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_IMAGE,
                selected    : false,
                images      : {
                    normal      : {
                        on  : null,
                        off : null},
                    selected    : {
                        on  : null,
                        off : null}},
                onCreate    : null}, settings);
            
            
            if ((!options.mode)
                    || ((options.mode != jQuery.classes.letram.tofou.TwoStateButton.MODE_ALL_DEFINED)
                        && (options.mode != jQuery.classes.letram.tofou.TwoStateButton.MODE_GENERATE_URL))) {
                options.mode = jQuery.classes.letram.tofou.TwoStateButton.MODE_GENERATE_URL;
                //options.part = 4;
            }
            
            if (!options.url) {
                options.url = null;
            }
            
            if ((!options.source)
                    || ((options.source != jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_IMAGE)
                        && (options.source != jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_BACKGROUND))) {
                options.source = jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_IMAGE;
            }
            
            $tools.$(self);
            self.$.classes.TwoStateButton = new jQuery.classes.letram.tofou.TwoStateButton();
            self.$.classes.TwoStateButton.element = self;
            self.$.classes.TwoStateButton.part = options.part;
            
            self.$.classes.TwoStateButton.sourceType = (self.nodeName.toLowerCase() == 'img')
                ? jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_IMAGE
                : jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_BACKGROUND;
            
            if (options.mode == jQuery.classes.letram.tofou.TwoStateButton.MODE_GENERATE_URL) {
                self.$.classes.TwoStateButton.setImage(options.url, options.part);
                
            } else {
                if (options.normal) {
                    if (options.normal.on) {
                        self.$.classes.TwoStateButton.setOnImage(options.normal.on);
                    };
                    if (options.normal.off) {
                        self.$.classes.TwoStateButton.setOffImage(options.normal.off);
                    };
                    
                };
                
                if (options.selected) {
                    if (options.selected.on) {
                        self.$.classes.TwoStateButton.setSelectedOnImage(options.selected.on);
                    };
                    
                    if (options.selected.off) {
                        self.$.classes.TwoStateButton.setSelectedOffImage(options.selected.off);
                    };
                    
                };
                
                
                
            };
            
            
            $(self).bind('mouseover', jQuery.classes.letram.tofou.TwoStateButton.TOGGLE_IMAGE)
                .bind('mouseout', jQuery.classes.letram.tofou.TwoStateButton.TOGGLE_IMAGE)
                .bind('click', jQuery.classes.letram.tofou.TwoStateButton.TOGGLE_STATE);
            
            if (options.onCreate != null) {
                options.onCreate.apply(self, []);
            };
        }
    },
    methods:        {
        getSourceImage:         function () {
            if (!this.element.src) {
                return null;
            };
            return this.element.src.unQuote('url(', ')').unQuote();
        },
        getBackgroundImage:     function () {
            url = $(this.element).css('background-image');
            if (!$tools.exists(url)) {
                url = new String(this.element.getActualStyle('backgroundImage'));
            };
            if (!$tools.exists(url)) {
                return null;
            };
            return url.unQuote('url(', ')').unQuote();
        },
        setImage:               function (url) {
            if (url == null) {
                if (!this.element) {
                    return false;
                };
                if (this.sourceType == jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_IMAGE) {
                    url = this.getSourceImage();
                } else if (this.sourceType == jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_BACKGROUND) {
                    url = this.getBackgroundImage();
                };
                if (!$tools.exists(url)) {
                    return false;
                };
            };
            switch (this.part) {
                case 2 : {
                    this.set2Images(url);
                } break;
                case 3 : {
                    this.set3Images(url);
                } break;
                case 4 : {
                    this.set4Images(url);
                } break;
                default : {
                    this.setOffImage(url);
                    this.setOnImage(url);
                    this.setSelectedOffImage(url);
                    this.setSelectedOnImage(url);
                } break;
            };
            return true;
        },
        set2Images:             function (url) {
            var fi = (new String(url)).getUrlInfo('-');
            if (fi === null) {
                return;
            };
            this.setOffImage(url);
            this.setOnImage(fi.changeSuffix(jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_NORMAL_ON));
            this.setSelectedOffImage(fi.changeSuffix(jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_NORMAL_OFF));
            this.setSelectedOnImage(fi.changeSuffix(jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_NORMAL_ON));
        },
        set3Images:             function (url) {
            var fi = (new String(url)).getUrlInfo('-');
            if (fi === null) {
                return;
            };
            this.setOffImage(url);
            this.setOnImage(fi.changeSuffix(jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_NORMAL_ON));
            this.setSelectedOffImage(fi.changeSuffix(jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_SELECTED_OFF));
            this.setSelectedOnImage(fi.changeSuffix(jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_SELECTED_OFF));
        },
        set4Images: function (url) {
            var fi = (new String(url)).getUrlInfo('-');
            if (fi === null) {
                return;
            };
            this.setOffImage(url);
            this.setOnImage(fi.changeSuffix(jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_NORMAL_ON));
            this.setSelectedOffImage(fi.changeSuffix(jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_SELECTED_OFF));
            this.setSelectedOnImage(fi.changeSuffix(jQuery.classes.letram.tofou.TwoStateButton.SUFFIX_SELECTED_ON));
        },
        setOffImage:            function (url) {
            var img = new Image();
            img.src = url;
            this.state.normal.off = img.src;
        },
        setOnImage:             function (url) {
            var img = new Image();
            img.src = url;
            this.state.normal.on = img.src;
        },
        setSelectedOffImage:    function (url) {
            var img = new Image();
            img.src = url;
            this.state.selected.off = img.src;
        },
        setSelectedOnImage:     function (url) {
            var img = new Image();
            img.src = url;
            this.state.selected.on = img.src;
        },
        on:                     function () {
            if (this.element == null) {
                return true;
            };
            var tmp = null;
            if (this.selected == true) {
                if (this.sourceType == jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_IMAGE) {
                    if (this.element.src != this.state.selected.on) {
                        this.element.src = this.state.selected.on;
                    };
                } else if (this.sourceType == jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_BACKGROUND) {
                    if (this.getBackgroundImage() != this.state.selected.on) {
                        $(this.element).css('background-image', 'url(' + this.state.selected.on + ')');
                    };
                };
            } else {
                if (this.sourceType == jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_IMAGE) {
                    if (this.element.src != this.state.normal.on) {
                        this.element.src = this.state.normal.on;
                    };
                } else if (this.sourceType == jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_BACKGROUND) {
                    if (this.getBackgroundImage() != this.state.normal.on) {
                        $(this.element).css('background-image', 'url(' + this.state.normal.on + ')');
                    };
                };
            };
            this.currentState = true;
            return true;
        },
        off:                    function () {
            if (this.element == null) {
                return true;
            };
            if (this.selected == true) {
                if (this.sourceType == jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_IMAGE) {
                    if (this.element.src != this.state.selected.off) {
                        this.element.src = this.state.selected.off;
                    };
                } else if (this.sourceType == jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_BACKGROUND) {
                    if (this.getBackgroundImage() != this.state.selected.off) {
                        $(this.element).css('background-image', 'url(' + this.state.selected.off + ')');
                    };
                };
            } else {
                if (this.sourceType == jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_IMAGE) {
                    if (this.element.src != this.state.normal.off) {
                        this.element.src = this.state.normal.off;
                    };
                } else if (this.sourceType == jQuery.classes.letram.tofou.TwoStateButton.SOURCE_TYPE_BACKGROUND) {
                    if (this.getBackgroundImage() != this.state.normal.off) {
                        $(this.element).css('background-image', 'url(' + this.state.normal.off + ')');
                    };
                };
            };
            this.currentState = false;
            return true;
        },
        toggleImage:            function () {
            return (this.currentState == true)
                ? this.off()
                : this.on();
        },
        unselect:               function () {
            if (this.element == null) {
                return true;
            };
            this.selected = false;
            return this.toggleImage();
        },
        select:                 function () {
            if (this.element == null) {
                return true;
            };
            this.selected = true;
            return this.toggleImage();
        },
        toggleState:            function () {
            if (this.element == null) {
                return true;
            };
            return (this.selected == true)
                ? this.unselect()
                : this.select();
        }
    }
});

jQuery.fn.twoState = function () {
    var selected = $tools.coalesce(arguments[0], []);
    var settings = $tools.coalesce(arguments[1], {checkUrl: true});
    
    return this.each(function () {
        var setting = jQuery.extend({}, settings);
        
        var pr = this.parentNode;
        if ((pr.href != document.location)
                || (pr.nodeName.toLowerCase() != 'a')) {
            pr = null;
        };
        
        /*
        if (pr==null) alert('jQuery.fn.twoState : null');
        else alert('Pas null !');
        */

        if ((0 <= selected.indexOf(this.id))
                || ((pr != null) && (setting.checkUrl == true))
                || (jQuery.classes.letram.tofou.TwoStateButton.isSelectedSource(this.src))) {
            var _onCreate = setting.onCreate || function() {};
            setting.onCreate = function () {
                arguments.callee._onCreate.apply(this, []);
                
                this.$.classes.TwoStateButton.select();
                
                if (pr) {
                    $(pr).bind('click', function (event) {
                        event.preventDefault();
                        event.stopPropagation();
                        return false;
                    }).css('cursor', 'default');
                    
                };
                
                if ([1, 3].contains(this.$.classes.TwoStateButton.part)) {
                    $(this)
                        .unbind('click', jQuery.classes.letram.tofou.TwoStateButton.TOGGLE_STATE)
                        .unbind('mouseover', jQuery.classes.letram.tofou.TwoStateButton.TOGGLE_IMAGE)
                        .unbind('mouseout', jQuery.classes.letram.tofou.TwoStateButton.TOGGLE_IMAGE);
                    
                }
                
            }
            setting.onCreate._onCreate = _onCreate;
            
        };
        
        jQuery.classes.letram.tofou.TwoStateButton.subscribes(this, setting);
    });
};







