/*
 * Gmap - Holds functionality related to the Football Google Maps interface
 */

try { void (document.execCommand("BackgroundImageCache", false, true)); } catch (e) {};

var ITVMap = {
    maps: [],
    create: function(oOptions){
        if (GBrowserIsCompatible()) {
            var iIndex = ITVMap.maps.length;
            document.write('<div id="ITVMap_' + iIndex + '"></div>');
            var eMap = document.getElementById('ITVMap_' + iIndex);
            
            if (oOptions.width) {
                eMap.style.width = oOptions.width + 'px';
            }
            
            if (oOptions.height) {
                eMap.style.height = oOptions.height + 'px';
            }
            var oMap = new GMap2(eMap);
            oMap.enableDoubleClickZoom();
            oMap.setCenter(new GLatLng(oOptions.location[0], oOptions.location[1]), oOptions.zoom);
            oMap.addControl(new GLargeMapControl());
            
            //oMap.addControl(new GMapTypeControl());
            oMap.setMapType(G_NORMAL_MAP);
            
            oMap.index = iIndex;
            oMap.layers = {};
            oMap.eMap = eMap;
            
            for (i in ITVMap.map.methods) {
                oMap[i] = ITVMap.map.methods[i];
            }
            
            ITVMap.maps.push(oMap);
            
            return oMap;
        }
    },
    
    defaultIcon: function(){
        var tinyIcon = new GIcon();
        tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
        tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        tinyIcon.iconSize = new GSize(12, 20);
        tinyIcon.shadowSize = new GSize(22, 20);
        tinyIcon.iconAnchor = new GPoint(6, 20);
        tinyIcon.infoWindowAnchor = new GPoint(5, 1);
        return {
            icon: tinyIcon
        };
    },
    
    map: {
        methods: {
            goTo: function(aLocation, iZoom){
            },
            scrollTo: function(aLocation, iZoom){
            },
            zoom: function(iZoom){
            },
            addLayer: function(oOptions){
                var sIndex = oOptions.name;
                this.layers[sIndex] = {};
                this.layers[sIndex].active = false;
                this.layers[sIndex].markers = {};
                this.layers[sIndex].index = sIndex;
                this.layers[sIndex].map = this;
                this.layers[sIndex].overlays = {};
                this.layers[sIndex].manager = {};
                
                var mgrOptions = {
                    borderPadding: 0,
                    trackMarkers: false,
                    maxZoom: 20
                };
                this.layers[sIndex].manager = new MarkerManager(this.layers[sIndex].map, mgrOptions);
                
                for (i in ITVMap.layer.methods) {
                    this.layers[sIndex][i] = ITVMap.layer.methods[i];
                }
                
                if (oOptions.markers !== undefined) {
                    this.layers[sIndex].addMarker(oOptions.markers);
                }
                
                if (oOptions.kml !== undefined) {
                    this.layers[sIndex].kml = oOptions.kml;
                }
                
                return this.layers[oOptions.name];
            },
            
            removeLayer: function(sName){
                for (i in this.layers[sName].markers) {
                    this.layers[sName].markers[i].hide();
                }
                delete this.layers[sName];
            },
            showAll: function(){
                for (i in this.layers) {
                    this.layers[i].show();
                }
            },
            hideAll: function(){
                for (i in this.layers) {
                    this.layers[i].hide();
                }
            }
        }
    },
    layer: {
        methods: {
            addMarker: function(oMarkers){
                var aMarkerInsert = {};
                for (i in oMarkers) {
                    this.markers[i] = oMarkers[i];
                    this.markers[i].layer = this;
                    this.markers[i].index = i;
                    this.markers[i].active = false;
                    
                    for (iMethod in ITVMap.marker.methods) {
                        this.markers[i][iMethod] = ITVMap.marker.methods[iMethod];
                    }
                    
                    if (oMarkers[i].active === true) {
                        this.markers[i].show();
                    }
                    aMarkerInsert[i] = this.markers[i];
                }
                return aMarkerInsert;
            },
            removeMarker: function(sIndex){
                this.markers[sIndex].hide();
                delete this.markers[sIndex];
            },
            
            toggle: function(fCallback){
                if (this.active === true) {
                    this.hide();
                }
                else {
                    this.show();
                }
                return this.active;
            },
            
            hide: function(){
                if (this.active === true) {
                    for (i in this.markers) {
                        this.markers[i].hide();
                    }
                    for (iKml in this.kml) {
                        if (this.overlays[iKml] !== undefined) {
                            this.map.removeOverlay(this.overlays[iKml]);
                        }
                    }
                    this.active = false;
                }
                
                this.manager.clearMarkers();
                
                return this.active;
            },
            show: function(){
                if (this.active === false) {
                    for (i in this.markers) {
                        this.markers[i].show();
                    }
                    for (iKml in this.kml) {
                        this.overlays[iKml] = new GGeoXml(this.kml[iKml]);
                        this.map.addOverlay(this.overlays[iKml]);
                    }
                    this.active = true;
                }
                return this.active;
            }
        }
    },
    marker: {
        methods: {
            hide: function(){
                if (this.active === true) {
                    this.active = false;
                }
            },
            show: function(){
                if (this.active === false) {
                    if (this.location === undefined) {
                        this.geoCode('show');
                    }
                    else 
                        if (this.location !== undefined && this.location.length === 2) {
                            if (this.icon === undefined) {
								
                                this.overlay = new GMarker(new GPoint(this.location[0], this.location[1]), ITVMap.defaultIcon());
                                this.layer.manager.addMarker(this.overlay, 1, 20);
								var oMarker = this;
								GEvent.addListener( this.overlay, "click", function()
								{
									if( typeof oMarker.bubbleHTML === 'function' )
									{
										oMarker.bubbleHTML = oMarker.bubbleHTML( oMarker );
									}
									oMarker.layer.map.openInfoWindowHtml( new GLatLng( oMarker.location[1], oMarker.location[0] ) , oMarker.bubbleHTML );						
								});
								
                            }
                            else {
                                if (typeof this.icon.length === 'number') {
                                    for (var i = 0; i < this.icon.length; i++) {
                                        if (this.icon[i].href !== undefined && this.icon[i].href !== '') {
                                            var customIcon = new GIcon();
                                            customIcon.image = this.icon[i].href;
                                            customIcon.iconSize = new GSize(this.icon[i].size[0], this.icon[i].size[1]);
                                            customIcon.iconAnchor = new GPoint(this.icon[i].size[0] / 2, this.icon[i].size[1] / 2);
                                            this.overlay = new GMarker(new GPoint(this.location[0], this.location[1]), {
                                                icon: customIcon
                                            });
                                            this.layer.manager.addMarker(this.overlay, this.icon[i].zoom[0], this.icon[i].zoom[1]);
                                        }
                                        else {
                                            this.overlay = new GMarker(new GPoint(this.location[0], this.location[1]), ITVMap.defaultIcon());
                                            this.layer.manager.addMarker(this.overlay, this.icon[i].zoom[0], this.icon[i].zoom[1]);
                                        }
										
										if( this.bubbleHTML !== undefined )
										{
											var oMarker = this;
											GEvent.addListener( this.overlay, "click", function()
											{
												if( typeof oMarker.bubbleHTML === 'function' )
												{
													oMarker.bubbleHTML = oMarker.bubbleHTML( oMarker );
												}
												oMarker.layer.map.openInfoWindowHtml( new GLatLng( oMarker.location[1], oMarker.location[0] ) , oMarker.bubbleHTML );						
											});
										}
                                    }
                                }
                            }
                            this.active = true;
                        }
                }
            },
            scrollTo: function(oMarker){
                if (this.location !== undefined && this.location.length === 2) {
                    this.layer.map.panTo(new GLatLng(this.location[0], this.location[1]));
                }
                else {
                    this.geoCode('scrollTo');
                }
            },
            
            center: function(oMarker){
                if (this.location !== undefined && this.location.length === 2) {
                    this.layer.map.setCenter(new GLatLng(this.location[0], this.location[1]));
                }
                else {
                    this.geoCode('center');
                }
            },
            
            geoCode: function(fCallBack){
				
//                var iMap = this.layer.map.index;
//                var sLayer = this.layer.index;
//                var sMarker = this.index;
//				
//				var sQuery = this.query;
//                
//                oGeocoder = new GClientGeocoder();
//                oGeocoder.getLatLng(sQuery, function(point){
//                    
//					var oMarker = ITVMap.maps[iMap].layers[sLayer].markers[sMarker];
//                    if (point) {
//                        oMarker.location = [point.lng(), point.lat()];
//                        allClubs[oMarker.index].location = oMarker.location;
//						
//						document.getElementById('log').innerHTML += 'Pass: ' + sQuery + '<br/>';
//	                    
//						if (fCallBack !== undefined) {
//	                        oMarker[ fCallBack ]();
//	                    }
//                    }
//                    else {
//						document.getElementById('log').innerHTML += 'Fail: ' + sQuery + '<br/>';
//                    }
//            
//                });
                
            }
        }
    }
}

//window.addEventListener("unload", GUnload, false); 


