// chapter google map support scripts
//
// Copyright (c) 2006-2007 San Jacinto High Rollers
// You may copy this script as long as you include attribution in your script.

// method to get parameter from page's url (general method)
// returns null if not present
function getUrlParam(name) {
    var start = location.search.indexOf('?' + name + '=');
    if (start < 0) {
        start = location.search.indexOf('&' + name + '=');
    }
    if (start < 0) {
        return null;
    }
    start += name.length + 2;
    if (start == location.search.length) {
        return '';
    }
    var end = location.search.indexOf('&', start);
    if (end < 0) {
        end = location.search.length;
    }
    var result = '';
    for (var i = start; i < end; ++i) {
        var c = location.search.charAt(i);
        result += c == '+' ? ' ' : c;
    }
    return unescape(result);
}

// chapter ids
//
// the order here determines the order the chapters are presented
//
// CAUTION: When changing these, make sure any chapmaps.htm page references
// are updated as well. TODO - add symbolic references!
var BAYTOWN = 0;
var CHANNELVIEW = 1;
var HARDIN = 2;
var HARRIS = 3;
var HUNTSVILLE = 4;
var KATY = 5;
var LA_PORTE = 6;
var LIBERTY = 7;
var LIVINGSTON = 8;
var LOST = 9;
var MONTGOMERY = 10;
var NW_HOUSTON = 11;
var PASADENA = 12;
var SAN_ANTONIO = 13;
var SO_CENTRAL = 14;
var SE_TEXAS = 15;
var SW_HOUSTON = 16;
var TRI_COUNTY = 17;
var WALLER = 18;

var CHAPTER_COUNT = 19;

// container for chapter information
function Chapter(name, url, meetingTime, meetingPlace, mapLabel, longtitude, latitude, zoom, contact) {
    this.name = name;
    this.url = url;
    this.meetingTime = meetingTime;
    this.meetingPlace = meetingPlace;
    this.mapLabel = mapLabel;
    this.longtitude = longtitude;
    this.latitude = latitude;
    this.zoom = zoom;
    this.contact = contact;
    this.location = null;
}


// chapter data
var chapters = new Array();

chapters[BAYTOWN] = new Chapter(
    'Baytown Chapter',
    'sjhrforever.com',
    'Second Friday of each month at 7&nbsp;pm at VFW in Baytown.<br><br>Fourth Friday of each month at 7&nbsp;pm at Roadawgs house. Call Rowdy for more information.',
    'VFW<br>' + 
        '8204 North Main St<br>' + 
        'Baytown, TX', 
    'VFW', -94.968213, 29.821545, 6, 
    '&quot;Rowdy J&quot;<br>' + 
        'Sec/Tres<br>' + 
        '(832) 314-0762<br>' + 
        '<a href="mailto:SandraG52@aol.com"><small>SandraG52@aol.com</small></a>'
);

chapters[CHANNELVIEW] = new Chapter(
    'Channelview Chapter',
    'www.sjhrChannelview.com',
    'First and third Friday of each month at 7:30 pm.',
    'American Legion<br>' + 
        '16417 Market St<br>' + 
        'Channelview, TX<br>' + 
        '(281) 452-0466', 
    'American Legion', -95.108398, 29.779906, 5, 
    'Richard &quot;Gadget&quot; Greene<br>' + 
        'President<br>' + 
        '(281) 658-3794<br>' + 
        '<a href="mailto:sjhrgadget1@aol.com"><small>sjhrgadget1@aol.com</small></a>'
);

chapters[HARDIN] = new Chapter(
    'Hardin Texas Chapter',
    null,
    'First Saturday of each month.',
    'Locations vary, contact Fit.',
    null, 0, 0, 0, 
    'Shane &quot;Fit&quot; Willingham<br>' + 
        'Secretary/Treasurer<br>' + 
        '(281) 917-2560<br>' + 
        '<a href="mailto:fit_hardinsjhr@yahoo.com"><small>fit_hardinsjhr@yahoo.com</small></a>'
);

chapters[HARRIS] = new Chapter(
    'Harris County Chapter',
    'www.hcsjhr.org',
    'Second and fourth Saturday of each month at 6:30 pm.',
    'Locations vary, contact Ran.',
    null, 0, 0, 0, 
    'Ran Everts<br>' + 
        '(832) 646-6847<br>' + 
        '<a href="mailto:raneverts@yahoo.com"><small>raneverts@yahoo.com</small></a>'
);

chapters[HUNTSVILLE] = new Chapter(
    'Huntsville Chapter',
    null,
    'First and third Wednesday of each month at 7:30 pm.',
    'American Legion Hall<br>' + 
        '1919 Industrial Dr<br>' +
        'Huntsville, TX', 
    'American Legion Hall', -95.558707, 30.745875, 4, 
    'Tom &quot;Buckshot&quot; Reilly Sr<br>' + 
        'President<br>' + 
        '(936) 827-4947<br>' + 
        '<a href="mailto:highrollerbuckshot@yahoo.com"><small>highrollerbuckshot@yahoo.com</small></a>' 
);

chapters[KATY] = new Chapter(
    'Katy Chapter',
    null,
    'Business meeting on second Friday of each month.<br><br>Ride meeting will be determined at the business meeting.',
    'Locations and times vary, contact chapter president.',
    null, 0, 0, 0, 
    'President<br>' + 
        '<a href="mailto:sjhrkaty@gmail.com"><small>sjhrkaty@gmail.com</small></a>'
);

chapters[LA_PORTE] = new Chapter(
    'La Porte Chapter',
    null,
    '&nbsp;',
    '&nbsp;', 
    null, 0, 0, 0, 
    'Les &quot;Race&quot; Rhoades<br>' + 
        'National President<br>' + 
        '(281) 392-7103<br>' + 
        '<a href="mailto:race@nwsjhr.com"><small>race@nwsjhr.com</small></a>'
);

chapters[LIBERTY] = new Chapter(
    'Liberty County Chapter',
    'www.sjhrliberty.com',
    'First Sunday of each month at Bojack\'s house at 2:00&nbsp;pm.',
    '"On the Back Porch" at Bojack\'s house<br>' + 
      '172 CR 4291<br>' +
      'Dayton, TX', 
    '"On the Back Porch" at Bojack\'s house', -94.836441, 29.928831, 6, 
    'Troy &quot;Bojack&quot; Bettes<br>' + 
        'President<br>' + 
        '(936) 641-3993<br>' + 
        '<a href="mailto:Springerhd00@aol.com"><small>Springerhd00@aol.com</small></a>'
);

chapters[LIVINGSTON] = new Chapter(
    'Livingston Chapter',
    null,
    'Second Friday of each month at 7:30&nbsp;pm.',
    'Paks Karate<br>' + 
        '616 Pan American Dr<br>' +
        'Livingston, TX', 
    'Paks Karate', -94.936786, 30.698414, 4, 
    'Scott &quot;DILLIGAF&quot; Nelson<br>' + 
        'President<br>' + 
        '(936) 327-2064<br>' + 
        '<a href="mailto:captain_dillligaf@yahoo.com"><small>captain_dillligaf@yahoo.com</small></a>'
);

chapters[LOST] = new Chapter(
    'Lost Chapter',
    null,
    'Second and fourth Sunday of each month at 1:30&nbsp;pm.',
    'Buffalo Fred\'s Ice House<br>' + 
      '2708 N. Shepherd<br>' + 
      'Houston, TX',
    'Buffalo Fred\'s Ice House', -95.410238, 29.811222, 4, 
    'Jack &quot;Lobo&quot; Castillo<br>' + 
        '(281) 355-9866 home<br>' + 
        '(281) 236-9422 mobile<br>' + 
        '<a href="mailto:jcasti@juno.com"><small>jcasti@juno.com</small></a>'
);

chapters[MONTGOMERY] = new Chapter(
    'Montgomery County Chapter',
    'www.mcsjhr.org',
    'First and third Sunday of each month at 12 noon.',
    '19285 Old Houston Rd<br>' + 
        'Conroe, TX<br>' +
        '(281) 572-1741',
    'Meeting location', -95.321468, 30.168491, 4, 
    'Marlon &quot;El Mago&quot; King<br>' + 
        'President<br>' + 
        '(832) 515-3274<br>' + 
        '<a href="mailto:mrln_kng@yahoo.com"><small>mrln_kng@yahoo.com</small></a>'
);

chapters[NW_HOUSTON] = new Chapter(
    'Northwest Houston Chapter',
    'www.nwsjhr.com',
    'First and third Friday of each month at 8 pm.',
    'Charro & Co<br>' + 
      '1743 Elmview Dr<br>' + 
      'Houston, TX',
    'Charro & Co', -95.528848, 29.804634, 4, 
    'Tim &quot;TJ&quot; Smock<br>' + 
        'President<br>' + 
        '(713) 446-2186<br>' + 
        '<a href="mailto:tjsmock@yahoo.com"><small>tjsmock@yahoo.com</small></a>'
);

chapters[PASADENA] = new Chapter(
    'Pasadena Chapter',
    'www.sjhrpasadena.com',
    'Business meeting on first Friday of each month at 8 pm.<br><br>Ride meeting on third Sunday of each month at 12 noon.',
    'VFW Post<br>' + 
        '7407 Spencer Hwy<br>' + 
        'Pasadena, TX', 
    'VFW Post', -95.125355, 29.663782, 5, 
    'Jeff &quot;Wild Card&quot; Adcock<br>' + 
        'President<br>' + 
        '(281) 481-2202<br>' + 
        '<a href="mailto:jadcock1@sbcglobal.net"><small>jadcock1@sbcglobal.net</small></a>'
);

chapters[SAN_ANTONIO] = new Chapter(
    'San Antonio Chapter',
    null,
    'Inactive.',
    'Contact Race',
    null, 0, 0, 0,
    'Les &quot;Race&quot; Rhoades<br>' + 
        '(281) 392-7103<br>' + 
        '<a href="mailto:race@nwsjhr.com"><small>race@nwsjhr.com</small></a>'
);

chapters[SO_CENTRAL] = new Chapter(
    'South Central Chapter',
    'www.southcentralsjhr.org',
    'Weekly.',
    'Contact Scooter',
    null, 0, 0, 0,
    'Scooter<br>' + 
        'President<br>' + 
        '(830) 305-4991'
);

chapters[SE_TEXAS] = new Chapter(
    'South East Texas Chapter',
    null,
    'Twice monthly, call (409) 550-7622 for meeting info.',
    'Slayers<br>' + 
        '150 Hooks Rd<br>' + 
        'Lumberton, TX',
    'Slayer', -94.204341, 30.262901, 5, 
    'Eddie &quot;Apache&quot; Henley<br>' + 
        'President<br>' + 
        '(409) 386-0822<br>' + 
        '<a href="mailto:ehapache@aol.com"><small>ehapache@aol.com</small></a>'
);

chapters[SW_HOUSTON] = new Chapter(
    'Southwest Houston Chapter',
    null,
    'General meetings on first and third Tuesday of each month at 7 pm. The second and fourth weekends of the month will be a social ride',
    'The Dam Ice House<br>' + 
        '1718 Hwy 6 South<br>' + 
        'Houston, TX',
    'The Dam Ice House', -95.64481, 29.75067, 5, 
    'Jody &quot;Papa Joe&quot; Rendon<br>' + 
        'President<br>' + 
        '(281) 565-0213<br>' + 
        '<a href="mailto:jodysherry@windstream.net"><small>jodysherry@windstream.net</small></a>'
);

chapters[TRI_COUNTY] = new Chapter(
    'Tri-County Chapter',
    null,
    'Meeting on second Sunday of each month at 12 noon.<br><br>Ride on fourth Saturday of ' + 
        'each month (get there early if you want to eat). Kickstands up  at 12 noon.',
    'West of the Brazos<br>' + 
        '23220 Highway 36<br>' + 
        'Damon, TX<br>' + 
        '(979) 742-3100',
    'West of the Brazos', -95.72937, 29.290137, 5, 
    'Tom &quot;Admiral&quot; Daniels<br>' + 
        'President<br>' + 
        '(832) 274-3624<br>' + 
        '<a href="mailto:sjhr.admiral@gmail.com"><small>sjhr.admiral@gmail.com</small></a><br>' +
        '&quot;Weird Walt&quot; Lysaght<br>' + 
        'Secretary<br>' + 
        '(281) 239-9666<br>' + 
        '<a href="mailto:waedly@comcast.net"><small>waedly@comcast.net</small></a>'
);

chapters[WALLER] = new Chapter(
    'Waller County Chapter',
    'www.sjhrwallerco.com',
    'First and third Saturday of each month at 11&nbsp;am.<br>' + 
        'Contact Higgy to confirm meeting.',
    'Thirsty Parrot<br>' + 
        '13200 FM 359<br>' + 
        'Hempstead, TX',
    "Thirsty Parrot", -96.039074, 29.950988, 6, 
    '&quot;Higgy&quot;<br>' + 
        '(832) 768-2527'
);

// sanity checks
if (chapters.length != CHAPTER_COUNT) {
    alert('Chapter mismatch, chapters.length=' + chapters.length + ', CHAPTER_COUNT=' + CHAPTER_COUNT);
}

// write the chapter rows in the chapter table
function writeChapterTable() {
    for (var c = 0; c < CHAPTER_COUNT; ++c) {
        document.writeln(getChapterRowHtml(c, true));
    }
}

// get the html for a table row for a chapter's meeting information
function getChapterRowHtml(c, linkMap) {
    // build representation for url
    var url = '';
    if (chapters[c].url != null) {
        url = '<br><br><font face="comic sans ms, arial, helvetica"><a href="http://' + 
            chapters[c].url + '" target="_blank"><small>' + chapters[c].url + '</small></a></font>';
    }
    
    // build representation for map link
    var link = '';
    if (linkMap && (chapters[c].mapLabel != null)) {
        link = '<br><a href="chapmaps.htm?chapter=' + c + '">Map</a>';
    }
    
    return '<tr>' + 
        '<td><font face="comic sans ms, arial, helvetica">' + chapters[c].name + '</font>' + url + '</td>' + 
        '<td><font face="comic sans ms, arial, helvetica">' + chapters[c].meetingTime + '</font></td>' + 
        '<td><font face="comic sans ms, arial, helvetica">' + chapters[c].meetingPlace + link + '</font></td>' + 
        '<td><font face="comic sans ms, arial, helvetica">' + chapters[c].contact + '</font></td>' + 
        '</tr>';
}

// specialization of MapLocation
function getChapterLocation(map, longtitude, latitude, zoom, name, info, chapter) {
    var self = new MapLocation(map, longtitude, latitude, zoom, name, info);
    
    self.chapter = chapter;
    
    // override onMarkerClickImpl
    self.ChapterLocation_super_onMarkerClickImpl = self.onMarkerClickImpl;
    self.onMarkerClickImpl = ChapterLocation_onMarkerClickImpl;
    
    // override onInfoWindowOpenImpl
    self.ChapterLocation_super_onInfoWindowOpenImpl = self.onInfoWindowOpenImpl;
    self.onInfoWindowOpenImpl = ChapterLocation_onInfoWindowOpenImpl;
    
    // override onInfoWindowCloseImpl
    self.ChapterLocation_super_onInfoWindowCloseImpl = self.onInfoWindowCloseImpl;
    self.onInfoWindowCloseImpl = ChapterLocation_onInfoWindowCloseImpl;
    
    return self;
}

function ChapterLocation_onMarkerClickImpl() {
    suppressLegendClear = true;
    this.ChapterLocation_super_onMarkerClickImpl();
    suppressLegendClear = false;
}
// method override

function ChapterLocation_onInfoWindowOpenImpl() {
    this.ChapterLocation_super_onInfoWindowOpenImpl();
    setLegend(this.chapter);
}
// method override

function ChapterLocation_onInfoWindowCloseImpl() {
    this.ChapterLocation_super_onInfoWindowCloseImpl();
    clearLegend();
}
// method override

// chapter map instance
var chapterMap = null;

// flag used to suppress legend clear when info window closes
var suppressLegendClear = false;

// create the map
function createMap() {
    // create the map
    var map = createMapCase('map', -96.470947, 29.773914, 10);
    chapterMap = map;
    if (map == null) {
        // no need to go further
        return;
    }
    
    // add chapter locations to map
    for (var c = 0; c < CHAPTER_COUNT; ++c) {
        if (chapters[c].mapLabel != null) {
            chapters[c].location = getChapterLocation(map, chapters[c].longtitude, 
                chapters[c].latitude, chapters[c].zoom, chapters[c].mapLabel, 
                '<b>' + chapters[c].name + '</b><br>' + chapters[c].mapLabel, c);
        }
    }
    
    // set buttons
    var buttonColumns = 3;
    var html= '<form name="buttons" method="get" action="" ' + 
        'onsubmit="return false;"><table width="700" cellpadding="2">';
    for (var c = 0; c < CHAPTER_COUNT; ++c) {
        if (c % buttonColumns == 0) {
            html += '<tr>';
        }
        html += '<td width="33%"><div align="center"><input ' + 
            'type="button" name="' + chapters[c].name + '" value="' + 
            chapters[c].name + '" onclick="clickChapter(' + c + 
            ')"></div></td>';
        if ((c + 1) % buttonColumns == 0) {
            html += '</tr>';
        }
    }
    if (CHAPTER_COUNT % buttonColumns != 0) {
        for (var c = CHAPTER_COUNT; c % buttonColumns != 0; ++c) {
            html += '<td>&nbsp;</td>';
        }
        html += '</tr>';
    }
    html += '</table></form>';
    document.getElementById('buttons').innerHTML = html;
    
    // if a chapter was passed as a parameter, go to that chapter
    var param = getUrlParam('chapter');
    if (param != null) {
        var c = parseInt(param);
        if (!isNaN(c) && (c >= 0) && (c < CHAPTER_COUNT)) {
            clickChapter(c);
            return;
        }
    }
    
    // no chapter parameter present, initialize the legend
    clearLegend();
}

// handle click on chapter button
function clickChapter(c) {
    // activate the chapter's location
    var loc = chapters[c].location;
    if (loc != null) {
        // (onMarkerClick will suppress legend clear)
        loc.onMarkerClick();
    } else {
        suppressLegendClear = true;
        
        // remove window (if open)
        if (chapterMap != null) {
            chapterMap.map.closeInfoWindow();
        }
    
        // fill in legend data
        setLegend(c);

        var msg = 'Sorry, there is no marker for this chapter\'s meeting location.\n\n';
        if (chapters[c].url != null) {
            msg += ' Please go to the chapter\'s web site for more information ' +
                ' or contact the chapter president.';
        } else {
            msg += ' Please contact the chapter president for more information.';
        }
        alert(msg);
        
        suppressLegendClear = false;
    }    
}

// clear legend data
function clearLegend() {
    if (!suppressLegendClear) {
        document.getElementById('legend').innerHTML = '<font face="comic sans ms, ' + 
            'arial, helvetica">Click on a map location or a chapter button for ' + 
            'a meeting location.</font>';
    }
}

// set legend data
function setLegend(c) {
    document.getElementById('legend').innerHTML = 
      '<table border="1" cellpadding="4" bordercolordark="#000000" bordercolorlight="#808080">' + 
      '  <tr>' + 
      '    <th width="25%"><h4><font color="#003333">Chapter</font></h4></th>' + 
      '    <th width="25%"><h4><font color="#003333">Meeting Time</font></h4></th>' + 
      '    <th width="25%"><h4><font color="#003333">Meeting Location</font></h4></th>' + 
      '    <th width="25%"><h4><font color="#003333">Contact</font></h4></th>' +
      '  </tr>' +
      getChapterRowHtml(c, false) + 
      '</table>';
}
