/* -----------------*/
/* Global Vars      */
/* -----------------*/

var OurGMap,OurGMap_Panorama,OurGMap_Directions,Our_GMap_Marker,OurGMap_InfoWindow;
var directionsService = new google.maps.DirectionsService();

var MapCentre_LatLng = new google.maps.LatLng(53.2755,-6.217833);
var ApexHouse_LatLng = new google.maps.LatLng(53.276826,-6.217833);
var LuasStation_LatLng = new google.maps.LatLng(53.278969,-6.210279);
var Beacon_LatLng = new google.maps.LatLng(53.276377,-6.220729);
var Bewleys_LatLng = new google.maps.LatLng(53.271694,-6.206095);
var Symantec_LatLng = new google.maps.LatLng(53.407351,-6.353933);

var OurGMapOptions =
    {
      zoom: 15,
      overviewMapControl: true,
      center: MapCentre_LatLng, // ApexHouse_LatLng,
      mapTypeId: google.maps.MapTypeId.ROADMAP // HYBRID
    };

// ------------------------------------------------------------------------------------------------------------------------------------------------
//  Main docready fn
// ------------------------------------------------------------------------------------------------------------------------------------------------

    $(document).ready(function()
    {

                /* Apply jQuery UI Tabs widget to our (initially hidden) tabbed content */
                $('#Tabs').tabs().show();

                /* Reveal any DIV and IMG we've hidden using the 'reveal' class */
                setTimeout("$('div.reveal').slideDown('slow');",4000);
                setTimeout("$('img.reveal').fadeIn('slow');",8000);

                /* Make a click on eihter of the tabs with scrolling content start their respective scrollers */
                $('#NewsClick'     ).click(function() {  StartNewsScroller();  });
                $('#WeCanHelpClick').click(function() {  StartHelpScroller();  });

                /* spam us not - lets avoid having email address in plain HTML on the mailto link... */
                InfoAddr = '*@%$£';
                InfoAddr = InfoAddr.substr(1,1);
                InfoAddr = 'info' + InfoAddr;
                InfoAddr = InfoAddr + '!hreatsc^pe.com';
                InfoAddr = InfoAddr.replace('!','t');
                InfoAddr = InfoAddr.replace('^','a');
                $('#mailto').html(InfoAddr);
                $('#mailto').attr('href','mail' + 'to:' + InfoAddr);

                /* Init the Google Map API for contact form */
                $('#ContactTabClick').click(function() {  Init_GMap();  });
                // Init_GMap();
    });


// ------------------------------------------------------------------------------------------------------------------------------------------------
//     Other fns
// ------------------------------------------------------------------------------------------------------------------------------------------------

function Init_GMap()
{
    OurGMap = new google.maps.Map(document.getElementById("map_canvas"),OurGMapOptions);

    // Add marker for Apex House
    // -------------------------

    Our_GMap_Marker = new google.maps.Marker(
        {
            position: ApexHouse_LatLng,
            map: OurGMap,
            title:"Threatscape"
        });

    // Add InfoWindow with our address as its content
    // ----------------------------------------------

    OurGMap_InfoWindow = new google.maps.InfoWindow(
        {
            content: $('#OurAddress').html()
        });

    // Make a click on our map marker over Apex House open the InfoWindow
    // --------------------------------------------------

    google.maps.event.addListener(Our_GMap_Marker, 'click', function()
            {
                 OurGMap_InfoWindow.open(OurGMap,Our_GMap_Marker);
            });

    // Set up street view so it is ready when needed
    // --------------------------------------------------

    var OurGMap_PanoOptions =
        {
          position: ApexHouse_LatLng,
          addressControl: false,
          /* pov: { heading: 44,pitch: 20,zoom: 0 }, */
          pov: { heading: 30.6,pitch: 20,zoom: 0.33 },

          visible:false
        };

    OurGMap_Panorama = new  google.maps.StreetViewPanorama(document.getElementById("map_canvas"), OurGMap_PanoOptions);
    OurGMap.setStreetView(OurGMap_Panorama);

    // Init the Directions Panel
    // --------------------------------------------------

    OurGMap_Directions = new google.maps.DirectionsRenderer();
    OurGMap_Directions.setMap(OurGMap);
    OurGMap_Directions.setPanel(document.getElementById("directions_DIV"));
    $('#directions_DIV').empty(); // In case we are reloading the map - wipe previous directions
}

// ------------------------------------------------------------------------------------------------------------------------------------------------

function Show_StreetView()
{
    OurGMap_Panorama.setVisible(true);
    $('#directions_DIV').empty();
}


function Show_SatView()
{
    OurGMap.setOptions(OurGMapOptions);                    // Reset map zoom, centre, etc
    OurGMap.setMapTypeId(google.maps.MapTypeId.HYBRID);     // And change to sat hybrid view
}

// ------------------------------------------------------------------------------------------------------------------------------------------------

function Show_Directions(StartPoint,TravelMode)
{
    $('#DirectionsSelection').hide();
    Init_GMap();
    var OurGMap_Directions_Parms =
    {
        origin:StartPoint,
        destination:ApexHouse_LatLng,
        travelMode: TravelMode
    };

    directionsService.route(OurGMap_Directions_Parms, function(response, status)
    {
      if (status == google.maps.DirectionsStatus.OK)
      {
        OurGMap_Directions.setDirections(response);

        // Default to starting zoom and centre for directions from nearby places which are on our default map (luas, Beacon, Bewleys)
        OurGMap.centre(MapCentre_LatLng);
      }
    });
  }

// ------------------------------------------------------------------------------------------------------------------------------------------------

function Show_StreetView_POV()
{
    var pov = OurGMap_Panorama.getPov();
    $('#my_pov').html('<B>Heading: </B>' + pov.heading + '<BR><B>Pitch: </B>' + pov.pitch + '<BR><B>Zoom: </B>' + pov.zoom);
}

// ------------------------------------------------------------------------------------------------------------------------------------------------

//News Scroller (Widget)
function StartNewsScroller()
{
 	$('#NewsNav').empty();  /* Avoid multiple sets of news nav anchors if user changes to news tab more than once */

 	$('.news-scroller').cycle({
	    fx: 'scrollVert',
		speed: 1000,
		rev: true,
		timeout: 4000,
		next: '#news-next',
	    prev: '#news-previous',

	    pause: 1,       /* Pause on hover */

        pager: '#NewsNav'  /* add numbered navigation links */

	 });
}

// ------------------------------------------------------------------------------------------------------------------------------------------------

function StartHelpScroller()
{
 	$('#HelpNav').empty();  /* Avoid multiple sets of news nav anchors if user changes to tab more than once */

 	$('#help-scroller').cycle({
	    /* fx: 'wipe', */
		speed: 1000,
		rev: true,
		timeout: 4000,
	    pause: 1,       /* Pause on hover */

        pager: '#HelpNav'  /* add numbered navigation links */

	 });
}

// ------------------------------------------------------------------------------------------------------------------------------------------------


