BSD.namespace('BSD.event.maps');

BSD.event.maps.event_map = function (type,rss_url,lat,lng,map_center_txt,map_container,preloader_container,filter,force_center,center_radius){
    
    this.type = type;
    this.rss_url = rss_url;
    this.lat = lat;
    this.lng = lng;
    this.map_center_txt = map_center_txt;
    this.map_container = map_container;
    this.preloader_container = preloader_container;
    this.filter = filter;
    this.force_center = force_center || false;
    this.center_radius = center_radius;
    this.flash_map_obj = null;
    this.map_center_point = null;
    this.flash_georss_obj = null;
    this.map_size_large = null;
    this.map_size_small = null;
    this.map_resize_link_text = {small:"&lt;&lt; Smaller Map",large:"Larger Map &gt;&gt;"}; 
    this.map_resize_link = null;
    
    this.flash_map = BSD.event.maps.flash_map;
    this.html_map = BSD.event.maps.html_map;
    this.has_preloader = BSD.event.maps.has_preloader;
    this.show_preloader = BSD.event.maps.show_preloader;
    this.hide_preloader = BSD.event.maps.hide_preloader;
    this.show_map_container = BSD.event.maps.show_map_container;
    this.hide_map_container = BSD.event.maps.hide_map_container;
    this.flash_init = BSD.event.maps.flash_init;
    this.filter_map_view = BSD.event.maps.filter_map_view;
    this.html_map_size = BSD.event.maps.html_map_size;
    this.convert_percent_to_pixel = BSD.event.maps.convert_percent_to_pixel;
    this.get_flash_map_obj = BSD.event.maps.flash_map_obj;
    this.get_flash_georss_obj = BSD.event.maps.flash_georss_obj;
    this.set_map_size = BSD.event.maps.set_map_size;
    this.get_ysize_obj = BSD.event.maps.get_ysize_obj;
    
    if(this.filter){
        this.rss_url = this.filter_map_view();
    }

    if(this.type == 'flash'){
        this.flash_map();
    } else if (this.type == 'html'){
        this.html_map();
    } else{
        alert("ERROR: Unknown map type '"+type+"' requested");
    }
    
    
}

BSD.event.maps.flash_map = function (rss_url,lat,lng,map_center_txt,map_container){
    
    if(this.has_preloader())
        this.show_preloader();
    
    this.map_center_point = (this.lat && this.lng) ? new LatLon(this.lat,this.lng) : this.map_center_txt;
    this.flash_map_obj = this.get_flash_map_obj();
    this.flash_map_obj.addEventListener(Map.EVENT_INITIALIZE, this.flash_init,this);
    this.flash_georss_obj = this.get_flash_georss_obj();

}

BSD.event.maps.html_map = function (){
    
    if(this.has_preloader())
        this.show_preloader();
    
    var map = new YMap(document.getElementById(this.map_container),YAHOO_MAP_REG,this.html_map_size());
    
    this.html_map = map;  //allow direct access to the YMap object
    this.map_center_point = (this.lat && this.lng) ? new YGeoPoint(this.lat,this.lng) : this.map_center_txt;
    
    if(this.has_preloader())
        YEvent.Capture(map,EventsList.onEndGeoRSS,this.hide_preloader,this);
    if (this.force_center)
        YEvent.Capture(map,EventsList.onEndGeoRSS,this.set_center,this);
    
    map.drawZoomAndCenter(this.map_center_point, 8);
    map.addZoomLong();
    map.addTypeControl();
    map.disableKeyControls();
    if (this.rss_url) {
        map.addOverlay(new YGeoRSS(encodeURI(this.rss_url)));
    } else if (this.has_preloader()) {
        this.hide_preloader();
    }
}

BSD.event.maps.set_center = function (){
    if (this.html_map) {
		// get zoom level given center radius
		var zoomLevel = this.html_map.getZoomLevel(new YMapDistance(this.center_radius));
        this.html_map.drawZoomAndCenter(this.map_center_point, zoomLevel);
    }
}

BSD.event.maps.show_map_container = function (){
    if(this.map_container){
        YAHOO.util.Dom.setStyle(this.map_container,'position','relative');
        YAHOO.util.Dom.setStyle(this.map_container,'visibility','visible');
        if(this.type != 'flash')
            YAHOO.util.Dom.setStyle(this.map_container,'display','block');
    }
    
}

BSD.event.maps.hide_map_container = function (){
    if(this.map_container){
        YAHOO.util.Dom.setStyle(this.map_container,'visibility','hidden');
        YAHOO.util.Dom.setStyle(this.map_container,'position','absolute');
        if(this.type != 'flash')
            YAHOO.util.Dom.setStyle(this.map_container,'display','none');

    }
    
}

BSD.event.maps.has_preloader = function (){
    return (document.getElementById(this.preloader_container)) ? true : false;
}

BSD.event.maps.show_preloader = function (){
    map_container_obj = document.getElementById(this.map_container);
    var map_container_pos = YAHOO.util.Dom.getXY(map_container_obj);
    var map_container_width = YAHOO.util.Dom.getStyle(map_container_obj,"width");
    var map_container_height = YAHOO.util.Dom.getStyle(map_container_obj,"height");
    this.hide_map_container();
    
    YAHOO.util.Dom.setStyle(this.preloader_container,'display','block');
    YAHOO.util.Dom.setStyle(this.preloader_container,'width',map_container_width);
    YAHOO.util.Dom.setStyle(this.preloader_container,'height',map_container_height);
    YAHOO.util.Dom.setXY(this.preloader_container,map_container_pos,true);
}

BSD.event.maps.hide_preloader = function (){
    YAHOO.util.Dom.setStyle(this.preloader_container,'display','none');
    this.show_map_container();
}

BSD.event.maps.flash_init = function (e) {
    
    if(this.has_preloader())
        this.flash_map_obj.addEventListener(this.flash_georss_obj.EVENT_CUSTOM_OVERLAY_LOADED, this.hide_preloader,this);
    
    this.flash_map_obj.addTool( new PanTool(), true);
    navWidget = new NavigatorWidget(); 
    this.flash_map_obj.addWidget(navWidget);
    this.flash_map_obj.addOverlay(this.flash_georss_obj);
}

BSD.event.maps.html_map_size = function(){
    if(el = document.getElementById(this.map_container)){
        var map_container_width = YAHOO.util.Dom.getStyle(this.map_container,'width');
        var width = (map_container_width.indexOf('%') != -1) ? this.convert_percent_to_pixel(YAHOO.util.Dom.getStyle(this.map_container,'width')) : YAHOO.util.Dom.getStyle(this.map_container,'width');
        return new YSize(width,parseFloat(YAHOO.util.Dom.getStyle(this.map_container,'height')));
    } else{
        return null;
    }
    
}

BSD.event.maps.flash_map_obj = function (){
    return new Map(this.map_container,"blue_address",this.map_center_point,14);
}

BSD.event.maps.flash_georss_obj = function(){
    return new GeoRSSOverlay(encodeURI(this.rss_url));
}

BSD.event.maps.filter_map_view = function (){
    var request_uri = "http://"+location.host+"/page/event/search_results";
    var query_string = "?mime=text/xml&format=geo_rss&wrap=no";
    var query_string_filter = "&"+this.filter.name+"="+this.filter.value;
    var rss_url = request_uri+query_string+query_string_filter;
    return rss_url;
}

BSD.event.maps.convert_percent_to_pixel = function (percent){
    var viewportwidth = parseFloat(YAHOO.util.Dom.getStyle(document.getElementById(this.map_container).parentNode,'width'));
    viewportwidth = (isNaN(viewportwidth)) ? YAHOO.util.Dom.getDocumentWidth() : viewportwidth;
    percent = parseFloat(percent)/100;
    return viewportwidth*percent;
    
}
BSD.event.maps.get_ysize_obj = function(size){
    return new YSize(parseInt(size.width),parseInt(size.height));
}

BSD.event.maps.set_map_size = function(){
    this.map_resize_link = YAHOO.util.Dom.get("eventmap_resize_link");
    size = (this.map_resize_link.innerHTML == this.map_resize_link_text.small) ? this.get_ysize_obj(this.map_size_small) : this.get_ysize_obj(this.map_size_large);
    this.html_map.resizeTo(size);
    this.map_resize_link.innerHTML = (this.map_resize_link.innerHTML == this.map_resize_link_text.small) ? this.map_resize_link_text.large : this.map_resize_link_text.small;
    return true;
}
