/*
map_functions.js Created JT Jan 2007 for De Bortoli WInes Pty Limited

Array of markers to be added to the map. Latitude and Longitude values represent the branches location, where the description is used for the office address 
within the popup window.
*/

var markers = [
    {
        //Bilbul
        'latitude': -34.27544722222222,
        'longitude': +146.1427166666667,
        'description': '<b>Bilbul - Head Office</b><br /> De Bortoli Road<br /> PO Box 21<br /> Bilbul, NSW, 2680<br /> Tel: 61 2 6966 0100<br /> Fax: 61 2 6966 0199 <br /><center><img src = "http://www.debortoli.com.au/fileadmin/google_maps/bilbul.jpg" HEIGHT="138", WIDTH="200"></center>'
    },
    {
        //Sydney
        'latitude': -33.797459,
        'longitude': +150.941658,
        'description': '<b>Sydney Office</b><br />134 Gilba Road <br /> Sydney, NSW, 2145<br /> Tel: 02 9636 6033<br /> Fax: 02 9896 4565<br /><center><img src = "http://www.debortoli.com.au/fileadmin/google_maps/sydney.jpg" HEIGHT="128", WIDTH="200"></center>'
    },
    { 
        //Yarra
        'latitude': -37.596860,
            'longitude': +145.396264,
        'description': '<b>Yarra Valley Office</b><br /> Pinnacle Lane<br /> Dixons Creek<br /> VIC, 3775<br /> Tel: 03 5965 2271<br /> Fax: 03 5965 2442<br /><center><img src = "http://www.debortoli.com.au/fileadmin/google_maps/yarravalley.jpg" HEIGHT="133", WIDTH="200"></center>'
    },
    { 
        //Perth
        'latitude': -31.973224888175604,
        'longitude': +115.85054039955139,
        'description': '<b>Perth Office</b><br /> Unit 55<br /> 15 Labouchere Road<br /> South Perth, WA, 6151<br /> Tel: 08 9474 7400<br /> Fax: 08 9474 7499<br /><center><img src = "http://www.debortoli.com.au/fileadmin/google_maps/logo_sig.gif" HEIGHT="44", WIDTH="150"></center>'
    },
    { 
        //Brisbane
        'latitude': -27.732334,
        'longitude': +153.237526,
        'description': '<b>Brisbane Office</b><br />2 Emeri Street<br />Stapylton<br /> QLD, 4207<br /> Tel: 07 3287 2500<br /> Fax: 07 3287 1133<br /><center><img src = "http://www.debortoli.com.au/fileadmin/google_maps/brisbane.jpg" HEIGHT="122", WIDTH="200"></center>'
    },
    { 
        //Hunter
        'latitude': -32.77457403504531,
        'longitude': +151.34173393249512,
        'description': '<b>Hunter Valley Office</b><br />532 Wine Country Drive<br />Pokolbin<br /> NSW, 2320<br /> Tel: 02 4993 8800<br />Fax: 02 4993 8899<br /><center><img src = "http://www.debortoli.com.au/fileadmin/google_maps/huntervalley.jpg" HEIGHT="153", WIDTH="200"></center>'
    },
    { 
        //Kimg Valley
        'latitude': -36.5949,
        'longitude': +146.383424,
        'description': '<b>King Valley - BellaRiva Vineyard</b><br />Vineyards and production only. No public access.<br /><center><img src = "http://www.debortoli.com.au/fileadmin/google_maps/kingvalley.jpg" HEIGHT="150", WIDTH="113"></center>'
    },
    { 
        //Melbourne
        'latitude': -37.817420,
        'longitude': +145.289550,
        'description': '<b>Melbourne Office</b><br />6-8 Research Drive<br />Croydon, VIC, 3136<br />Tel: 03 9761 4100<br />Fax: 03 9761 4192<br /><center><img src = "http://www.debortoli.com.au/fileadmin/google_maps/melbourne.jpg" HEIGHT="136", WIDTH="200"></center>'
    },   
    { 
        //USA
        'latitude': +40.73880,
        'longitude': -74.03826,
        'description': '<b>De Bortoli Wines USA Inc</b><br />500 First St<br />Hoboken, NJ 07030, USA<br />Tel: + 1 201 714 9700<br />Fax: + 1 201 714 9733<br /><center><img src = "http://www.debortoli.com.au/fileadmin/google_maps/logo_sig.gif" HEIGHT="44", WIDTH="150"></center>'
    },
    { 
        //UK
        'latitude': +50.934064783941416,
        'longitude': -2.05619215965217,
        'description': '<b>De Bortoli Wines UK Inc</b><br />Minchington Farm<br />Farnham, Blandford Forum<br />Dorset DT11 8DE<br />Tel: + 44 1725 516 467<br />Fax: + 44 1725 516 403<br /><center><img src = "http://www.debortoli.com.au/fileadmin/google_maps/logo_sig.gif" HEIGHT="44", WIDTH="150"></center>'
      },
       { 
        //Belgium
        'latitude': +51.19731066379804,
        'longitude': +4.40826416015625,
        'description': '<b>De Bortoli Wines Europe N.V.</b><br />Jan Van Rijswijcklaan 33-35<br />B-2018 Antwerp, Belgium<br />Tel: + +32 3 231 2083<br />Fax: + 32 3 231 8878<br /><center><img src = "http://www.debortoli.com.au/fileadmin/google_maps/logo_sig.gif" HEIGHT="44", WIDTH="150"></center>'
    }
];

//Starting Details for centering the map
var centerLatitude = -29.251022;
var centerLongitude = +137.423851;
var startZoom = 4;

//Creating the instance of the map
var map;

//This function is passed 5 variables from the init() function. It will create a
//marker on the map at the provided point, with the description and image passed
function addMarker(latitude, longitude, index, description, myImage){
    var marker = new GMarker(new GLatLng(latitude, longitude)); 
    GEvent.addListener(marker, 'click', function(){                      //adds the event listener for the mouse clicks on the markers provided
            marker.openInfoWindowHtml(description);                      //the listener opens a html window displaying the description
        });
  if (markers && (index < markers.length)) {      //if the index isnt greater than the length of the markers array
    markers[index].marker = marker;        //adds the new marker to the markers array
  } else { alert("markers["+index+"] doesn't exist!");}    //else alert of an error.
  
  map.addOverlay(marker);                                                 //adds the marker to the map
}

//This function is used by the reset button on the toolbar to focus the map over australia
function setToHome()
{
  map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
}

//This function is called by the table created within the html file to change map focus
function changeLocation(siteId)
{
  siteId = siteId*1;
  if(typeof map == 'object' && typeof siteId == 'number') {
    map.setZoom(13);                                                       //changes zoom level closer
    map.panTo(markers[siteId].marker.getPoint());                  //lastly pans to the new desired location.
    GEvent.trigger(markers[siteId].marker, "click");      //Trigers event listener when item is selected from toolbar
  }
}

//ini() function is responsible for initialising the map and adding the controls to it.
function init()                                                             
{
    if(GBrowserIsCompatible())
    {
        
        map = new GMap2(document.getElementById("map"),{size:new GSize(560,507)});     //creates the map
        //map controls (zoom, etc)
        map.addControl(new GLargeMapControl());                //large zoom and directional control 
        map.addControl(new GScaleControl());             //scale control for the bottom left
        map.addControl(new GMapTypeControl());             //control to allow switching between hybrid, satellite and map images
        //var olcontrol = new GOverviewMapControl(new GSize(150,150));
        //map.addControl(olcontrol); //Add corner overview map
        //olcontrol.hide(true);
        map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom); //sets the focus of the map to the coordinates named above
        map.setMapType(G_HYBRID_MAP);          //sets default view to hybrid
        for (id in markers){                                                    //for in loop to add each of the markers in the array to the map.
            addMarker(markers[id].latitude, markers[id].longitude, id, markers[id].description);
        }
       map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
   }
}

//window.onload = init;     //Call function to load Australian Map with all sites
window.onunload = GUnload;
//This function is used to extract the site number passed to the page. This is only used if the viewer is passed here from the Contact us page in TYPO3.
//Note this is required to handle passing server side PHP params to client side Javascript
function parseQueryString (str) {
  str = str ? str : location.search;
  str.replace(/\?/,'',str);
  var fields = str.split('&');
  var field = '';
  jQuery(fields).each(function() {
    if(this.search(/site/) != -1) {
      field = this.split('=')[1];
    }
  });
  if(field == '') {
    field = -1;
  }
  return field;
}

//This will set the variable focusOn to the site number selected from the Contact us page, ie bilbul is 0, Sydney is 2, etc.
//var focusOn = parseQueryString();
var siteId = parseQueryString();
//This line passes the site number (if any) to the changeLocation function which will change the focus of the map. The timeout ensures that the page is fully loaded before the function is executed.
jQuery(window).load(function() {
  init();
  if(siteId != -1 && siteId<=9 ) {
    setTimeout("changeLocation(siteId);",600);
  } else {
    setTimeout("setToHome();",2000);
  }
});
//window.setTimeout("changeLocation(focusOn)", 400);
