var _togglingFavourite = false; // used to prevent multiple calls

function scrollToElement(theElement){
  var selectedPosX = 0;
  var selectedPosY = 0;
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
  window.scrollTo(selectedPosX,selectedPosY);
}

//FIXME - function probably needs removing entirely
function setAddToMyStuffLink( enable ) {
   var link = document.getElementById('add-my-stuff');
   var icon = document.getElementById('add-my-stuff-icon');
   if (enable) {
      log('enable "add to my stuff" link');
      link.setAttribute('class','enabled');
      link.setAttribute('className','enabled');
      link.href = link.href.replace( 'false);','true);' );
      icon.src = icon.src.replace( 'placemark_not.gif','placemark.gif' );
   }
   else {
      log('disable "add to my stuff" link');
      link.setAttribute('class','disabled');
      link.setAttribute('className','disabled');
      link.href = link.href.replace( 'true);','false);' );
      icon.src = icon.src.replace( 'placemark.gif','placemark_not.gif' );
   }
}

//FIXME - function probably needs removing entirely
function setAddToMyFavsLink( enable ) {
   var link = document.getElementById('fav-label');
   var icon = document.getElementById('fav-label-icon');
   if (enable) {
      log('change to "Add"');
      link.innerHTML = "Add to My Favourites";
      icon.setAttribute('src','/icons/favorite.gif');
   }
   else {
      log('change to "Remove"');
      link.innerHTML = "Remove from My Favourites";
      icon.setAttribute('src','/icons/favorite_not.gif');
   }
}

function deleteDetailsCallback( doc,baseURL,label,point,ppid ) {
  var jsonData = eval('(' + doc + ')');
  log('deleteDetailsCallback()');
  if (jsonData.status == 'OK') {
    if (jsonData.view == 'home') {
      window.location.href = baseURL;
    } else if (jsonData.view == 'details') {
//        if (jsonData.fav != null && jsonData.fav == 'false') {
//        setAddToMyFavsLink( true );
//        }
//        if (jsonData.mystuff != null && jsonData.mystuff == 'false') {
//        setAddToMyStuffLink( true );
//        }
        viewDetails(label,point,ppid);
    } else {
// for map page
//      window.location.href = baseURL + '&ptype=' + jsonData.view + '&op=sha';
// for dashboard
        window.location.href = baseURL;
    }
  } else if (jsonData.status == 'SORRY'){
    showSorryForm();
  } else {
    showExpiredForm();
  }
}

function toggleFavouriteCallback( doc,label,point,ppid ) {
  log('toggleFavouriteCallback()');
  _togglingFavourite = false;
  try {
  var jsonData = eval('(' + doc + ')');
  if(jsonData.status == 'OK' ) {
//    if( jsonData.favourite == 'true' ){
//      setAddToMyFavsLink( false );
//    } else {
//      setAddToMyFavsLink( true );
//    }
    viewDetails(label,point,ppid);
  }else if (jsonData.status == 'SORRY'){
    showSorryForm();
  } else{
    showExpiredForm();
  }
  }
  catch (e) {
  //probably html from a login, open lightbox with content
   _currentLB = new lightbox( 'toggleFavouriteCallback',null );
   _currentLB.activate();
   var f = document.getElementById('lbLoadMessage');
   f.innerHTML = doc + " " + e;
   smartFocus(f);
  }
}

function fastAddToMyStuffCallback( doc,label,point,ppid ) {
  log('fastAddToMyStuffCallback()');
  try {
  var jsonData = eval('(' + doc + ')');
  if(jsonData.status == 'OK' ) {
//    setAddToMyStuffLink( false );
    viewDetails(label,point,ppid);
  }else if (jsonData.status == 'SORRY'){
    showSorryForm();
  } else{
    showExpiredForm();
  }
  }
  catch (e) {
  //probably html from a login, open lightbox with content
   _currentLB = new lightbox( 'fastAddToMyStuffCallback',null );
   _currentLB.activate();
   var f = document.getElementById('lbLoadMessage');
   f.innerHTML = doc;
   smartFocus(f);
  }
}
