lf.comcap = {
    
    businesses: [],
    initMainNavigationMenu: function() {
        var mainNavigationMenu = new YAHOO.widget.MenuBar(
            "wrapper-main-nav",
            {
                autosubmenudisplay: true,
                lazyload: true
            }
        );
        
        mainNavigationMenu.render();
    },
    
    initPropertySearch: function() {
        var sliderContainer = lf.element.get("property-search-slider");
        if(sliderContainer) {
            
             //get default values
            var low     = lf.element.get('property-slider-min-value').value || lf.comcap.PRICE_SEARCH_MINIMUM;
            var high    = lf.element.get('property-slider-max-value').value || lf.comcap.PRICE_SEARCH_MAXIMUM;
            
            //instantiate slider
            this.propertySlider = YAHOO.widget.Slider.getHorizDualSlider(
                'property-search-slider',
                'property-slider-thumb-min',
                'property-slider-thumb-max',
            lf.comcap.PRICE_SLIDER_WIDTH,1);
            
           //set the default values
            var lowAdjusted     = (low - lf.comcap.PRICE_SEARCH_MINIMUM)/(lf.comcap.PRICE_SEARCH_MAXIMUM - lf.comcap.PRICE_SEARCH_MINIMUM) * lf.comcap.PRICE_SLIDER_WIDTH;
            var highAdjusted    = (high - lf.comcap.PRICE_SEARCH_MINIMUM)/(lf.comcap.PRICE_SEARCH_MAXIMUM - lf.comcap.PRICE_SEARCH_MINIMUM) * lf.comcap.PRICE_SLIDER_WIDTH;
            
            this.propertySlider.setMinValue(Math.ceil(lowAdjusted));
            this.propertySlider.setMaxValue(Math.ceil(highAdjusted));
            
            //setup change listener
            this.propertySlider.subscribe('change', function(slider) {
                
                //get the value of both sliders
                var low     = slider.minVal;
                var high    = slider.maxVal;
                
                //get low and high adjusted values
                var lowAdjusted     = low/lf.comcap.PRICE_SLIDER_WIDTH * (lf.comcap.PRICE_SEARCH_MAXIMUM - lf.comcap.PRICE_SEARCH_MINIMUM) + lf.comcap.PRICE_SEARCH_MINIMUM;
                var highAdjusted    = high/lf.comcap.PRICE_SLIDER_WIDTH * (lf.comcap.PRICE_SEARCH_MAXIMUM - lf.comcap.PRICE_SEARCH_MINIMUM) + lf.comcap.PRICE_SEARCH_MINIMUM;
                
                //update label
                this.updatePropertySearchLabel(lowAdjusted, highAdjusted);
                
            }, this, true);
            
            var me = this;
            setTimeout(function() {
                me.propertySlider.setMinValue(Math.ceil(lowAdjusted));
                me.propertySlider.setMaxValue(Math.ceil(highAdjusted));

                me.updatePropertySearchLabel(low, high);
                
            },250);
        }
    },
    
    updatePropertySearchLabel: function(low, high) {
        
         //go to nearest factor of 5
        var remainder = low % 10;
        low -= remainder;
                
        remainder = high % 10;
        high -= remainder;
        
        //update label
        lf.element.get('property-search-label').innerHTML = "Between: $" + low + " and $" + high;
        
        //update hidden fields
        lf.element.get('property-slider-min-value').value = low;
        lf.element.get('property-slider-max-value').value = high;
        
    },
    
    initPropertyList: function() {
        lf.effects.scrollTo('wrapper-property-list-outer',0,0,0.5);
    },
    
    togglePropertyListView: function(viewType) {
        
        var map     = lf.element.get('property-list-map-container');
        var list    = lf.element.get('property-list-container');
        
        switch(viewType) {
            case 'map':
                
                    lf.element.addClass('property-map-link','selected');
                    lf.element.removeClass('property-list-link','selected');
                    lf.effects.scrollTo('wrapper-property-list-outer', 570,0,.5,this.initPropertyMap,this);
                
                break;
            
            case 'list':
                
                    lf.element.addClass('property-list-link','selected');
                    lf.element.removeClass('property-map-link','selected');
                    lf.effects.scrollTo('wrapper-property-list-outer',0,0,.5);
                
                break;
        }
    },
    
    initPropertyMap: function() {
        
        if(!this.mapRendered) {
            
            this.map = new GMap2(lf.element.get('property-list-map-container'));
           
            this.mapRendered = true;
            
            var geocoder = new GClientGeocoder();
            
            //Set Littleton as center of the map
            geocoder.getLatLng('Littleton, CO', function(point) {
                lf.comcap.map.setCenter(point, 8);
            });
            
            //Add zoom controls to the map
            this.map.addControl(new GLargeMapControl);
            
            for (var i=0; i<this.addresses.length; i++) {
                this.addAddress(this.addresses[i], this.propertyInfoWindowHtml[i]);
            }            
        }
    },

    addAddress: function (address, infoWindowHtml) {
        
        var geocoder = new GClientGeocoder();

        //Converting string address to Lat & Lon.
        geocoder.getLatLng(address, function(point) {
            if (!point) {
                alert(address + " not found");
            } else {
                
                var marker = new GMarker(point);
                lf.comcap.map.addOverlay(marker);
                //lf.trace(point);

                //On click, info window opens.
                GEvent.addListener(marker, "click", function() {
                    marker.openInfoWindowHtml(infoWindowHtml);
                });
            }
        });
    },
    
    closeMessageBox: function() {
        var box = lf.element.get('messages');
        lf.element.hide(box);
    },
    
    
    initBusinessSearch: function() {

        this.map = new GMap2(lf.element.get('local-business-map'));
        this.map.addControl(new GLargeMapControl());

        var geocoder = new GClientGeocoder();
            
        //Set Denver as center of the map
        geocoder.getLatLng('Denver, CO', function(point) {
            lf.comcap.map.setCenter(point, 8);
        });
        
        this.refreshBusinessSearch();
    },
    
    refreshBusinessSearch: function(recenter) {
        
        if (lf.isUndefined(recenter)) {
            recenter = true;
        }
        
        var propertyId = lf.element.get('local-business-dropdown').value;
        
        if (propertyId == '0') {
            return;
        }
        
        this.map.clearOverlays();
        
        var address = propertyAddresses[propertyId];
        var name    = propertyNames[propertyId];
        
        //Center map on property
        var g = new GClientGeocoder();
                
        g.getLatLng(address, function(point) {

           if (recenter) { 
               lf.comcap.map.setCenter(point, 13);
           }
           
            //Place point for select property, set overlay to green icon.
            var icon        = new GIcon(G_DEFAULT_ICON);
            icon.image      = "http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png";
            icon.iconSize   = new GSize(32,32);
            
            var marker = new GMarker(point, {
                icon:icon
            });
            
            GEvent.addListener(marker, "click", function(){
                marker.openInfoWindowHtml("<h1>" + name + "</h1><p>" + address + "</p>");
            });
            
            lf.comcap.map.addOverlay(marker);

        });
        
        //Find checked categories
        var checkboxes = lf.element.query('#local-business-category-checkboxes li input');
        var categories = {};
        
        lf.foreach(checkboxes, function(checkbox){
            categories[checkbox.value] = checkbox.checked;
        });
        
    
        
        //Populate map with businesses
        lf.foreach(this.businesses, function(biz) {
            if (biz.propertyId == propertyId && categories[biz.categoryId]) {
               biz.placeMarker();
            }
        });
        
    },
    
    addBusiness: function(propertyId, categoryId, name, phone, bio, website, address, infoWindowAddress) {
        var biz = {
            propertyId:         propertyId,
            categoryId:         categoryId,
            name:               name,
            phone:              phone,
            bio:                bio,
            website:            website,
            address:            address,
            infoWindowAddress:  infoWindowAddress,
            point:              false,
            attemptsToLoad:     5,
            
            getHtml:    function() {
                
                var string = '<h3>'   + this.name+'</h3>';
                string += 'Phone: '   + this.phone + '<br />';
                string += this.infoWindowAddress + '<br />';
                string += '<a target="_blank" href="' + this.website + '">' + this.website + '</a>';
                
                return string;
            },
            
            getPointAndPlaceMarker: function() {
                
                if(this.point) {
                    return this.point;
                }
                
                var g = new GClientGeocoder();
                var me = this;
                
                g.getLatLng(this.address, function(point) {
                   me.point = point;
                   me.placeMarker(me.attemptsToLoad --);
                });
                
                return false;
            
            },
            
            placeMarker: function(loadPoint) {
                
                if(lf.isUndefined(loadPoint)) {
                    loadPoint = true;
                }
                
                if(!this.point) {
                    if(loadPoint) {
                        this.getPointAndPlaceMarker();
                    }
                } else {
                    
                    var marker = new GMarker(this.point);
                    GEvent.addListener(marker, "click", function(){
                        marker.openInfoWindowHtml(biz.getHtml());
                    });
                    
                    lf.comcap.map.addOverlay(marker);
                }
            }
        };
        this.businesses.push(biz);
    },
    
    initEventSearch: function() {
        
        var propertyDropdown = lf.element.get('property');
        lf.element.hide('wrapper-event-list-outer');
        
        lf.event.addListener(propertyDropdown,'change',function() {
            
            lf.element.show('wrapper-event-list-outer');
            
            lf.progresBarDiv({
                element: lf.element.get('wrapper-event-list-outer')
            });
            
            lf.asyncUpdate('wrapper-event-list-outer','/content/events/' + propertyDropdown.value);
            
        });
    },
    
    initRequestForm: function(hideRequestForm) {
        
        if(!lf.isBool(hideRequestForm)) {
            hideRequestForm = true;
        }
        
        var propertyDropdown = lf.element.get('property');
        
        if(hideRequestForm) {
            lf.element.hide('wrapper-maintenance-request-outer');
        }
        
        lf.event.addListener(propertyDropdown,'change',function() {
            
            lf.element.show('wrapper-maintenance-request-outer');
            
            lf.progresBarDiv({
                element: lf.element.get('wrapper-maintenance-request-outer')
            });
            
            lf.asyncUpdate('wrapper-maintenance-request-outer','/content/maintenance-form/property/' + propertyDropdown.value);
            
        });
    },
    
    initPropertyPage: function() {
        //get width of outer container so we know width of all inner sections
        this.propertyPageOuter          = lf.element.get('property-page-content-outer');
        this.propertyPageOuterWidth     = lf.element.getWidth(this.propertyPageOuter);
        this.propertyPageOuterWidth     = 860; //for some reason safari doesn't report the same, so force it
        
        //find num elements inside scroll div
        this.propertyPageScroll         = lf.element.get('property-scroller');
        this.propertyPageSections       = lf.element.query("#property-scroller .property-inner");
        
        var width = this.propertyPageOuterWidth * this.propertyPageSections.length;
        
        lf.element.setStyle(this.propertyPageScroll,'width',width + 'px');
    },
    
    jumpToPropertySection: function(sectionName) {
        
        var sectionIndex    = 0;
        var index           = 0;
        
        lf.foreach(this.propertyPageSections,function(section) {
            if(section.id == 'property-' + sectionName) {
                sectionIndex = index;
            }
            
            index ++;
        });
        
        var scrollLeft = sectionIndex * this.propertyPageOuterWidth;
        lf.effects.scrollTo(this.propertyPageOuter, scrollLeft, 0, .5, null, null, YAHOO.util.Easing.easeOut);
 
    },
    
    jumpToPropertyApplication: function() {
        lf.comcap.jumpToPropertySection('application');
    }
    
};

YAHOO.util.Event.onDOMReady(lf.comcap.initPropertySearch, lf.comcap, true);
YAHOO.util.Event.onDOMReady(lf.comcap.initMainNavigationMenu, lf.comcap, true);

lf.comcap.PRICE_SEARCH_MINIMUM  = 200.00;
lf.comcap.PRICE_SEARCH_MAXIMUM  = 2500.00;
lf.comcap.PRICE_SLIDER_WIDTH    = 190;