/**
 * @author Jerry Holmes
 * @file FTT Venue Javascript
 */
var divMap;
var gMap;
var geocoder;
var currentAddress = "";
var currentFrom = "";
var geocoder;
var sql = "";
var gvc = 0;
var directions;

var weekDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

function drawVenues(){
    // GET MAP DIV AND TEST FOR BROWSER COMPATIBILITY
    
    divMap = $('divMap');
    if (GBrowserIsCompatible() == false) {
        divMap.innerHtml = "Your browser is not compatible with the Venues Map.";
        return;
    }
    Event.observe(window, 'unload', GUnload);
    
    // ADJUST RIGHT PANE
    
    var right = $('dnn_RightPane');
    right.style.width = "200px";
    
    // CREATE MAP AND CENTER JUST WEST OF TAMPA
    
    gMap = new GMap2(divMap);
    geocoder = new GClientGeocoder();
	var divDirectionText = $('divDirectionsText');
	directions = new GDirections(gMap, divDirectionText);
    gMap.enableScrollWheelZoom();
    gMap.enableContinuousZoom();
    geocoder = new GClientGeocoder();
    gMap.setCenter(new GLatLng(28.53774, -83.459269), 6);
    
    GEvent.addListener(gMap, "click", mapClick);
    
    // ADD CONTROLS
    
    gMap.addControl(new GLargeMapControl());
    
    // ADD MARKERS
    
    venues.each(addVenueMarker);
	
	// ADD DAYS
	
	var divDays = $('divDays');
	var date = new Date();
	var day = date.getDay();
	Element.insert(divDays,{bottom:"<a href='javascript:showAll();'>All</a>"});
	Element.insert(divDays,{bottom:" | <a href='javascript:showToday();'>Today</a>"});
	for (i = 0; i < 6; i++) {
		day += 1;
		if (day > 6) {
			day = 0;
		}
		Element.insert(divDays, {
			bottom: " | <a href='javascript:showToday(" + (i+1) + ");'>" + weekDays[day] + "</a>"
		});
	}
	
    
    //sql = "";
    //gvc = 0;
    //geocode(venues[gvc]);
}

Event.observe(window, 'load', drawVenues);

function addVenueMarker(venue){
    var point = new GLatLng(venue.lat, venue.lng);
    var marker = new GMarker(point, {
        title: venue.name
    });
	//console.log("http://www.finaltabletour.com/portals/2/gmarkers/" + venue.iconColor + ".png");
	
    gMap.addOverlay(marker);
	marker.setImage("http://www.texasholdempokertours.com/portals/2/gmarkers/" + venue.iconColor + ".png");
    marker.venueid = venue.id;
    venue.marker = marker;
    
}

function gotoLocation(){
    if (geocoder == null) {
        return;
    }
    
    var address = $F('fldAddress');
	if(address.blank() == true)
	{
		showAll();
		gMap.setCenter(new GLatLng(28.53774, -83.459269), 6);
		return;
	}
    geocoder.getLatLng(address, function(point){
        if (!point) {
            alert(address + " not found");
        }
        else {
            gMap.setCenter(point, 10);
        }
    });
    
}

function mapClick(obj){
    if (obj == null) {
        return;
    }
    var point = obj.getPoint();
	var zoom = gMap.getZoom();
	if(zoom < 10)
	{
		zoom = 10;
	}
    gMap.setCenter(point, zoom);
    
    lxFetch(543, 0, "venueid=" + obj.venueid);
}

function showToday(day){
	d = day;
	if(d == null)
	{
		d = ""
	}
	d = "today" + d;
	
    venues.each(function(v){

        if (v[d] == 1) {
            v.marker.show();
        }
		else
		{
			v.marker.hide();
		}
		
    });
}

function showAll(){
    venues.each(function(v){
        v.marker.show();
    });
    
}

function showName(){
	name = $F('fldName');
	name = name.toLowerCase();
	
    venues.each(function(v){
        if (v.name.toLowerCase().indexOf(name) >= 0) {
            v.marker.show();
        }
		else
		{
			v.marker.hide();
		}
		
    });
}

function showDirectionsBox(){
    $('divDirections').show();
    $('fldFrom').value = currentFrom;
}

function showDirections()
{
    directions.clear();
    currentFrom = $F('fldFrom');
    var dir = "from:" + currentFrom + " to:" + currentAddress;
    directions.load(dir);   
}

function geocode(venue){

    // GEO CODE
    
    var address = venue.addr1 + "; " + venue.city + " " + venue.state + " " + venue.zip;
	
	if(venue.city.include("foobar") == true)
	{
		gvc++;
		if (gvc < venues.length) {
            geocode(venues[gvc]);
        }
		else{
			console.log(sql);
		}
		return;
	}   
	 
    geocoder.getLatLng(address, function(point){
        if (!point) {
            console.log(gvc +  ":Not found:" + address);
        }
        else {
			sql += "update ftt_venues set ven_lat=" + point.y + ", ven_long=" + point.x + " where ven_uniqueid='"+venue.id+"'<br />";
        }
        gvc++;
        if (gvc < venues.length) {
            geocode(venues[gvc]);
        }
		else{
			var out = $("divDirectionsText");
			out.innerHTML = sql;
		}
    });
    
    
}

