(function() {

    YAHOO.namespace("shift.assist");       
    
    /**
     * Client Check Window
     * This window asks if the user already is a customer
     */
    YAHOO.shift.assist.clientCheckWindow = function() {
        
        /**
         * YAHOO SimpleDialog Widget
         * @var object
         */
        var simpledialog;        
        
        /**
         * Event Handler (returning customer)
         * Call main window
         */
        function onYes() {
            this.hide();
            YAHOO.util.Dom.get("c_isnew").value = "0";
            YAHOO.util.Dom.get("c_tosagreed").checked = true;
            YAHOO.shift.assist.tabView.selectModuleTab();
            YAHOO.shift.assist.mainWindow.show();
        }        
        
        /**
         * Event Handler (new customer)
         * Call main window
         */
        function onNo() {
            this.hide();            
            YAHOO.util.Dom.get("c_isnew").value = "1";
            YAHOO.util.Dom.get("c_tosagreed").checked = false;
            YAHOO.shift.assist.tabView.selectNewClientTab();
            YAHOO.shift.assist.mainWindow.show();
        }        
        
        /**
         * Event Handler         
         */
        function onCancel() {
            this.hide();
        }
        
        return {                    
            
            /**
             * Initialization Sequence
             * @param string id HTML el
             */
            init: function(id) {
                var conf = {
                    width: "350px",
                    modal: true,
                    fixedcenter: true,
                    visible: false,
                    draggable: false,
                    close: true,			   
                    icon: YAHOO.widget.SimpleDialog.ICON_INFO,
                    constraintoviewport: true,
                    buttons: [ 
                        {text:"Oui", handler: onYes},
                        {text:"Non",  handler: onNo},
                        {text:"Annuler", handler: onCancel} 
                    ]
                };
                simpledialog = new YAHOO.widget.SimpleDialog(id, conf);
                simpledialog.render();
            },            
            
            /**
             * Display Window
             */
            show: function() {
                simpledialog.show();
            },            
            
            /**
             * Hide Window
             */
            hide: function() {
                simpledialog.hide();
            }
        }
    }();
    
    /**
     * Message Window
     * This widget is used to display informative message to the user
     */
    YAHOO.shift.assist.msgWindow = function() {
        
        /**
         * YAHOO SimpleDialog Widget
         * @var object
         */
        var simpledialog;
        
        /**
         * Event Handler
         */
        function onOk() {
            this.hide();
        }
        
        return {
            
            /**
             * Initialization Sequence
             * @param string id HTML el
             */
            init: function(id) {
                var conf = {
                    visible: false,
                    fixedcenter: true,
                    modal: true,
                    buttons: [
                        {text: "Ok", handler: onOk, isDefault: true}
                    ]
                };
                simpledialog = new YAHOO.widget.SimpleDialog(id, conf);
                simpledialog.render();
            },
            
            /**
             * Display the Message Window
             * @param object conf configuration object, key/value pair (icon, text)
             */
            show: function(conf) {
                if (!YAHOO.lang.hasOwnProperty(conf, "icon")) {
                    conf.icon = YAHOO.widget.SimpleDialog.ICON_INFO;
                }
                if (!YAHOO.lang.hasOwnProperty(conf, "text")) {
                    conf.text = "No Message";
                }
                simpledialog.cfg.setProperty("icon", conf.icon);
                simpledialog.cfg.setProperty("text", conf.text);
                simpledialog.show();
            }
        }
    }();
    
    /**
     * Loading Window
     * This widget displays a loading bar
     */
    YAHOO.shift.assist.loadingWindow = function() {
        
        /**
         * YAHOO Panel Widget
         * @var object
         */
        var panel;   
        
        return {
            
            /**
             * Initialization Sequence
             * @param string id HTML el
             */
            init: function(id) {
                var conf = { 
                    width: "240px", 
                    fixedcenter: true, 
                    close: false, 
                    draggable: false, 
                    zindex: 4,
                    modal: true,
                    visible: false
                };
                panel = new YAHOO.widget.Panel(id, conf);
                panel.render();   
            }, 
            
            /**
             * Display modal "Loading Window" for N ms            
             */
            wait: function(ms) {
                panel.show();
                YAHOO.lang.later(ms, panel, 'hide');
            }
        }
    }();    
    
    /**
     * TabView inside Main Window
     */
    YAHOO.shift.assist.tabView = function() {
        
        /**
         * YAHOO TabView Widget
         * @var object
         */
        var tabview;
        
        return {
            
            /**
             * Initialization Sequence
             * @param string id HTML el
             */
            init: function(id) {
                tabview = new YAHOO.widget.TabView(id);                
            },
            
            /**
             * Make the "module" tab the active tab
             */
            selectModuleTab: function() {
                tabview.selectTab(0);
            },
            
            /**
             * Make the "new client" tab the active tab
             */
            selectNewClientTab: function() {
                tabview.selectTab(1);
            }
        }
    }();
    
    /**
     * Main Application Window
     */
    YAHOO.shift.assist.mainWindow = function() {
        
        /**
         * YAHOO Dialog Widget
         * @var object
         */
        var dialog;        
        
        /**
         * Event Handler
         * Perform a check on required fields before submitting the form
         */
        function onSubmit() {
            if (this.validate()) {
                this.submit();
                YAHOO.shift.assist.loadingWindow.wait(3500);
            }
        }
        
        /**
         * Event Handler         
         */
        function onCancel() {
            this.cancel();
        }
        
        /**
         * Fields Checking
         * If some required fields are empty the function calls the Message Window
         */
        function validate() {
            var data = this.getData(),
                msg  = [];
            
            if (data.c_isnew == "1") {
                if (!YAHOO.lang.trim(data.c_name) || !YAHOO.lang.trim(data.c_company) || !YAHOO.lang.trim(data.c_email) || !YAHOO.lang.trim(data.c_phone) || !YAHOO.lang.trim(data.c_address1) || !YAHOO.lang.trim(data.c_zip) || !YAHOO.lang.trim(data.c_city)) {
                    msg.push("<li>Veuillez remplir tous les champs obligatoires.</li>");
                }
            }
            
            if (!YAHOO.lang.hasOwnProperty(data, "c_winver")) {
                msg.push("<li>Veuillez s&eacute;lectionner votre version de Windows.</li>");
            }
            
            if (!data.c_tosagreed) {
                msg.push("<li>Vous devez accepter nos conditions avant de pouvoir b&eacute;n&eacute;ficier de notre assistance.</li>");
            }
            
            if (msg.length) {
                YAHOO.shift.assist.msgWindow.show({
                    icon: YAHOO.widget.SimpleDialog.ICON_BLOCK,
                    text: "<ul>" + msg.join("") + "</ul>"
                });
                return false;
            }
            
            return true;
        }
        
        return {
            
            /**
             * Initialization Sequence
             * @param string id HTML el
             */
            init: function(id) {                
                var conf = {
                    width: "350px", 
                    modal: true,
                    fixedcenter: true, 
                    visible: false,  
                    constraintoviewport: true, 
                    hideaftersubmit: false,      
                    postmethod: "form",
                    buttons: [ 
                        { text:"T&eacute;l&eacute;charger", handler:onSubmit },
                        { text:"Annuler", handler:onCancel }
                    ]
                };
                dialog = new YAHOO.widget.Dialog(id, conf);
                dialog.validate = validate;                
                dialog.render();                    
            },
            
            /**
             * Display Main Window
             */
            show: function() {                
                dialog.show();
            },
            
            /**
             * Hide Main Window
             */
            hide: function() {
                dialog.hide();
            }
        }
    }();
    
    /**
     * Application Launcher     
     */
    YAHOO.shift.assist.mainWindowLauncher = function() {
        
        /**
         * Event Handler
         */
        function onClick() {            
            YAHOO.shift.assist.clientCheckWindow.show();
        }
        
        return {
            
            /**
             * Initialization Sequence
             * @param string id HTML el
             */
            init: function(id) {              
                YAHOO.util.Event.addListener(id, "click", onClick);                
            }
        }
    }();
    
    /**#@+
     * Execute the initialization sequence as soon as all required DOM elements are in ready state
     */    
    YAHOO.util.Event.onContentReady("shiftAssistMainWindow", function(){
        YAHOO.shift.assist.mainWindow.init("shiftAssistMainWindow");
        YAHOO.shift.assist.tabView.init("shiftAssistTabView");
    });    
    YAHOO.util.Event.onContentReady("shiftAssistMiscWindows", function(){
        YAHOO.shift.assist.clientCheckWindow.init("shiftAssistClientCheckWindow");
        YAHOO.shift.assist.msgWindow.init("shiftAssistMsgWindow");
        YAHOO.shift.assist.loadingWindow.init("shiftAssistLoadingWindow");
    });    
    YAHOO.util.Event.onContentReady("shiftAssistMainWindowLauncher", function(){
        YAHOO.shift.assist.mainWindowLauncher.init("shiftAssistMainWindowLauncher");
    });
	    YAHOO.util.Event.onContentReady("shiftAssistMainWindowLauncher2", function(){
        YAHOO.shift.assist.mainWindowLauncher.init("shiftAssistMainWindowLauncher2");
    });
    /**#@-*/
    
})();
