  // Array of info objects, one for each widget (w/ photos) on a page
  var photoInfo = new Array(); 
  
  function setupPhoto(photosObj, photoIndex, uniq)
  {
    // Add this widget's photo data to the photoInfo array
    var size = photoInfo.length;
     
    photoInfo[size] = new Object();
    photoInfo[size].uniq = uniq; //unique key for this widget's data
    photoInfo[size].index = photoIndex; //index of current photo shown
    photoInfo[size].photos = new Array(); //list of photo URLs
    
    // Convert comma-sep list of photo data into array
    // (data may have internal commas so need to split on "http";
    // ignore first entry which is opening bracket only)
    var tmp = photosObj.split("http");
    for (var i=1; i < tmp.length; i++)
    {
      var tmpUrl = "http" + tmp[i];
      // Clean up trailing brackets, commas and spaces
      if (i == tmp.length-1)
      {
        tmpUrl = tmpUrl.substring(0,tmpUrl.length-1);
      }
      if (tmpUrl.substring(tmpUrl.length-2) == ", ") // remove trailing comma
      {
        tmpUrl = tmpUrl.substring(0,tmpUrl.length-2);
      }
      if (tmpUrl.substring(tmpUrl.length-1) == ",") // remove trailing comma
      {
        tmpUrl = tmpUrl.substring(0,tmpUrl.length-1);
      }
      photoInfo[size].photos[i-1] = tmpUrl;
    }
  }
  
  function changePhoto(increment, uniq, photoIndex, photosObj)
  {
    // Get correct info object for this uniq
    var curr = -1;
    for (var i=0; i < photoInfo.length; i++)
    {
      if (photoInfo[i].uniq == uniq)
      {
        curr = i;
        break;
      }
    }
    if (curr == -1)
    {
      // This widget's photos haven't been stored yet
      setupPhoto(photosObj, photoIndex, uniq);
      curr = photoInfo.length - 1;
    }

    // Add increment to previous index, cycle past
    // beginning/end of photos if necessary
    var tmpIndex = photoInfo[curr].index + increment;
    if (tmpIndex >= photoInfo[curr].photos.length)
    {
      tmpIndex = 0;
    }
    else if (tmpIndex < 0)
    {
      tmpIndex = photoInfo[curr].photos.length - 1;
    }
    photoInfo[curr].index = tmpIndex;
    
    // Get photo info for new index; includes URL and
    // possibly photo attribution string
    var photoFields = photoInfo[curr].photos[tmpIndex];
    if (photoFields == null)
    {
      return;
    }
    var photoFieldArr = photoFields.split("!");
    if (photoFieldArr == null)
    {
      return;
    }
    
    // Set the new photo URL on the page
    var photoUrl = photoFieldArr[0];
    var pImg = document.getElementById("photoImage" + photoInfo[curr].uniq);
    if (pImg != null)
    {
      pImg.setAttribute("src", photoUrl);
    }

    // Set the new photo attrib string on the page, if provided
    if (photoFieldArr.length > 1)
    {
      var photoAttrib = photoFieldArr[1];
      var pAttrib = document.getElementById("photoAttribution" + photoInfo[curr].uniq);
      if (pAttrib != null)
      {
        var attribElem = document.createTextNode(photoAttrib);
        while (pAttrib.firstChild != null)
        {
          pAttrib.removeChild(pAttrib.firstChild);
        }
        pAttrib.appendChild(attribElem);
      }
    }
        
  }
  
  function doPopup(url, wname)
  {
    if (wname == null || wname == '')
    {
      wname='widgetPopupWin';
    }
    popwindow = window.open(url, wname, 'width=600,height=600,scrollbars=yes');
    if (window.focus)
    {
      popwindow.focus();
    }
  }
  
  function doNewTAWindow(url, wname)
  {
    if (wname == null || wname == '')
    {
      wname='widgetNewTaWin';
    }
    window.open(url, wname, 'toolbar=1,resizable=1,menubar=1,location=1,status=1,scrollbars=1,width=800,height=600');
  }
  
  function doSort(sortValue)
  {
    var reviewElems = getElementsByName_iefix("dd", "sortableReview");
    if (reviewElems == null || reviewElems.length <= 1)
    {
      return; // nothing to sort
    }
    var arrToSort = new Array();
	
    for (var i=0; i < reviewElems.length; i++)
    {
      var currId = reviewElems[i].id.split("-");
      if (sortValue == null || sortValue.indexOf("date") == 0)
      {
        arrToSort.push(currId[1] + "-" + currId[0] + "-" + currId[2]);
      }
      else 
      {
        arrToSort.push(currId[2] + "-" + currId[0] + "-" + currId[1]);
      }
    }
    arrToSort.sort(); // sorts ascending
    if (sortValue.indexOf("desc") > -1)
    {
      arrToSort.reverse();
    }
	
    var newContainer = document.createElement("div");

    for (var i=0; i < arrToSort.length; i++)
    {
      var currIdInfo = arrToSort[i].split("-");
      var elemId = "";
      if (sortValue == null || sortValue.indexOf("date") == 0)
      {
        elemId=currIdInfo[1] + "-" + currIdInfo[0] + "-" + currIdInfo[2];
      }
      else
      {
        elemId=currIdInfo[1] + "-" + currIdInfo[2] + "-" + currIdInfo[0];
      }
      var revElem = document.getElementById(elemId);
      newContainer.appendChild(revElem);
    }
    var container = document.getElementById("reviewsContainer");
    while (container.firstChild != null)
    {
      container.removeChild(container.firstChild);
    }
    container.appendChild(newContainer);
  }

  function getElementsByName_iefix(tag, name) 
  {
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
  }
  
  function expandReview(index, idPrefix, isExpand)
  {
    var shortDisplay = "none"; // default to "more"
    var fullDisplay = "block";
    
    if (isExpand == 0) // we're actually doing "less"
    {
      shortDisplay = "block";
      fullDisplay = "none";
    }
    
    var rtextElems = getElementsByName_iefix("dd", "reviewText");
    for (var i=0; i < rtextElems.length; i++)
    {
      var currElem = rtextElems[i];
      if (currElem.id == idPrefix + "short" + index)
      {
        currElem.style.display = shortDisplay;
      }
      else if (currElem.id == idPrefix + "full" + index)
      {
        currElem.style.display= fullDisplay;
      }
      else if (currElem.id.indexOf("short") != -1)
      {
        currElem.style.display="block";
      } 
      else
      {
        currElem.style.display="none";
      }
    }
  }
  
  function toggleLanguage(count, idPrefix)
  {
    var revElems = getElementsByName_iefix("dd", "sortableReview");
    for (var i=0; i < revElems.length; i++)
    {
      var currElem = revElems[i];
      if (currElem.id.substr(0, 1) == idPrefix)
      {
        currElem.style.display="block";
        var subElems = currElem.getElementsByTagName("dd");
        for (var j=0; j < subElems.length; j++)
        {
          var tmpElem = subElems[j];
          var tmpId = tmpElem.id;
          if (tmpId.substr(1, 4) == "full" || tmpId.substr(1, 5) == "short")
          {
            // If it matches the count, show full text, otherwise short
            if (tmpId.substr(tmpId.length-1, 1) == count)
            {
              if (tmpId.substr(0, 5) == (idPrefix + "full"))
              {
                tmpElem.style.display="block";
              }
              else
              {
                tmpElem.style.display="none";
              }
            }
            else
            {
              if (tmpId.substr(0, 6) == (idPrefix + "short"))
              {
                tmpElem.style.display="block";
              }
              else
              {
                tmpElem.style.display="none";
              }
            }
          }
        }
      }
      else // id doesn't start with prefix; hide all
      {
        currElem.style.display="none";
      } // end if (currElem.id.startsWith(idPrefix))
      
    } // end for loop in revElems
  
  }
  
  function showElem(elemId)
  {
    var elem = document.getElementById(elemId);
    if (elem != null)
    {
      elem.style.display="block";
    }
  }
  
  function hideElem(elemId)
  {
    var elem = document.getElementById(elemId);
    if (elem != null)
    {
      elem.style.display="none";
    }
  }
  
  function changeTab(tabId)
  {
    var tabElems = getElementsByName_iefix("a", "propertyTab");
    if (tabElems == null || tabElems.length <= 1)
    {
      return; // nothing to do
    }
    // Display the right selected tab heading
    for (var i=0; i < tabElems.length; i++)
    {
      if (tabElems[i].id == tabId)
      {
        tabElems[i].className="tabSelected"; // selected tab class
      }
      else
      {
        tabElems[i].className=""; // not selected
      }
    }
    // Now display the right list
    var propListElems = getElementsByName_iefix("div", "propertyList");
    if (propListElems == null || propListElems.length <= 1)
    {
      return; // nothing to do
    }
    for (var i=0; i < propListElems.length; i++)
    {
      if (propListElems[i].id == "list" + tabId)
      {
        propListElems[i].style.display="block"; 
      }
      else
      {
        propListElems[i].style.display="none";
      }
    }

  }
  
  function getPartnerLink(taUrl, locationId, popupWindow, newWindow)
  {
    if (window.taPartnerLink) // partner function is defined
    {
      var pData = taPartnerLink(taUrl, locationId);
      // If partner function returned a value that is not the 
      // TA URL we passed in, redirect to that page.
      if (pData != null && pData != "" && pData != taUrl)
      {
          // redirect entire page, not just iframe
          window.open(pData, '_top', '', false);
          return;
      }
    }
    
    // If the TA URL should open in a popup, invoke the popup window
    if (popupWindow != null && popupWindow != "")
    {
      doPopup(taUrl, popupWindow);
      return;
    }
    
    // If the TA URL should open in a new window, invoke the new window
    if (newWindow != null && newWindow != "")
    {
      doNewTAWindow(taUrl,newWindow);
      return;
    }
    
    // Otherwise just redirect
    window.location = taUrl;
    return;
  }
  
  function doCRPopup(url, wname)
  {
    popwindow = window.open(url, wname, 'toolbar=0,resizable=1,menubar=0,location=0,status=0,scrollbars=1,width=255,height=730,screenX=5,left=5,screenY=5,top=5');
    if (window.focus)
    {
      popwindow.focus();
    }
  }
  
  function doMore(elemId)
  {
    var fullElem  = document.getElementById(elemId + "full");
    var shortElem = document.getElementById(elemId + "short");
    
    fullElem.style.display="block";
    shortElem.style.display="none";
  }
  
  function doLess(elemId)
  {
    var fullElem  = document.getElementById(elemId + "full");
    var shortElem = document.getElementById(elemId + "short");
    
    fullElem.style.display="none";
    shortElem.style.display="block";
  }
  
  // Functions for maps in destination widget
  function showMap(elemId, mapLat, mapLong, nZoom)
  {
    var mapDiv = document.getElementById(elemId);
    // Display the map, with some controls and set the initial location
    mapDiv.map = new TAMap(mapDiv, {
      origLat: mapLat,
      origLng:mapLong,
      zoom: nZoom,
      servlet: 'WidgetEmbed-cdsdest',
      smallMap:     true,
      typeControl:  false,
      scaleControl: false
    });
    mapDiv.map.addIcon('hotel', {name: 'Hotel'});
    mapDiv.map.addIcon('restaurant', {name: 'Restaurant'});
    mapDiv.map.addIcon('attraction', {name: 'ThingToDo'});
    
    // recenter the map when user clicks on star icon
    var mapTitleElem = $('mapTitle');
    if (mapTitleElem)
    {
      mapTitleElem.addEvent('click', mapDiv.map.reset.bind(mapDiv.map));
    }
    
    // use markerData
    mapDiv.map.addMarkers(markerData.hotels, 'hotel');
    mapDiv.map.addMarkers(markerData.restaurants, 'restaurant');
    mapDiv.map.addMarkers(markerData.attractions, 'attraction');
    
    // show error if no pins
    mapDiv.errorDiv = $('mapError');
    var errorDiv = mapDiv.errorDiv;
    if (errorDiv)
    {
      mapDiv.map.addEvent("noPins", function() {errorDiv.show();});
      mapDiv.map.addEvent("allPins", function() {errorDiv.hide();});
      mapDiv.map.addEvent("homePinOnly", function() {errorDiv.show();});
    }
  }
  
  function toggleMapMarkers(elemId, type)
  {
    var mapDiv = document.getElementById(elemId);
    mapDiv.map.toggleType(type);
  }
  
