YUI().use("node", "anim", "event", function(Y) {

    
    
    var widgetFindPreschool = {
    
        tooltip : null,
        tooltipAnim : null,
        textField : null,
        validLength : 6,
        status : 'closing',
        map : null,
        map_toggle : null,
        map_container : null,
        geocoder : null,
        marker : null,
        
        KML_FILE : "http://www.spf.sk.ca/fileadmin/template1/common/javascript/schools1.kml",
        
        
        
        init : function()
        {
            var obj = this;
            var duration = ( Y.UA.ie ) ? 0.3 : 1;
            
            this.tooltip = Y.one('#postal_code_tooltip');
            this.tooltip.plug(Y.Plugin.NodeFX, {
                from: { opacity: 1 },
                to: {
                    opacity: 0
                },
         
                easing: Y.Easing.easeOut,
                duration: duration
            });

            this.map_container = Y.one('#map_container');
            this.map_container.plug(Y.Plugin.NodeFX, {
                from: { bottom: -465 },
                to: { bottom: -45 },

                easing: Y.Easing.easeOut,
                duration: 0.7
            }); 
            
            this.textField = Y.one('#pcode');
            this.geocoder = new GClientGeocoder();
            
            this.map_toggle = Y.one('#map_toggle');
            
            Y.on("loadmap|focus", obj.loadMap, '#pcode', obj);
            Y.on('keyup', obj.onKeyUpEvent, '#pcode', obj);
            
            Y.on("click", obj.toggleMap, "#map_toggle", obj);
            Y.on("loadmap|mouseenter", obj.loadMap, "#map_toggle", obj);            
        },
        
        
        getNormalizedValue : function()
        {
            var value = this.textField.get('value');
            value = value.replace(/[\s]*/mig, "" );
            value = value.toUpperCase();
            
            return value;
        },
        
        isValidLength : function ()
        {
            var value = this.getNormalizedValue();
            if ( value.length == this.validLength ) {
                return true;
            }
            return false;
        },

        isValidFormat : function ()
        {
            var value = this.getNormalizedValue();
            var valid = value.match(/(S7[HJKLMNPRSTV]{1,1}|S0(L|K))\d\w\d/mig);
            
            if ( valid ) {
                return true;   
            }
            return false;
        },
        
        isValidCode : function()
        {
            if ( this.isValidLength() && this.isValidFormat() ) {
                return true;
            }
            return false;
        },
        
        setTooltipText : function( text )
        {
            this.tooltip.one('.bd').set( 'text', text );    
        },
        
        onStartEvent : function( evt )
        {
            if ( this.status == 'opening' ) {
                this.tooltip.setStyle('opacity',0);
                this.tooltip.removeClass('hide');
            }
        },
        
        onEndEvent : function( evt )
        {
            if ( this.status == 'closing' ) {
                this.tooltip.addClass('hide');
            } else {
                this.tooltip.setStyle('filter',''); // for IE, this will remove the filter layer used for opacity
            }
        },

        onKeyUpEvent : function( e )
        {
            // arrow keys or space, do nothing
            if ( ( e.keyCode > 36 && e.keyCode < 41 ) || e.keyCode == 32  ) {
                return;
            }
            
            // enter key
            if ( e.keyCode == 13 ) {
                  
                if ( this.isValidCode() ) {
                   
 
                    this.lookupPostalCode();
                    if ( !this.isMapOpen() ) {
                        this.toggleMap();
                    }
                    this.hideTooltip();                    
               


                } else {
                    this.setTooltipText('Please enter a valid postal code within Saskatoon.');
                    this.showTooltip();            
                }
            } else {

                if ( this.isValidLength() ) {
                    
                    if ( this.isValidFormat() ) {
                    
                        this.setTooltipText('Press \'Enter\' to view map.');
                        this.showTooltip();

                    } else {
                        this.setTooltipText('Please enter a valid postal code within Saskatoon.');
                        this.showTooltip();
                    }
                } else {
                    this.hideTooltip();
                }
            }            
        },
        
        showTooltip : function()
        {
            if ( this.status == 'closing' ) {
                this.status = 'opening';
                this.toggleTooltip();
            }
        },
        
        hideTooltip : function()
        {
            if ( this.status == 'opening' ) {
                this.status = 'closing';
                this.toggleTooltip();
            }    
        },
        
        toggleTooltip : function()
        {
            this.tooltip.toggleClass('yui-closed');
            this.tooltip.fx.set('reverse', !this.tooltip.fx.get('reverse')); // toggle reverse
            this.tooltip.fx.on('start',this.onStartEvent, this); 
            this.tooltip.fx.on('end',this.onEndEvent,this);       
            this.tooltip.fx.run();
        },
        
        loadMap : function()
        {            
            Y.detach("loadmap|*");
            
            this.map = new google.maps.Map2(document.getElementById("map_placeholder"));
           
            this.map.setCenter(new google.maps.LatLng(52.1397521, -106.6489768), 14);
            this.map.setUIToDefault();
            
            var kml = new GGeoXml( this.KML_FILE );
            this.map.addOverlay(kml);
        },
        
        isMapOpen : function()
        {
            if ( this.map_toggle.hasClass('hide_map') ) {
                return true;
            }
            return false;
        },
        
        toggleMap: function()
        {
            this.map_toggle.blur();
            
            if ( !this.map ) {
                this.loadMap();
            }

            if ( this.isMapOpen() ) {
                this.map_toggle.removeClass('hide_map');
            } else {
                this.map_toggle.addClass('hide_map');
            }
            
            this.map_container.toggleClass('yui-closed');
            this.map_container.fx.set('reverse', !this.map_container.fx.get('reverse')); // toggle reverse
            this.map_container.fx.run();            
        },
        
        lookupPostalCode : function()
        {
            var obj = this;
            var address = this.getNormalizedValue() + ' canada';

            this.geocoder.getLatLng( address,
                function(point) {
                    if (!point) {
                        alert(address + " not found");
                    } else {
                        
                        // remove previous markers
                        if ( obj.marker ) {
                            obj.map.removeOverlay(obj.marker);
                        }

                        obj.map.setCenter(point, 14);
                        var marker = new GMarker(point);
                        obj.map.addOverlay(marker);
                        obj.marker = marker;
                        
                        //marker.openInfoWindowHtml("<p>You are here.</p><p>" + obj.getNormalizedValue() + "</p>");
                    }
                }
            );

        }
    };
    
    widgetFindPreschool.init();



    
    

});
