/**
 * hdsbl_summary.js
 *
 * For the www.hdbsl.org.uk website, will show a list of participating
 * clubs and the where they exist on a google map
 *
 * by R.E.Shine
 * 11.09.2009
 *
 * Depandancies :
 * dialog.js - handles the popup boxes.
 *
 * Version 1.0
 *
**/


var hdsblsummary = Class.create({

   initialize: function(connectURL) {
      
      hdsblsummary._connectURL = connectURL;
      hdsblsummary._tabs = new tabNavigation('tablist','tabclublisting');
      hdsblsummary.clubsummary = '';
      hdsblsummary.dialog = new Dialog.Box('iddiv_clubmap');
      
      $('maincontent').show();
      Event.observe($('tabclubmap'), 'click', this.ontab_clubmap_click.bindAsEventListener(this),false);   
      
      new Ajax.Request(hdsblsummary._connectURL, {
         method: 'post',
         parameters: {action : 'getclubsummary'},
         on500: function(transport) {
            alert("Error receiving a 500 server error.");
         },
         onSuccess: function(transport) {
            
            //draw club tab
            clubxhtml = '<table width="100%" border="0" cellpadding="5" class="tblclublist"><tbody valign="top">';
            clubxhtml = clubxhtml + '<tr><th id="idth_club" valign="middle" style="font-size:14px">CLUB NAME/ADDRESS/TELEPHONE</th>'+
                      '<th id="idth_tabminage" style="font-size:14px" >TABLES/<br>MIN.&nbsp;AGE</th>'+
                      '<th id="idth_notes"  style="font-size:14px" align="center" valign="middle">NOTES</th>';
            hdsblsummary.clubsummary = $A(transport.responseJSON.hdsblsummary.clubsummary);
            var i=0;
            hdsblsummary.clubsummary.each(function(item) {
               clubxhtml = clubxhtml + '<tr class="'+( ((i%2) != 0) ? "sumrow" : "sumrowalt")+'">'+
                         '<td headers="idth_club" style="font-size:14px">'+item.name+
                         '&nbsp;&nbsp;<a href="#" onclick="hdsblsummary.prototype.show_clubmap(this,'+i+');return false;">map</a>'+
                         '<br><span style="font-size:12px">'+item.address+'</span><br>'+item.telephone+'</td>'+
                         '<td headers="idth_tabminage"  style="font-size:14px" align="center">'+item.tablenumber+'<br>'+(item.minage == '0' ? '&nbsp;' : item.minage)+'</td>'+
                         '<td headers="idth_notes" style="font-size:12px">';
               
               var notes = $A(item.notes);
               clubnotes = '';
               if (notes.size() !== 0) {
                  notes.each(function(noteitem) {
                     clubnotes = clubnotes + noteitem.note+'<br>'
                  });
               } else {
                  clubnotes = '&nbsp;';
               }
               // clubxhtml = clubxhtml + (clubnotes ? clubnotes.Left(clubnotes, clubnotes.length() - 5) : 'nbsp;'</td>';
               clubxhtml = clubxhtml + clubnotes + '</td>';
               i = i + 1;
            });
            $('id_tblclublist').innerHTML = clubxhtml;
         },
         onFailure: function(transport) {
            alert("Failure : " + transport.responseJSON);
         }
      });
   },
   
   
   ontab_clubmap_click: function(ev) {
   
      new Ajax.Request(hdsblsummary._connectURL, {
         method: 'post',
         parameters: {action: 'getclubmapsummary'},
         on500: function(transport) {
            alert("Error receiving a 500 server error.");
         },
         onSuccess: function(transport) {
            var json = transport.responseJSON.hdsblsummary.clubmap;
            hdsblsummary._clubmap = hdsblsummary.prototype.gmapDraw('clubmap_canvas',
                                    json.centerLat,
                                    json.centerLong,11);
            hdsblsummary._clubmap.addControl(new GLargeMapControl());
            hdsblsummary._clubmap.addControl(new GMapTypeControl());
            
            clubmapjson = $A(json.venues);
            
            var point = '';
            var marker = '';
            var htmltab1 = '';
            var htmltab2 = '';
            var clubnames ='';
                              
            clubmapjson.each( function(venueitem) {
              
               var clubarray = $A(venueitem.venueattribs);  
                                
               clubnames = '';
               htmltab2 = '<div style="font-size:12px;padding-top:10px;"><ul style="width:280px;">';
               clubarray.each( function(clubitem) {
                  clubnames = clubnames + clubitem.name + '<br>';
               });
               
               var vnotesarray = $A(venueitem.venuenotes);
               if (vnotesarray.size() > 0 ) {
                  vnotesarray.each( function( notesitem ) {
                     htmltab2 = htmltab2 + '<li>'+notesitem.note + '</li>';
                  });
                  htmltab2 = htmltab2 + '<ul></div>';
                  htmltab1 = '<div style="width:280px;border-bottom:1px solid;margin-bottom: 6px;">'+clubnames+'</div><div style="font-size:12px;">'+venueitem.address+"</div><br><div>"+venueitem.telephone+"<br></div>";
                  point = new GLatLng(venueitem.latitude, venueitem.longitude);
                  marker = hdsblsummary.prototype.gmapCreateTabbedMarker(point, htmltab1, htmltab2, "Details", "Club Notes");
               } else {
                  htmltab1 = '<div style="border-bottom:1px solid;margin-bottom: 6px;">'+clubnames+'</div><div style="font-size:12px;">'+venueitem.address+"</div><br><div>"+venueitem.telephone+"<br></div>";
                  point = new GLatLng(venueitem.latitude, venueitem.longitude);
                  marker = hdsblsummary.prototype.gmapCreateMarker(point, htmltab1);
               }
               hdsblsummary._clubmap.addOverlay(marker);                
            });

            

         },
         onFailure: function(transport) {
            alert("Failure : " + transport.responseJSON);
         }
      });
   },
   
   show_clubmap: function( ev, club_id) {
   
      var dialogHeight = 500;
      var dialogWidth = 680;
      
      var viewportWidth = document.viewport.getWidth();
      var viewportHeight = document.viewport.getHeight();
      
      var x = Math.max((viewportWidth - dialogWidth) / 2, 0);
      var y = (parseInt(document.viewport.getScrollOffsets().top)) + Math.max((viewportHeight - dialogHeight) / 2, 0);
      
      hdsblsummary.dialog.show();

      setTimeout( function() {       
         $('iddiv_clubmap_name').innerHTML = hdsblsummary.clubsummary[club_id].name;
         var clubmapHandle = hdsblsummary.prototype.gmapDraw("iddiv_clubmap_canvas",
                              hdsblsummary.clubsummary[club_id].latitude,
                              hdsblsummary.clubsummary[club_id].longitude,
                              hdsblsummary.clubsummary[club_id].zoom);
         clubmapHandle.setUIToDefault();
         var clubPoint = new GLatLng(hdsblsummary.clubsummary[club_id].latitude,
                                     hdsblsummary.clubsummary[club_id].longitude);
         var clubMarker = hdsblsummary.prototype.gmapCreateMarker(clubPoint, 
                                               '<div style="font-size:16px;text-align:left;width:270px;border-bottom:1px solid #00cc00;">'+
                                               hdsblsummary.clubsummary[club_id].name+
                                               '</div><br><div style="font-size: 12px;text-align:left;">'+
                                               hdsblsummary.clubsummary[club_id].address+
                                                         '</div');
         clubmapHandle.addOverlay(clubMarker);
         return (false);
      },50);
   
   },
   
   // Google Map Code
   
   gmapDraw: function(idmap, centerLatitude, centerLongitude, zoom) {
      if (GBrowserIsCompatible()) {
         var gmapHandle = new GMap2(document.getElementById(idmap));
         
         gmapHandle.setCenter(new GLatLng(centerLatitude, centerLongitude), parseInt(zoom));
         // gmapHandle.setUIToDefault();
         gmapHandle.clearOverlays();
         return gmapHandle;
         
      } else {
         // Not browser compatible
         $(idmap).innerHTML = "<center><b>Browser is not compatible with googlemaps</b></center>";
         return;
      }
   },
   
   gmapCreateMarker: function(point, html) {
      var marker = new GMarker(point);
      GEvent.addListener(marker, "click", function() {
         marker.openInfoWindow(html);   
      });
      return marker;
   },

   gmapCreateTabbedMarker: function(point, html1, html2, label1, label2) {
   
      var marker = new GMarker(point);
      
      GEvent.addListener(marker, "click", function() {
         
         marker.openInfoWindowTabsHtml([new GInfoWindowTab(label1,html1), new GInfoWindowTab(label2,html2)]);
         
      });
     
     return marker;
   }

});

try {
   Event.observe(window,"load",function() {
      summary = new hdsblsummary('cgi-bin/hdsbl_summary.php');
      
      Event.observe(window,"unload", function() {
         GUnload();
      });
   }, true);
} catch (ex) {}

   
