


google.load("gdata", "1.x");



var contactsService;

function setupContactsService() {
  contactsService = new google.gdata.contacts.ContactsService('SecretSanta');
}

function logMeIn() {
  var scope = 'http://www.google.com/m8/feeds';
  var token = google.accounts.user.login(scope);
}

function initFunc() {
  setupContactsService();
  logMeIn();
  getMyContacts();
  
}

function getMyContacts() {
  var contactsFeedUri = 'http://www.google.com/m8/feeds/contacts/default/full';
  var query = new google.gdata.contacts.ContactQuery(contactsFeedUri);
  // Set the maximum of the result set to be 5
  query.setMaxResults(1000);
  contactsService.getContactFeed(query, handleContactsFeed, handleError);
}

var handleContactsFeed = function(result) {
  $.cookie("GmailPopup","yes");
  $.cookie("YahooPopup",null);	
  var entries = result.feed.entry;
  $('#importcontact li').each(function(){
  	$(this).remove();
  	});
  for (var i = 0; i < entries.length; i++) {
    var contactEntry = entries[i];
    var emailAddresses = contactEntry.getEmailAddresses();
    var name = contactEntry.getTitle().getText();
    
    for (var j = 0; j < emailAddresses.length; j++) {
  
      var emailAddress = emailAddresses[j].getAddress();
      
      $('#importcontact').append('<li class="exclitem'+j+'"><img src="img/invitpic.gif"><div><h4>'+name+'</h4><p>'+emailAddress+'</p></div><input type="checkbox" class="ibox" name="ibox'+j+'" value="'+emailAddress+'"></li>');
      
    }    
  }
  document.getElementById('excl_popup').setAttribute("class", "down");
  document.getElementById('excl_popup').setAttribute("className", "down");
}
function handleError(e) {
  alert(e.cause ? e.cause.statusText : e.message);
}
