﻿// JScript File

/* Photo Rollovers */
Event.observe(window, 'load', function ()
{
  $A(document.getElementsByClassName('photoRollover')).each(
    function(img)
    {
      Event.observe(img, 'mouseover', function (e)
      {
        var photoDestination = document.getElementsByClassName('photoRolloverDest');
        if (photoDestination && photoDestination[0])
        {
          var strLargePhotoSrc = photoDestination[0].src;
          var strLargePhotoAlt = photoDestination[0].alt;
          photoDestination[0].src = img.src;
          photoDestination[0].alt = img.alt;
          img.src = strLargePhotoSrc;
          img.alt = strLargePhotoAlt;
        }
      });
    }
  );
});

/* End Photo Rollovers */

/* Start Maps Section */
  var addressList = null;
  var mapLoaded = false;
  var map = null; 
  var geocoder = null;
  var coordsArray = null;
  
  function setAddressList(addressArray)
  {
    addressList = addressArray;
    // Set an array to hold the coordiantes
    coordsArray = new Array(addressList.length);
  }
  
  function initializeGoogleApi()
  {
    if (GBrowserIsCompatible())
    {
      var cityIcon = new GIcon();
      var imgPath = "/img/";
      cityIcon.image = imgPath + "city_icon.png";
      cityIcon.iconSize = new GSize(30, 27);
      cityIcon.iconAnchor = new GPoint(18, 25);
      cityIcon.infoWindowAnchor = new GPoint(2, 1);
    }
    
    if (GBrowserIsCompatible())
    {
      GMap2.prototype.centerAndZoomOnBounds = function(bounds)
      { 
        var center_lat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2.0;
        var center_lng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2.0;
        if(bounds.getNorthEast().lng() < bounds.getSouthWest().lng())
        { 
          center_lng += 180;
        } 
        var center = new GLatLng(center_lat,center_lng) 
        var zoomLevel = map.getBoundsZoomLevel(bounds);
        map.setCenter(center, (zoomLevel > 13)?13:zoomLevel);
      }
    }
  }
  
  function loadGeo(addressArray)
  { 
    if (mapLoaded)
      return false;
    
    if (GBrowserIsCompatible())
    {
      // Set up the map
      map = new GMap2(document.getElementById("map"));
      
      map.addControl(new GSmallZoomControl());
      map.addControl(new GMapTypeControl());
      
      map.setCenter(new GLatLng(51.2, -115.43), 6); // By default, zoom in on Brewster HQ.  This will be changed later.
      
      // Initialize the geocoder
      geocoder = new GClientGeocoder();
      
      // Now loop through the addressList array and get the coordiantes for each city.
      //for (var i = 0; i < addressList.length; i++)
      //{
      //  getAddressCoords(addressList[i], i);
      //}
      for (var i = 0; i < addressList.length; i++)
      {
        getAddressCoords(addressList[i], i);
      }
    }
    mapLoaded = true;
  }
  
  function showAddress(address, arrayIndex) { 
    if (geocoder) { 
      geocoder.getLatLng( 
        address, 
        function(point) { 
          if (point)
          { 
            coordsArray[arrayIndex] = point;
          }
          if (arrayIndex + 1 == addressList.length)
          {
            window.setTimeout('editMap()', 333);
          } 
        } 
      ); 
    } 
  }
  
  function getAddressCoords(address, arrayIndex)
  {
    if (geocoder)
    {
      geocoder.getLatLng(
        address, 
        function(point)
        {
          if (point) // If a coordinate was returned, add it to the coords array
          {
            coordsArray[arrayIndex] = point;
          }
          if (arrayIndex + 1 == addressList.length)
          {
            window.setTimeout('editMap()', 333);
          }
        }
      );
    }
  }
  
  function editMap()
  {
    markCities();
    connectCities();
    centerMap();
    map.checkResize();
  }
  
  function markCities()
  {
    for (var i = 0; i < coordsArray.length; i++) // Loop through the coordiantes
    {
      if (coordsArray[i]) // Check that the coordiante is not empty.
      {
        //var city = new GMarker(coordsArray[i], cityIcon);
        var city = new GMarker(coordsArray[i]);
        map.addOverlay(city);
        city.bindInfoWindowHtml('<h2>' + addressList[i] + '<h2>');
      }
    }
  }
  
  function connectCities()
  {
    if (coordsArray.length > 0)
    {
      var prevCoords = coordsArray[0];
      for (var i = 0; i < coordsArray.length; i++) // Loop through the coordiantes
      {
        if (coordsArray[i]) // Check that the coordiante is not empty.
        {
          if (prevCoords)
          {
            try
            {
                var polyline = new GPolyline([prevCoords, coordsArray[i]], "#FF0000", 3);                            
                map.addOverlay(polyline);
            }
            catch(er)
            {
              // Do nothing.
            }
            prevCoords = coordsArray[i];
          }
        }
      }
    }
  }
  
  function centerMap()
  {
    var minX, minY, maxX, maxY;
    for (var i = 0; i < coordsArray.length; i++) // Loop through the coordiantes
    {
      if (coordsArray[i]) // Check that the coordiante is not empty.
      {
        if (!minX || coordsArray[i].x < minX)
          minX = coordsArray[i].x;
        if (!minY || coordsArray[i].y < minY)
          minY = coordsArray[i].y;
        if (!maxX || coordsArray[i].x > maxX)
          maxX = coordsArray[i].x;
        if (!maxY || coordsArray[i].y > maxY)
          maxY = coordsArray[i].y;
      }
    }
    var bound = new GLatLngBounds(new GLatLng(minY, minX), new GLatLng(maxY, maxX));
    map.centerAndZoomOnBounds(bound);
  }

Event.observe(window, 'load', initializeGoogleApi);
/* End Maps Section */

/* Testimonials Section */
var testimonialWin = null;
function openAddWin()
{
    var winParams = 'width=450,height=500,location=no,left=225,top=150';
    testimonialWin = window.open('/testimonials-add.aspx?PACKID=' + window.packageID,'testimonials',winParams);
    testimonialWin.focus();
    return false;
}

// Create the AJAX request object.
var testimonialRequest = null;
// Try and instanctiate the XMLHttp object
try {
	testimonialRequest = new XMLHttpRequest(); // Non-IE browsers
} catch (trymicrosoft) {
	try {
		testimonialRequest = new ActiveXObject("Msxml2.XMLHTTP"); // IE try #1
	} catch (othermicrosoft) {
		try {
			testimonialRequest = new ActiveXObject("Microsoft.XMLHTTP"); // IE try #2
		} catch (failed) {
			testimonialRequest = false;
		}  
	}
}

function getTestimonialsPage(pageID)
{
  if (testimonialRequest)
  {
    document.body.style.cursor = 'wait';
    var ajaxURL = "/testimonials-list.aspx?packageid=" + window.packageID + "&page=" + pageID;
    testimonialRequest.open("GET", ajaxURL, true);
    testimonialRequest.onreadystatechange = showTestimonialsPage;
    testimonialRequest.send('');
  }
  else
  {
    // Ajax not supported.
    var postBackURL = window.location.href;
    if (postBackURL.indexOf('?') > 0)
      postBackURL = postBackURL.substr(0, postBackURL.indexOf('?'));
    window.location.href = postBackURL + '?tp=' + pageID + '#testimonials';
  }
}

function showTestimonialsPage()
{
  if (testimonialRequest.readyState == 4) { // We only care for readyState of 4
		document.body.style.cursor = 'default';
		if (testimonialRequest.status == 200)
		{
		  var testimonialsHTML = testimonialRequest.responseText;
      document.getElementById("testimonialOutput").innerHTML = testimonialsHTML; 
		}
  }
}
/* End Testimonials */

/* Videos section */
function loadVideoToPlayer(videoID)
{
  var newVideo = $('videoCodeBlock' + videoID);
  if (newVideo)
  {
    var codeBlock = newVideo.getElementsByClassName('videoCode');
    if (codeBlock && codeBlock[0])
    {
      $('videoPlayerBlock').update(codeBlock[0].innerHTML);
    }
  }
}
Event.observe(window, 'load', function () {loadVideoToPlayer(1)});
/* End Video Section */

/* Hotel Tab functions */
var hotelWin = null;
function openHotelWindow(hotelUrl)
{
  hotelWin = window.open(hotelUrl, 'hotelWin', 'width=550,height=500,scrollbars=yes');
  hotelWin.focus();
}
/* End Hotel Tab */
